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