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