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