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