annotate toys/pending/ifconfig.c @ 1128:9eaec92e3713

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