# HG changeset patch # User Rob Landley # Date 1368410956 18000 # Node ID f6db085748752a43171ebf87031d81d22f44bf27 # Parent 849e14fecf2b8736ad5f5fd60b9548717ddfd7ee Ifconfig cleanup to option parsing. diff -r 849e14fecf2b -r f6db08574875 toys/pending/ifconfig.c --- a/toys/pending/ifconfig.c Fri May 10 18:57:01 2013 -0500 +++ b/toys/pending/ifconfig.c Sun May 12 21:09:16 2013 -0500 @@ -740,6 +740,8 @@ void ifconfig_main(void) { char **argv = toys.optargs; + struct ifreq ifre; + int i, sockfd = 0; if(*argv && (strcmp(*argv, "--help") == 0)) show_help(); @@ -748,49 +750,43 @@ return; } - //set ifconfig params. - { - struct ifreq ifre; - int sockfd = 0; - //get interface name - memset(&ifre, 0, sizeof(struct ifreq)); - strncpy(ifre.ifr_name, *argv, IFNAMSIZ); - ifre.ifr_name[IFNAMSIZ-1] = 0; - if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) perror_exit("socket"); - - while(*++argv != NULL) { - /* flags settings */ - if (!strcmp(*argv, "up")) - set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); - else if (!strcmp(*argv, "down")) - set_flags(sockfd, &ifre, 0, IFF_UP); + //get interface name + memset(&ifre, 0, sizeof(struct ifreq)); + xstrncpy(ifre.ifr_name, *argv, IFNAMSIZ); + sockfd = xsocket(AF_INET, SOCK_DGRAM, 0); - else if (!strcmp(*argv, "arp")) - set_flags(sockfd, &ifre, 0, IFF_NOARP); - else if (!strcmp(*argv, "-arp")) - set_flags(sockfd, &ifre, IFF_NOARP, 0); - else if (!strcmp(*argv, "trailers")) - set_flags(sockfd, &ifre, 0, IFF_NOTRAILERS); - else if (!strcmp(*argv, "-trailers")) - set_flags(sockfd, &ifre, IFF_NOTRAILERS, 0); + while(*++argv) { + struct { + char *name; + int flags[2]; // set, clear + } try[] = { + {"up", {IFF_UP|IFF_RUNNING, 0}}, + {"down", {0, IFF_UP}}, + {"arp", {0, IFF_NOARP}}, + {"trailers", {0, IFF_NOTRAILERS}}, + {"promisc", {IFF_PROMISC, 0}}, + {"allmulti", {IFF_ALLMULTI, 0}}, + {"multicast", {IFF_MULTICAST, 0}}, + {"dynamic", {IFF_DYNAMIC, 0}} + }; + char *s = *argv; + int rev = (*s == '-'); - else if (!strcmp(*argv, "promisc")) - set_flags(sockfd, &ifre, IFF_PROMISC, 0); - else if (!strcmp(*argv, "-promisc")) - set_flags(sockfd, &ifre, 0, IFF_PROMISC); - else if (!strcmp(*argv, "allmulti")) - set_flags(sockfd, &ifre, IFF_ALLMULTI, 0); - else if (!strcmp(*argv, "-allmulti")) - set_flags(sockfd, &ifre, 0, IFF_ALLMULTI); - else if (!strcmp(*argv, "multicast")) - set_flags(sockfd, &ifre, IFF_MULTICAST, 0); - else if (!strcmp(*argv, "-multicast")) - set_flags(sockfd, &ifre, 0, IFF_MULTICAST); - else if (!strcmp(*argv, "dynamic")) - set_flags(sockfd, &ifre, IFF_DYNAMIC, 0); - else if (!strcmp(*argv, "-dynamic")) - set_flags(sockfd, &ifre, 0, IFF_DYNAMIC); - else if (!strcmp(*argv, "-pointopoint")) + s += rev; + + for (i = 0; i < sizeof(try)/sizeof(*try); i++) { + if (strcmp(try[i].name, s)) continue; + + xioctl(sockfd, SIOCGIFFLAGS, &ifre); + ifre.ifr_flags &= ~try[i].flags[rev^1]; + ifre.ifr_flags |= try[i].flags[rev]; + + xioctl(sockfd, SIOCSIFFLAGS, &ifre); + break; + } + if (i != sizeof(try)/sizeof(*try)) continue; + + if (!strcmp(*argv, "-pointopoint")) set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT); /*value setup */ else if (!strcmp(*argv, "pointopoint")) { @@ -868,5 +864,4 @@ } if(sockfd > 0) close(sockfd); - } }