Mercurial > hg > toybox
annotate toys/other/ifconfig.c @ 1132:615505bb38b6 draft
Promote ifconfig from pending to other.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 28 Nov 2013 22:13:04 -0600 |
parents | toys/pending/ifconfig.c@9eaec92e3713 |
children | f8c926309a21 |
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" | |
921 | 13 default y |
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" | |
844
f8250900f94b
More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents:
843
diff
changeset
|
33 |
841 | 34 #include <net/if_arp.h> |
35 #include <net/ethernet.h> | |
36 | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
37 GLOBALS( |
921 | 38 int sockfd; |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
39 ) |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
40 |
841 | 41 //for ipv6 add/del |
42 struct ifreq_inet6 { | |
43 struct in6_addr ifrinte6_addr; | |
44 uint32_t ifrinet6_prefixlen; | |
45 int ifrinet6_ifindex; | |
46 }; | |
47 | |
1128 | 48 // Convert hostname to binary address for AF_INET or AF_INET6 |
49 void get_addrinfo(char *host, sa_family_t af, void *addr) | |
841 | 50 { |
1128 | 51 struct addrinfo hints, *result, *rp = 0; |
52 int status, len; | |
53 char *from; | |
841 | 54 |
921 | 55 memset(&hints, 0 , sizeof(struct addrinfo)); |
56 hints.ai_family = af; | |
57 hints.ai_socktype = SOCK_STREAM; | |
58 | |
59 status = getaddrinfo(host, NULL, &hints, &result); | |
1128 | 60 if (!status) |
61 for (rp = result; rp; rp = rp->ai_next) | |
62 if (rp->ai_family == af) break; | |
63 if (!rp) error_exit("bad address '%s' : %s", host, gai_strerror(status)); | |
852 | 64 |
1128 | 65 // You'd think ipv4 and ipv6 would ahve some basic compatability, but no. |
66 len = 4; | |
67 from = ((char *)rp->ai_addr->sa_data) + 2; | |
68 if (af == AF_INET6) { | |
69 len = 16; | |
70 from += 4; | |
841 | 71 } |
1128 | 72 memcpy(addr, from, len); |
73 freeaddrinfo(result); | |
841 | 74 } |
75 | |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
76 static void display_ifconfig(char *name, int always, unsigned long long val[]) |
841 | 77 { |
78 struct ifreq ifre; | |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
79 struct { |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
80 int type; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
81 char *title; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
82 } types[] = { |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
83 {ARPHRD_LOOPBACK, "Local Loopback"}, {ARPHRD_ETHER, "Ethernet"}, |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
84 {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
|
85 {ARPHRD_SIT, "IPv6-in-IPv4"}, {-1, "UNSPEC"} |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
86 }; |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
87 int i; |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
88 char *pp; |
921 | 89 FILE *fp; |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
90 short flags; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
91 |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
92 xstrncpy(ifre.ifr_name, name, IFNAMSIZ); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
93 if (ioctl(TT.sockfd, SIOCGIFFLAGS, &ifre)<0) perror_exit("%s", name); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
94 flags = ifre.ifr_flags; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
95 if (!always && !(flags & IFF_UP)) return; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
96 |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
97 // query hardware type and hardware address |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
98 i = ioctl(TT.sockfd, SIOCGIFHWADDR, &ifre); |
841 | 99 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
100 for (i=0; i < (sizeof(types)/sizeof(*types))-1; i++) |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
101 if (ifre.ifr_hwaddr.sa_family == types[i].type) break; |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
102 |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
103 xprintf("%-9s Link encap:%s ", name, types[i].title); |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
104 if(i >= 0 && ifre.ifr_hwaddr.sa_family == ARPHRD_ETHER) { |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
105 xprintf("HWaddr "); |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
106 for (i=0; i<6; i++) xprintf(":%02X"+!i, ifre.ifr_hwaddr.sa_data[i]); |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
107 } |
852 | 108 xputc('\n'); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
109 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
110 // If an address is assigned record that. |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
111 |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
112 ifre.ifr_addr.sa_family = AF_INET; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
113 memset(&ifre.ifr_addr, 0, sizeof(ifre.ifr_addr)); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
114 ioctl(TT.sockfd, SIOCGIFADDR, &ifre); |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
115 pp = (char *)&ifre.ifr_addr; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
116 for (i = 0; i<sizeof(ifre.ifr_addr); i++) if (pp[i]) break; |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
117 |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
118 if (i != sizeof(ifre.ifr_addr)) { |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
119 struct sockaddr_in *si = (struct sockaddr_in *)&ifre.ifr_addr; |
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
|
120 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
|
121 char *name; |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
122 int flag, ioctl; |
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
|
123 } addr[] = { |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
124 {"addr", 0, 0}, |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
125 {"P-t-P", IFF_POINTOPOINT, SIOCGIFDSTADDR}, |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
126 {"Bcast", IFF_BROADCAST, SIOCGIFBRDADDR}, |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
127 {"Mask", 0, SIOCGIFNETMASK} |
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
|
128 }; |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
129 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
130 xprintf("%10c%s", ' ', (si->sin_family == AF_INET) ? "inet" : |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
131 (si->sin_family == AF_INET6) ? "inet6" : "unspec"); |
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
|
132 |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
133 for (i=0; i < sizeof(addr)/sizeof(*addr); i++) { |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
134 if (!addr[i].flag || (flags & addr[i].flag)) { |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
135 if (addr[i].ioctl && ioctl(TT.sockfd, addr[i].ioctl, &ifre)) |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
136 si->sin_family = 0; |
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
|
137 xprintf(" %s:%s ", addr[i].name, |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
138 (si->sin_family == 0xFFFF || !si->sin_family) |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
139 ? "[NOT SET]" : inet_ntoa(si->sin_addr)); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
140 } |
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
|
141 } |
ecb0befac82a
More ifconfig cleanup, inlining show_ip_addr(), hex_to_binary(), and set_hw_address().
Rob Landley <rob@landley.net>
parents:
906
diff
changeset
|
142 |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
143 xputc('\n'); |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
144 } |
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
145 |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
146 fp = fopen(pp = "/proc/net/if_inet6", "r"); |
921 | 147 if (fp) { |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
148 char iface_name[IFNAMSIZ]; |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
149 int plen, iscope; |
921 | 150 |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
151 while (fgets(toybuf, sizeof(toybuf), fp)) { |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
152 int nitems; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
153 char ipv6_addr[40]; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
154 |
921 | 155 nitems = sscanf(toybuf, "%32s %*08x %02x %02x %*02x %15s\n", |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
156 ipv6_addr, &plen, &iscope, iface_name); |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
157 if (nitems<0 && feof(fp)) break; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
158 if (nitems != 4) perror_exit("bad %s", pp); |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
159 |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
160 if (!strcmp(name, iface_name)) { |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
161 struct sockaddr_in6 s6; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
162 char *ptr = ipv6_addr+sizeof(ipv6_addr)-1; |
921 | 163 |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
164 // convert giant hex string into colon-spearated ipv6 address by |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
165 // inserting ':' every 4 characters. |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
166 for (i = 32; i; i--) |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
167 if ((*(ptr--) = ipv6_addr[i])) if (!(i&3)) *(ptr--) = ':'; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
168 |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
169 // Convert to binary and back to get abbreviated :: version |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
170 if (inet_pton(AF_INET6, ipv6_addr, (void *)&s6.sin6_addr) > 0) { |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
171 if (inet_ntop(AF_INET6, &s6.sin6_addr, toybuf, sizeof(toybuf))) { |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
172 char *scopes[] = {"Global","Host","Link","Site","Compat"}, |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
173 *scope = "Unknown"; |
921 | 174 |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
175 for (i=0; i < sizeof(scopes)/sizeof(*scopes); i++) |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
176 if (iscope == (!!i)<<(i+3)) scope = scopes[i]; |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
177 xprintf("%10cinet6 addr: %s/%d Scope: %s\n", |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
178 ' ', toybuf, plen, scope); |
921 | 179 } |
180 } | |
181 } | |
182 } | |
183 fclose(fp); | |
184 } | |
185 | |
879
bc7aa2d123b9
And another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
878
diff
changeset
|
186 xprintf("%10c", ' '); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
187 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
188 if (flags) { |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
189 unsigned short mask = 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
190 char **s, *str[] = { |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
191 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
192 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST", |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
193 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
194 }; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
195 |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
196 for (s = str; *s; s++) { |
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
197 if (flags & mask) xprintf("%s ", *s); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
198 mask = mask << 1; |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
199 } |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
200 } else xprintf("[NO FLAGS] "); |
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
201 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
202 if (ioctl(TT.sockfd, SIOCGIFMTU, &ifre) < 0) ifre.ifr_mtu = 0; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
203 xprintf(" MTU:%d", ifre.ifr_mtu); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
204 if (ioctl(TT.sockfd, SIOCGIFMETRIC, &ifre) < 0) ifre.ifr_metric = 0; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
205 if (!ifre.ifr_metric) ifre.ifr_metric = 1; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
206 xprintf(" Metric:%d", ifre.ifr_metric); |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
207 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
208 // non-virtual interface |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
209 |
1128 | 210 if (val) { |
870 | 211 char *label[] = {"RX bytes", "RX packets", "errors", "dropped", "overruns", |
212 "frame", 0, 0, "TX bytes", "TX packets", "errors", "dropped", "overruns", | |
213 "collisions", "carrier", 0, "txqueuelen"}; | |
214 signed char order[] = {-1, 1, 2, 3, 4, 5, -1, 9, 10, 11, 12, 14, -1, | |
215 13, 16, -1, 0, 8}; | |
216 int i; | |
217 | |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
218 // Query txqueuelen |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
219 if (ioctl(TT.sockfd, SIOCGIFTXQLEN, &ifre) >= 0) val[16] = ifre.ifr_qlen; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
220 else val[16] = -1; |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
221 |
870 | 222 for (i = 0; i < sizeof(order); i++) { |
223 int j = order[i]; | |
841 | 224 |
870 | 225 if (j < 0) xprintf("\n%10c", ' '); |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
226 else xprintf("%s:%llu ", label[j], val[j]); |
870 | 227 } |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
228 } |
870 | 229 xputc('\n'); |
230 | |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
231 if(!ioctl(TT.sockfd, SIOCGIFMAP, &ifre) && (ifre.ifr_map.irq || |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
232 ifre.ifr_map.mem_start || ifre.ifr_map.dma || ifre.ifr_map.base_addr)) |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
233 { |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
234 xprintf("%10c", ' '); |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
235 if(ifre.ifr_map.irq) xprintf("Interrupt:%d ", ifre.ifr_map.irq); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
236 if(ifre.ifr_map.base_addr >= 0x100) // IO_MAP_INDEX |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
237 xprintf("Base address:0x%lx ", ifre.ifr_map.base_addr); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
238 if(ifre.ifr_map.mem_start) |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
239 xprintf("Memory:%lx-%lx ", ifre.ifr_map.mem_start, ifre.ifr_map.mem_end); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
240 if(ifre.ifr_map.dma) xprintf("DMA chan:%x ", ifre.ifr_map.dma); |
852 | 241 xputc('\n'); |
841 | 242 } |
852 | 243 xputc('\n'); |
841 | 244 } |
245 | |
870 | 246 static void show_iface(char *iface_name) |
841 | 247 { |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
248 char *name; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
249 struct string_list *ifaces = 0, *sl; |
870 | 250 int i, j; |
251 FILE *fp; | |
252 | |
253 fp = xfopen("/proc/net/dev", "r"); | |
254 | |
255 for (i=0; fgets(toybuf, sizeof(toybuf), fp); i++) { | |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
256 char *buf = toybuf; |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
257 unsigned long long val[17]; |
870 | 258 |
259 if (i<2) continue; | |
866
0fa773e2a4fe
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
864
diff
changeset
|
260 |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
261 while (isspace(*buf)) buf++; |
870 | 262 name = strsep(&buf, ":"); |
263 if(!buf) error_exit("bad name %s", name); | |
264 | |
265 errno = 0; | |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
266 for (j=0; j<16 && !errno; j++) val[j] = strtoll(buf, &buf, 0); |
870 | 267 if (errno) perror_exit("bad %s at %s", name, buf); |
268 | |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
269 if (iface_name) { |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
270 if (!strcmp(iface_name, name)) { |
1127
e0a8cbb1a5e3
The return of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
1104
diff
changeset
|
271 display_ifconfig(iface_name, 1, val); |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
272 |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
273 return; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
274 } |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
275 } else { |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
276 sl = xmalloc(sizeof(*sl)+strlen(name)+1); |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
277 strcpy(sl->str, name); |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
278 sl->next = ifaces; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
279 ifaces = sl; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
280 |
1128 | 281 display_ifconfig(sl->str, toys.optflags & FLAG_a, val); |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
282 } |
870 | 283 } |
284 fclose(fp); | |
842
af5aab6e6678
An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents:
841
diff
changeset
|
285 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
286 if (iface_name) display_ifconfig(iface_name, 1, 0); |
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
287 else { |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
288 struct ifconf ifcon; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
289 struct ifreq *ifre; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
290 int num; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
291 |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
292 // Loop until buffer's big enough |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
293 ifcon.ifc_buf = NULL; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
294 for (num = 30;;num += 10) { |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
295 ifcon.ifc_len = sizeof(struct ifreq)*num; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
296 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len); |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
297 xioctl(TT.sockfd, SIOCGIFCONF, &ifcon); |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
298 if (ifcon.ifc_len != sizeof(struct ifreq)*num) break; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
299 } |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
300 |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
301 ifre = ifcon.ifc_req; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
302 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
303 { |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
304 // Skip duplicates |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
305 for(sl = ifaces; sl; sl = sl->next) |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
306 if(!strcmp(sl->str, ifre->ifr_name)) break; |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
307 |
958
019f54d50c8b
More ifconfig cleanup: Remove if_list, unify get_device_info and display_ifconfig(), inline another magic constant #define that's only used once.
Rob Landley <rob@landley.net>
parents:
957
diff
changeset
|
308 if(!sl) display_ifconfig(ifre->ifr_name, toys.optflags & FLAG_a, 0); |
841 | 309 } |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
310 |
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
311 free(ifcon.ifc_buf); |
841 | 312 } |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
313 |
957
1ec29d7540e2
Another round of ifconfig cleanup.
Rob Landley <rob@landley.net>
parents:
921
diff
changeset
|
314 llist_traverse(ifaces, free); |
841 | 315 } |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
316 |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
317 // Encode offset and size of field into an int, and make result negative |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
318 #define IFREQ_OFFSZ(x) -(int)((offsetof(struct ifreq, x)<<16) + sizeof(ifre.x)) |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
319 |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
320 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
|
321 { |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
322 char **argv = toys.optargs; |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
323 struct ifreq ifre; |
921 | 324 int i; |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
325 |
921 | 326 TT.sockfd = xsocket(AF_INET, SOCK_DGRAM, 0); |
878
d4263ebf6f9b
Yet more drive-by cleanup of ifconfig.
Rob Landley <rob@landley.net>
parents:
870
diff
changeset
|
327 if(toys.optc < 2) { |
870 | 328 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
|
329 return; |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
330 } |
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
331 |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
332 // Open interface |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
333 memset(&ifre, 0, sizeof(struct ifreq)); |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
334 xstrncpy(ifre.ifr_name, *argv, IFNAMSIZ); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
335 |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
336 // Perform operations on interface |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
337 while(*++argv) { |
1128 | 338 // Table of known operations |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
339 struct argh { |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
340 char *name; |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
341 int on, off; // set, clear |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
342 } try[] = { |
1128 | 343 {0, IFF_UP|IFF_RUNNING, SIOCSIFADDR}, |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
344 {"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
|
345 {"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
|
346 {"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
|
347 {"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
|
348 {"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
|
349 {"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
|
350 {"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
|
351 {"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
|
352 {"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
|
353 {"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
|
354 {"netmask", 0, SIOCSIFNETMASK}, |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
355 {"dstaddr", 0, SIOCSIFDSTADDR}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
356 {"mtu", IFREQ_OFFSZ(ifr_mtu), SIOCSIFMTU}, |
921 | 357 {"keepalive", IFREQ_OFFSZ(ifr_data), SIOCDEVPRIVATE}, // SIOCSKEEPALIVE |
358 {"outfill", IFREQ_OFFSZ(ifr_data), SIOCDEVPRIVATE+2}, // SIOCSOUTFILL | |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
359 {"metric", IFREQ_OFFSZ(ifr_metric), SIOCSIFMETRIC}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
360 {"txqueuelen", IFREQ_OFFSZ(ifr_qlen), SIOCSIFTXQLEN}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
361 {"mem_start", IFREQ_OFFSZ(ifr_map.mem_start), SIOCSIFMAP}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
362 {"io_addr", IFREQ_OFFSZ(ifr_map.base_addr), SIOCSIFMAP}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
363 {"irq", IFREQ_OFFSZ(ifr_map.irq), SIOCSIFMAP}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
364 {"inet", 0, 0}, |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
365 {"inet6", 0, 0} |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
366 }; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
367 char *s = *argv; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
368 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
|
369 |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
370 s += rev; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
371 |
1128 | 372 // "set hardware address" is oddball enough to special case |
373 if (!strcmp(*argv, "hw")) { | |
374 char *hw_addr, *ptr, *p; | |
375 struct sockaddr *sock = &ifre.ifr_hwaddr; | |
376 int count = 6; | |
377 | |
378 ptr = p = (char *)sock->sa_data; | |
379 memset(sock, 0, sizeof(struct sockaddr)); | |
380 if (!argv[1]) { | |
381 if (!strcmp("ether", *++argv)) sock->sa_family = ARPHRD_ETHER; | |
382 else if (!strcmp("infiniband", *argv)) { | |
383 sock->sa_family = ARPHRD_INFINIBAND; | |
384 count = 20; | |
385 p = ptr = toybuf; | |
386 } | |
387 } | |
388 if (!sock->sa_family || !argv[1]) { | |
389 toys.exithelp++; | |
390 error_exit("bad hw '%s'", *argv); | |
391 } | |
392 hw_addr = *++argv; | |
393 | |
394 // Parse and verify address. | |
395 while (*hw_addr && (p-ptr) < count) { | |
396 int val, len = 0; | |
397 | |
398 if (*hw_addr == ':') hw_addr++; | |
399 sscanf(hw_addr, "%2x%n", &val, &len); | |
400 if (len != 2) break; | |
401 hw_addr += len; | |
402 *p++ = val; | |
403 } | |
404 | |
405 if ((p-ptr) != count || *hw_addr) | |
406 error_exit("bad hw-addr '%s'", hw_addr ? hw_addr : ""); | |
407 | |
408 // the linux kernel's "struct sockaddr" (include/linux/socket.h in the | |
409 // kernel source) only has 14 bytes of sa_data, and an infiniband address | |
410 // is 20. So if we go through the ioctl, the kernel will truncate | |
411 // infiniband addresses, meaning we have to go through sysfs instead. | |
412 if (sock->sa_family == ARPHRD_INFINIBAND && !strchr(ifre.ifr_name, '/')) { | |
413 int fd; | |
414 | |
415 sprintf(toybuf, "/sys/class/net/%s/address", ifre.ifr_name); | |
416 fd = xopen(toybuf, O_RDWR); | |
417 xwrite(fd, *argv, strlen(*argv)); | |
418 close(fd); | |
419 } else xioctl(TT.sockfd, SIOCSIFHWADDR, &ifre); | |
420 continue; | |
421 | |
422 // Add/remove ipv6 address to interface | |
423 | |
424 } else if (!strcmp(*argv, "add") || !strcmp(*argv, "del")) { | |
425 struct ifreq_inet6 { | |
426 struct in6_addr addr; | |
427 unsigned prefix; | |
428 int index; | |
429 } ifre6; | |
430 int plen = 128, fd6 = xsocket(AF_INET6, SOCK_DGRAM, 0); | |
431 char *prefix; | |
432 | |
433 if (!argv[1]) { | |
434 toys.exithelp++; | |
435 error_exit(*argv); | |
436 } | |
437 | |
438 prefix = strchr(argv[1], '/'); | |
439 if (prefix) { | |
440 plen = atolx_range(prefix+1, 0, 128); | |
441 *prefix = 0; | |
442 } | |
443 | |
444 get_addrinfo(argv[1], AF_INET6, &ifre6.addr); | |
445 xioctl(fd6, SIOCGIFINDEX, &ifre); | |
446 ifre6.index = ifre.ifr_ifindex; | |
447 ifre6.prefix = plen; | |
448 xioctl(fd6, **(argv++)=='a' ? SIOCSIFADDR : SIOCDIFADDR, &ifre6); | |
449 | |
450 close(fd6); | |
451 continue; | |
452 // Iterate through table to find/perform operation | |
453 } else 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
|
454 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
|
455 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
|
456 |
1128 | 457 if (!t->name) { |
458 if (isdigit(**argv) || !strcmp(*argv, "default")) argv--; | |
459 else continue; | |
460 } else if (strcmp(t->name, s)) continue; | |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
461 |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
462 // Is this an SIOCSI entry? |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
463 if ((off|0xff) == 0x89ff) { |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
464 if (!rev) { |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
465 if (!*++argv) show_help(); |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
466 |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
467 // Assign value to ifre field and call ioctl? (via IFREQ_OFFSZ.) |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
468 if (on < 0) { |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
469 long l = strtoul(*argv, 0, 0); |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
470 |
921 | 471 if (off == SIOCSIFMAP) xioctl(TT.sockfd, SIOCGIFMAP, &ifre); |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
472 on = -on; |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
473 poke((on>>16) + (char *)&ifre, l, on&15); |
921 | 474 xioctl(TT.sockfd, off, &ifre); |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
475 break; |
1128 | 476 } else if (t->name || !strchr(ifre.ifr_name, ':')) { |
477 struct sockaddr_in *si = (struct sockaddr_in *)&ifre.ifr_addr; | |
478 | |
479 si->sin_family = AF_INET; | |
480 | |
481 if (!strcmp(*argv, "default")) si->sin_addr.s_addr = INADDR_ANY; | |
482 else get_addrinfo(*argv, AF_INET, &si->sin_addr); | |
483 xioctl(TT.sockfd, off, &ifre); | |
484 } | |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
485 } |
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
486 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
|
487 } |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
488 |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
489 // Set flags |
906
ce0a9368b82f
Next round of ifconfig cleanup, to be described on mailing list.
Rob Landley <rob@landley.net>
parents:
905
diff
changeset
|
490 if (on || off) { |
921 | 491 xioctl(TT.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
|
492 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
|
493 ifre.ifr_flags |= (rev ? off : on); |
921 | 494 xioctl(TT.sockfd, SIOCSIFFLAGS, &ifre); |
905
e875b03e1fcc
Yet more ifconfig cleanup. (As described on the mailing list [CLEANUP] thread.)
Rob Landley <rob@landley.net>
parents:
898
diff
changeset
|
495 } |
898
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
496 |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
497 break; |
f6db08574875
Ifconfig cleanup to option parsing.
Rob Landley <rob@landley.net>
parents:
883
diff
changeset
|
498 } |
1128 | 499 if (i == sizeof(try)/sizeof(*try)) { |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
500 toys.exithelp++; |
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
501 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
|
502 } |
919
a186f9a1406d
More ifconfig cleanup, described on list.
Rob Landley <rob@landley.net>
parents:
907
diff
changeset
|
503 } |
921 | 504 close(TT.sockfd); |
869
571e95bdf6af
ifconfig cleanup (Reorder functions to eliminate need for prototypes, put main at end).
Rob Landley <rob@landley.net>
parents:
866
diff
changeset
|
505 } |