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