blob: 80c0e5ce0ab8510a251a24d1e6b39d6c0807f1bd [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
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
16static void die(const char *s)
17{
18 fprintf(stderr,"error: %s (%s)\n", s, strerror(errno));
19 exit(-1);
20}
21
22static 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
29static inline void init_sockaddr_in(struct sockaddr_in *sin, const char *addr)
30{
San Mehat54a1eb72009-05-13 17:17:24 -070031 sin->sin_family = AF_INET;
32 sin->sin_port = 0;
33 sin->sin_addr.s_addr = inet_addr(addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034}
35
San Mehat54a1eb72009-05-13 17:17:24 -070036static 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 Mehatfa49e282009-05-13 10:05:06 -070042static void setdstaddr(int s, struct ifreq *ifr, const char *addr)
43{
San Mehat54a1eb72009-05-13 17:17:24 -070044 init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_dstaddr, addr);
San Mehatfa49e282009-05-13 10:05:06 -070045 if(ioctl(s, SIOCSIFDSTADDR, ifr) < 0) die("SIOCSIFDSTADDR");
46}
47
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048static void setnetmask(int s, struct ifreq *ifr, const char *addr)
49{
San Mehat54a1eb72009-05-13 17:17:24 -070050 init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_netmask, addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 if(ioctl(s, SIOCSIFNETMASK, ifr) < 0) die("SIOCSIFNETMASK");
52}
53
54static void setaddr(int s, struct ifreq *ifr, const char *addr)
55{
San Mehat54a1eb72009-05-13 17:17:24 -070056 init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_addr, addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 if(ioctl(s, SIOCSIFADDR, ifr) < 0) die("SIOCSIFADDR");
58}
59
60int 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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 return 0;
125 }
126
San Mehat54a1eb72009-05-13 17:17:24 -0700127 while(argc > 0) {
128 if (!strcmp(argv[0], "up")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 setflags(s, &ifr, IFF_UP, 0);
San Mehat54a1eb72009-05-13 17:17:24 -0700130 } 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")) {
San Mehatfa49e282009-05-13 10:05:06 -0700138 setflags(s, &ifr, IFF_POINTOPOINT, 1);
San Mehat54a1eb72009-05-13 17:17:24 -0700139 } 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")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 setflags(s, &ifr, 0, IFF_UP);
San Mehat54a1eb72009-05-13 17:17:24 -0700149 } 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])) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 setaddr(s, &ifr, argv[0]);
San Mehat54a1eb72009-05-13 17:17:24 -0700158 setflags(s, &ifr, IFF_UP, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
160 argc--, argv++;
161 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 return 0;
163}