Mercurial > hg > toybox
annotate toys/pending/ifconfig.c @ 862:53cee02f1811
More ifconfig cleanup.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 16 Apr 2013 23:01:22 -0500 |
parents | 35b059e1654b |
children | 493f412fc5da |
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 | |
852 | 384 if(*argv && (strcmp(*argv, "--help") == 0)) |
841 | 385 show_help(); |
386 | |
387 //"ifconfig" / "ifconfig eth0" | |
388 if(!argv[0] || !argv[1]) { //one or no argument | |
852 | 389 toys.exitval = show_iface(*argv); |
841 | 390 //free allocated memory. |
391 clear_list(); | |
392 return; | |
393 } | |
394 | |
395 //set ifconfig params. | |
396 { | |
397 struct ifreq ifre; | |
398 int sockfd = 0; | |
399 //get interface name | |
400 memset(&ifre, 0, sizeof(struct ifreq)); | |
401 strncpy(ifre.ifr_name, *argv, IFNAMSIZ); | |
402 ifre.ifr_name[IFNAMSIZ-1] = 0; | |
403 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | |
852 | 404 perror_exit("socket"); |
841 | 405 |
406 while(*++argv != NULL) { | |
407 /* flags settings */ | |
852 | 408 if (!strcmp(*argv, "up")) |
841 | 409 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); |
852 | 410 else if (!strcmp(*argv, "down")) |
841 | 411 set_flags(sockfd, &ifre, 0, IFF_UP); |
412 | |
852 | 413 else if (!strcmp(*argv, "arp")) |
841 | 414 set_flags(sockfd, &ifre, 0, IFF_NOARP); |
852 | 415 else if (!strcmp(*argv, "-arp")) |
841 | 416 set_flags(sockfd, &ifre, IFF_NOARP, 0); |
852 | 417 else if (!strcmp(*argv, "trailers")) |
841 | 418 set_flags(sockfd, &ifre, 0, IFF_NOTRAILERS); |
852 | 419 else if (!strcmp(*argv, "-trailers")) |
841 | 420 set_flags(sockfd, &ifre, IFF_NOTRAILERS, 0); |
421 | |
852 | 422 else if (!strcmp(*argv, "promisc")) |
841 | 423 set_flags(sockfd, &ifre, IFF_PROMISC, 0); |
852 | 424 else if (!strcmp(*argv, "-promisc")) |
841 | 425 set_flags(sockfd, &ifre, 0, IFF_PROMISC); |
852 | 426 else if (!strcmp(*argv, "allmulti")) |
841 | 427 set_flags(sockfd, &ifre, IFF_ALLMULTI, 0); |
852 | 428 else if (!strcmp(*argv, "-allmulti")) |
841 | 429 set_flags(sockfd, &ifre, 0, IFF_ALLMULTI); |
852 | 430 else if (!strcmp(*argv, "multicast")) |
841 | 431 set_flags(sockfd, &ifre, IFF_MULTICAST, 0); |
852 | 432 else if (!strcmp(*argv, "-multicast")) |
841 | 433 set_flags(sockfd, &ifre, 0, IFF_MULTICAST); |
852 | 434 else if (!strcmp(*argv, "dynamic")) |
841 | 435 set_flags(sockfd, &ifre, IFF_DYNAMIC, 0); |
852 | 436 else if (!strcmp(*argv, "-dynamic")) |
841 | 437 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC); |
852 | 438 else if (!strcmp(*argv, "-pointopoint")) |
841 | 439 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT); |
440 /*value setup */ | |
852 | 441 else if (!strcmp(*argv, "pointopoint")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
442 show_help(); |
841 | 443 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); |
444 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0); | |
852 | 445 } else if (!strcmp(*argv, "netmask")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
446 show_help(); |
841 | 447 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK"); |
852 | 448 } else if (!strcmp(*argv, "-broadcast")) { |
841 | 449 set_flags(sockfd, &ifre, 0, IFF_BROADCAST); |
852 | 450 } else if (!strcmp(*argv, "broadcast")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
451 show_help(); |
841 | 452 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR"); |
453 set_flags(sockfd, &ifre, IFF_BROADCAST, 0); | |
852 | 454 } else if (!strcmp(*argv, "dstaddr")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
455 show_help(); |
841 | 456 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); |
852 | 457 } else if (!strcmp(*argv, "hw")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
458 show_help(); |
841 | 459 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR"); |
852 | 460 } else if (!strcmp(*argv, "mtu")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
461 show_help(); |
852 | 462 set_mtu(sockfd, &ifre, *argv); |
463 } else if (!strcmp(*argv, "metric")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
464 show_help(); |
852 | 465 set_metric(sockfd, &ifre, *argv); |
466 } else if (!strcmp(*argv, "txqueuelen")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
467 show_help(); |
852 | 468 set_qlen(sockfd, &ifre, *argv); |
469 } else if (!strcmp(*argv, "keepalive")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
470 show_help(); |
852 | 471 set_data(sockfd, &ifre, *argv, SIOCSKEEPALIVE, "SIOCSKEEPALIVE"); |
841 | 472 }//end of keepalive |
852 | 473 else if (!strcmp(*argv, "outfill")) { |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
474 show_help(); |
852 | 475 set_data(sockfd, &ifre, *argv, SIOCSOUTFILL, "SIOCSOUTFILL"); |
476 } else if (!strcmp(*argv, "add")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
477 show_help(); |
852 | 478 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR"); |
479 } else if (!strcmp(*argv, "del")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
480 show_help(); |
852 | 481 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR"); |
482 } else if (!strcmp(*argv, "mem_start")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
483 show_help(); |
852 | 484 set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
485 } else if (!strcmp(*argv, "io_addr")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
486 show_help(); |
852 | 487 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
488 } else if (!strcmp(*argv, "irq")) { | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
489 show_help(); |
852 | 490 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); |
491 } else { | |
492 if(isdigit(**argv) || !strcmp(*argv, "default")) { | |
841 | 493 char *iface_name = ifre.ifr_name; |
494 short int is_colon = 0; | |
495 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR"); | |
496 while(*iface_name) { | |
497 if(*iface_name == ':') { | |
498 is_colon = 1; | |
499 break; | |
500 } | |
501 iface_name++; | |
502 } | |
503 //if the interface name is not an alias; set the flag and continue. | |
504 if(!is_colon) | |
505 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); | |
852 | 506 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) |
841 | 507 continue; |
508 else { | |
509 errno = EINVAL; | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
510 toys.exithelp++; |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
511 error_exit("bad argument"); |
841 | 512 } |
852 | 513 } |
841 | 514 |
852 | 515 } |
516 if(sockfd > 0) close(sockfd); | |
841 | 517 } |
852 | 518 } |
841 | 519 |
520 | |
521 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag) | |
522 { | |
523 if(ioctl(sockfd, SIOCGIFFLAGS, ifre) < 0) | |
524 perror_exit("SIOCGIFFLAGS"); | |
525 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag; | |
526 if(ioctl(sockfd, SIOCSIFFLAGS, ifre) < 0) | |
527 perror_exit("SIOCSIFFLAGS"); | |
528 return; | |
529 } | |
530 | |
852 | 531 static void set_data(int sockfd, struct ifreq *ifre, char *kval, int request, char *req_name) |
841 | 532 { |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
533 unsigned long val = strtoul(kval, NULL, 0); |
841 | 534 char *ptr; |
535 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
|
536 (*(char * *)ptr) = (char *)val; |
841 | 537 |
538 if(ioctl(sockfd, request, ifre) < 0) { | |
539 perror_exit((char *)req_name); | |
540 } | |
541 return; | |
542 } | |
852 | 543 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu) |
841 | 544 { |
545 ifre->ifr_mtu = strtoul(mtu, NULL, 0); | |
546 if(ioctl(sockfd, SIOCSIFMTU, ifre) < 0) | |
547 perror_exit("SIOCSIFMTU"); | |
548 return; | |
549 } | |
550 | |
852 | 551 static void set_metric(int sockfd, struct ifreq *ifre, char *metric) |
841 | 552 { |
553 ifre->ifr_metric = strtoul(metric, NULL, 0); | |
554 if(ioctl(sockfd, SIOCSIFMETRIC, ifre) < 0) | |
555 perror_exit("SIOCSIFMETRIC"); | |
556 return; | |
557 } | |
558 | |
852 | 559 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen) |
841 | 560 { |
561 ifre->ifr_qlen = strtoul(qlen, NULL, 0); | |
562 if(ioctl(sockfd, SIOCSIFTXQLEN, ifre) < 0) | |
563 perror_exit("SIOCSIFTXQLEN"); | |
564 return; | |
565 } | |
566 | |
852 | 567 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name) |
841 | 568 { |
569 char *prefix; | |
570 int plen = 0; | |
571 sockaddr_with_len *swl = NULL; | |
572 | |
573 prefix = strchr(ipv6_addr, '/'); | |
574 if(prefix) { | |
575 plen = get_int_value(prefix + 1, 0, 128); | |
576 *prefix = '\0'; | |
577 } | |
578 swl = get_sockaddr(ipv6_addr, 0, AF_INET6); | |
579 if(!swl) error_exit("error in resolving host name"); | |
580 int sockfd6; | |
581 struct ifreq_inet6 ifre6; | |
582 memcpy((char *) &ifre6.ifrinte6_addr, | |
583 (char *) &(swl->sock_u.sock_in6.sin6_addr), | |
584 sizeof(struct in6_addr)); | |
585 //Create a channel to the NET kernel. | |
586 if( (sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) | |
587 perror_exit("AF_INET6 SOCK_DGRAM", 0); | |
588 if(ioctl(sockfd6, SIOGIFINDEX, ifre) < 0) | |
589 perror_exit("SIOGIFINDEX"); | |
590 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex; | |
591 ifre6.ifrinet6_prefixlen = plen; | |
592 | |
593 if(ioctl(sockfd6, request, &ifre6) < 0) | |
594 perror_exit((char *)req_name); | |
595 if(swl != NULL) { | |
596 free(swl); | |
597 swl = NULL; | |
598 } | |
599 return; | |
600 } | |
601 | |
852 | 602 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name) |
841 | 603 { |
604 struct sockaddr_in sock_in; | |
605 sockaddr_with_len *swl = NULL; | |
606 sock_in.sin_family = AF_INET; | |
607 sock_in.sin_port = 0; | |
608 | |
609 //Default 0.0.0.0 | |
610 if(strcmp(host_name, "default") == 0) | |
611 sock_in.sin_addr.s_addr = INADDR_ANY; | |
612 else { | |
613 swl = get_sockaddr(host_name, 0, AF_INET); | |
614 if(!swl) error_exit("error in resolving host name"); | |
615 | |
616 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr; | |
617 } | |
618 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr)); | |
619 if(ioctl(sockfd, request, ifre) < 0) | |
620 perror_exit((char *)req_name); | |
621 | |
622 if(swl != NULL) { | |
623 free(swl); | |
624 swl = NULL; | |
625 } | |
626 return; | |
627 } | |
628 | |
852 | 629 static int hex_to_binary(char *hw_addr, struct sockaddr *sock, int count) |
841 | 630 { |
631 int i = 0, j = 0; | |
632 unsigned char nib_val; | |
633 unsigned char ch; | |
634 | |
635 char *ptr = (char *) sock->sa_data; | |
636 if(count == ETH_ALEN) | |
637 sock->sa_family = ARPHRD_ETHER; | |
638 else if(count == INFINIBAND_ALEN) | |
639 sock->sa_family = ARPHRD_INFINIBAND; | |
640 else | |
641 return -1; | |
642 //e.g. hw_addr "62:2D:A6:9E:2D:BE" | |
643 for(; *hw_addr && (i < count); i++) { | |
644 if(*hw_addr == ':') | |
645 hw_addr++; | |
646 j = nib_val = 0; | |
647 for(;j < 2; j++) { | |
648 ch = *hw_addr; | |
649 //0-9 = 10 chars. | |
650 if(((unsigned char)(ch - '0')) < 10) | |
651 ch = (ch - '0'); | |
652 //a-f = 6 chars. | |
653 else if(((unsigned char)((ch) - 'a')) < 6) | |
654 ch = (ch - ('a'-10)); | |
655 //A-F = 6 chars. | |
656 else if(((unsigned char)((ch) - 'A')) < 6) | |
657 ch = (ch - ('A'-10)); | |
658 else if(j && (ch == ':' || ch == 0)) | |
659 break; | |
660 else | |
661 return -1; | |
662 hw_addr++; | |
663 nib_val <<= 4; | |
664 nib_val += ch; | |
665 } | |
666 *ptr++ = nib_val; | |
667 } | |
668 if(*hw_addr) | |
669 return -1; | |
670 return 0; | |
671 } | |
672 | |
852 | 673 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name) |
841 | 674 { |
675 int hw_class = 0; | |
676 char *hw_addr; | |
677 struct sockaddr sock; | |
678 char *ptr; | |
679 char *hw_class_strings[] = { | |
680 "ether", | |
681 "infiniband", | |
682 NULL | |
683 }; | |
684 | |
685 if(strcmp(hw_class_strings[0], **argv) == 0) | |
686 hw_class = 1; | |
687 else if(strcmp(hw_class_strings[1], **argv) == 0) | |
688 hw_class = 2; | |
861
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
689 if(!hw_class || !(*argv += 1)) { |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
690 errno = EINVAL; |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
691 toys.exithelp++; |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
692 error_exit("bad hardware class"); |
35b059e1654b
Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents:
856
diff
changeset
|
693 } |
841 | 694 |
695 memset(&sock, 0, sizeof(struct sockaddr)); | |
696 hw_addr = **argv; | |
697 if(hw_class == 1) { | |
698 if(hex_to_binary(hw_addr, &sock, ETH_ALEN)) | |
699 error_exit("invalid hw-addr %s", hw_addr); | |
700 } | |
701 else { | |
702 if(hex_to_binary(hw_addr, &sock, INFINIBAND_ALEN)) | |
703 error_exit("invalid hw-addr %s", hw_addr); | |
704 } | |
705 ptr = (char *)&sock; | |
706 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr)); | |
707 if(ioctl(sockfd, request, ifre) < 0) | |
708 perror_exit((char *)req_name); | |
709 return; | |
710 } | |
711 | |
852 | 712 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name) |
841 | 713 { |
714 unsigned long mem_start = strtoul(start_addr, NULL, 0); | |
715 | |
716 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) | |
717 perror_exit("SIOCGIFMAP"); | |
718 ifre->ifr_map.mem_start = mem_start; | |
719 if(ioctl(sockfd, request, ifre) < 0) | |
720 perror_exit((char *)req_name); | |
721 return; | |
722 } | |
723 | |
852 | 724 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name) |
841 | 725 { |
726 unsigned short int base_addr = strtoul(baddr, NULL, 0); | |
727 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) | |
728 perror_exit("SIOCGIFMAP"); | |
729 ifre->ifr_map.base_addr = base_addr; | |
730 if(ioctl(sockfd, request, ifre) < 0) | |
731 perror_exit((char *)req_name); | |
732 return; | |
733 } | |
734 | |
852 | 735 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name) |
841 | 736 { |
737 unsigned short int irq = strtoul(irq_val, NULL, 0); | |
738 char *ptr; | |
739 struct ifmap *map; | |
740 | |
852 | 741 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) perror_exit("SIOCGIFMAP"); |
841 | 742 |
743 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map); | |
744 map = (struct ifmap *)ptr; | |
745 map->irq = irq; | |
852 | 746 if(ioctl(sockfd, request, ifre) < 0) perror_exit(req_name); |
841 | 747 return; |
748 } | |
749 | |
750 /* Display ifconfig info. */ | |
751 static void get_proc_info(char *buff, IFACE_LIST *l_ptr, int version) | |
752 { | |
753 char *name; | |
754 memset(&l_ptr->dev_info, 0, sizeof(PROC_NET_DEV_INFO)); | |
755 | |
756 buff = omit_whitespace(buff); | |
757 name = strsep(&buff, ":"); | |
758 if(!buff) | |
759 error_exit("error in getting the device name:"); | |
760 | |
761 if(strlen(name) < (IFNAMSIZ)) { | |
762 strncpy(l_ptr->dev_info.ifrname, name, IFNAMSIZ-1); | |
763 l_ptr->dev_info.ifrname[IFNAMSIZ-1] = '\0'; | |
764 } | |
765 else { | |
766 l_ptr->dev_info.ifrname[0] = '\0'; | |
767 } | |
768 | |
769 sscanf(buff, field_format[version], | |
770 &l_ptr->dev_info.receive_bytes, | |
771 &l_ptr->dev_info.receive_packets, | |
772 &l_ptr->dev_info.receive_errors, | |
773 &l_ptr->dev_info.receive_drop, | |
774 &l_ptr->dev_info.receive_fifo, | |
775 &l_ptr->dev_info.receive_frame, | |
776 &l_ptr->dev_info.receive_compressed, | |
777 &l_ptr->dev_info.receive_multicast, | |
778 &l_ptr->dev_info.transmit_bytes, | |
779 &l_ptr->dev_info.transmit_packets, | |
780 &l_ptr->dev_info.transmit_errors, | |
781 &l_ptr->dev_info.transmit_drop, | |
782 &l_ptr->dev_info.transmit_fifo, | |
783 &l_ptr->dev_info.transmit_colls, | |
784 &l_ptr->dev_info.transmit_carrier, | |
785 &l_ptr->dev_info.transmit_compressed | |
786 ); | |
787 | |
788 if(version == 0) | |
789 l_ptr->dev_info.receive_bytes = l_ptr->dev_info.transmit_bytes = 0; | |
790 if(version == 1) | |
791 l_ptr->dev_info.receive_multicast = l_ptr->dev_info.receive_compressed = l_ptr->dev_info.transmit_compressed = 0; | |
792 return; | |
793 } | |
794 | |
795 static void add_iface_to_list(IFACE_LIST *newnode) | |
796 { | |
797 IFACE_LIST *head_ref = iface_list_head; | |
798 | |
799 if((head_ref == NULL) || strcmp(newnode->dev_info.ifrname, head_ref->dev_info.ifrname) < 0) { | |
800 newnode->next = head_ref; | |
801 head_ref = newnode; | |
862 | 802 } else { |
841 | 803 IFACE_LIST *current = head_ref; |
804 while(current->next != NULL && (strcmp(current->next->dev_info.ifrname, newnode->dev_info.ifrname)) < 0) | |
805 current = current->next; | |
806 newnode->next = current->next; | |
807 current->next = newnode; | |
808 } | |
809 iface_list_head = head_ref; | |
810 } | |
811 | |
812 static int get_device_info(IFACE_LIST *l_ptr) | |
813 { | |
814 struct ifreq ifre; | |
815 char *ifrname = l_ptr->dev_info.ifrname; | |
816 int sokfd; | |
817 | |
862 | 818 if ((sokfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return sokfd; |
841 | 819 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); |
820 if(ioctl(sokfd, SIOCGIFFLAGS, &ifre) < 0) { | |
821 close(sokfd); | |
822 return NO_RANGE; | |
823 } | |
824 l_ptr->ifrflags = ifre.ifr_flags; | |
825 | |
826 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
827 if(ioctl(sokfd, SIOCGIFHWADDR, &ifre) >= 0) | |
828 memcpy(l_ptr->ifrhwaddr.sa_data, ifre.ifr_hwaddr.sa_data, sizeof(l_ptr->ifrhwaddr.sa_data)); | |
829 | |
830 l_ptr->hw_type = ifre.ifr_hwaddr.sa_family; | |
831 | |
832 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
833 if(ioctl(sokfd, SIOCGIFMETRIC, &ifre) >= 0) | |
834 l_ptr->ifrmetric = ifre.ifr_metric; | |
835 | |
836 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
837 if(ioctl(sokfd, SIOCGIFMTU, &ifre) >= 0) | |
838 l_ptr->ifrmtu = ifre.ifr_mtu; | |
839 | |
840 #ifdef SIOCGIFMAP | |
841 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
842 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) | |
843 l_ptr->ifrmap = ifre.ifr_map; | |
844 #endif | |
845 | |
846 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
847 l_ptr->txqueuelen = NO_RANGE; | |
848 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) | |
849 l_ptr->txqueuelen = ifre.ifr_qlen; | |
850 | |
851 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
852 ifre.ifr_addr.sa_family = AF_INET; | |
853 | |
854 if(ioctl(sokfd, SIOCGIFADDR, &ifre) == 0) { | |
855 l_ptr->ifaddr = 1; | |
856 l_ptr->ifraddr = ifre.ifr_addr; | |
857 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
858 if(ioctl(sokfd, SIOCGIFDSTADDR, &ifre) >= 0) | |
859 l_ptr->ifrdstaddr = ifre.ifr_dstaddr; | |
860 | |
861 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
862 if(ioctl(sokfd, SIOCGIFBRDADDR, &ifre) >= 0) | |
863 l_ptr->ifrbroadaddr = ifre.ifr_broadaddr; | |
864 | |
865 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ); | |
866 if(ioctl(sokfd, SIOCGIFNETMASK, &ifre) >= 0) | |
867 l_ptr->ifrnetmask = ifre.ifr_netmask; | |
868 } | |
869 close(sokfd); | |
870 return 0; | |
871 } | |
872 | |
873 static void get_ifconfig_info(void) | |
874 { | |
875 IFACE_LIST *l_ptr; | |
876 int version_num = 0; | |
877 | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
878 FILE *fp = fopen("/proc/net/dev", "r"); |
862 | 879 if (!fp) return; |
841 | 880 |
862 | 881 fgets(toybuf, sizeof(toybuf), fp); //skip 1st header line. |
882 fgets(toybuf, sizeof(toybuf), fp); //skip 2nd header line. | |
841 | 883 |
862 | 884 if(strstr(toybuf, "compressed")) version_num = 2; |
885 else if(strstr(toybuf, "bytes")) version_num = 1; | |
886 else version_num = 0; | |
841 | 887 |
862 | 888 while(fgets(toybuf, sizeof(toybuf), fp)) { |
841 | 889 l_ptr = xzalloc(sizeof(IFACE_LIST)); |
862 | 890 get_proc_info(toybuf, l_ptr, version_num); |
841 | 891 add_iface_to_list(l_ptr); |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
892 l_ptr->non_virtual_iface = 1; |
841 | 893 errno = 0; |
862 | 894 if(get_device_info(l_ptr) < 0) perror_exit("%s", l_ptr->dev_info.ifrname); |
895 } | |
841 | 896 fclose(fp); |
897 } | |
898 | |
899 static void get_hw_info(int hw_type, HW_INFO *hw_info) | |
900 { | |
852 | 901 memset(hw_info, 0, sizeof(HW_INFO)); |
902 | |
841 | 903 switch(hw_type) { |
904 case ARPHRD_LOOPBACK: //Loopback device. | |
905 strncpy(hw_info->hw_name, "loop", HW_NAME_LEN); | |
906 strncpy(hw_info->hw_title, "Local Loopback", HW_TITLE_LEN); | |
907 hw_info->hw_addrlen = 0; | |
908 break; | |
909 case ARPHRD_ETHER: //Ethernet | |
910 strncpy(hw_info->hw_name, "ether", HW_NAME_LEN); | |
911 strncpy(hw_info->hw_title, "Ethernet", HW_TITLE_LEN); | |
912 hw_info->hw_addrlen = ETH_ALEN; | |
913 break; | |
914 case ARPHRD_PPP: //ARPHRD_PPP | |
915 strncpy(hw_info->hw_name, "ppp", HW_NAME_LEN); | |
916 strncpy(hw_info->hw_title, "Point-to-Point Protocol", HW_TITLE_LEN); | |
917 hw_info->hw_addrlen = 0; | |
918 break; | |
919 case ARPHRD_INFINIBAND: //InfiniBand | |
920 strncpy(hw_info->hw_name, "infiniband", HW_NAME_LEN); | |
921 strncpy(hw_info->hw_title, "InfiniBand", HW_TITLE_LEN); | |
922 hw_info->hw_addrlen = 20; | |
923 break; | |
924 case ARPHRD_SIT: //sit0 device - IPv6-in-IPv4 | |
925 strncpy(hw_info->hw_name, "sit", HW_NAME_LEN); | |
926 strncpy(hw_info->hw_title, "IPv6-in-IPv4", HW_TITLE_LEN); | |
927 hw_info->hw_addrlen = 0; | |
928 break; | |
852 | 929 case -1: |
841 | 930 strncpy(hw_info->hw_name, "unspec", HW_NAME_LEN); |
931 strncpy(hw_info->hw_title, "UNSPEC", HW_TITLE_LEN); | |
932 hw_info->hw_addrlen = 0; | |
933 break; | |
934 default: | |
935 break; | |
936 } | |
937 } | |
938 | |
939 static void print_hw_addr(int hw_type, HW_INFO hw_info, IFACE_LIST *l_ptr) | |
940 { | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
941 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
|
942 |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
943 if(!address || !hw_info.hw_addrlen) return; |
841 | 944 xprintf("HWaddr "); |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
945 if(hw_type == ARPHRD_ETHER) { |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
946 int 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 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
|
949 } |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
950 |
841 | 951 return; |
952 } | |
953 | |
852 | 954 static char *get_ip_addr(struct sockaddr *skaddr) |
841 | 955 { |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
956 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr; |
841 | 957 |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
958 if(skaddr->sa_family == 0xFFFF || !skaddr->sa_family) return "[NOT SET]"; |
841 | 959 if(sin->sin_family != AF_INET) { |
960 errno = EAFNOSUPPORT; | |
961 return NULL; | |
962 } | |
963 | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
964 return inet_ntoa(sin->sin_addr); |
841 | 965 } |
966 | |
967 static void print_ip_addr(IFACE_LIST *l_ptr) | |
968 { | |
852 | 969 char *af_name; |
841 | 970 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
|
971 |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
972 if (af == AF_INET) af_name = "inet"; |
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
973 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
|
974 else if (af == AF_UNSPEC) af_name = "unspec"; |
841 | 975 |
976 xprintf("%10s%s addr:%s ", " ", af_name, get_ip_addr(&l_ptr->ifraddr)); | |
977 if(l_ptr->ifrflags & IFF_POINTOPOINT) | |
978 xprintf(" P-t-P:%s ", get_ip_addr(&l_ptr->ifrdstaddr)); | |
979 if(l_ptr->ifrflags & IFF_BROADCAST) | |
980 xprintf(" Bcast:%s ", get_ip_addr(&l_ptr->ifrbroadaddr)); | |
981 xprintf(" Mask:%s\n", get_ip_addr(&l_ptr->ifrnetmask)); | |
982 return; | |
983 } | |
984 | |
985 static void print_iface_flags(IFACE_LIST *l_ptr) | |
986 { | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
987 if (l_ptr->ifrflags != 0) { |
841 | 988 unsigned short mask = 1; |
989 char **str = iface_flags_str; | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
990 |
841 | 991 for(; *str != NULL; str++) { |
992 if(l_ptr->ifrflags & mask) | |
993 xprintf("%s ", *str); | |
994 mask = mask << 1; | |
995 } | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
996 } else xprintf("[NO FLAGS] "); |
841 | 997 return; |
998 } | |
999 | |
1000 static void print_media(IFACE_LIST *l_ptr) | |
1001 { | |
1002 #ifdef IFF_PORTSEL | |
1003 if(l_ptr->ifrflags & IFF_PORTSEL) { | |
1004 xprintf("Media:"); | |
1005 if(l_ptr->ifrmap.port == IF_PORT_UNKNOWN) | |
1006 xprintf("%s", "unknown"); | |
1007 else if(l_ptr->ifrmap.port == IF_PORT_10BASE2) | |
1008 xprintf("%s", "10base2"); | |
1009 else if(l_ptr->ifrmap.port == IF_PORT_10BASET) | |
1010 xprintf("%s", "10baseT"); | |
1011 else if(l_ptr->ifrmap.port == IF_PORT_AUI) | |
1012 xprintf("%s", "AUI"); | |
1013 else if(l_ptr->ifrmap.port == IF_PORT_100BASET) | |
1014 xprintf("%s", "100baseT"); | |
1015 else if(l_ptr->ifrmap.port == IF_PORT_100BASETX) | |
1016 xprintf("%s", "100baseTX"); | |
1017 else if(l_ptr->ifrmap.port == IF_PORT_100BASEFX) | |
1018 xprintf("%s", "100baseFX"); | |
1019 if(l_ptr->ifrflags & IFF_AUTOMEDIA) | |
1020 xprintf("(auto)"); | |
1021 } | |
1022 #endif | |
1023 return; | |
1024 } | |
1025 | |
1026 static void print_ip6_addr(IFACE_LIST *l_ptr) | |
1027 { | |
1028 char iface_name[IFNAMSIZ] = {0,}; | |
1029 char buf[BUFSIZ] = {0,}; | |
1030 int plen, scope; | |
1031 | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
1032 FILE *fp = fopen("/proc/net/if_inet6", "r"); |
841 | 1033 if(fp == NULL) |
1034 return; | |
1035 | |
1036 while(fgets(buf, BUFSIZ, fp)) { | |
1037 int nitems = 0; | |
1038 char ipv6_addr[40] = {0,}; | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1039 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n", |
841 | 1040 ipv6_addr+7, &plen, &scope, iface_name); |
1041 if(nitems != 4) { | |
1042 if((nitems < 0) && feof(fp)) | |
1043 break; | |
1044 perror_exit("sscanf"); | |
1045 } | |
1046 if(strcmp(l_ptr->dev_info.ifrname,iface_name) == 0) { | |
1047 int i = 0; | |
1048 struct sockaddr_in6 sock_in6; | |
1049 int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]); | |
1050 char *ptr = ipv6_addr+7; | |
1051 while((i < len-2) && (*ptr)) { | |
1052 ipv6_addr[i++] = *ptr++; | |
1053 //put ':' after 4th bit | |
1054 if(!((i+1) % 5)) | |
1055 ipv6_addr[i++] = ':'; | |
1056 } | |
1057 ipv6_addr[i+1] = '\0'; | |
1058 if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) { | |
1059 sock_in6.sin6_family = AF_INET6; | |
1060 memset(buf, 0, (sizeof(buf) /sizeof(buf[0]))); | |
1061 if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, buf, BUFSIZ) > 0) { | |
1062 xprintf("%10sinet6 addr: %s/%d", " ", buf, plen); | |
1063 xprintf(" Scope:"); | |
1064 if(scope == IPV6_ADDR_ANY) xprintf(" Global"); | |
1065 else if(scope == IPV6_ADDR_LOOPBACK) xprintf(" Host"); | |
1066 else if(scope == IPV6_ADDR_LINKLOCAL) xprintf(" Link"); | |
1067 else if(scope == IPV6_ADDR_SITELOCAL) xprintf(" Site"); | |
1068 else if(scope == IPV6_ADDR_COMPATv4) xprintf(" Compat"); | |
1069 else xprintf("Unknown"); | |
1070 xprintf("\n"); | |
1071 } | |
1072 } | |
1073 } | |
1074 }//end of while. | |
1075 fclose(fp); | |
1076 fp = NULL; | |
1077 return; | |
1078 } | |
1079 | |
1080 static void display_ifconfig(IFACE_LIST *l_ptr) | |
1081 { | |
1082 HW_INFO hw_info; | |
1083 int hw_type = l_ptr->hw_type; | |
1084 | |
1085 get_hw_info(hw_type, &hw_info); | |
1086 xprintf("%-9s Link encap:%s ", l_ptr->dev_info.ifrname, hw_info.hw_title); | |
1087 print_hw_addr(hw_type, hw_info, l_ptr); | |
1088 | |
1089 print_media(l_ptr); | |
1090 | |
852 | 1091 xputc('\n'); |
841 | 1092 if(l_ptr->ifaddr) |
1093 print_ip_addr(l_ptr); //print addr, p-p addr, broadcast addr and mask addr. | |
1094 | |
1095 //for ipv6 to do. | |
1096 print_ip6_addr(l_ptr); | |
1097 xprintf("%10s", " "); | |
1098 //print flags | |
1099 print_iface_flags(l_ptr); | |
852 | 1100 if(!l_ptr->ifrmetric) l_ptr->ifrmetric = 1; |
841 | 1101 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
|
1102 xprintf("\n"); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1103 if(l_ptr->non_virtual_iface) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1104 xprintf("%10s", " "); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1105 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
|
1106 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
|
1107 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
|
1108 l_ptr->dev_info.receive_frame); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1109 //Dummy types for non ARP hardware. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1110 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
|
1111 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
|
1112 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
|
1113 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
|
1114 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
|
1115 l_ptr->dev_info.transmit_carrier); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1116 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
|
1117 //Dummy types for non ARP hardware. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1118 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
|
1119 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
|
1120 if(l_ptr->txqueuelen != NO_RANGE) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1121 xprintf("txqueuelen:%d ", l_ptr->txqueuelen); |
841 | 1122 |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1123 xprintf("\n%10s", " "); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1124 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
|
1125 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
|
1126 } |
841 | 1127 if(l_ptr->ifrmap.irq || l_ptr->ifrmap.mem_start || l_ptr->ifrmap.dma || l_ptr->ifrmap.base_addr) { |
1128 xprintf("%10s", " "); | |
1129 if(l_ptr->ifrmap.irq) | |
1130 xprintf("Interrupt:%d ", l_ptr->ifrmap.irq); | |
1131 if(l_ptr->ifrmap.base_addr >= IO_MAP_INDEX) | |
1132 xprintf("Base address:0x%lx ", l_ptr->ifrmap.base_addr); | |
1133 if(l_ptr->ifrmap.mem_start) | |
1134 xprintf("Memory:%lx-%lx ", l_ptr->ifrmap.mem_start, l_ptr->ifrmap.mem_end); | |
1135 if(l_ptr->ifrmap.dma) | |
1136 xprintf("DMA chan:%x ", l_ptr->ifrmap.dma); | |
852 | 1137 xputc('\n'); |
841 | 1138 } |
852 | 1139 xputc('\n'); |
841 | 1140 return; |
1141 } | |
1142 | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1143 static int readconf(void) |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1144 { |
852 | 1145 int num_of_req = 30; |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1146 struct ifconf ifcon; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1147 struct ifreq *ifre; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1148 int num, status = -1, sokfd; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1149 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1150 ifcon.ifc_buf = NULL; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1151 sokfd = socket(AF_INET, SOCK_DGRAM, 0); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1152 if(sokfd < 0) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1153 perror_msg("error: no inet socket available"); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1154 return -1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1155 } |
843
1b36ac92e2cc
Drive-by cleanup on ifconfig.
Rob Landley <rob@landley.net>
parents:
842
diff
changeset
|
1156 for (;;) { |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1157 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
|
1158 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
|
1159 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1160 if((status = ioctl(sokfd, SIOCGIFCONF, &ifcon)) == -1) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1161 perror_msg("ioctl %#x failed", SIOCGIFCONF); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1162 goto LOOP_BREAK; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1163 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1164 //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
|
1165 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
|
1166 num_of_req += 10; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1167 continue; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1168 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1169 break; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1170 }//End of while loop |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1171 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1172 ifre = ifcon.ifc_req; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1173 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
|
1174 //Escape duplicate values from the list. |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1175 IFACE_LIST *list_ptr; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1176 int match_found = 0; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1177 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
|
1178 //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
|
1179 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
|
1180 match_found = 1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1181 break; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1182 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1183 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1184 if(!match_found) { |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1185 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
|
1186 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
|
1187 add_iface_to_list(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1188 errno = 0; |
862 | 1189 if(get_device_info(l_ptr) < 0) |
1190 perror_exit("%s", l_ptr->dev_info.ifrname); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1191 } |
862 | 1192 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1193 |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1194 LOOP_BREAK: |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1195 close(sokfd); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1196 free(ifcon.ifc_buf); |
852 | 1197 |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1198 return status; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1199 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1200 |
841 | 1201 static int show_iface(char *iface_name) |
1202 { | |
1203 get_ifconfig_info(); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1204 |
841 | 1205 if(iface_name) { |
1206 IFACE_LIST *l_ptr; | |
1207 int is_dev_found = 0; | |
1208 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) { | |
1209 if(strcmp(l_ptr->dev_info.ifrname, iface_name) == 0) { | |
1210 is_dev_found = 1; | |
1211 display_ifconfig(l_ptr); | |
1212 break; | |
1213 } | |
1214 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1215 //if the given interface is not in the list. |
841 | 1216 if(!is_dev_found) { |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1217 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
|
1218 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
|
1219 errno = 0; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1220 if(get_device_info(l_ptr) < 0) { |
852 | 1221 char *errmsg; |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1222 if(errno == ENODEV) errmsg = "Device not found"; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1223 else errmsg = strerror(errno); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1224 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
|
1225 free(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1226 return 1; |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1227 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1228 else display_ifconfig(l_ptr); |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1229 free(l_ptr); |
841 | 1230 } |
852 | 1231 } else { |
841 | 1232 IFACE_LIST *l_ptr; |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
1233 if(readconf() < 0) return 1; |
841 | 1234 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) { |
1235 if((l_ptr->ifrflags & IFF_UP) || (toys.optflags & FLAG_a)) | |
1236 display_ifconfig(l_ptr); | |
1237 } | |
1238 } | |
1239 return 0; | |
1240 } | |
1241 | |
1242 static void clear_list(void) | |
1243 { | |
1244 IFACE_LIST *temp_ptr; | |
1245 while(iface_list_head != NULL) { | |
1246 temp_ptr = iface_list_head->next; | |
1247 free(iface_list_head); | |
1248 iface_list_head = temp_ptr; | |
1249 } | |
1250 return; | |
1251 } |