The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | { |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 31 | sin->sin_family = AF_INET; |
| 32 | sin->sin_port = 0; |
| 33 | sin->sin_addr.s_addr = inet_addr(addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | } |
| 35 | |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 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 | } |
San Mehat | fa49e28 | 2009-05-13 10:05:06 -0700 | [diff] [blame] | 42 | static void setdstaddr(int s, struct ifreq *ifr, const char *addr) |
| 43 | { |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 44 | init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_dstaddr, addr); |
San Mehat | fa49e28 | 2009-05-13 10:05:06 -0700 | [diff] [blame] | 45 | if(ioctl(s, SIOCSIFDSTADDR, ifr) < 0) die("SIOCSIFDSTADDR"); |
| 46 | } |
| 47 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | static void setnetmask(int s, struct ifreq *ifr, const char *addr) |
| 49 | { |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 50 | init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_netmask, addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | if(ioctl(s, SIOCSIFNETMASK, ifr) < 0) die("SIOCSIFNETMASK"); |
| 52 | } |
| 53 | |
| 54 | static void setaddr(int s, struct ifreq *ifr, const char *addr) |
| 55 | { |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 56 | init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_addr, addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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; |
Bhanu Chetlapalli | 9875ee9 | 2014-06-18 17:11:27 -0700 | [diff] [blame] | 64 | unsigned int flags; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | char astring[20]; |
| 66 | char mstring[20]; |
| 67 | char *updown, *brdcst, *loopbk, *ppp, *running, *multi; |
Bhanu Chetlapalli | 9875ee9 | 2014-06-18 17:11:27 -0700 | [diff] [blame] | 68 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 |
Bhanu Chetlapalli | 9875ee9 | 2014-06-18 17:11:27 -0700 | [diff] [blame] | 88 | strlcpy(astring, |
| 89 | inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), |
| 90 | sizeof(astring)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | |
| 92 | if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) { |
| 93 | perror(ifr.ifr_name); |
| 94 | return -1; |
| 95 | } else |
Bhanu Chetlapalli | 9875ee9 | 2014-06-18 17:11:27 -0700 | [diff] [blame] | 96 | strlcpy(mstring, |
| 97 | inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), |
| 98 | sizeof(mstring)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | |
| 100 | if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { |
| 101 | perror(ifr.ifr_name); |
| 102 | return -1; |
| 103 | } else |
| 104 | flags = ifr.ifr_flags; |
| 105 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 121 | while(argc > 0) { |
| 122 | if (!strcmp(argv[0], "up")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 123 | setflags(s, &ifr, IFF_UP, 0); |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 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")) { |
San Mehat | fa49e28 | 2009-05-13 10:05:06 -0700 | [diff] [blame] | 132 | setflags(s, &ifr, IFF_POINTOPOINT, 1); |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 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")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | setflags(s, &ifr, 0, IFF_UP); |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 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])) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | setaddr(s, &ifr, argv[0]); |
San Mehat | 54a1eb7 | 2009-05-13 17:17:24 -0700 | [diff] [blame] | 152 | setflags(s, &ifr, IFF_UP, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | } |
| 154 | argc--, argv++; |
| 155 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |