Mercurial > hg > toybox
annotate toys/pending/ifconfig.c @ 878:d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 24 Apr 2013 21:52:08 -0500 |
parents | aa5bd0a358dd |
children | bc7aa2d123b9 |
rev | line source |
---|---|
841 | 1 /* ifconfig.c - Configure network interface. |
2 * | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com> |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
4 * Copyright 2012 Kyungwan Han <asura321@gamil.com> |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
5 * Reviewed by Kyungsu Kim <kaspyx@gmail.com> |
841 | 6 * |
7 * Not in SUSv4. | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
8 |
841 | 9 USE_IFCONFIG(NEWTOY(ifconfig, "?a", TOYFLAG_BIN)) |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
10 |
841 | 11 config IFCONFIG |
12 bool "ifconfig" | |
852 | 13 default n |
841 | 14 help |
15 usage: ifconfig [-a] interface [address] | |
16 | |
17 Configure network interface. | |
18 | |
19 [add ADDRESS[/PREFIXLEN]] | |
20 [del ADDRESS[/PREFIXLEN]] | |
21 [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]] | |
22 [netmask ADDRESS] [dstaddr ADDRESS] | |
23 [outfill NN] [keepalive NN] | |
24 [hw ether|infiniband ADDRESS] [metric NN] [mtu NN] | |
25 [[-]trailers] [[-]arp] [[-]allmulti] | |
26 [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic] | |
27 [mem_start NN] [io_addr NN] [irq NN] | |
28 [up|down] ... | |
29 */ | |
30 | |
31 #define FOR_ifconfig | |
32 #include "toys.h" | |
33 #include "toynet.h" | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
34 |
841 | 35 #include <net/if.h> |
36 #include <net/if_arp.h> | |
37 #include <net/ethernet.h> | |
38 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
39 GLOBALS( |
870 | 40 void *if_list; |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
41 ) |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
42 |
841 | 43 typedef struct sockaddr_with_len { |
44 union { | |
45 struct sockaddr sock; | |
46 struct sockaddr_in sock_in; | |
47 struct sockaddr_in6 sock_in6; | |
48 }sock_u; | |
49 socklen_t socklen; | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
50 } sockaddr_with_len; |
841 | 51 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
52 // man netdevice |
870 | 53 struct if_list { |
54 struct if_list *next; | |
55 int hw_type, mtu, metric, txqueuelen, non_virtual_iface; | |
56 short flags, hasaddr; | |
57 struct sockaddr addr, dstaddr, broadaddr, netmask, hwaddr; | |
58 struct ifmap map; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
59 |
870 | 60 char name[IFNAMSIZ]; |
61 unsigned long long val[16]; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
62 }; |
841 | 63 |
64 #define HW_NAME_LEN 20 | |
65 #define HW_TITLE_LEN 30 | |
66 | |
67 typedef struct _hw_info { | |
68 char hw_name[HW_NAME_LEN]; | |
69 char hw_title[HW_TITLE_LEN]; | |
870 | 70 int hw_addrlen; |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
71 } HW_INFO; |
841 | 72 |
73 #define NO_RANGE -1 | |
74 #define IO_MAP_INDEX 0x100 | |
75 | |
76 //from kernel header ipv6.h | |
77 #define IPV6_ADDR_ANY 0x0000U | |
78 #define IPV6_ADDR_LOOPBACK 0x0010U | |
79 #define IPV6_ADDR_LINKLOCAL 0x0020U | |
80 #define IPV6_ADDR_SITELOCAL 0x0040U | |
81 #define IPV6_ADDR_COMPATv4 0x0080U | |
82 | |
83 //for the param settings. | |
84 | |
85 //for ipv6 add/del | |
86 struct ifreq_inet6 { | |
87 struct in6_addr ifrinte6_addr; | |
88 uint32_t ifrinet6_prefixlen; | |
89 int ifrinet6_ifindex; | |
90 }; | |
91 | |
92 #ifndef SIOCSKEEPALIVE | |
93 # define SIOCSKEEPALIVE (SIOCDEVPRIVATE) /* Set keepalive timeout in sec */ | |
94 # define SIOCGKEEPALIVE (SIOCDEVPRIVATE+1) /* Get keepalive timeout */ | |
95 #endif | |
96 | |
97 #ifndef SIOCSOUTFILL | |
98 # define SIOCSOUTFILL (SIOCDEVPRIVATE+2) /* Set outfill timeout */ | |
99 # define SIOCGOUTFILL (SIOCDEVPRIVATE+3) /* Get outfill timeout */ | |
100 #endif | |
101 | |
102 #ifndef INFINIBAND_ALEN | |
103 # define INFINIBAND_ALEN 20 | |
104 #endif | |
105 | |
864 | 106 void xioctl(int fd, int request, void *data) |
107 { | |
108 if (ioctl(fd, request, data) < 0) perror_exit("ioctl %d", request); | |
109 } | |
110 | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
111 int xsocket(int domain, int type, int protocol) |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
112 { |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
113 int fd = socket(domain, type, protocol); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
114 |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
115 if (fd < 0) perror_exit("socket %x %x", type, protocol); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
116 return fd; |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
117 } |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
118 |
841 | 119 /* |
120 * verify the host is local unix path. | |
121 * if so, set the swl input param accordingly. | |
122 */ | |
852 | 123 static int is_host_unix(char *host, sockaddr_with_len **swl) |
841 | 124 { |
864 | 125 if (strncmp(host, "local:", 6) == 0) { |
841 | 126 struct sockaddr_un *sockun; |
864 | 127 |
841 | 128 *swl = xzalloc(sizeof(struct sockaddr_with_len)); |
129 (*swl)->socklen = sizeof(struct sockaddr_un); | |
130 (*swl)->sock_u.sock.sa_family = AF_UNIX; | |
131 sockun = (struct sockaddr_un *)&(*swl)->sock_u.sock; | |
870 | 132 xstrncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path)); |
841 | 133 return 1; |
134 } | |
135 return 0; | |
136 } | |
137 | |
138 /* | |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
139 * used to converts string into int and validate the input str for invalid int value or out-of-range. |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
140 */ |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
141 unsigned get_strtou(char *str, char **endp, int base) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
142 { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
143 unsigned long uli; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
144 char *endptr; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
145 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
146 if(!isalnum(str[0])) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
147 errno = ERANGE; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
148 return UINT_MAX; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
149 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
150 errno = 0; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
151 uli = strtoul(str, &endptr, base); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
152 if(uli > UINT_MAX) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
153 errno = ERANGE; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
154 return UINT_MAX; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
155 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
156 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
157 if(endp) *endp = endptr; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
158 if(endptr[0]) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
159 if(isalnum(endptr[0]) || errno) { //"123abc" or out-of-range |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
160 errno = ERANGE; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
161 return UINT_MAX; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
162 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
163 errno = EINVAL; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
164 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
165 return uli; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
166 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
167 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
168 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
169 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
170 /* |
841 | 171 * validate the input param (host) for valid ipv6 ip and extract port number (if there). |
172 */ | |
173 static void get_host_and_port(char **host, int *port) | |
174 { | |
175 char *ch_ptr; | |
852 | 176 char *org_host = *host; |
864 | 177 if (*host[0] == '[') { |
841 | 178 (*host)++; |
179 ch_ptr = strchr(*host, ']'); | |
864 | 180 if (!ch_ptr || (ch_ptr[1] != ':' && ch_ptr[1] != '\0')) |
841 | 181 error_exit("bad address '%s'", org_host); |
864 | 182 } else { |
841 | 183 ch_ptr = strrchr(*host, ':'); |
184 //There is more than one ':' like "::1" | |
864 | 185 if(ch_ptr && strchr(*host, ':') != ch_ptr) ch_ptr = NULL; |
841 | 186 } |
864 | 187 if (ch_ptr) { //pointer to ":" or "]:" |
841 | 188 int size = ch_ptr - (*host) + 1; |
870 | 189 xstrncpy(*host, *host, size); |
864 | 190 if (*ch_ptr != ':') { |
841 | 191 ch_ptr++; //skip ']' |
192 //[nn] without port | |
864 | 193 if (!*ch_ptr) return; |
841 | 194 } |
195 ch_ptr++; //skip ':' to get the port number. | |
196 *port = get_strtou(ch_ptr, NULL, 10); | |
864 | 197 if (errno || (unsigned)*port > 65535) error_exit("bad port '%s'", org_host); |
198 } | |
841 | 199 } |
200 | |
201 /* | |
202 * used to extract the address info from the given host ip | |
203 * and update the swl param accordingly. | |
204 */ | |
852 | 205 static int get_socket_stream(char *host, sa_family_t af, sockaddr_with_len **swl) |
841 | 206 { |
852 | 207 struct addrinfo hints, *result, *rp; |
208 int status; | |
841 | 209 |
210 memset(&hints, 0 , sizeof(struct addrinfo)); | |
211 hints.ai_family = af; | |
212 hints.ai_socktype = SOCK_STREAM; | |
213 | |
852 | 214 status = getaddrinfo(host, NULL, &hints, &result); |
864 | 215 if (status) error_exit("bad address '%s' : %s", host, gai_strerror(status)); |
841 | 216 |
864 | 217 for (rp = result; rp; rp = rp->ai_next) { |
218 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) { | |
841 | 219 *swl = xmalloc(sizeof(struct sockaddr_with_len)); |
220 (*swl)->socklen = rp->ai_addrlen; | |
221 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen); | |
222 break; | |
223 } | |
224 } | |
225 freeaddrinfo(result); | |
852 | 226 return rp ? 0 : -1; |
841 | 227 } |
228 | |
229 /* | |
230 * use to get the socket address with the given host ip. | |
231 */ | |
852 | 232 sockaddr_with_len *get_sockaddr(char *host, int port, sa_family_t af) |
841 | 233 { |
234 sockaddr_with_len *swl = NULL; | |
852 | 235 in_port_t port_num = htons(port); |
841 | 236 |
852 | 237 if(is_host_unix(host, &swl) && swl) return swl; |
841 | 238 |
239 //[IPV6_ip]:port_num | |
240 if(host[0] == '[' || strrchr(host, ':')) get_host_and_port((char **)&host, &port); | |
241 | |
852 | 242 if (get_socket_stream(host, af, &swl)) return NULL; |
841 | 243 |
852 | 244 if(swl->sock_u.sock.sa_family == AF_INET) |
245 swl->sock_u.sock_in.sin_port = port_num; | |
246 else if(swl->sock_u.sock.sa_family == AF_INET6) | |
247 swl->sock_u.sock_in6.sin6_port = port_num; | |
248 | |
841 | 249 return swl; |
250 } | |
251 | |
252 /* | |
253 * get the numeric hostname and service name, for a given socket address. | |
254 */ | |
852 | 255 char *address_to_name(struct sockaddr *sock) |
841 | 256 { |
257 //man page of getnameinfo. | |
258 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,}; | |
259 int status = 0; | |
864 | 260 |
841 | 261 if(sock->sa_family == AF_INET) { |
262 socklen_t len = sizeof(struct sockaddr_in); | |
263 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) | |
264 return xmsprintf("%s:%s", hbuf, sbuf); | |
265 else { | |
266 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); | |
267 return NULL; | |
268 } | |
864 | 269 } else if(sock->sa_family == AF_INET6) { |
841 | 270 socklen_t len = sizeof(struct sockaddr_in6); |
271 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) { | |
272 //verification for resolved hostname. | |
273 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf); | |
274 else return xmsprintf("%s:%s", hbuf, sbuf); | |
864 | 275 } else { |
841 | 276 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); |
277 return NULL; | |
278 } | |
864 | 279 } else if(sock->sa_family == AF_UNIX) { |
841 | 280 struct sockaddr_un *sockun = (void*)sock; |
281 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path); | |
864 | 282 } else return NULL; |
841 | 283 } |
284 | |
285 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag) | |
286 { | |
864 | 287 xioctl(sockfd, SIOCGIFFLAGS, ifre); |
841 | 288 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag; |
864 | 289 xioctl(sockfd, SIOCSIFFLAGS, ifre); |
841 | 290 } |
291 | |
852 | 292 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu) |
841 | 293 { |
294 ifre->ifr_mtu = strtoul(mtu, NULL, 0); | |
864 | 295 xioctl(sockfd, SIOCSIFMTU, ifre); |
841 | 296 } |
297 | |
852 | 298 static void set_metric(int sockfd, struct ifreq *ifre, char *metric) |
841 | 299 { |
300 ifre->ifr_metric = strtoul(metric, NULL, 0); | |
864 | 301 xioctl(sockfd, SIOCSIFMETRIC, ifre); |
841 | 302 } |
303 | |
852 | 304 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen) |
841 | 305 { |
306 ifre->ifr_qlen = strtoul(qlen, NULL, 0); | |
864 | 307 xioctl(sockfd, SIOCSIFTXQLEN, ifre); |
841 | 308 } |
309 | |
852 | 310 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name) |
841 | 311 { |
312 char *prefix; | |
313 int plen = 0; | |
314 sockaddr_with_len *swl = NULL; | |
315 | |
316 prefix = strchr(ipv6_addr, '/'); | |
317 if(prefix) { | |
318 plen = get_int_value(prefix + 1, 0, 128); | |
319 *prefix = '\0'; | |
320 } | |
321 swl = get_sockaddr(ipv6_addr, 0, AF_INET6); | |
322 if(!swl) error_exit("error in resolving host name"); | |
323 int sockfd6; | |
324 struct ifreq_inet6 ifre6; | |
325 memcpy((char *) &ifre6.ifrinte6_addr, | |
326 (char *) &(swl->sock_u.sock_in6.sin6_addr), | |
327 sizeof(struct in6_addr)); | |
328 //Create a channel to the NET kernel. | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
329 sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0); |
864 | 330 xioctl(sockfd6, SIOGIFINDEX, ifre); |
841 | 331 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex; |
332 ifre6.ifrinet6_prefixlen = plen; | |
333 | |
864 | 334 xioctl(sockfd6, request, &ifre6); |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
335 free(swl); |
841 | 336 } |
337 | |
852 | 338 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name) |
841 | 339 { |
340 struct sockaddr_in sock_in; | |
341 sockaddr_with_len *swl = NULL; | |
342 sock_in.sin_family = AF_INET; | |
343 sock_in.sin_port = 0; | |
344 | |
345 //Default 0.0.0.0 | |
864 | 346 if(strcmp(host_name, "default") == 0) sock_in.sin_addr.s_addr = INADDR_ANY; |
841 | 347 else { |
348 swl = get_sockaddr(host_name, 0, AF_INET); | |
349 if(!swl) error_exit("error in resolving host name"); | |
350 | |
351 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr; | |
352 } | |
353 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr)); | |
864 | 354 xioctl(sockfd, request, ifre); |
841 | 355 |
356 if(swl != NULL) { | |
357 free(swl); | |
358 swl = NULL; | |
359 } | |
360 } | |
361 | |
852 | 362 static int hex_to_binary(char *hw_addr, struct sockaddr *sock, int count) |
841 | 363 { |
364 int i = 0, j = 0; | |
864 | 365 unsigned char nib_val, ch; |
841 | 366 |
367 char *ptr = (char *) sock->sa_data; | |
864 | 368 if (count == ETH_ALEN) sock->sa_family = ARPHRD_ETHER; |
369 else if (count == INFINIBAND_ALEN) sock->sa_family = ARPHRD_INFINIBAND; | |
370 else return -1; | |
841 | 371 //e.g. hw_addr "62:2D:A6:9E:2D:BE" |
864 | 372 for (; *hw_addr && (i < count); i++) { |
373 if (*hw_addr == ':') hw_addr++; | |
841 | 374 j = nib_val = 0; |
864 | 375 for (;j < 2; j++) { |
841 | 376 ch = *hw_addr; |
377 //0-9 = 10 chars. | |
864 | 378 if (((unsigned char)(ch - '0')) < 10) ch = (ch - '0'); |
841 | 379 //a-f = 6 chars. |
864 | 380 else if (((unsigned char)((ch) - 'a')) < 6) ch = (ch - ('a'-10)); |
841 | 381 //A-F = 6 chars. |
864 | 382 else if (((unsigned char)((ch) - 'A')) < 6) ch = (ch - ('A'-10)); |
383 else if (j && (ch == ':' || ch == 0)) break; | |
384 else return -1; | |
841 | 385 hw_addr++; |
386 nib_val <<= 4; | |
387 nib_val += ch; | |
388 } | |
389 *ptr++ = nib_val; | |
390 } | |
864 | 391 if (*hw_addr) return -1; |
841 | 392 return 0; |
393 } | |
394 | |
852 | 395 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name) |
841 | 396 { |
397 int hw_class = 0; | |
398 char *hw_addr; | |
399 struct sockaddr sock; | |
400 char *ptr; | |
401 char *hw_class_strings[] = { | |
402 "ether", | |
403 "infiniband", | |
404 NULL | |
405 }; | |
406 | |
407 if(strcmp(hw_class_strings[0], **argv) == 0) | |
408 hw_class = 1; | |
409 else if(strcmp(hw_class_strings[1], **argv) == 0) | |
410 hw_class = 2; | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
411 if(!hw_class || !(*argv += 1)) { |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
412 errno = EINVAL; |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
413 toys.exithelp++; |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
414 error_exit("bad hardware class"); |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
415 } |
841 | 416 |
417 memset(&sock, 0, sizeof(struct sockaddr)); | |
418 hw_addr = **argv; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
419 if(hex_to_binary(hw_addr, &sock, hw_class == 1 ? ETH_ALEN : INFINIBAND_ALEN)) |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
420 error_exit("bad hw-addr %s", hw_addr); |
841 | 421 ptr = (char *)&sock; |
422 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr)); | |
864 | 423 xioctl(sockfd, request, ifre); |
841 | 424 } |
425 | |
852 | 426 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name) |
841 | 427 { |
428 unsigned long mem_start = strtoul(start_addr, NULL, 0); | |
429 | |
864 | 430 xioctl(sockfd, SIOCGIFMAP, ifre); |
841 | 431 ifre->ifr_map.mem_start = mem_start; |
864 | 432 xioctl(sockfd, request, ifre); |
841 | 433 } |
434 | |
852 | 435 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name) |
841 | 436 { |
437 unsigned short int base_addr = strtoul(baddr, NULL, 0); | |
864 | 438 xioctl(sockfd, SIOCGIFMAP, ifre); |
841 | 439 ifre->ifr_map.base_addr = base_addr; |
864 | 440 xioctl(sockfd, request, ifre); |
841 | 441 } |
442 | |
852 | 443 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name) |
841 | 444 { |
445 unsigned short int irq = strtoul(irq_val, NULL, 0); | |
446 char *ptr; | |
447 struct ifmap *map; | |
448 | |
864 | 449 xioctl(sockfd, SIOCGIFMAP, ifre); |
841 | 450 |
451 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map); | |
452 map = (struct ifmap *)ptr; | |
453 map->irq = irq; | |
864 | 454 xioctl(sockfd, request, ifre); |
841 | 455 } |
456 | |
870 | 457 static void add_iface_to_list(struct if_list *newnode) |
841 | 458 { |
870 | 459 struct if_list *head_ref = TT.if_list; |
841 | 460 |
870 | 461 if(!head_ref || strcmp(newnode->name, head_ref->name) < 0) { |
841 | 462 newnode->next = head_ref; |
463 head_ref = newnode; | |
862 | 464 } else { |
870 | 465 struct if_list *current = head_ref; |
466 while(current->next && strcmp(current->next->name, newnode->name) < 0) | |
841 | 467 current = current->next; |
468 newnode->next = current->next; | |
469 current->next = newnode; | |
470 } | |
870 | 471 TT.if_list = (void *)head_ref; |
841 | 472 } |
473 | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
474 static void get_device_info(struct if_list *il) |
841 | 475 { |
476 struct ifreq ifre; | |
870 | 477 char *name = il->name; |
841 | 478 int sokfd; |
479 | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
480 sokfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
481 xstrncpy(ifre.ifr_name, name, IFNAMSIZ); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
482 if(ioctl(sokfd, SIOCGIFFLAGS, &ifre)<0) perror_exit("%s", il->name); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
483 il->flags = ifre.ifr_flags; |
841 | 484 |
485 if(ioctl(sokfd, SIOCGIFHWADDR, &ifre) >= 0) | |
870 | 486 memcpy(il->hwaddr.sa_data, ifre.ifr_hwaddr.sa_data, sizeof(il->hwaddr.sa_data)); |
841 | 487 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
488 il->hw_type = ifre.ifr_hwaddr.sa_family; |
841 | 489 |
490 if(ioctl(sokfd, SIOCGIFMETRIC, &ifre) >= 0) | |
870 | 491 il->metric = ifre.ifr_metric; |
841 | 492 |
493 if(ioctl(sokfd, SIOCGIFMTU, &ifre) >= 0) | |
870 | 494 il->mtu = ifre.ifr_mtu; |
841 | 495 |
496 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) | |
870 | 497 il->map = ifre.ifr_map; |
841 | 498 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
499 il->txqueuelen = NO_RANGE; |
841 | 500 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
501 il->txqueuelen = ifre.ifr_qlen; |
841 | 502 |
503 ifre.ifr_addr.sa_family = AF_INET; | |
504 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
505 if(!ioctl(sokfd, SIOCGIFADDR, &ifre)) { |
870 | 506 il->hasaddr = 1; |
507 il->addr = ifre.ifr_addr; | |
841 | 508 if(ioctl(sokfd, SIOCGIFDSTADDR, &ifre) >= 0) |
870 | 509 il->dstaddr = ifre.ifr_dstaddr; |
841 | 510 |
511 if(ioctl(sokfd, SIOCGIFBRDADDR, &ifre) >= 0) | |
870 | 512 il->broadaddr = ifre.ifr_broadaddr; |
841 | 513 |
514 if(ioctl(sokfd, SIOCGIFNETMASK, &ifre) >= 0) | |
870 | 515 il->netmask = ifre.ifr_netmask; |
841 | 516 } |
517 close(sokfd); | |
518 } | |
519 | |
520 static void get_hw_info(int hw_type, HW_INFO *hw_info) | |
521 { | |
852 | 522 memset(hw_info, 0, sizeof(HW_INFO)); |
523 | |
841 | 524 switch(hw_type) { |
525 case ARPHRD_LOOPBACK: //Loopback device. | |
526 strncpy(hw_info->hw_name, "loop", HW_NAME_LEN); | |
527 strncpy(hw_info->hw_title, "Local Loopback", HW_TITLE_LEN); | |
528 hw_info->hw_addrlen = 0; | |
529 break; | |
530 case ARPHRD_ETHER: //Ethernet | |
531 strncpy(hw_info->hw_name, "ether", HW_NAME_LEN); | |
532 strncpy(hw_info->hw_title, "Ethernet", HW_TITLE_LEN); | |
533 hw_info->hw_addrlen = ETH_ALEN; | |
534 break; | |
535 case ARPHRD_PPP: //ARPHRD_PPP | |
536 strncpy(hw_info->hw_name, "ppp", HW_NAME_LEN); | |
537 strncpy(hw_info->hw_title, "Point-to-Point Protocol", HW_TITLE_LEN); | |
538 hw_info->hw_addrlen = 0; | |
539 break; | |
540 case ARPHRD_INFINIBAND: //InfiniBand | |
541 strncpy(hw_info->hw_name, "infiniband", HW_NAME_LEN); | |
542 strncpy(hw_info->hw_title, "InfiniBand", HW_TITLE_LEN); | |
543 hw_info->hw_addrlen = 20; | |
544 break; | |
545 case ARPHRD_SIT: //sit0 device - IPv6-in-IPv4 | |
546 strncpy(hw_info->hw_name, "sit", HW_NAME_LEN); | |
547 strncpy(hw_info->hw_title, "IPv6-in-IPv4", HW_TITLE_LEN); | |
548 hw_info->hw_addrlen = 0; | |
549 break; | |
852 | 550 case -1: |
841 | 551 strncpy(hw_info->hw_name, "unspec", HW_NAME_LEN); |
552 strncpy(hw_info->hw_title, "UNSPEC", HW_TITLE_LEN); | |
553 hw_info->hw_addrlen = 0; | |
554 break; | |
555 default: | |
556 break; | |
557 } | |
558 } | |
559 | |
870 | 560 static void print_hw_addr(int hw_type, HW_INFO hw_info, struct if_list *il) |
841 | 561 { |
870 | 562 char *address = il->hwaddr.sa_data; |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
563 |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
564 if(!address || !hw_info.hw_addrlen) return; |
841 | 565 xprintf("HWaddr "); |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
566 if(hw_type == ARPHRD_ETHER) { |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
567 int i; |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
568 |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
569 for (i=0; i<6; i++) xprintf(":%02X"+!i, address[i]); |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
570 } |
841 | 571 } |
572 | |
852 | 573 static char *get_ip_addr(struct sockaddr *skaddr) |
841 | 574 { |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
575 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr; |
841 | 576 |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
577 if(skaddr->sa_family == 0xFFFF || !skaddr->sa_family) return "[NOT SET]"; |
841 | 578 if(sin->sin_family != AF_INET) { |
579 errno = EAFNOSUPPORT; | |
580 return NULL; | |
581 } | |
582 | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
583 return inet_ntoa(sin->sin_addr); |
841 | 584 } |
585 | |
870 | 586 static void print_ip_addr(struct if_list *il) |
841 | 587 { |
852 | 588 char *af_name; |
870 | 589 int af = il->addr.sa_family; |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
590 |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
591 if (af == AF_INET) af_name = "inet"; |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
592 else if (af == AF_INET6) af_name = "inet6"; |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
593 else if (af == AF_UNSPEC) af_name = "unspec"; |
841 | 594 |
870 | 595 xprintf("%10s%s addr:%s ", " ", af_name, get_ip_addr(&il->addr)); |
596 if(il->flags & IFF_POINTOPOINT) | |
597 xprintf(" P-t-P:%s ", get_ip_addr(&il->dstaddr)); | |
598 if(il->flags & IFF_BROADCAST) | |
599 xprintf(" Bcast:%s ", get_ip_addr(&il->broadaddr)); | |
600 xprintf(" Mask:%s\n", get_ip_addr(&il->netmask)); | |
841 | 601 } |
602 | |
870 | 603 static void print_ip6_addr(struct if_list *il) |
841 | 604 { |
605 char iface_name[IFNAMSIZ] = {0,}; | |
606 int plen, scope; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
607 FILE *fp; |
841 | 608 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
609 if(!(fp = fopen("/proc/net/if_net6", "r"))) return; |
841 | 610 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
611 while(fgets(toybuf, sizeof(toybuf), fp)) { |
841 | 612 int nitems = 0; |
613 char ipv6_addr[40] = {0,}; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
614 nitems = sscanf(toybuf, "%32s %*08x %02x %02x %*02x %15s\n", |
841 | 615 ipv6_addr+7, &plen, &scope, iface_name); |
616 if(nitems != 4) { | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
617 if((nitems < 0) && feof(fp)) break; |
841 | 618 perror_exit("sscanf"); |
619 } | |
870 | 620 if(strcmp(il->name, iface_name) == 0) { |
841 | 621 int i = 0; |
622 struct sockaddr_in6 sock_in6; | |
623 int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]); | |
624 char *ptr = ipv6_addr+7; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
625 |
841 | 626 while((i < len-2) && (*ptr)) { |
627 ipv6_addr[i++] = *ptr++; | |
628 //put ':' after 4th bit | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
629 if(!((i+1) % 5)) ipv6_addr[i++] = ':'; |
841 | 630 } |
631 ipv6_addr[i+1] = '\0'; | |
632 if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) { | |
633 sock_in6.sin6_family = AF_INET6; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
634 if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, toybuf, BUFSIZ)) { |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
635 xprintf("%10sinet6 addr: %s/%d Scope:", " ", toybuf, plen); |
841 | 636 if(scope == IPV6_ADDR_ANY) xprintf(" Global"); |
637 else if(scope == IPV6_ADDR_LOOPBACK) xprintf(" Host"); | |
638 else if(scope == IPV6_ADDR_LINKLOCAL) xprintf(" Link"); | |
639 else if(scope == IPV6_ADDR_SITELOCAL) xprintf(" Site"); | |
640 else if(scope == IPV6_ADDR_COMPATv4) xprintf(" Compat"); | |
641 else xprintf("Unknown"); | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
642 xputc('\n'); |
841 | 643 } |
644 } | |
645 } | |
864 | 646 } |
841 | 647 fclose(fp); |
648 } | |
649 | |
870 | 650 static void display_ifconfig(struct if_list *il) |
841 | 651 { |
652 HW_INFO hw_info; | |
653 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
654 get_hw_info(il->hw_type, &hw_info); |
870 | 655 xprintf("%-9s Link encap:%s ", il->name, hw_info.hw_title); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
656 print_hw_addr(il->hw_type, hw_info, il); |
852 | 657 xputc('\n'); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
658 |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
659 //print addr, p-p addr, broadcast addr and mask addr. |
870 | 660 if(il->hasaddr) print_ip_addr(il); |
841 | 661 |
662 //for ipv6 to do. | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
663 print_ip6_addr(il); |
841 | 664 xprintf("%10s", " "); |
665 //print flags | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
666 |
870 | 667 if (il->flags) { |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
668 unsigned short mask = 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
669 char **s, *str[] = { |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
670 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
671 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
672 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
673 }; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
674 |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
675 for(s = str; *s; s++) { |
870 | 676 if(il->flags & mask) xprintf("%s ", *s); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
677 mask = mask << 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
678 } |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
679 } else xprintf("[NO FLAGS] "); |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
680 |
870 | 681 if(!il->metric) il->metric = 1; |
682 xprintf(" MTU:%d Metric:%d", il->mtu, il->metric); | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
683 |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
684 if(il->non_virtual_iface) { |
870 | 685 char *label[] = {"RX bytes", "RX packets", "errors", "dropped", "overruns", |
686 "frame", 0, 0, "TX bytes", "TX packets", "errors", "dropped", "overruns", | |
687 "collisions", "carrier", 0, "txqueuelen"}; | |
688 signed char order[] = {-1, 1, 2, 3, 4, 5, -1, 9, 10, 11, 12, 14, -1, | |
689 13, 16, -1, 0, 8}; | |
690 int i; | |
691 | |
692 for (i = 0; i < sizeof(order); i++) { | |
693 int j = order[i]; | |
841 | 694 |
870 | 695 if (j < 0) xprintf("\n%10c", ' '); |
696 else xprintf("%s:%llu ", label[j], | |
697 j==16 ? (unsigned long long)il->txqueuelen : il->val[j]); | |
698 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
699 } |
870 | 700 xputc('\n'); |
701 | |
702 if(il->map.irq || il->map.mem_start || il->map.dma || il->map.base_addr) { | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
703 xprintf("%10c", ' '); |
870 | 704 if(il->map.irq) xprintf("Interrupt:%d ", il->map.irq); |
705 if(il->map.base_addr >= IO_MAP_INDEX) | |
706 xprintf("Base address:0x%lx ", il->map.base_addr); | |
707 if(il->map.mem_start) | |
708 xprintf("Memory:%lx-%lx ", il->map.mem_start, il->map.mem_end); | |
709 if(il->map.dma) xprintf("DMA chan:%x ", il->map.dma); | |
852 | 710 xputc('\n'); |
841 | 711 } |
852 | 712 xputc('\n'); |
841 | 713 } |
714 | |
870 | 715 static void readconf(void) |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
716 { |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
717 int num_of_req = 30; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
718 struct ifconf ifcon; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
719 struct ifreq *ifre; |
870 | 720 int num, sokfd; |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
721 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
722 ifcon.ifc_buf = NULL; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
723 sokfd = socket(AF_INET, SOCK_DGRAM, 0); |
870 | 724 if(sokfd < 0) perror_exit("socket"); |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
725 for (;;) { |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
726 ifcon.ifc_len = sizeof(struct ifreq) * num_of_req; //Size of buffer. |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
727 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len); |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
728 |
870 | 729 xioctl(sokfd, SIOCGIFCONF, &ifcon); |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
730 //in case of overflow, increase number of requests and retry. |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
731 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) { |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
732 num_of_req += 10; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
733 continue; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
734 } |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
735 break; |
864 | 736 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
737 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
738 ifre = ifcon.ifc_req; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
739 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) { |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
740 //Escape duplicate values from the list. |
870 | 741 struct if_list *il; |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
742 |
870 | 743 for(il = TT.if_list; il; il = il->next) |
744 if(!strcmp(ifre->ifr_name, il->name)) break; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
745 if(!il) { |
870 | 746 il = xzalloc(sizeof(struct if_list)); |
747 xstrncpy(il->name, ifre->ifr_name, IFNAMSIZ); | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
748 add_iface_to_list(il); |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
749 errno = 0; |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
750 get_device_info(il); |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
751 } |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
752 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
753 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
754 close(sokfd); |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
755 free(ifcon.ifc_buf); |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
756 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
757 |
870 | 758 static void show_iface(char *iface_name) |
841 | 759 { |
870 | 760 struct if_list *il; |
761 int i, j; | |
762 FILE *fp; | |
763 | |
764 fp = xfopen("/proc/net/dev", "r"); | |
765 | |
766 for (i=0; fgets(toybuf, sizeof(toybuf), fp); i++) { | |
767 char *name, *buf; | |
768 | |
769 if (i<2) continue; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
770 |
870 | 771 il = xzalloc(sizeof(struct if_list)); |
772 for (buf = toybuf; isspace(*buf); buf++); | |
773 name = strsep(&buf, ":"); | |
774 if(!buf) error_exit("bad name %s", name); | |
775 xstrncpy(il->name, name, IFNAMSIZ); | |
776 | |
777 errno = 0; | |
778 for (j=0; j<16 && !errno; j++) il->val[j] = strtoll(buf, &buf, 0); | |
779 if (errno) perror_exit("bad %s at %s", name, buf); | |
780 | |
781 add_iface_to_list(il); | |
782 il->non_virtual_iface = 1; | |
783 errno = 0; | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
784 get_device_info(il); |
870 | 785 } |
786 fclose(fp); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
787 |
841 | 788 if(iface_name) { |
870 | 789 for(il = TT.if_list; il; il = il->next) { |
790 if(!strcmp(il->name, iface_name)) { | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
791 display_ifconfig(il); |
841 | 792 break; |
793 } | |
794 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
795 //if the given interface is not in the list. |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
796 if(!il) { |
870 | 797 il = xzalloc(sizeof(struct if_list)); |
798 xstrncpy(il->name, iface_name, IFNAMSIZ); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
799 errno = 0; |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
800 get_device_info(il); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
801 display_ifconfig(il); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
802 free(il); |
841 | 803 } |
852 | 804 } else { |
870 | 805 readconf(); |
806 for(il = TT.if_list; il; il = il->next) | |
807 if((il->flags & IFF_UP) || (toys.optflags & FLAG_a)) | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
808 display_ifconfig(il); |
841 | 809 } |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
810 |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
811 if (CFG_TOYBOX_FREE) llist_traverse(TT.if_list, free); |
841 | 812 } |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
813 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
814 void ifconfig_main(void) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
815 { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
816 char **argv = toys.optargs; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
817 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
818 if(*argv && (strcmp(*argv, "--help") == 0)) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
819 |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
820 if(toys.optc < 2) { |
870 | 821 show_iface(*argv); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
822 return; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
823 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
824 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
825 //set ifconfig params. |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
826 { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
827 struct ifreq ifre; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
828 int sockfd = 0; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
829 //get interface name |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
830 memset(&ifre, 0, sizeof(struct ifreq)); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
831 strncpy(ifre.ifr_name, *argv, IFNAMSIZ); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
832 ifre.ifr_name[IFNAMSIZ-1] = 0; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
833 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) perror_exit("socket"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
834 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
835 while(*++argv != NULL) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
836 /* flags settings */ |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
837 if (!strcmp(*argv, "up")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
838 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
839 else if (!strcmp(*argv, "down")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
840 set_flags(sockfd, &ifre, 0, IFF_UP); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
841 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
842 else if (!strcmp(*argv, "arp")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
843 set_flags(sockfd, &ifre, 0, IFF_NOARP); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
844 else if (!strcmp(*argv, "-arp")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
845 set_flags(sockfd, &ifre, IFF_NOARP, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
846 else if (!strcmp(*argv, "trailers")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
847 set_flags(sockfd, &ifre, 0, IFF_NOTRAILERS); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
848 else if (!strcmp(*argv, "-trailers")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
849 set_flags(sockfd, &ifre, IFF_NOTRAILERS, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
850 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
851 else if (!strcmp(*argv, "promisc")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
852 set_flags(sockfd, &ifre, IFF_PROMISC, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
853 else if (!strcmp(*argv, "-promisc")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
854 set_flags(sockfd, &ifre, 0, IFF_PROMISC); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
855 else if (!strcmp(*argv, "allmulti")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
856 set_flags(sockfd, &ifre, IFF_ALLMULTI, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
857 else if (!strcmp(*argv, "-allmulti")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
858 set_flags(sockfd, &ifre, 0, IFF_ALLMULTI); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
859 else if (!strcmp(*argv, "multicast")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
860 set_flags(sockfd, &ifre, IFF_MULTICAST, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
861 else if (!strcmp(*argv, "-multicast")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
862 set_flags(sockfd, &ifre, 0, IFF_MULTICAST); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
863 else if (!strcmp(*argv, "dynamic")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
864 set_flags(sockfd, &ifre, IFF_DYNAMIC, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
865 else if (!strcmp(*argv, "-dynamic")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
866 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
867 else if (!strcmp(*argv, "-pointopoint")) |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
868 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
869 /*value setup */ |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
870 else if (!strcmp(*argv, "pointopoint")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
871 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
872 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
873 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
874 } else if (!strcmp(*argv, "netmask")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
875 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
876 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
877 } else if (!strcmp(*argv, "-broadcast")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
878 set_flags(sockfd, &ifre, 0, IFF_BROADCAST); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
879 } else if (!strcmp(*argv, "broadcast")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
880 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
881 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
882 set_flags(sockfd, &ifre, IFF_BROADCAST, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
883 } else if (!strcmp(*argv, "dstaddr")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
884 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
885 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
886 } else if (!strcmp(*argv, "hw")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
887 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
888 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
889 } else if (!strcmp(*argv, "mtu")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
890 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
891 set_mtu(sockfd, &ifre, *argv); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
892 } else if (!strcmp(*argv, "metric")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
893 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
894 set_metric(sockfd, &ifre, *argv); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
895 } else if (!strcmp(*argv, "txqueuelen")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
896 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
897 set_qlen(sockfd, &ifre, *argv); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
898 } else if (!strcmp(*argv, "keepalive")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
899 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
900 ifre.ifr_data = (void *)strtoul(*argv, 0, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
901 xioctl(sockfd, SIOCSKEEPALIVE, &ifre); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
902 } else if (!strcmp(*argv, "outfill")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
903 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
904 ifre.ifr_data = (void *)strtoul(*argv, 0, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
905 xioctl(sockfd, SIOCSOUTFILL, &ifre); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
906 } else if (!strcmp(*argv, "add")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
907 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
908 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
909 } else if (!strcmp(*argv, "del")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
910 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
911 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
912 } else if (!strcmp(*argv, "mem_start")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
913 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
914 set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
915 } else if (!strcmp(*argv, "io_addr")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
916 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
917 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
918 } else if (!strcmp(*argv, "irq")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
919 if (!*++argv) show_help(); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
920 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
921 } else { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
922 if (isdigit(**argv) || !strcmp(*argv, "default")) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
923 char *iface_name = ifre.ifr_name; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
924 short int is_colon = 0; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
925 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
926 while (*iface_name) { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
927 if (*iface_name == ':') { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
928 is_colon = 1; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
929 break; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
930 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
931 iface_name++; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
932 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
933 //if the interface name is not an alias; set the flag and continue. |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
934 if(!is_colon) set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
935 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) continue; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
936 else { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
937 errno = EINVAL; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
938 toys.exithelp++; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
939 error_exit("bad argument"); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
940 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
941 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
942 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
943 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
944 if(sockfd > 0) close(sockfd); |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
945 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
946 } |