blob: b95317638899137c7008b1430a65843aae1b43c4 [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;
Bhanu Chetlapalli9875ee92014-06-18 17:11:27 -070064 unsigned int flags;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065 char astring[20];
66 char mstring[20];
67 char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
Bhanu Chetlapalli9875ee92014-06-18 17:11:27 -070068
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 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 Chetlapalli9875ee92014-06-18 17:11:27 -070088 strlcpy(astring,
89 inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
90 sizeof(astring));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
92 if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) {
93 perror(ifr.ifr_name);
94 return -1;
95 } else
Bhanu Chetlapalli9875ee92014-06-18 17:11:27 -070096 strlcpy(mstring,
97 inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
98 sizeof(mstring));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
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 Projectdd7bc332009-03-03 19:32:55 -0800106 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 Projectdd7bc332009-03-03 19:32:55 -0800118 return 0;
119 }
120
San Mehat54a1eb72009-05-13 17:17:24 -0700121 while(argc > 0) {
122 if (!strcmp(argv[0], "up")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 setflags(s, &ifr, IFF_UP, 0);
San Mehat54a1eb72009-05-13 17:17:24 -0700124 } 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 Mehatfa49e282009-05-13 10:05:06 -0700132 setflags(s, &ifr, IFF_POINTOPOINT, 1);
San Mehat54a1eb72009-05-13 17:17:24 -0700133 } 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 Projectdd7bc332009-03-03 19:32:55 -0800142 setflags(s, &ifr, 0, IFF_UP);
San Mehat54a1eb72009-05-13 17:17:24 -0700143 } 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 Projectdd7bc332009-03-03 19:32:55 -0800151 setaddr(s, &ifr, argv[0]);
San Mehat54a1eb72009-05-13 17:17:24 -0700152 setflags(s, &ifr, IFF_UP, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 }
154 argc--, argv++;
155 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 return 0;
157}