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