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 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 strlcpy(astring, 89 inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 90 sizeof(astring)); 91 92 if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) { 93 perror(ifr.ifr_name); 94 return -1; 95 } else 96 strlcpy(mstring, 97 inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 98 sizeof(mstring)); 99 100 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { 101 perror(ifr.ifr_name); 102 return -1; 103 } else 104 flags = ifr.ifr_flags; 105 106 printf("%s: ip %s mask %s flags [", ifr.ifr_name, 107 astring, 108 mstring 109 ); 110 111 updown = (flags & IFF_UP) ? "up" : "down"; 112 brdcst = (flags & IFF_BROADCAST) ? " broadcast" : ""; 113 loopbk = (flags & IFF_LOOPBACK) ? " loopback" : ""; 114 ppp = (flags & IFF_POINTOPOINT) ? " point-to-point" : ""; 115 running = (flags & IFF_RUNNING) ? " running" : ""; 116 multi = (flags & IFF_MULTICAST) ? " multicast" : ""; 117 printf("%s%s%s%s%s%s]\n", updown, brdcst, loopbk, ppp, running, multi); 118 return 0; 119 } 120 121 while(argc > 0) { 122 if (!strcmp(argv[0], "up")) { 123 setflags(s, &ifr, IFF_UP, 0); 124 } else if (!strcmp(argv[0], "mtu")) { 125 argc--, argv++; 126 if (!argc) { 127 errno = EINVAL; 128 die("expecting a value for parameter \"mtu\""); 129 } 130 setmtu(s, &ifr, argv[0]); 131 } else if (!strcmp(argv[0], "-pointopoint")) { 132 setflags(s, &ifr, IFF_POINTOPOINT, 1); 133 } else if (!strcmp(argv[0], "pointopoint")) { 134 argc--, argv++; 135 if (!argc) { 136 errno = EINVAL; 137 die("expecting an IP address for parameter \"pointtopoint\""); 138 } 139 setdstaddr(s, &ifr, argv[0]); 140 setflags(s, &ifr, IFF_POINTOPOINT, 0); 141 } else if (!strcmp(argv[0], "down")) { 142 setflags(s, &ifr, 0, IFF_UP); 143 } else if (!strcmp(argv[0], "netmask")) { 144 argc--, argv++; 145 if (!argc) { 146 errno = EINVAL; 147 die("expecting an IP address for parameter \"netmask\""); 148 } 149 setnetmask(s, &ifr, argv[0]); 150 } else if (isdigit(argv[0][0])) { 151 setaddr(s, &ifr, argv[0]); 152 setflags(s, &ifr, IFF_UP, 0); 153 } 154 argc--, argv++; 155 } 156 return 0; 157 } 158