Mercurial > hg > toybox
annotate toys/pending/ifconfig.c @ 843:1b36ac92e2cc
Drive-by cleanup on ifconfig.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 04 Apr 2013 20:27:08 -0500 |
parents | af5aab6e6678 |
children | f8250900f94b |
rev | line source |
---|---|
841 | 1 /* ifconfig.c - Configure network interface. |
2 * | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com> Kyungwan Han <asura321@gamil.com> |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
4 * Reviewed by Kyungsu Kim <kaspyx@gmail.com>, Kyungwan Han <asura321@gamil.com> |
841 | 5 * |
6 * Not in SUSv4. | |
7 * | |
8 USE_IFCONFIG(NEWTOY(ifconfig, "?a", TOYFLAG_BIN)) | |
9 config IFCONFIG | |
10 bool "ifconfig" | |
11 default y | |
12 help | |
13 usage: ifconfig [-a] interface [address] | |
14 | |
15 Configure network interface. | |
16 | |
17 [add ADDRESS[/PREFIXLEN]] | |
18 [del ADDRESS[/PREFIXLEN]] | |
19 [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]] | |
20 [netmask ADDRESS] [dstaddr ADDRESS] | |
21 [outfill NN] [keepalive NN] | |
22 [hw ether|infiniband ADDRESS] [metric NN] [mtu NN] | |
23 [[-]trailers] [[-]arp] [[-]allmulti] | |
24 [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic] | |
25 [mem_start NN] [io_addr NN] [irq NN] | |
26 [up|down] ... | |
27 */ | |
28 | |
29 #define FOR_ifconfig | |
30 #include "toys.h" | |
31 #include "toynet.h" | |
32 #include <net/route.h> | |
33 #include <stddef.h> | |
34 #include <sys/un.h> | |
35 #include <net/if.h> | |
36 #include <net/if_arp.h> | |
37 #include <net/ethernet.h> | |
38 #include <alloca.h> | |
39 | |
40 typedef struct sockaddr_with_len { | |
41 union { | |
42 struct sockaddr sock; | |
43 struct sockaddr_in sock_in; | |
44 struct sockaddr_in6 sock_in6; | |
45 }sock_u; | |
46 socklen_t socklen; | |
47 }sockaddr_with_len; | |
48 | |
49 //Function Declaration. | |
50 int get_hostname(const char *, struct sockaddr_in *); | |
51 int get_addrinfo(const char *, struct sockaddr_in6 *); | |
52 void get_flag_value(char **, int); | |
53 void display_routes(int, int); | |
54 void setport(struct sockaddr *, unsigned); | |
55 unsigned get_strtou(const char *, char **, int); | |
56 char *address_to_name(const struct sockaddr *); | |
57 sockaddr_with_len *get_sockaddr(const char *, int, sa_family_t); | |
58 | |
59 //Structure Declaration | |
60 typedef struct _proc_net_dev_info { | |
61 char ifrname[IFNAMSIZ]; //interface name. | |
62 unsigned long long receive_bytes; //total bytes received | |
63 unsigned long long receive_packets; //total packets received | |
64 unsigned long receive_errors; //bad packets received | |
65 unsigned long receive_drop; //no space in linux buffers | |
66 unsigned long receive_fifo; //receiver fifo overrun | |
67 unsigned long receive_frame; //received frame alignment error | |
68 unsigned long receive_compressed; | |
69 unsigned long receive_multicast; //multicast packets received | |
70 | |
71 unsigned long long transmit_bytes; //total bytes transmitted | |
72 unsigned long long transmit_packets; //total packets transmitted | |
73 unsigned long transmit_errors; //packet transmit problems | |
74 unsigned long transmit_drop; //no space available in linux | |
75 unsigned long transmit_fifo; | |
76 unsigned long transmit_colls; | |
77 unsigned long transmit_carrier; | |
78 unsigned long transmit_compressed; //num_tr_compressed; | |
79 }PROC_NET_DEV_INFO; | |
80 | |
81 //man netdevice | |
82 typedef struct _iface_list { | |
83 int hw_type; | |
84 short ifrflags; //used for addr, broadcast, and mask. | |
85 short ifaddr; //if set print ifraddr, irrdstaddr, ifrbroadaddr and ifrnetmask. | |
86 struct sockaddr ifraddr; | |
87 struct sockaddr ifrdstaddr; | |
88 struct sockaddr ifrbroadaddr; | |
89 struct sockaddr ifrnetmask; | |
90 struct sockaddr ifrhwaddr; | |
91 int ifrmtu; | |
92 int ifrmetric; | |
93 PROC_NET_DEV_INFO dev_info; | |
94 int txqueuelen; | |
95 struct ifmap ifrmap; | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
96 int non_virtual_iface; |
841 | 97 struct _iface_list *next; //, *prev; |
98 }IFACE_LIST; | |
99 | |
100 | |
101 #define HW_NAME_LEN 20 | |
102 #define HW_TITLE_LEN 30 | |
103 | |
104 typedef struct _hw_info { | |
105 char hw_name[HW_NAME_LEN]; | |
106 char hw_title[HW_TITLE_LEN]; | |
107 int hw_addrlen; | |
108 }HW_INFO; | |
109 | |
110 static const char *const field_format[] = { | |
111 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", | |
112 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u", | |
113 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u" | |
114 }; | |
115 | |
116 #define PROC_NET_DEV "/proc/net/dev" | |
117 #define PROC_NET_IFINET6 "/proc/net/if_inet6" | |
118 #define NO_RANGE -1 | |
119 #define IO_MAP_INDEX 0x100 | |
120 | |
121 static int show_iface(char *iface_name); | |
122 static void print_ip6_addr(IFACE_LIST *l_ptr); | |
123 static void clear_list(void); | |
124 | |
125 //from /net/if.h | |
126 static char *iface_flags_str[] = { | |
127 "UP", | |
128 "BROADCAST", | |
129 "DEBUG", | |
130 "LOOPBACK", | |
131 "POINTOPOINT", | |
132 "NOTRAILERS", | |
133 "RUNNING", | |
134 "NOARP", | |
135 "PROMISC", | |
136 "ALLMULTI", | |
137 "MASTER", | |
138 "SLAVE", | |
139 "MULTICAST", | |
140 "PORTSEL", | |
141 "AUTOMEDIA", | |
142 "DYNAMIC", | |
143 NULL | |
144 }; | |
145 //from /usr/include/linux/netdevice.h | |
146 #ifdef IFF_PORTSEL | |
147 //Media selection options. | |
148 # ifndef IF_PORT_UNKNOWN | |
149 enum { | |
150 IF_PORT_UNKNOWN = 0, | |
151 IF_PORT_10BASE2, | |
152 IF_PORT_10BASET, | |
153 IF_PORT_AUI, | |
154 IF_PORT_100BASET, | |
155 IF_PORT_100BASETX, | |
156 IF_PORT_100BASEFX | |
157 }; | |
158 # endif | |
159 #endif | |
160 | |
161 //from kernel header ipv6.h | |
162 #define IPV6_ADDR_ANY 0x0000U | |
163 #define IPV6_ADDR_LOOPBACK 0x0010U | |
164 #define IPV6_ADDR_LINKLOCAL 0x0020U | |
165 #define IPV6_ADDR_SITELOCAL 0x0040U | |
166 #define IPV6_ADDR_COMPATv4 0x0080U | |
167 | |
168 //================================================================================== | |
169 //for the param settings. | |
170 | |
171 //for ipv6 add/del | |
172 struct ifreq_inet6 { | |
173 struct in6_addr ifrinte6_addr; | |
174 uint32_t ifrinet6_prefixlen; | |
175 int ifrinet6_ifindex; | |
176 }; | |
177 | |
178 #ifndef SIOCSKEEPALIVE | |
179 # define SIOCSKEEPALIVE (SIOCDEVPRIVATE) /* Set keepalive timeout in sec */ | |
180 # define SIOCGKEEPALIVE (SIOCDEVPRIVATE+1) /* Get keepalive timeout */ | |
181 #endif | |
182 | |
183 #ifndef SIOCSOUTFILL | |
184 # define SIOCSOUTFILL (SIOCDEVPRIVATE+2) /* Set outfill timeout */ | |
185 # define SIOCGOUTFILL (SIOCDEVPRIVATE+3) /* Get outfill timeout */ | |
186 #endif | |
187 | |
188 #ifndef INFINIBAND_ALEN | |
189 # define INFINIBAND_ALEN 20 | |
190 #endif | |
191 | |
192 static void set_data(int sockfd, struct ifreq *ifre, char *kval, int request, const char *req_name); | |
193 static void set_flags(int sockfd, struct ifreq *ifre, int arg_flag, int flag); //verify | |
194 static void set_mtu(int sockfd, struct ifreq *ifre, const char *mtu); //verify | |
195 static void set_metric(int sockfd, struct ifreq *ifre, const char *metric); //verify | |
196 static void set_qlen(int sockfd, struct ifreq *ifre, const char *qlen); //verify | |
197 static void set_address(int sockfd, const char *host_name, struct ifreq *ifre, int request, const char *req_name); | |
198 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, const char *req_name); | |
199 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, const char *ipv6_addr, int request, const char *req_name); | |
200 static void set_memstart(int sockfd, struct ifreq *ifre, const char *start_addr, int request, const char *req_name); | |
201 static void set_ioaddr(int sockfd, struct ifreq *ifre, const char *baddr, int request, const char *req_name); | |
202 static void set_irq(int sockfd, struct ifreq *ifre, const char *irq_val, int request, const char *req_name); | |
203 | |
204 char *omit_whitespace(const char *s) | |
205 { | |
206 while(*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9)) s++; | |
207 return (char *) s; | |
208 } | |
209 | |
210 char *safe_strncpy(char *dst, const char *src, size_t size) | |
211 { | |
212 if(!size) return dst; | |
213 dst[--size] = '\0'; | |
214 return strncpy(dst, src, size); | |
215 } | |
216 | |
217 /* | |
218 * used to get the host name from the given ip. | |
219 */ | |
220 int get_hostname(const char *ipstr, struct sockaddr_in *sockin) | |
221 { | |
222 struct hostent *host; | |
223 sockin->sin_family = AF_INET; | |
224 sockin->sin_port = 0; | |
225 | |
226 if(strcmp(ipstr, "default") == 0) { | |
227 sockin->sin_addr.s_addr = INADDR_ANY; | |
228 return 1; | |
229 } | |
230 | |
231 if(inet_aton(ipstr, &sockin->sin_addr)) return 0; | |
232 | |
233 host = gethostbyname(ipstr); | |
234 if(host == NULL) return -1; | |
235 memcpy(&sockin->sin_addr, host->h_addr_list[0], sizeof(struct in_addr)); | |
236 return 0; | |
237 } | |
238 | |
239 /* | |
240 * used to extract the address info from the given ip. | |
241 */ | |
242 int get_addrinfo(const char *ip, struct sockaddr_in6 *sock_in6) | |
243 { | |
244 struct addrinfo hints, *result; | |
245 int status = 0; | |
246 | |
247 memset(&hints, 0, sizeof(struct addrinfo)); | |
248 hints.ai_family = AF_INET6; | |
249 if((status = getaddrinfo(ip, NULL, &hints, &result)) != 0) { | |
250 perror_msg("getaddrinfo: %s", gai_strerror(status)); | |
251 return -1; | |
252 } | |
253 if(result) { | |
254 memcpy(sock_in6, result->ai_addr, sizeof(*sock_in6)); | |
255 freeaddrinfo(result); | |
256 } | |
257 return 0; | |
258 } | |
259 | |
260 /* | |
261 * used to get the flag values for route command. | |
262 */ | |
263 void get_flag_value(char **flagstr, int flags) | |
264 { | |
265 int i = 0; | |
266 char *str = *flagstr; | |
267 static const char flagchars[] = "GHRDMDAC"; | |
268 static const unsigned flagarray[] = { | |
269 RTF_GATEWAY, | |
270 RTF_HOST, | |
271 RTF_REINSTATE, | |
272 RTF_DYNAMIC, | |
273 RTF_MODIFIED, | |
274 RTF_DEFAULT, | |
275 RTF_ADDRCONF, | |
276 RTF_CACHE | |
277 }; | |
278 *str++ = 'U'; | |
279 while( (*str = flagchars[i]) != 0) { | |
280 if(flags & flagarray[i++]) ++str; | |
281 } | |
282 } | |
283 | |
284 /* | |
285 * extract inet4 route info from /proc/net/route file and display it. | |
286 */ | |
287 void display_routes(int is_more_info, int notresolve) | |
288 { | |
289 #define IPV4_MASK (RTF_GATEWAY|RTF_HOST|RTF_REINSTATE|RTF_DYNAMIC|RTF_MODIFIED) | |
290 unsigned long dest, gate, mask; | |
291 int flags, ref, use, metric, mss, win, irtt; | |
292 char iface[64]={0,}; | |
293 char buf[BUFSIZ] = {0,}; | |
294 char *flag_val = xzalloc(10); //there are 9 flags "UGHRDMDAC" for route. | |
295 | |
296 FILE *fp = xfopen("/proc/net/route", "r"); | |
297 | |
298 xprintf("Kernel IP routing table\n" | |
299 "Destination Gateway Genmask Flags %s Iface\n", | |
300 is_more_info ? " MSS Window irtt" : "Metric Ref Use"); | |
301 fgets(buf, BUFSIZ, fp); //skip 1st line. | |
302 while(fgets(buf, BUFSIZ, fp)) { | |
303 int nitems = 0; | |
304 char *destip = NULL, *gateip = NULL, *maskip = NULL; | |
305 memset(flag_val, 0, 10); | |
306 | |
307 nitems = sscanf(buf, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n", | |
308 iface, &dest, &gate, &flags, &ref, &use, &metric, &mask, &mss, &win, &irtt); | |
309 if(nitems != 11) {//EOF with no (nonspace) chars read... | |
310 if((nitems < 0) && feof(fp)) break; | |
311 perror_exit("fscanf"); | |
312 } | |
313 //skip down interfaces... | |
314 if(!(flags & RTF_UP)) continue; | |
315 | |
316 //For Destination | |
317 if(dest){ | |
318 if(inet_ntop(AF_INET, &dest, buf, BUFSIZ) > 0) destip = xstrdup(buf); | |
319 } | |
320 else { | |
321 if(!notresolve) destip = xstrdup("default"); | |
322 else destip = xstrdup("0.0.0.0"); | |
323 } | |
324 //For Gateway | |
325 if(gate){ | |
326 if(inet_ntop(AF_INET, &gate, buf, BUFSIZ) > 0) gateip = xstrdup(buf); | |
327 } | |
328 else { | |
329 if(!notresolve) gateip = xstrdup("*"); | |
330 else gateip = xstrdup("0.0.0.0"); | |
331 } | |
332 //For Mask | |
333 if(inet_ntop(AF_INET, &mask, buf, BUFSIZ) > 0) maskip = xstrdup(buf); | |
334 | |
335 //Get flag Values | |
336 get_flag_value(&flag_val, (flags & IPV4_MASK)); | |
337 if(flags & RTF_REJECT) flag_val[0] = '!'; | |
338 xprintf("%-15.15s %-15.15s %-16s%-6s", destip, gateip, maskip, flag_val); | |
339 if(destip) free(destip); | |
340 if(gateip) free(gateip); | |
341 if(maskip) free(maskip); | |
342 if(is_more_info) xprintf("%5d %-5d %6d %s\n", mss, win, irtt, iface); | |
343 else xprintf("%-6d %-2d %7d %s\n", metric, ref, use, iface); | |
344 }//end of while... | |
345 fclose(fp); | |
346 if(flag_val) free(flag_val); | |
347 #undef IPV4_MASK | |
348 return; | |
349 } | |
350 | |
351 /* | |
352 * verify the host is local unix path. | |
353 * if so, set the swl input param accordingly. | |
354 */ | |
355 static int is_host_unix(const char *host, sockaddr_with_len **swl) | |
356 { | |
357 if(strncmp(host, "local:", 6) == 0) { | |
358 struct sockaddr_un *sockun; | |
359 *swl = xzalloc(sizeof(struct sockaddr_with_len)); | |
360 (*swl)->socklen = sizeof(struct sockaddr_un); | |
361 (*swl)->sock_u.sock.sa_family = AF_UNIX; | |
362 sockun = (struct sockaddr_un *)&(*swl)->sock_u.sock; | |
363 safe_strncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path)); | |
364 return 1; | |
365 } | |
366 return 0; | |
367 } | |
368 | |
369 /* | |
370 * validate the input param (host) for valid ipv6 ip and extract port number (if there). | |
371 */ | |
372 static void get_host_and_port(char **host, int *port) | |
373 { | |
374 char *ch_ptr; | |
375 const char *org_host = *host; | |
376 if(*host[0] == '[') { | |
377 (*host)++; | |
378 ch_ptr = strchr(*host, ']'); | |
379 if(!ch_ptr || (ch_ptr[1] != ':' && ch_ptr[1] != '\0')) | |
380 error_exit("bad address '%s'", org_host); | |
381 } | |
382 else { | |
383 ch_ptr = strrchr(*host, ':'); | |
384 //There is more than one ':' like "::1" | |
385 if(ch_ptr && strchr(*host, ':') != ch_ptr) | |
386 ch_ptr = NULL; | |
387 } | |
388 if(ch_ptr) { //pointer to ":" or "]:" | |
389 int size = ch_ptr - (*host) + 1; | |
390 safe_strncpy(*host, *host, size); | |
391 if(*ch_ptr != ':') { | |
392 ch_ptr++; //skip ']' | |
393 //[nn] without port | |
394 if(*ch_ptr == '\0') | |
395 return; | |
396 } | |
397 ch_ptr++; //skip ':' to get the port number. | |
398 *port = get_strtou(ch_ptr, NULL, 10); | |
843
1b36ac92e2cc
Drive-by cleanup on ifconfig.
Rob Landley <rob@landley.net>
parents:
842
diff
changeset
|
399 if(errno || (unsigned)*port > 65535) |
841 | 400 error_exit("bad port spec '%s'", org_host); |
401 } | |
402 return; | |
403 } | |
404 | |
405 /* | |
406 * used to extract the address info from the given host ip | |
407 * and update the swl param accordingly. | |
408 */ | |
409 static int get_socket_stream(const char *host, sa_family_t af, sockaddr_with_len **swl) | |
410 { | |
411 struct addrinfo hints; | |
412 struct addrinfo *result, *rp; | |
413 int status = 0; | |
414 | |
415 memset(&hints, 0 , sizeof(struct addrinfo)); | |
416 hints.ai_family = af; | |
417 hints.ai_socktype = SOCK_STREAM; | |
418 | |
419 if((status = getaddrinfo(host, NULL, &hints, &result)) != 0) { | |
420 perror_exit("bad address '%s' : %s", host, gai_strerror(status)); | |
421 return status; | |
422 } | |
423 | |
424 for(rp = result; rp != NULL; rp = rp->ai_next) { | |
425 if( (rp->ai_family == AF_INET) || (rp->ai_family == AF_INET6)) { | |
426 *swl = xmalloc(sizeof(struct sockaddr_with_len)); | |
427 (*swl)->socklen = rp->ai_addrlen; | |
428 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen); | |
429 break; | |
430 } | |
431 } | |
432 freeaddrinfo(result); | |
433 return ((!rp)? -1: status); | |
434 } | |
435 | |
436 /* | |
437 * use to get the socket address with the given host ip. | |
438 */ | |
439 sockaddr_with_len *get_sockaddr(const char *host, int port, sa_family_t af) | |
440 { | |
441 sockaddr_with_len *swl = NULL; | |
442 int status = 0; | |
443 | |
444 //for unix | |
445 int is_unix = is_host_unix(host, &swl); | |
446 if(is_unix && swl) return swl; | |
447 | |
448 //[IPV6_ip]:port_num | |
449 if(host[0] == '[' || strrchr(host, ':')) get_host_and_port((char **)&host, &port); | |
450 | |
451 //for the socket streams. | |
452 status = get_socket_stream(host, af, &swl); | |
453 if(status) return NULL; | |
454 | |
455 setport(&swl->sock_u.sock, htons(port)); | |
456 return swl; | |
457 } | |
458 | |
459 /* | |
460 * get the numeric hostname and service name, for a given socket address. | |
461 */ | |
462 char *address_to_name(const struct sockaddr *sock) | |
463 { | |
464 //man page of getnameinfo. | |
465 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,}; | |
466 int status = 0; | |
467 if(sock->sa_family == AF_INET) { | |
468 socklen_t len = sizeof(struct sockaddr_in); | |
469 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) | |
470 return xmsprintf("%s:%s", hbuf, sbuf); | |
471 else { | |
472 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); | |
473 return NULL; | |
474 } | |
475 } | |
476 else if(sock->sa_family == AF_INET6) { | |
477 socklen_t len = sizeof(struct sockaddr_in6); | |
478 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) { | |
479 //verification for resolved hostname. | |
480 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf); | |
481 else return xmsprintf("%s:%s", hbuf, sbuf); | |
482 } | |
483 else { | |
484 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); | |
485 return NULL; | |
486 } | |
487 } | |
488 else if(sock->sa_family == AF_UNIX) { | |
489 struct sockaddr_un *sockun = (void*)sock; | |
490 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path); | |
491 } | |
492 else | |
493 return NULL; | |
494 } | |
495 | |
496 /* | |
497 * used to set the port number for ipv4 / ipv6 addresses. | |
498 */ | |
499 void setport(struct sockaddr *sock, unsigned port_num) | |
500 { | |
501 //for ipv4 | |
502 if(sock->sa_family == AF_INET) { | |
503 struct sockaddr_in *sock_in = (void*)sock; | |
504 sock_in->sin_port = port_num; | |
505 } | |
506 //for ipv6 | |
507 else if(sock->sa_family == AF_INET6) { | |
508 struct sockaddr_in6 *sock_in6 = (void*)sock; | |
509 sock_in6->sin6_port = port_num; | |
510 } | |
511 return; | |
512 } | |
513 | |
514 /* | |
515 * used to converts string into int and validate the input str for invalid int value or out-of-range. | |
516 */ | |
517 unsigned get_strtou(const char *str, char **endp, int base) | |
518 { | |
519 unsigned long uli; | |
520 char *endptr; | |
521 | |
522 if(!isalnum(str[0])) { | |
523 errno = ERANGE; | |
524 return UINT_MAX; | |
525 } | |
526 errno = 0; | |
527 uli = strtoul(str, &endptr, base); | |
528 if(uli > UINT_MAX) { | |
529 errno = ERANGE; | |
530 return UINT_MAX; | |
531 } | |
532 | |
533 if(endp) *endp = endptr; | |
534 if(endptr[0]) { | |
535 if(isalnum(endptr[0]) || errno) { //"123abc" or out-of-range | |
536 errno = ERANGE; | |
537 return UINT_MAX; | |
538 } | |
539 errno = EINVAL; | |
540 } | |
541 return uli; | |
542 } | |
543 | |
544 | |
545 IFACE_LIST *iface_list_head; | |
546 | |
547 /* | |
548 * display help info and exit from application. | |
549 */ | |
550 static void show_help(void) | |
551 { | |
552 char **arg = xzalloc(sizeof(char*) *3); | |
553 arg[0] = "help"; | |
554 arg[1] = xstrdup(toys.which->name); | |
555 toy_exec(arg); | |
556 } | |
557 | |
558 void ifconfig_main(void) | |
559 { | |
560 char **argv = toys.optargs; | |
561 | |
562 if(argv[0] && (strcmp(argv[0], "--help") == 0)) | |
563 show_help(); | |
564 | |
565 //"ifconfig" / "ifconfig eth0" | |
566 if(!argv[0] || !argv[1]) { //one or no argument | |
567 toys.exitval = show_iface(argv[0]); | |
568 //free allocated memory. | |
569 clear_list(); | |
570 return; | |
571 } | |
572 | |
573 //set ifconfig params. | |
574 { | |
575 struct ifreq ifre; | |
576 int sockfd = 0; | |
577 //get interface name | |
578 memset(&ifre, 0, sizeof(struct ifreq)); | |
579 strncpy(ifre.ifr_name, *argv, IFNAMSIZ); | |
580 ifre.ifr_name[IFNAMSIZ-1] = 0; | |
581 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | |
582 perror_exit("cannot open control socket\n", 1); | |
583 | |
584 while(*++argv != NULL) { | |
585 /* flags settings */ | |
586 if(strcmp(argv[0], "up") == 0) | |
587 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); | |
588 else if(strcmp(argv[0], "down") == 0) | |
589 set_flags(sockfd, &ifre, 0, IFF_UP); | |
590 | |
591 else if(strcmp(argv[0], "arp") == 0) | |
592 set_flags(sockfd, &ifre, 0, IFF_NOARP); | |
593 else if(strcmp(argv[0], "-arp") == 0) | |
594 set_flags(sockfd, &ifre, IFF_NOARP, 0); | |
595 else if(strcmp(argv[0], "trailers") == 0) | |
596 set_flags(sockfd, &ifre, 0, IFF_NOTRAILERS); | |
597 else if(strcmp(argv[0], "-trailers") == 0) | |
598 set_flags(sockfd, &ifre, IFF_NOTRAILERS, 0); | |
599 | |
600 else if(strcmp(argv[0], "promisc") == 0) | |
601 set_flags(sockfd, &ifre, IFF_PROMISC, 0); | |
602 else if(strcmp(argv[0], "-promisc") == 0) | |
603 set_flags(sockfd, &ifre, 0, IFF_PROMISC); | |
604 else if(strcmp(argv[0], "allmulti") == 0) | |
605 set_flags(sockfd, &ifre, IFF_ALLMULTI, 0); | |
606 else if(strcmp(argv[0], "-allmulti") == 0) | |
607 set_flags(sockfd, &ifre, 0, IFF_ALLMULTI); | |
608 else if(strcmp(argv[0], "multicast") == 0) | |
609 set_flags(sockfd, &ifre, IFF_MULTICAST, 0); | |
610 else if(strcmp(argv[0], "-multicast") == 0) | |
611 set_flags(sockfd, &ifre, 0, IFF_MULTICAST); | |
612 else if(strcmp(argv[0], "dynamic") == 0) | |
613 set_flags(sockfd, &ifre, IFF_DYNAMIC, 0); | |
614 else if(strcmp(argv[0], "-dynamic") == 0) | |
615 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC); | |
616 else if(strcmp(argv[0], "-pointopoint") == 0) | |
617 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT); | |
618 /*value setup */ | |
619 else if(strcmp(argv[0], "pointopoint") == 0) { | |
620 if(*++argv == NULL) { | |
621 errno = EINVAL; | |
622 show_help(); | |
623 } | |
624 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); | |
625 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0); | |
626 } | |
627 else if(strcmp(argv[0], "netmask") == 0) { | |
628 if(*++argv == NULL) { | |
629 errno = EINVAL; | |
630 show_help(); | |
631 } | |
632 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK"); | |
633 } | |
634 else if(strcmp(argv[0], "-broadcast") == 0) { | |
635 set_flags(sockfd, &ifre, 0, IFF_BROADCAST); | |
636 } | |
637 else if(strcmp(argv[0], "broadcast") == 0) { | |
638 if(*++argv == NULL) { | |
639 errno = EINVAL; | |
640 show_help(); | |
641 } | |
642 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR"); | |
643 set_flags(sockfd, &ifre, IFF_BROADCAST, 0); | |
644 } | |
645 else if(strcmp(argv[0], "dstaddr") == 0) { | |
646 if(*++argv == NULL) { | |
647 errno = EINVAL; | |
648 show_help(); | |
649 } | |
650 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); | |
651 } | |
652 else if(strcmp(argv[0], "hw") == 0) { | |
653 if(*++argv == NULL) { | |
654 errno = EINVAL; | |
655 show_help(); | |
656 } | |
657 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR"); | |
658 } | |
659 else if(strcmp(argv[0], "mtu") == 0) { | |
660 if(*++argv == NULL) { | |
661 errno = EINVAL; | |
662 show_help(); | |
663 } | |
664 set_mtu(sockfd, &ifre, argv[0]); | |
665 }//end of mtu | |
666 else if(strcmp(argv[0], "metric") == 0) { | |
667 if(*++argv == NULL) { | |
668 errno = EINVAL; | |
669 show_help(); | |
670 } | |
671 set_metric(sockfd, &ifre, argv[0]); | |
672 }//end of metric | |
673 else if(strcmp(argv[0], "txqueuelen") == 0) { | |
674 if(*++argv == NULL) { | |
675 errno = EINVAL; | |
676 show_help(); | |
677 } | |
678 set_qlen(sockfd, &ifre, argv[0]); | |
679 }//end of txqueuelen | |
680 else if(strcmp(argv[0], "keepalive") == 0) { | |
681 if(*++argv == NULL) { | |
682 errno = EINVAL; | |
683 show_help(); | |
684 } | |
685 set_data(sockfd, &ifre, argv[0], SIOCSKEEPALIVE, "SIOCSKEEPALIVE"); | |
686 }//end of keepalive | |
687 else if(strcmp(argv[0], "outfill") == 0) { | |
688 if(*++argv == NULL) { | |
689 errno = EINVAL; | |
690 show_help(); | |
691 } | |
692 set_data(sockfd, &ifre, argv[0], SIOCSOUTFILL, "SIOCSOUTFILL"); | |
693 }//end of outfill | |
694 else if(strcmp(argv[0], "add") == 0) { | |
695 if(*++argv == NULL) { | |
696 errno = EINVAL; | |
697 show_help(); | |
698 } | |
699 set_ipv6_addr(sockfd, &ifre, argv[0], SIOCSIFADDR, "SIOCSIFADDR"); | |
700 }//end of add ipv6 addr | |
701 else if(strcmp(argv[0], "del") == 0) { | |
702 if(*++argv == NULL) { | |
703 errno = EINVAL; | |
704 show_help(); | |
705 } | |
706 set_ipv6_addr(sockfd, &ifre, argv[0], SIOCDIFADDR, "SIOCDIFADDR"); | |
707 }//end of del ipv6 addr | |
708 else if(strcmp(argv[0], "mem_start") == 0) { | |
709 if(*++argv == NULL) { | |
710 errno = EINVAL; | |
711 show_help(); | |
712 } | |
713 set_memstart(sockfd, &ifre, argv[0], SIOCSIFMAP, "SIOCSIFMAP"); | |
714 }//end of mem_start | |
715 else if(strcmp(argv[0], "io_addr") == 0) { | |
716 if(*++argv == NULL) { | |
717 errno = EINVAL; | |
718 show_help(); | |
719 } | |
720 set_ioaddr(sockfd, &ifre, argv[0], SIOCSIFMAP, "SIOCSIFMAP"); | |
721 }//end of io_addr | |
722 else if(strcmp(argv[0], "irq") == 0) { | |
723 if(*++argv == NULL) { | |
724 errno = EINVAL; | |
725 show_help(); | |
726 } | |
727 set_irq(sockfd, &ifre, argv[0], SIOCSIFMAP, "SIOCSIFMAP"); | |
728 }//end of irq | |
729 else { | |
730 if(isdigit(argv[0][0]) || (strcmp(argv[0], "default") == 0)) { | |
731 char *iface_name = ifre.ifr_name; | |
732 short int is_colon = 0; | |
733 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR"); | |
734 while(*iface_name) { | |
735 if(*iface_name == ':') { | |
736 is_colon = 1; | |
737 break; | |
738 } | |
739 iface_name++; | |
740 } | |
741 //if the interface name is not an alias; set the flag and continue. | |
742 if(!is_colon) | |
743 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); | |
744 } | |
745 else if((strcmp(argv[0], "inet") == 0) || (strcmp(argv[0], "inet6") == 0)) | |
746 continue; | |
747 else { | |
748 errno = EINVAL; | |
749 show_help(); | |
750 } | |
751 }//set ip for an interface. | |
752 | |
753 }//end of while. | |
754 if(sockfd > 0) | |
755 close(sockfd); | |
756 } | |
757 return; | |
758 }//End of main function. | |
759 | |
760 | |
761 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag) | |
762 { | |
763 if(ioctl(sockfd, SIOCGIFFLAGS, ifre) < 0) | |
764 perror_exit("SIOCGIFFLAGS"); | |
765 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag; | |
766 if(ioctl(sockfd, SIOCSIFFLAGS, ifre) < 0) | |
767 perror_exit("SIOCSIFFLAGS"); | |
768 return; | |
769 } | |
770 | |
771 static void set_data(int sockfd, struct ifreq *ifre, char *kval, int request, const char *req_name) | |
772 { | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
773 unsigned long val = strtoul(kval, NULL, 0); |
841 | 774 char *ptr; |
775 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_data); | |
776 (*(__caddr_t *)ptr) = (__caddr_t)val; | |
777 | |
778 if(ioctl(sockfd, request, ifre) < 0) { | |
779 perror_exit((char *)req_name); | |
780 } | |
781 return; | |
782 } | |
783 static void set_mtu(int sockfd, struct ifreq *ifre, const char *mtu) | |
784 { | |
785 ifre->ifr_mtu = strtoul(mtu, NULL, 0); | |
786 if(ioctl(sockfd, SIOCSIFMTU, ifre) < 0) | |
787 perror_exit("SIOCSIFMTU"); | |
788 return; | |
789 } | |
790 | |
791 static void set_metric(int sockfd, struct ifreq *ifre, const char *metric) | |
792 { | |
793 ifre->ifr_metric = strtoul(metric, NULL, 0); | |
794 if(ioctl(sockfd, SIOCSIFMETRIC, ifre) < 0) | |
795 perror_exit("SIOCSIFMETRIC"); | |
796 return; | |
797 } | |
798 | |
799 static void set_qlen(int sockfd, struct ifreq *ifre, const char *qlen) | |
800 { | |
801 ifre->ifr_qlen = strtoul(qlen, NULL, 0); | |
802 if(ioctl(sockfd, SIOCSIFTXQLEN, ifre) < 0) | |
803 perror_exit("SIOCSIFTXQLEN"); | |
804 return; | |
805 } | |
806 | |
807 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, const char *ipv6_addr, int request, const char *req_name) | |
808 { | |
809 char *prefix; | |
810 int plen = 0; | |
811 sockaddr_with_len *swl = NULL; | |
812 | |
813 prefix = strchr(ipv6_addr, '/'); | |
814 if(prefix) { | |
815 plen = get_int_value(prefix + 1, 0, 128); | |
816 *prefix = '\0'; | |
817 } | |
818 swl = get_sockaddr(ipv6_addr, 0, AF_INET6); | |
819 if(!swl) error_exit("error in resolving host name"); | |
820 int sockfd6; | |
821 struct ifreq_inet6 ifre6; | |
822 memcpy((char *) &ifre6.ifrinte6_addr, | |
823 (char *) &(swl->sock_u.sock_in6.sin6_addr), | |
824 sizeof(struct in6_addr)); | |
825 //Create a channel to the NET kernel. | |
826 if( (sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) | |
827 perror_exit("AF_INET6 SOCK_DGRAM", 0); | |
828 if(ioctl(sockfd6, SIOGIFINDEX, ifre) < 0) | |
829 perror_exit("SIOGIFINDEX"); | |
830 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex; | |
831 ifre6.ifrinet6_prefixlen = plen; | |
832 | |
833 if(ioctl(sockfd6, request, &ifre6) < 0) | |
834 perror_exit((char *)req_name); | |
835 if(swl != NULL) { | |
836 free(swl); | |
837 swl = NULL; | |
838 } | |
839 return; | |
840 } | |
841 | |
842 static void set_address(int sockfd, const char *host_name, struct ifreq *ifre, int request, const char *req_name) | |
843 { | |
844 struct sockaddr_in sock_in; | |
845 sockaddr_with_len *swl = NULL; | |
846 sock_in.sin_family = AF_INET; | |
847 sock_in.sin_port = 0; | |
848 | |
849 //Default 0.0.0.0 | |
850 if(strcmp(host_name, "default") == 0) | |
851 sock_in.sin_addr.s_addr = INADDR_ANY; | |
852 else { | |
853 swl = get_sockaddr(host_name, 0, AF_INET); | |
854 if(!swl) error_exit("error in resolving host name"); | |
855 | |
856 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr; | |
857 } | |
858 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr)); | |
859 if(ioctl(sockfd, request, ifre) < 0) | |
860 perror_exit((char *)req_name); | |
861 | |
862 if(swl != NULL) { | |
863 free(swl); | |
864 swl = NULL; | |
865 } | |
866 return; | |
867 } | |
868 | |
869 static int hex_to_binary(const char *hw_addr, struct sockaddr *sock, int count) | |
870 { | |
871 int i = 0, j = 0; | |
872 unsigned char nib_val; | |
873 unsigned char ch; | |
874 | |
875 char *ptr = (char *) sock->sa_data; | |
876 if(count == ETH_ALEN) | |
877 sock->sa_family = ARPHRD_ETHER; | |
878 else if(count == INFINIBAND_ALEN) | |
879 sock->sa_family = ARPHRD_INFINIBAND; | |
880 else | |
881 return -1; | |
882 //e.g. hw_addr "62:2D:A6:9E:2D:BE" | |
883 for(; *hw_addr && (i < count); i++) { | |
884 if(*hw_addr == ':') | |
885 hw_addr++; | |
886 j = nib_val = 0; | |
887 for(;j < 2; j++) { | |
888 ch = *hw_addr; | |
889 //0-9 = 10 chars. | |
890 if(((unsigned char)(ch - '0')) < 10) | |
891 ch = (ch - '0'); | |
892 //a-f = 6 chars. | |
893 else if(((unsigned char)((ch) - 'a')) < 6) | |
894 ch = (ch - ('a'-10)); | |
895 //A-F = 6 chars. | |
896 else if(((unsigned char)((ch) - 'A')) < 6) | |
897 ch = (ch - ('A'-10)); | |
898 else if(j && (ch == ':' || ch == 0)) | |
899 break; | |
900 else | |
901 return -1; | |
902 hw_addr++; | |
903 nib_val <<= 4; | |
904 nib_val += ch; | |
905 } | |
906 *ptr++ = nib_val; | |
907 } | |
908 if(*hw_addr) | |
909 return -1; | |
910 return 0; | |
911 } | |
912 | |
913 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, const char *req_name) | |
914 { | |
915 int hw_class = 0; | |
916 char *hw_addr; | |
917 struct sockaddr sock; | |
918 char *ptr; | |
919 char *hw_class_strings[] = { | |
920 "ether", | |
921 "infiniband", | |
922 NULL | |
923 }; | |
924 | |
925 if(strcmp(hw_class_strings[0], **argv) == 0) | |
926 hw_class = 1; | |
927 else if(strcmp(hw_class_strings[1], **argv) == 0) | |
928 hw_class = 2; | |
929 if(!hw_class || !(*argv += 1)) | |
930 show_help(); | |
931 | |
932 memset(&sock, 0, sizeof(struct sockaddr)); | |
933 hw_addr = **argv; | |
934 if(hw_class == 1) { | |
935 if(hex_to_binary(hw_addr, &sock, ETH_ALEN)) | |
936 error_exit("invalid hw-addr %s", hw_addr); | |
937 } | |
938 else { | |
939 if(hex_to_binary(hw_addr, &sock, INFINIBAND_ALEN)) | |
940 error_exit("invalid hw-addr %s", hw_addr); | |
941 } | |
942 ptr = (char *)&sock; | |
943 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr)); | |
944 if(ioctl(sockfd, request, ifre) < 0) | |
945 perror_exit((char *)req_name); | |
946 return; | |
947 } | |
948 | |
949 static void set_memstart(int sockfd, struct ifreq *ifre, const char *start_addr, int request, const char *req_name) | |
950 { | |
951 unsigned long mem_start = strtoul(start_addr, NULL, 0); | |
952 | |
953 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) | |
954 perror_exit("SIOCGIFMAP"); | |
955 ifre->ifr_map.mem_start = mem_start; | |
956 if(ioctl(sockfd, request, ifre) < 0) | |
957 perror_exit((char *)req_name); | |
958 return; | |
959 } | |
960 | |
961 static void set_ioaddr(int sockfd, struct ifreq *ifre, const char *baddr, int request, const char *req_name) | |
962 { | |
963 unsigned short int base_addr = strtoul(baddr, NULL, 0); | |
964 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) | |
965 perror_exit("SIOCGIFMAP"); | |
966 ifre->ifr_map.base_addr = base_addr; | |
967 if(ioctl(sockfd, request, ifre) < 0) | |
968 perror_exit((char *)req_name); | |
969 return; | |
970 } | |
971 | |
972 static void set_irq(int sockfd, struct ifreq *ifre, const char *irq_val, int request, const char *req_name) | |
973 { | |
974 unsigned short int irq = strtoul(irq_val, NULL, 0); | |
975 char *ptr; | |
976 struct ifmap *map; | |
977 | |
978 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) | |
979 perror_exit("SIOCGIFMAP"); | |
980 | |
981 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map); | |
982 map = (struct ifmap *)ptr; | |
983 map->irq = irq; | |
984 if(ioctl(sockfd, request, ifre) < 0) | |
985 perror_exit((char *)req_name); | |
986 return; | |
987 } | |
988 | |
989 /* Display ifconfig info. */ | |
990 static void get_proc_info(char *buff, IFACE_LIST *l_ptr, int version) | |
991 { | |
992 char *name; | |
993 memset(&l_ptr->dev_info, 0, sizeof(PROC_NET_DEV_INFO)); | |
994 | |
995 buff = omit_whitespace(buff); | |
996 name = strsep(&buff, ":"); | |
997 if(!buff) | |
998 error_exit("error in getting the device name:"); | |
999 | |
1000 if(strlen(name) < (IFNAMSIZ)) { | |
1001 strncpy(l_ptr->dev_info.ifrname, name, IFNAMSIZ-1); | |
1002 l_ptr->dev_info.ifrname[IFNAMSIZ-1] = '\0'; | |
1003 } | |
1004 else { | |
1005 l_ptr->dev_info.ifrname[0] = '\0'; | |
1006 } | |
1007 | |
1008 sscanf(buff, field_format[version], | |
1009 &l_ptr->dev_info.receive_bytes, | |
1010 &l_ptr->dev_info.receive_packets, | |
1011 &l_ptr->dev_info.receive_errors, | |
1012 &l_ptr->dev_info.receive_drop, | |
1013 &l_ptr->dev_info.receive_fifo, | |
1014 &l_ptr->dev_info.receive_frame, | |
1015 &l_ptr->dev_info.receive_compressed, | |
1016 &l_ptr->dev_info.receive_multicast, | |
1017 &l_ptr->dev_info.transmit_bytes, | |
1018 &l_ptr->dev_info.transmit_packets, | |
1019 &l_ptr->dev_info.transmit_errors, | |
1020 &l_ptr->dev_info.transmit_drop, | |
1021 &l_ptr->dev_info.transmit_fifo, | |
1022 &l_ptr->dev_info.transmit_colls, | |
1023 &l_ptr->dev_info.transmit_carrier, | |
1024 &l_ptr->dev_info.transmit_compressed | |
1025 ); | |
1026 | |
1027 if(version == 0) | |
1028 l_ptr->dev_info.receive_bytes = l_ptr->dev_info.transmit_bytes = 0; | |
1029 if(version == 1) | |
1030 l_ptr->dev_info.receive_multicast = l_ptr->dev_info.receive_compressed = l_ptr->dev_info.transmit_compressed = 0; | |
1031 return; | |
1032 } | |
1033 | |
1034 static void add_iface_to_list(IFACE_LIST *newnode) | |
1035 { | |
1036 IFACE_LIST *head_ref = iface_list_head; | |
1037 | |
1038 if((head_ref == NULL) || strcmp(newnode->dev_info.ifrname, head_ref->dev_info.ifrname) < 0) { | |
1039 newnode->next = head_ref; | |
1040 head_ref = newnode; | |
1041 } | |
1042 else { | |
1043 IFACE_LIST *current = head_ref; | |
1044 while(current->next != NULL && (strcmp(current->next->dev_info.ifrname, newnode->dev_info.ifrname)) < 0) | |
1045 current = current->next; | |
1046 newnode->next = current->next; | |
1047 current->next = newnode; | |
1048 } | |
1049 iface_list_head = head_ref; | |
1050 return; | |
1051 } | |
1052 | |
1053 static int get_device_info(IFACE_LIST *l_ptr) | |
1054 { | |
1055 struct ifreq ifre; | |
1056 char *ifrname = l_ptr->dev_info.ifrname; | |
1057 int sokfd; | |
1058 | |
1059 sokfd = socket(AF_INET, SOCK_DGRAM, 0); | |
1060 if(sokfd < 0) | |
1061 return sokfd; | |
1062 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1063 if(ioctl(sokfd, SIOCGIFFLAGS, &ifre) < 0) { | |
1064 close(sokfd); | |
1065 return NO_RANGE; | |
1066 } | |
1067 l_ptr->ifrflags = ifre.ifr_flags; | |
1068 | |
1069 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1070 if(ioctl(sokfd, SIOCGIFHWADDR, &ifre) >= 0) | |
1071 memcpy(l_ptr->ifrhwaddr.sa_data, ifre.ifr_hwaddr.sa_data, sizeof(l_ptr->ifrhwaddr.sa_data)); | |
1072 | |
1073 l_ptr->hw_type = ifre.ifr_hwaddr.sa_family; | |
1074 | |
1075 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1076 if(ioctl(sokfd, SIOCGIFMETRIC, &ifre) >= 0) | |
1077 l_ptr->ifrmetric = ifre.ifr_metric; | |
1078 | |
1079 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1080 if(ioctl(sokfd, SIOCGIFMTU, &ifre) >= 0) | |
1081 l_ptr->ifrmtu = ifre.ifr_mtu; | |
1082 | |
1083 #ifdef SIOCGIFMAP | |
1084 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1085 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) | |
1086 l_ptr->ifrmap = ifre.ifr_map; | |
1087 #endif | |
1088 | |
1089 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1090 l_ptr->txqueuelen = NO_RANGE; | |
1091 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) | |
1092 l_ptr->txqueuelen = ifre.ifr_qlen; | |
1093 | |
1094 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1095 ifre.ifr_addr.sa_family = AF_INET; | |
1096 | |
1097 if(ioctl(sokfd, SIOCGIFADDR, &ifre) == 0) { | |
1098 l_ptr->ifaddr = 1; | |
1099 l_ptr->ifraddr = ifre.ifr_addr; | |
1100 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1101 if(ioctl(sokfd, SIOCGIFDSTADDR, &ifre) >= 0) | |
1102 l_ptr->ifrdstaddr = ifre.ifr_dstaddr; | |
1103 | |
1104 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1105 if(ioctl(sokfd, SIOCGIFBRDADDR, &ifre) >= 0) | |
1106 l_ptr->ifrbroadaddr = ifre.ifr_broadaddr; | |
1107 | |
1108 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
1109 if(ioctl(sokfd, SIOCGIFNETMASK, &ifre) >= 0) | |
1110 l_ptr->ifrnetmask = ifre.ifr_netmask; | |
1111 } | |
1112 close(sokfd); | |
1113 return 0; | |
1114 } | |
1115 | |
1116 static void get_ifconfig_info(void) | |
1117 { | |
1118 IFACE_LIST *l_ptr; | |
1119 char buff[BUFSIZ] = {0,}; | |
1120 int version_num = 0; | |
1121 | |
1122 FILE *fp = fopen(PROC_NET_DEV, "r"); | |
1123 if(fp == NULL) | |
1124 return; | |
1125 | |
1126 fgets(buff, sizeof(buff), fp); //skip 1st header line. | |
1127 fgets(buff, sizeof(buff), fp); //skip 2nd header line. | |
1128 | |
1129 if(strstr(buff, "compressed")) | |
1130 version_num = 2; | |
1131 else if(strstr(buff, "bytes")) | |
1132 version_num = 1; | |
1133 else | |
1134 version_num = 0; | |
1135 | |
1136 while(fgets(buff, BUFSIZ, fp)) { | |
1137 l_ptr = xzalloc(sizeof(IFACE_LIST)); | |
1138 get_proc_info(buff, l_ptr, version_num); | |
1139 add_iface_to_list(l_ptr); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1140 l_ptr->non_virtual_iface = 1; |
841 | 1141 errno = 0; |
1142 if(get_device_info(l_ptr) < 0) { | |
1143 const char *errstr = strerror(errno); | |
1144 fclose(fp); | |
1145 fp = NULL; | |
1146 clear_list(); | |
1147 perror_exit("%s: error getting interface info: %s", l_ptr->dev_info.ifrname, errstr); | |
1148 } | |
1149 }//end of while. | |
1150 fclose(fp); | |
1151 fp = NULL; | |
1152 return; | |
1153 } | |
1154 | |
1155 static void get_hw_info(int hw_type, HW_INFO *hw_info) | |
1156 { | |
1157 #define HW_UNSPEC -1 | |
1158 switch(hw_type) { | |
1159 case ARPHRD_LOOPBACK: //Loopback device. | |
1160 strncpy(hw_info->hw_name, "loop", HW_NAME_LEN); | |
1161 strncpy(hw_info->hw_title, "Local Loopback", HW_TITLE_LEN); | |
1162 hw_info->hw_addrlen = 0; | |
1163 break; | |
1164 case ARPHRD_ETHER: //Ethernet | |
1165 strncpy(hw_info->hw_name, "ether", HW_NAME_LEN); | |
1166 strncpy(hw_info->hw_title, "Ethernet", HW_TITLE_LEN); | |
1167 hw_info->hw_addrlen = ETH_ALEN; | |
1168 break; | |
1169 case ARPHRD_PPP: //ARPHRD_PPP | |
1170 strncpy(hw_info->hw_name, "ppp", HW_NAME_LEN); | |
1171 strncpy(hw_info->hw_title, "Point-to-Point Protocol", HW_TITLE_LEN); | |
1172 hw_info->hw_addrlen = 0; | |
1173 break; | |
1174 case ARPHRD_INFINIBAND: //InfiniBand | |
1175 strncpy(hw_info->hw_name, "infiniband", HW_NAME_LEN); | |
1176 strncpy(hw_info->hw_title, "InfiniBand", HW_TITLE_LEN); | |
1177 hw_info->hw_addrlen = 20; | |
1178 break; | |
1179 case ARPHRD_SIT: //sit0 device - IPv6-in-IPv4 | |
1180 strncpy(hw_info->hw_name, "sit", HW_NAME_LEN); | |
1181 strncpy(hw_info->hw_title, "IPv6-in-IPv4", HW_TITLE_LEN); | |
1182 hw_info->hw_addrlen = 0; | |
1183 break; | |
1184 case HW_UNSPEC: //UNSPEC | |
1185 strncpy(hw_info->hw_name, "unspec", HW_NAME_LEN); | |
1186 strncpy(hw_info->hw_title, "UNSPEC", HW_TITLE_LEN); | |
1187 hw_info->hw_addrlen = 0; | |
1188 break; | |
1189 default: | |
1190 break; | |
1191 } | |
1192 #undef HW_UNSPEC | |
1193 return; | |
1194 } | |
1195 | |
1196 static void print_hw_addr(int hw_type, HW_INFO hw_info, IFACE_LIST *l_ptr) | |
1197 { | |
1198 unsigned char *address = (unsigned char *) l_ptr->ifrhwaddr.sa_data; | |
1199 if(!address || (hw_info.hw_addrlen == 0)) | |
1200 return; | |
1201 xprintf("HWaddr "); | |
1202 if(hw_type == ARPHRD_ETHER) | |
1203 xprintf("%02X:%02X:%02X:%02X:%02X:%02X", address[0], address[1], address[2], | |
1204 address[3], address[4], address[5]); | |
1205 return; | |
1206 } | |
1207 | |
1208 static const char *get_ip_addr(struct sockaddr *skaddr) | |
1209 { | |
1210 static char *ip_str = NULL; | |
1211 struct sockaddr_in *sin; | |
1212 | |
1213 if(skaddr->sa_family == 0xFFFF || skaddr->sa_family == 0) | |
1214 return "[NOT SET]"; | |
1215 sin = (struct sockaddr_in *)skaddr; | |
1216 if(sin->sin_family != AF_INET) { | |
1217 errno = EAFNOSUPPORT; | |
1218 return NULL; | |
1219 } | |
1220 if( (ip_str = inet_ntoa(sin->sin_addr)) != NULL) | |
1221 return ip_str; | |
1222 | |
1223 return NULL; | |
1224 } | |
1225 | |
1226 static void print_ip_addr(IFACE_LIST *l_ptr) | |
1227 { | |
1228 const char *af_name; | |
1229 int af = l_ptr->ifraddr.sa_family; | |
1230 if(af == AF_INET) | |
1231 af_name = "inet"; | |
1232 else if(af == AF_INET6) | |
1233 af_name = "inet6"; | |
1234 else if(af == AF_UNSPEC) | |
1235 af_name = "unspec"; | |
1236 | |
1237 xprintf("%10s%s addr:%s ", " ", af_name, get_ip_addr(&l_ptr->ifraddr)); | |
1238 if(l_ptr->ifrflags & IFF_POINTOPOINT) | |
1239 xprintf(" P-t-P:%s ", get_ip_addr(&l_ptr->ifrdstaddr)); | |
1240 if(l_ptr->ifrflags & IFF_BROADCAST) | |
1241 xprintf(" Bcast:%s ", get_ip_addr(&l_ptr->ifrbroadaddr)); | |
1242 xprintf(" Mask:%s\n", get_ip_addr(&l_ptr->ifrnetmask)); | |
1243 return; | |
1244 } | |
1245 | |
1246 static void print_iface_flags(IFACE_LIST *l_ptr) | |
1247 { | |
1248 if(l_ptr->ifrflags != 0) { | |
1249 unsigned short mask = 1; | |
1250 char **str = iface_flags_str; | |
1251 for(; *str != NULL; str++) { | |
1252 if(l_ptr->ifrflags & mask) | |
1253 xprintf("%s ", *str); | |
1254 mask = mask << 1; | |
1255 } | |
1256 } | |
1257 else | |
1258 xprintf("[NO FLAGS] "); | |
1259 return; | |
1260 } | |
1261 | |
1262 static void print_media(IFACE_LIST *l_ptr) | |
1263 { | |
1264 #ifdef IFF_PORTSEL | |
1265 if(l_ptr->ifrflags & IFF_PORTSEL) { | |
1266 xprintf("Media:"); | |
1267 if(l_ptr->ifrmap.port == IF_PORT_UNKNOWN) | |
1268 xprintf("%s", "unknown"); | |
1269 else if(l_ptr->ifrmap.port == IF_PORT_10BASE2) | |
1270 xprintf("%s", "10base2"); | |
1271 else if(l_ptr->ifrmap.port == IF_PORT_10BASET) | |
1272 xprintf("%s", "10baseT"); | |
1273 else if(l_ptr->ifrmap.port == IF_PORT_AUI) | |
1274 xprintf("%s", "AUI"); | |
1275 else if(l_ptr->ifrmap.port == IF_PORT_100BASET) | |
1276 xprintf("%s", "100baseT"); | |
1277 else if(l_ptr->ifrmap.port == IF_PORT_100BASETX) | |
1278 xprintf("%s", "100baseTX"); | |
1279 else if(l_ptr->ifrmap.port == IF_PORT_100BASEFX) | |
1280 xprintf("%s", "100baseFX"); | |
1281 if(l_ptr->ifrflags & IFF_AUTOMEDIA) | |
1282 xprintf("(auto)"); | |
1283 } | |
1284 #endif | |
1285 return; | |
1286 } | |
1287 | |
1288 static void print_ip6_addr(IFACE_LIST *l_ptr) | |
1289 { | |
1290 char iface_name[IFNAMSIZ] = {0,}; | |
1291 char buf[BUFSIZ] = {0,}; | |
1292 int plen, scope; | |
1293 | |
1294 FILE *fp = fopen(PROC_NET_IFINET6, "r"); | |
1295 if(fp == NULL) | |
1296 return; | |
1297 | |
1298 while(fgets(buf, BUFSIZ, fp)) { | |
1299 int nitems = 0; | |
1300 char ipv6_addr[40] = {0,}; | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1301 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n", |
841 | 1302 ipv6_addr+7, &plen, &scope, iface_name); |
1303 if(nitems != 4) { | |
1304 if((nitems < 0) && feof(fp)) | |
1305 break; | |
1306 perror_exit("sscanf"); | |
1307 } | |
1308 if(strcmp(l_ptr->dev_info.ifrname,iface_name) == 0) { | |
1309 int i = 0; | |
1310 struct sockaddr_in6 sock_in6; | |
1311 int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]); | |
1312 char *ptr = ipv6_addr+7; | |
1313 while((i < len-2) && (*ptr)) { | |
1314 ipv6_addr[i++] = *ptr++; | |
1315 //put ':' after 4th bit | |
1316 if(!((i+1) % 5)) | |
1317 ipv6_addr[i++] = ':'; | |
1318 } | |
1319 ipv6_addr[i+1] = '\0'; | |
1320 if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) { | |
1321 sock_in6.sin6_family = AF_INET6; | |
1322 memset(buf, 0, (sizeof(buf) /sizeof(buf[0]))); | |
1323 if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, buf, BUFSIZ) > 0) { | |
1324 xprintf("%10sinet6 addr: %s/%d", " ", buf, plen); | |
1325 xprintf(" Scope:"); | |
1326 if(scope == IPV6_ADDR_ANY) xprintf(" Global"); | |
1327 else if(scope == IPV6_ADDR_LOOPBACK) xprintf(" Host"); | |
1328 else if(scope == IPV6_ADDR_LINKLOCAL) xprintf(" Link"); | |
1329 else if(scope == IPV6_ADDR_SITELOCAL) xprintf(" Site"); | |
1330 else if(scope == IPV6_ADDR_COMPATv4) xprintf(" Compat"); | |
1331 else xprintf("Unknown"); | |
1332 xprintf("\n"); | |
1333 } | |
1334 } | |
1335 } | |
1336 }//end of while. | |
1337 fclose(fp); | |
1338 fp = NULL; | |
1339 return; | |
1340 } | |
1341 | |
1342 static void display_ifconfig(IFACE_LIST *l_ptr) | |
1343 { | |
1344 HW_INFO hw_info; | |
1345 int hw_type = l_ptr->hw_type; | |
1346 | |
1347 memset(&hw_info, 0, sizeof(HW_INFO)); | |
1348 | |
1349 get_hw_info(hw_type, &hw_info); | |
1350 xprintf("%-9s Link encap:%s ", l_ptr->dev_info.ifrname, hw_info.hw_title); | |
1351 print_hw_addr(hw_type, hw_info, l_ptr); | |
1352 | |
1353 print_media(l_ptr); | |
1354 | |
1355 xprintf("\n"); | |
1356 if(l_ptr->ifaddr) | |
1357 print_ip_addr(l_ptr); //print addr, p-p addr, broadcast addr and mask addr. | |
1358 | |
1359 //for ipv6 to do. | |
1360 print_ip6_addr(l_ptr); | |
1361 xprintf("%10s", " "); | |
1362 //print flags | |
1363 print_iface_flags(l_ptr); | |
1364 if(!l_ptr->ifrmetric) | |
1365 l_ptr->ifrmetric = 1; | |
1366 xprintf(" MTU:%d Metric:%d", l_ptr->ifrmtu, l_ptr->ifrmetric); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1367 xprintf("\n"); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1368 if(l_ptr->non_virtual_iface) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1369 xprintf("%10s", " "); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1370 xprintf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n", |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1371 l_ptr->dev_info.receive_packets, l_ptr->dev_info.receive_errors, |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1372 l_ptr->dev_info.receive_drop, l_ptr->dev_info.receive_fifo, |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1373 l_ptr->dev_info.receive_frame); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1374 //Dummy types for non ARP hardware. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1375 if((hw_type == ARPHRD_CSLIP) || (hw_type == ARPHRD_CSLIP6)) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1376 xprintf("%10scompressed:%lu\n", " ", l_ptr->dev_info.receive_compressed); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1377 xprintf("%10sTX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n", " ", |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1378 l_ptr->dev_info.transmit_packets, l_ptr->dev_info.transmit_errors, |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1379 l_ptr->dev_info.transmit_drop, l_ptr->dev_info.transmit_fifo, |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1380 l_ptr->dev_info.transmit_carrier); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1381 xprintf("%10scollisions:%lu ", " ", l_ptr->dev_info.transmit_colls); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1382 //Dummy types for non ARP hardware. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1383 if((hw_type == ARPHRD_CSLIP) || (hw_type == ARPHRD_CSLIP6)) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1384 xprintf("compressed:%lu ", l_ptr->dev_info.transmit_compressed); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1385 if(l_ptr->txqueuelen != NO_RANGE) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1386 xprintf("txqueuelen:%d ", l_ptr->txqueuelen); |
841 | 1387 |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1388 xprintf("\n%10s", " "); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1389 xprintf("RX bytes:%llu ", l_ptr->dev_info.receive_bytes); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1390 xprintf("TX bytes:%llu\n", l_ptr->dev_info.transmit_bytes); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1391 } |
841 | 1392 if(l_ptr->ifrmap.irq || l_ptr->ifrmap.mem_start || l_ptr->ifrmap.dma || l_ptr->ifrmap.base_addr) { |
1393 xprintf("%10s", " "); | |
1394 if(l_ptr->ifrmap.irq) | |
1395 xprintf("Interrupt:%d ", l_ptr->ifrmap.irq); | |
1396 if(l_ptr->ifrmap.base_addr >= IO_MAP_INDEX) | |
1397 xprintf("Base address:0x%lx ", l_ptr->ifrmap.base_addr); | |
1398 if(l_ptr->ifrmap.mem_start) | |
1399 xprintf("Memory:%lx-%lx ", l_ptr->ifrmap.mem_start, l_ptr->ifrmap.mem_end); | |
1400 if(l_ptr->ifrmap.dma) | |
1401 xprintf("DMA chan:%x ", l_ptr->ifrmap.dma); | |
1402 xprintf("\n"); | |
1403 } | |
1404 xprintf("\n"); | |
1405 return; | |
1406 } | |
1407 | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1408 static int readconf(void) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1409 { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1410 #define NUM_OF_REQUESTS 30 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1411 int num_of_req = NUM_OF_REQUESTS; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1412 struct ifconf ifcon; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1413 struct ifreq *ifre; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1414 int num, status = -1, sokfd; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1415 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1416 ifcon.ifc_buf = NULL; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1417 sokfd = socket(AF_INET, SOCK_DGRAM, 0); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1418 if(sokfd < 0) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1419 perror_msg("error: no inet socket available"); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1420 return -1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1421 } |
843
1b36ac92e2cc
Drive-by cleanup on ifconfig.
Rob Landley <rob@landley.net>
parents:
842
diff
changeset
|
1422 for (;;) { |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1423 ifcon.ifc_len = sizeof(struct ifreq) * num_of_req; //Size of buffer. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1424 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1425 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1426 if((status = ioctl(sokfd, SIOCGIFCONF, &ifcon)) == -1) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1427 perror_msg("ioctl %#x failed", SIOCGIFCONF); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1428 goto LOOP_BREAK; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1429 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1430 //in case of overflow, increase number of requests and retry. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1431 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1432 num_of_req += 10; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1433 continue; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1434 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1435 break; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1436 }//End of while loop |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1437 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1438 ifre = ifcon.ifc_req; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1439 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1440 //Escape duplicate values from the list. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1441 IFACE_LIST *list_ptr; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1442 int match_found = 0; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1443 for(list_ptr = iface_list_head; list_ptr != NULL; list_ptr = list_ptr->next) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1444 //if interface already in the list then donot add it in the list. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1445 if(!strcmp(ifre->ifr_name, list_ptr->dev_info.ifrname)) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1446 match_found = 1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1447 break; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1448 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1449 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1450 if(!match_found) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1451 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST)); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1452 safe_strncpy(l_ptr->dev_info.ifrname, ifre->ifr_name, IFNAMSIZ); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1453 add_iface_to_list(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1454 errno = 0; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1455 if(get_device_info(l_ptr) < 0) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1456 clear_list(); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1457 perror_exit("%s: error getting interface info: %s", l_ptr->dev_info.ifrname, strerror(errno)); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1458 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1459 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1460 }//End of for loop. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1461 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1462 LOOP_BREAK: |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1463 close(sokfd); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1464 free(ifcon.ifc_buf); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1465 #undef NUM_OF_REQUESTS |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1466 return status; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1467 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1468 |
841 | 1469 static int show_iface(char *iface_name) |
1470 { | |
1471 get_ifconfig_info(); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1472 |
841 | 1473 if(iface_name) { |
1474 IFACE_LIST *l_ptr; | |
1475 int is_dev_found = 0; | |
1476 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) { | |
1477 if(strcmp(l_ptr->dev_info.ifrname, iface_name) == 0) { | |
1478 is_dev_found = 1; | |
1479 display_ifconfig(l_ptr); | |
1480 break; | |
1481 } | |
1482 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1483 //if the given interface is not in the list. |
841 | 1484 if(!is_dev_found) { |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1485 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST)); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1486 safe_strncpy(l_ptr->dev_info.ifrname, iface_name, IFNAMSIZ); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1487 errno = 0; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1488 if(get_device_info(l_ptr) < 0) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1489 const char *errmsg; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1490 if(errno == ENODEV) errmsg = "Device not found"; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1491 else errmsg = strerror(errno); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1492 error_msg("%s: error getting interface info: %s", iface_name, errmsg); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1493 free(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1494 return 1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1495 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1496 else display_ifconfig(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1497 free(l_ptr); |
841 | 1498 } |
1499 } | |
1500 else { | |
1501 IFACE_LIST *l_ptr; | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1502 if(readconf() < 0) return 1; |
841 | 1503 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) { |
1504 if((l_ptr->ifrflags & IFF_UP) || (toys.optflags & FLAG_a)) | |
1505 display_ifconfig(l_ptr); | |
1506 } | |
1507 } | |
1508 return 0; | |
1509 } | |
1510 | |
1511 static void clear_list(void) | |
1512 { | |
1513 IFACE_LIST *temp_ptr; | |
1514 while(iface_list_head != NULL) { | |
1515 temp_ptr = iface_list_head->next; | |
1516 free(iface_list_head); | |
1517 iface_list_head = temp_ptr; | |
1518 } | |
1519 return; | |
1520 } |