Home | History | Annotate | Download | only in toolbox
      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <unistd.h>
      5 
      6 #include <errno.h>
      7 #include <string.h>
      8 #include <ctype.h>
      9 
     10 #include <sys/socket.h>
     11 #include <netinet/in.h>
     12 #include <linux/if.h>
     13 #include <linux/sockios.h>
     14 #include <arpa/inet.h>
     15 
     16 static void die(const char *s)
     17 {
     18     fprintf(stderr,"error: %s (%s)\n", s, strerror(errno));
     19     exit(-1);
     20 }
     21 
     22 static void setflags(int s, struct ifreq *ifr, int set, int clr)
     23 {
     24     if(ioctl(s, SIOCGIFFLAGS, ifr) < 0) die("SIOCGIFFLAGS");
     25     ifr->ifr_flags = (ifr->ifr_flags & (~clr)) | set;
     26     if(ioctl(s, SIOCSIFFLAGS, ifr) < 0) die("SIOCSIFFLAGS");
     27 }
     28 
     29 static inline void init_sockaddr_in(struct sockaddr_in *sin, const char *addr)
     30 {
     31     sin->sin_family = AF_INET;
     32     sin->sin_port = 0;
     33     sin->sin_addr.s_addr = inet_addr(addr);
     34 }
     35 
     36 static void setmtu(int s, struct ifreq *ifr, const char *mtu)
     37 {
     38     int m = atoi(mtu);
     39     ifr->ifr_mtu = m;
     40     if(ioctl(s, SIOCSIFMTU, ifr) < 0) die("SIOCSIFMTU");
     41 }
     42 static void setdstaddr(int s, struct ifreq *ifr, const char *addr)
     43 {
     44     init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_dstaddr, addr);
     45     if(ioctl(s, SIOCSIFDSTADDR, ifr) < 0) die("SIOCSIFDSTADDR");
     46 }
     47 
     48 static void setnetmask(int s, struct ifreq *ifr, const char *addr)
     49 {
     50     init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_netmask, addr);
     51     if(ioctl(s, SIOCSIFNETMASK, ifr) < 0) die("SIOCSIFNETMASK");
     52 }
     53 
     54 static void setaddr(int s, struct ifreq *ifr, const char *addr)
     55 {
     56     init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_addr, addr);
     57     if(ioctl(s, SIOCSIFADDR, ifr) < 0) die("SIOCSIFADDR");
     58 }
     59 
     60 int ifconfig_main(int argc, char *argv[])
     61 {
     62     struct ifreq ifr;
     63     int s;
     64     unsigned int addr, mask, flags;
     65     char astring[20];
     66     char mstring[20];
     67     char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
     68 
     69     argc--;
     70     argv++;
     71 
     72     if(argc == 0) return 0;
     73 
     74     memset(&ifr, 0, sizeof(struct ifreq));
     75     strncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
     76     ifr.ifr_name[IFNAMSIZ-1] = 0;
     77     argc--, argv++;
     78 
     79     if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
     80         die("cannot open control socket\n");
     81     }
     82 
     83     if (argc == 0) {
     84         if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
     85             perror(ifr.ifr_name);
     86             return -1;
     87         } else
     88             addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
     89 
     90         if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) {
     91             perror(ifr.ifr_name);
     92             return -1;
     93         } else
     94             mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
     95 
     96         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
     97             perror(ifr.ifr_name);
     98             return -1;
     99         } else
    100             flags = ifr.ifr_flags;
    101 
    102         sprintf(astring, "%d.%d.%d.%d",
    103                 addr & 0xff,
    104                 ((addr >> 8) & 0xff),
    105                 ((addr >> 16) & 0xff),
    106                 ((addr >> 24) & 0xff));
    107         sprintf(mstring, "%d.%d.%d.%d",
    108                 mask & 0xff,
    109                 ((mask >> 8) & 0xff),
    110                 ((mask >> 16) & 0xff),
    111                 ((mask >> 24) & 0xff));
    112         printf("%s: ip %s mask %s flags [", ifr.ifr_name,
    113                astring,
    114                mstring
    115                );
    116 
    117         updown =  (flags & IFF_UP)           ? "up" : "down";
    118         brdcst =  (flags & IFF_BROADCAST)    ? " broadcast" : "";
    119         loopbk =  (flags & IFF_LOOPBACK)     ? " loopback" : "";
    120         ppp =     (flags & IFF_POINTOPOINT)  ? " point-to-point" : "";
    121         running = (flags & IFF_RUNNING)      ? " running" : "";
    122         multi =   (flags & IFF_MULTICAST)    ? " multicast" : "";
    123         printf("%s%s%s%s%s%s]\n", updown, brdcst, loopbk, ppp, running, multi);
    124         return 0;
    125     }
    126 
    127     while(argc > 0) {
    128         if (!strcmp(argv[0], "up")) {
    129             setflags(s, &ifr, IFF_UP, 0);
    130         } else if (!strcmp(argv[0], "mtu")) {
    131             argc--, argv++;
    132             if (!argc) {
    133                 errno = EINVAL;
    134                 die("expecting a value for parameter \"mtu\"");
    135             }
    136             setmtu(s, &ifr, argv[0]);
    137         } else if (!strcmp(argv[0], "-pointopoint")) {
    138             setflags(s, &ifr, IFF_POINTOPOINT, 1);
    139         } else if (!strcmp(argv[0], "pointopoint")) {
    140             argc--, argv++;
    141             if (!argc) {
    142                 errno = EINVAL;
    143                 die("expecting an IP address for parameter \"pointtopoint\"");
    144             }
    145             setdstaddr(s, &ifr, argv[0]);
    146             setflags(s, &ifr, IFF_POINTOPOINT, 0);
    147         } else if (!strcmp(argv[0], "down")) {
    148             setflags(s, &ifr, 0, IFF_UP);
    149         } else if (!strcmp(argv[0], "netmask")) {
    150             argc--, argv++;
    151             if (!argc) {
    152                 errno = EINVAL;
    153                 die("expecting an IP address for parameter \"netmask\"");
    154             }
    155             setnetmask(s, &ifr, argv[0]);
    156         } else if (isdigit(argv[0][0])) {
    157             setaddr(s, &ifr, argv[0]);
    158             setflags(s, &ifr, IFF_UP, 0);
    159         }
    160         argc--, argv++;
    161     }
    162     return 0;
    163 }
    164