Mercurial > hg > toybox
annotate toys/pending/ifconfig.c @ 907:ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 21 May 2013 00:23:23 -0500 |
parents | ce0a9368b82f |
children | a186f9a1406d |
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_arp.h> |
36 #include <net/ethernet.h> | |
37 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
38 GLOBALS( |
870 | 39 void *if_list; |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
40 ) |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
41 |
841 | 42 typedef struct sockaddr_with_len { |
43 union { | |
44 struct sockaddr sock; | |
45 struct sockaddr_in sock_in; | |
46 struct sockaddr_in6 sock_in6; | |
47 }sock_u; | |
48 socklen_t socklen; | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
49 } sockaddr_with_len; |
841 | 50 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
51 // man netdevice |
870 | 52 struct if_list { |
53 struct if_list *next; | |
54 int hw_type, mtu, metric, txqueuelen, non_virtual_iface; | |
55 short flags, hasaddr; | |
56 struct sockaddr addr, dstaddr, broadaddr, netmask, hwaddr; | |
57 struct ifmap map; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
58 |
870 | 59 char name[IFNAMSIZ]; |
60 unsigned long long val[16]; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
61 }; |
841 | 62 |
63 #define IO_MAP_INDEX 0x100 | |
64 | |
65 //for the param settings. | |
66 | |
67 //for ipv6 add/del | |
68 struct ifreq_inet6 { | |
69 struct in6_addr ifrinte6_addr; | |
70 uint32_t ifrinet6_prefixlen; | |
71 int ifrinet6_ifindex; | |
72 }; | |
73 | |
74 #ifndef SIOCSKEEPALIVE | |
75 # define SIOCSKEEPALIVE (SIOCDEVPRIVATE) /* Set keepalive timeout in sec */ | |
76 # define SIOCGKEEPALIVE (SIOCDEVPRIVATE+1) /* Get keepalive timeout */ | |
77 #endif | |
78 | |
79 #ifndef SIOCSOUTFILL | |
80 # define SIOCSOUTFILL (SIOCDEVPRIVATE+2) /* Set outfill timeout */ | |
81 # define SIOCGOUTFILL (SIOCDEVPRIVATE+3) /* Get outfill timeout */ | |
82 #endif | |
83 | |
84 /* | |
85 * used to extract the address info from the given host ip | |
86 * and update the swl param accordingly. | |
87 */ | |
852 | 88 static int get_socket_stream(char *host, sa_family_t af, sockaddr_with_len **swl) |
841 | 89 { |
852 | 90 struct addrinfo hints, *result, *rp; |
91 int status; | |
841 | 92 |
93 memset(&hints, 0 , sizeof(struct addrinfo)); | |
94 hints.ai_family = af; | |
95 hints.ai_socktype = SOCK_STREAM; | |
96 | |
852 | 97 status = getaddrinfo(host, NULL, &hints, &result); |
864 | 98 if (status) error_exit("bad address '%s' : %s", host, gai_strerror(status)); |
841 | 99 |
864 | 100 for (rp = result; rp; rp = rp->ai_next) { |
101 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) { | |
841 | 102 *swl = xmalloc(sizeof(struct sockaddr_with_len)); |
103 (*swl)->socklen = rp->ai_addrlen; | |
104 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen); | |
105 break; | |
106 } | |
107 } | |
108 freeaddrinfo(result); | |
852 | 109 return rp ? 0 : -1; |
841 | 110 } |
111 | |
112 /* | |
113 * use to get the socket address with the given host ip. | |
114 */ | |
852 | 115 sockaddr_with_len *get_sockaddr(char *host, int port, sa_family_t af) |
841 | 116 { |
117 sockaddr_with_len *swl = NULL; | |
852 | 118 in_port_t port_num = htons(port); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
119 char *s; |
841 | 120 |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
121 if (!strncmp(host, "local:", 6)) { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
122 struct sockaddr_un *sockun; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
123 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
124 swl = xzalloc(sizeof(struct sockaddr_with_len)); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
125 swl->socklen = sizeof(struct sockaddr_un); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
126 swl->sock_u.sock.sa_family = AF_UNIX; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
127 sockun = (struct sockaddr_un *)&swl->sock_u.sock; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
128 xstrncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path)); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
129 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
130 return swl; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
131 } |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
132 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
133 // [ipv6]:port or exactly one : |
841 | 134 |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
135 if (*host == '[') { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
136 host++; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
137 s = strchr(host, ']'); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
138 if (s && !s[1]) s = 0; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
139 else { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
140 if (!s || s[1] != ':') error_exit("bad address '%s'", host-1); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
141 s++; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
142 } |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
143 } else { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
144 s = strrchr(host, ':'); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
145 if (s && strchr(host, ':') != s) s = 0; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
146 } |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
147 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
148 if (s++) { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
149 char *ss; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
150 unsigned long p = strtoul(s, &ss, 0); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
151 if (*ss || p > 65535) error_exit("bad port '%s'", s); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
152 port = p; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
153 } |
841 | 154 |
852 | 155 if (get_socket_stream(host, af, &swl)) return NULL; |
841 | 156 |
852 | 157 if(swl->sock_u.sock.sa_family == AF_INET) |
158 swl->sock_u.sock_in.sin_port = port_num; | |
159 else if(swl->sock_u.sock.sa_family == AF_INET6) | |
160 swl->sock_u.sock_in6.sin6_port = port_num; | |
161 | |
841 | 162 return swl; |
163 } | |
164 | |
165 /* | |
166 * get the numeric hostname and service name, for a given socket address. | |
167 */ | |
852 | 168 char *address_to_name(struct sockaddr *sock) |
841 | 169 { |
170 //man page of getnameinfo. | |
171 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,}; | |
172 int status = 0; | |
864 | 173 |
841 | 174 if(sock->sa_family == AF_INET) { |
175 socklen_t len = sizeof(struct sockaddr_in); | |
176 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) | |
177 return xmsprintf("%s:%s", hbuf, sbuf); | |
178 else { | |
179 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); | |
180 return NULL; | |
181 } | |
864 | 182 } else if(sock->sa_family == AF_INET6) { |
841 | 183 socklen_t len = sizeof(struct sockaddr_in6); |
184 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) { | |
185 //verification for resolved hostname. | |
186 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf); | |
187 else return xmsprintf("%s:%s", hbuf, sbuf); | |
864 | 188 } else { |
841 | 189 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); |
190 return NULL; | |
191 } | |
864 | 192 } else if(sock->sa_family == AF_UNIX) { |
841 | 193 struct sockaddr_un *sockun = (void*)sock; |
194 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path); | |
864 | 195 } else return NULL; |
841 | 196 } |
197 | |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
198 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
199 int reset_flag) |
841 | 200 { |
864 | 201 xioctl(sockfd, SIOCGIFFLAGS, ifre); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
202 ifre->ifr_flags &= ~reset_flag; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
203 ifre->ifr_flags |= set_flag; |
864 | 204 xioctl(sockfd, SIOCSIFFLAGS, ifre); |
841 | 205 } |
206 | |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
207 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request) |
841 | 208 { |
209 char *prefix; | |
210 int plen = 0; | |
211 sockaddr_with_len *swl = NULL; | |
212 | |
213 prefix = strchr(ipv6_addr, '/'); | |
214 if(prefix) { | |
215 plen = get_int_value(prefix + 1, 0, 128); | |
216 *prefix = '\0'; | |
217 } | |
218 swl = get_sockaddr(ipv6_addr, 0, AF_INET6); | |
219 if(!swl) error_exit("error in resolving host name"); | |
220 int sockfd6; | |
221 struct ifreq_inet6 ifre6; | |
222 memcpy((char *) &ifre6.ifrinte6_addr, | |
223 (char *) &(swl->sock_u.sock_in6.sin6_addr), | |
224 sizeof(struct in6_addr)); | |
225 //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
|
226 sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0); |
864 | 227 xioctl(sockfd6, SIOGIFINDEX, ifre); |
841 | 228 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex; |
229 ifre6.ifrinet6_prefixlen = plen; | |
230 | |
864 | 231 xioctl(sockfd6, request, &ifre6); |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
232 free(swl); |
841 | 233 } |
234 | |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
235 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request) |
841 | 236 { |
237 struct sockaddr_in sock_in; | |
238 sockaddr_with_len *swl = NULL; | |
239 sock_in.sin_family = AF_INET; | |
240 sock_in.sin_port = 0; | |
241 | |
242 //Default 0.0.0.0 | |
864 | 243 if(strcmp(host_name, "default") == 0) sock_in.sin_addr.s_addr = INADDR_ANY; |
841 | 244 else { |
245 swl = get_sockaddr(host_name, 0, AF_INET); | |
246 if(!swl) error_exit("error in resolving host name"); | |
247 | |
248 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr; | |
249 } | |
250 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr)); | |
864 | 251 xioctl(sockfd, request, ifre); |
841 | 252 |
253 if(swl != NULL) { | |
254 free(swl); | |
255 swl = NULL; | |
256 } | |
257 } | |
258 | |
870 | 259 static void add_iface_to_list(struct if_list *newnode) |
841 | 260 { |
870 | 261 struct if_list *head_ref = TT.if_list; |
841 | 262 |
870 | 263 if(!head_ref || strcmp(newnode->name, head_ref->name) < 0) { |
841 | 264 newnode->next = head_ref; |
265 head_ref = newnode; | |
862 | 266 } else { |
870 | 267 struct if_list *current = head_ref; |
268 while(current->next && strcmp(current->next->name, newnode->name) < 0) | |
841 | 269 current = current->next; |
270 newnode->next = current->next; | |
271 current->next = newnode; | |
272 } | |
870 | 273 TT.if_list = (void *)head_ref; |
841 | 274 } |
275 | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
276 static void get_device_info(struct if_list *il) |
841 | 277 { |
278 struct ifreq ifre; | |
870 | 279 char *name = il->name; |
841 | 280 int sokfd; |
281 | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
282 sokfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
283 xstrncpy(ifre.ifr_name, name, IFNAMSIZ); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
284 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
|
285 il->flags = ifre.ifr_flags; |
841 | 286 |
287 if(ioctl(sokfd, SIOCGIFHWADDR, &ifre) >= 0) | |
870 | 288 memcpy(il->hwaddr.sa_data, ifre.ifr_hwaddr.sa_data, sizeof(il->hwaddr.sa_data)); |
841 | 289 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
290 il->hw_type = ifre.ifr_hwaddr.sa_family; |
841 | 291 |
292 if(ioctl(sokfd, SIOCGIFMETRIC, &ifre) >= 0) | |
870 | 293 il->metric = ifre.ifr_metric; |
841 | 294 |
295 if(ioctl(sokfd, SIOCGIFMTU, &ifre) >= 0) | |
870 | 296 il->mtu = ifre.ifr_mtu; |
841 | 297 |
298 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0) | |
870 | 299 il->map = ifre.ifr_map; |
841 | 300 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
301 il->txqueuelen = -1; |
841 | 302 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0) |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
303 il->txqueuelen = ifre.ifr_qlen; |
841 | 304 |
305 ifre.ifr_addr.sa_family = AF_INET; | |
306 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
307 if(!ioctl(sokfd, SIOCGIFADDR, &ifre)) { |
870 | 308 il->hasaddr = 1; |
309 il->addr = ifre.ifr_addr; | |
841 | 310 if(ioctl(sokfd, SIOCGIFDSTADDR, &ifre) >= 0) |
870 | 311 il->dstaddr = ifre.ifr_dstaddr; |
841 | 312 |
313 if(ioctl(sokfd, SIOCGIFBRDADDR, &ifre) >= 0) | |
870 | 314 il->broadaddr = ifre.ifr_broadaddr; |
841 | 315 |
316 if(ioctl(sokfd, SIOCGIFNETMASK, &ifre) >= 0) | |
870 | 317 il->netmask = ifre.ifr_netmask; |
841 | 318 } |
319 close(sokfd); | |
320 } | |
321 | |
870 | 322 static void print_ip6_addr(struct if_list *il) |
841 | 323 { |
324 char iface_name[IFNAMSIZ] = {0,}; | |
325 int plen, scope; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
326 FILE *fp; |
841 | 327 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
328 if(!(fp = fopen("/proc/net/if_net6", "r"))) return; |
841 | 329 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
330 while(fgets(toybuf, sizeof(toybuf), fp)) { |
841 | 331 int nitems = 0; |
332 char ipv6_addr[40] = {0,}; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
333 nitems = sscanf(toybuf, "%32s %*08x %02x %02x %*02x %15s\n", |
841 | 334 ipv6_addr+7, &plen, &scope, iface_name); |
335 if(nitems != 4) { | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
336 if((nitems < 0) && feof(fp)) break; |
841 | 337 perror_exit("sscanf"); |
338 } | |
870 | 339 if(strcmp(il->name, iface_name) == 0) { |
841 | 340 int i = 0; |
341 struct sockaddr_in6 sock_in6; | |
342 int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]); | |
343 char *ptr = ipv6_addr+7; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
344 |
841 | 345 while((i < len-2) && (*ptr)) { |
346 ipv6_addr[i++] = *ptr++; | |
347 //put ':' after 4th bit | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
348 if(!((i+1) % 5)) ipv6_addr[i++] = ':'; |
841 | 349 } |
350 ipv6_addr[i+1] = '\0'; | |
351 if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) { | |
352 sock_in6.sin6_family = AF_INET6; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
353 if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, toybuf, BUFSIZ)) { |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
354 char *names[] = {"Global","Host","Link","Site","Compat"}, |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
355 *name = "Unknown"; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
356 int j; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
357 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
358 for (j=0; j < sizeof(names)/sizeof(*names); j++) |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
359 if (scope == (!!j)<<(j+3)) name = names[j]; |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
360 xprintf("%10cinet6 addr: %s/%d Scope: %s\n", ' ', toybuf, plen, name); |
841 | 361 } |
362 } | |
363 } | |
864 | 364 } |
841 | 365 fclose(fp); |
366 } | |
367 | |
870 | 368 static void display_ifconfig(struct if_list *il) |
841 | 369 { |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
370 struct { |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
371 int type; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
372 char *title; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
373 } types[] = { |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
374 {ARPHRD_LOOPBACK, "Local Loopback"}, {ARPHRD_ETHER, "Ethernet"}, |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
375 {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
|
376 {ARPHRD_SIT, "IPv6-in-IPv4"}, {-1, "UNSPEC"} |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
377 }; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
378 int i; |
841 | 379 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
380 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
|
381 if (il->hw_type == types[i].type) break; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
382 |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
383 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
|
384 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
|
385 xprintf("HWaddr "); |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
386 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
|
387 } |
852 | 388 xputc('\n'); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
389 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
390 if(il->hasaddr) { |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
391 int af = il->addr.sa_family; |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
392 struct { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
393 char *name; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
394 int flag, offset; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
395 } addr[] = { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
396 {"addr", 0, offsetof(struct if_list, addr)}, |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
397 {"P-t-P", IFF_POINTOPOINT, offsetof(struct if_list, dstaddr)}, |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
398 {"Bcast", IFF_BROADCAST, offsetof(struct if_list, broadaddr)}, |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
399 {"Mask", 0, offsetof(struct if_list, netmask)} |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
400 }; |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
401 |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
402 xprintf("%10c%s", ' ', |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
403 (af == AF_INET) ? "inet" : (af == AF_INET6) ? "inet6" : "unspec"); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
404 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
405 for (i=0; i < sizeof(addr)/sizeof(*addr); i++) { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
406 struct sockaddr_in *s = (struct sockaddr_in *)(addr[i].offset+(char *)il); |
841 | 407 |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
408 if (!addr[i].flag || (il->flags & addr[i].flag)) |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
409 xprintf(" %s:%s ", addr[i].name, |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
410 (s->sin_family == 0xFFFF || !s->sin_family) ? "[NOT SET]" : |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
411 inet_ntoa(s->sin_addr)); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
412 } |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
413 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
414 xputc('\n'); |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
415 } |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
416 |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
417 print_ip6_addr(il); |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
418 xprintf("%10c", ' '); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
419 |
870 | 420 if (il->flags) { |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
421 unsigned short mask = 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
422 char **s, *str[] = { |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
423 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
424 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
425 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
426 }; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
427 |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
428 for(s = str; *s; s++) { |
870 | 429 if(il->flags & mask) xprintf("%s ", *s); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
430 mask = mask << 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
431 } |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
432 } else xprintf("[NO FLAGS] "); |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
433 |
870 | 434 if(!il->metric) il->metric = 1; |
435 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
|
436 |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
437 if(il->non_virtual_iface) { |
870 | 438 char *label[] = {"RX bytes", "RX packets", "errors", "dropped", "overruns", |
439 "frame", 0, 0, "TX bytes", "TX packets", "errors", "dropped", "overruns", | |
440 "collisions", "carrier", 0, "txqueuelen"}; | |
441 signed char order[] = {-1, 1, 2, 3, 4, 5, -1, 9, 10, 11, 12, 14, -1, | |
442 13, 16, -1, 0, 8}; | |
443 int i; | |
444 | |
445 for (i = 0; i < sizeof(order); i++) { | |
446 int j = order[i]; | |
841 | 447 |
870 | 448 if (j < 0) xprintf("\n%10c", ' '); |
449 else xprintf("%s:%llu ", label[j], | |
450 j==16 ? (unsigned long long)il->txqueuelen : il->val[j]); | |
451 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
452 } |
870 | 453 xputc('\n'); |
454 | |
455 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
|
456 xprintf("%10c", ' '); |
870 | 457 if(il->map.irq) xprintf("Interrupt:%d ", il->map.irq); |
458 if(il->map.base_addr >= IO_MAP_INDEX) | |
459 xprintf("Base address:0x%lx ", il->map.base_addr); | |
460 if(il->map.mem_start) | |
461 xprintf("Memory:%lx-%lx ", il->map.mem_start, il->map.mem_end); | |
462 if(il->map.dma) xprintf("DMA chan:%x ", il->map.dma); | |
852 | 463 xputc('\n'); |
841 | 464 } |
852 | 465 xputc('\n'); |
841 | 466 } |
467 | |
870 | 468 static void readconf(void) |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
469 { |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
470 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
|
471 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
|
472 struct ifreq *ifre; |
870 | 473 int num, sokfd; |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
474 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
475 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
|
476 sokfd = socket(AF_INET, SOCK_DGRAM, 0); |
870 | 477 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
|
478 for (;;) { |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
479 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
|
480 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
|
481 |
870 | 482 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
|
483 //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
|
484 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
|
485 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
|
486 continue; |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
487 } |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
488 break; |
864 | 489 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
490 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
491 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
|
492 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
|
493 //Escape duplicate values from the list. |
870 | 494 struct if_list *il; |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
495 |
870 | 496 for(il = TT.if_list; il; il = il->next) |
497 if(!strcmp(ifre->ifr_name, il->name)) break; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
498 if(!il) { |
870 | 499 il = xzalloc(sizeof(struct if_list)); |
500 xstrncpy(il->name, ifre->ifr_name, IFNAMSIZ); | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
501 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
|
502 errno = 0; |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
503 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
|
504 } |
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
505 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
506 |
863
493f412fc5da
Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents:
862
diff
changeset
|
507 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
|
508 free(ifcon.ifc_buf); |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
509 } |
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
510 |
870 | 511 static void show_iface(char *iface_name) |
841 | 512 { |
870 | 513 struct if_list *il; |
514 int i, j; | |
515 FILE *fp; | |
516 | |
517 fp = xfopen("/proc/net/dev", "r"); | |
518 | |
519 for (i=0; fgets(toybuf, sizeof(toybuf), fp); i++) { | |
520 char *name, *buf; | |
521 | |
522 if (i<2) continue; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
523 |
870 | 524 il = xzalloc(sizeof(struct if_list)); |
525 for (buf = toybuf; isspace(*buf); buf++); | |
526 name = strsep(&buf, ":"); | |
527 if(!buf) error_exit("bad name %s", name); | |
528 xstrncpy(il->name, name, IFNAMSIZ); | |
529 | |
530 errno = 0; | |
531 for (j=0; j<16 && !errno; j++) il->val[j] = strtoll(buf, &buf, 0); | |
532 if (errno) perror_exit("bad %s at %s", name, buf); | |
533 | |
534 add_iface_to_list(il); | |
535 il->non_virtual_iface = 1; | |
536 errno = 0; | |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
537 get_device_info(il); |
870 | 538 } |
539 fclose(fp); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
540 |
841 | 541 if(iface_name) { |
870 | 542 for(il = TT.if_list; il; il = il->next) { |
543 if(!strcmp(il->name, iface_name)) { | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
544 display_ifconfig(il); |
841 | 545 break; |
546 } | |
547 } | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
548 //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
|
549 if(!il) { |
870 | 550 il = xzalloc(sizeof(struct if_list)); |
551 xstrncpy(il->name, iface_name, IFNAMSIZ); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
552 errno = 0; |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
553 get_device_info(il); |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
554 display_ifconfig(il); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
555 free(il); |
841 | 556 } |
852 | 557 } else { |
870 | 558 readconf(); |
559 for(il = TT.if_list; il; il = il->next) | |
560 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
|
561 display_ifconfig(il); |
841 | 562 } |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
563 |
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
564 if (CFG_TOYBOX_FREE) llist_traverse(TT.if_list, free); |
841 | 565 } |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
566 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
567 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
|
568 { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
569 char **argv = toys.optargs; |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
570 struct ifreq ifre; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
571 int i, sockfd = 0; |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
572 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
573 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
|
574 |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
575 if(toys.optc < 2) { |
870 | 576 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
|
577 return; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
578 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
579 |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
580 //get interface name |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
581 memset(&ifre, 0, sizeof(struct ifreq)); |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
582 xstrncpy(ifre.ifr_name, *argv, IFNAMSIZ); |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
583 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
584 |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
585 while(*++argv) { |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
586 struct argh { |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
587 char *name; |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
588 int on, off; // set, clear |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
589 } try[] = { |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
590 {"up", IFF_UP|IFF_RUNNING, 0}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
591 {"down", 0, IFF_UP}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
592 {"arp", 0, IFF_NOARP}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
593 {"trailers", 0, IFF_NOTRAILERS}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
594 {"promisc", IFF_PROMISC, 0}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
595 {"allmulti", IFF_ALLMULTI, 0}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
596 {"multicast", IFF_MULTICAST, 0}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
597 {"dynamic", IFF_DYNAMIC, 0}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
598 {"pointopoint", IFF_POINTOPOINT, SIOCSIFDSTADDR}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
599 {"broadcast", IFF_BROADCAST, SIOCSIFBRDADDR}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
600 {"netmask", 0, SIOCSIFNETMASK}, |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
601 {"dstaddr", 0, SIOCSIFDSTADDR} |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
602 }; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
603 char *s = *argv; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
604 int rev = (*s == '-'); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
605 |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
606 s += rev; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
607 |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
608 for (i = 0; i < sizeof(try)/sizeof(*try); i++) { |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
609 struct argh *t = try+i; |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
610 int on = t->on, off = t->off; |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
611 |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
612 if (strcmp(t->name, s)) continue; |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
613 |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
614 // Is this an SIOCSI entry? |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
615 if ((t->off | 0xff) == 0x89ff) { |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
616 if (!rev) { |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
617 if (!*++argv) show_help(); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
618 set_address(sockfd, *argv, &ifre, off); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
619 } |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
620 off = 0; |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
621 } |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
622 if (on || off) { |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
623 xioctl(sockfd, SIOCGIFFLAGS, &ifre); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
624 ifre.ifr_flags &= ~(rev ? on : off); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
625 ifre.ifr_flags |= (rev ? off : on); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
626 xioctl(sockfd, SIOCSIFFLAGS, &ifre); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
627 } |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
628 |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
629 break; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
630 } |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
631 if (i != sizeof(try)/sizeof(*try)) continue; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
632 |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
633 if (!strcmp(*argv, "hw")) { |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
634 char *hw_addr, *ptr, *p; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
635 struct sockaddr *sock = &ifre.ifr_hwaddr; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
636 int count = 6; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
637 |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
638 if (!*++argv) show_help(); |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
639 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
640 memset(sock, 0, sizeof(struct sockaddr)); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
641 if (!strcmp("ether", *argv)) sock->sa_family = ARPHRD_ETHER; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
642 else if (!strcmp("infiniband", *argv)) { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
643 sock->sa_family = ARPHRD_INFINIBAND; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
644 count = 20; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
645 } else { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
646 toys.exithelp++; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
647 error_exit("bad hw '%s'", *argv); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
648 } |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
649 hw_addr = *++argv; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
650 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
651 ptr = p = (char *) sock->sa_data; |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
652 |
907
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
653 while (*hw_addr && (p-ptr) < count) { |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
654 int val, len = 0; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
655 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
656 if (*hw_addr == ':') hw_addr++; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
657 sscanf(hw_addr, "%2x%n", &val, &len); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
658 if (len != 2) break; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
659 hw_addr += len; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
660 *p++ = val; |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
661 } |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
662 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
663 if ((p-ptr) != count || *hw_addr) |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
664 error_exit("bad hw-addr '%s'", hw_addr ? hw_addr : ""); |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
665 xioctl(sockfd, SIOCSIFHWADDR, &ifre); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
666 } 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
|
667 if (!*++argv) show_help(); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
668 ifre.ifr_mtu = strtoul(*argv, NULL, 0); |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
669 xioctl(sockfd, SIOCSIFMTU, &ifre); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
670 } 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
|
671 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
|
672 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
|
673 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
|
674 } 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
|
675 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
|
676 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
|
677 xioctl(sockfd, SIOCSOUTFILL, &ifre); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
678 } else if (!strcmp(*argv, "metric")) { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
679 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
680 ifre.ifr_metric = strtoul(*argv, NULL, 0); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
681 xioctl(sockfd, SIOCSIFMETRIC, &ifre); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
682 } else if (!strcmp(*argv, "txqueuelen")) { |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
683 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
684 ifre.ifr_qlen = strtoul(*argv, NULL, 0); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
685 xioctl(sockfd, SIOCSIFTXQLEN, &ifre); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
686 |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
687 } 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
|
688 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
689 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
690 } 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
|
691 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
692 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
693 } 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
|
694 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
695 xioctl(sockfd, SIOCGIFMAP, &ifre); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
696 ifre.ifr_map.mem_start = strtoul(*argv, NULL, 0); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
697 xioctl(sockfd, SIOCSIFMAP, &ifre); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
698 } 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
|
699 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
700 xioctl(sockfd, SIOCGIFMAP, &ifre); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
701 ifre.ifr_map.base_addr = strtoul(*argv, NULL, 0); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
702 xioctl(sockfd, SIOCSIFMAP, &ifre); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
703 } 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
|
704 if (!*++argv) show_help(); |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
705 xioctl(sockfd, SIOCGIFMAP, &ifre); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
706 ifre.ifr_map.irq = strtoul(*argv, NULL, 0); |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
707 xioctl(sockfd, SIOCSIFMAP, &ifre); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
708 } else { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
709 if (isdigit(**argv) || !strcmp(*argv, "default")) { |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
710 set_address(sockfd, *argv, &ifre, SIOCSIFADDR); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
711 //if the interface name is not an alias; set the flag and continue. |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
712 if(!strchr(ifre.ifr_name, ':')) |
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
713 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
714 } 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
|
715 else { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
716 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
|
717 toys.exithelp++; |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
718 error_exit("bad argument '%s'", *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
|
719 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
720 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
721 |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
722 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
723 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
|
724 } |