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