1 /*
2  * libjingle
3  * Copyright 2012, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ifaddrs-android.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/utsname.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
36 #include <net/if.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <linux/netlink.h>
40 #include <linux/rtnetlink.h>
41 struct netlinkrequest {
42   nlmsghdr header;
43   ifaddrmsg msg;
44 };
45 namespace {
46 const int kMaxReadSize = 4096;
47 };
set_ifname(struct ifaddrs * ifaddr,int interface)48 int set_ifname(struct ifaddrs* ifaddr, int interface) {
49   char buf[IFNAMSIZ] = {0};
50   char* name = if_indextoname(interface, buf);
51   if (name == NULL) {
52     return -1;
53   }
54   ifaddr->ifa_name = new char[strlen(name) + 1];
55   strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
56   return 0;
57 }
set_flags(struct ifaddrs * ifaddr)58 int set_flags(struct ifaddrs* ifaddr) {
59   int fd = socket(AF_INET, SOCK_DGRAM, 0);
60   if (fd == -1) {
61     return -1;
62   }
63   ifreq ifr;
64   memset(&ifr, 0, sizeof(ifr));
65   strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
66   int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
67   close(fd);
68   if (rc == -1) {
69     return -1;
70   }
71   ifaddr->ifa_flags = ifr.ifr_flags;
72   return 0;
73 }
set_addresses(struct ifaddrs * ifaddr,ifaddrmsg * msg,void * data,size_t len)74 int set_addresses(struct ifaddrs* ifaddr, ifaddrmsg* msg, void* data,
75                   size_t len) {
76   if (msg->ifa_family == AF_INET) {
77     sockaddr_in* sa = new sockaddr_in;
78     sa->sin_family = AF_INET;
79     memcpy(&sa->sin_addr, data, len);
80     ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
81   } else if (msg->ifa_family == AF_INET6) {
82     sockaddr_in6* sa = new sockaddr_in6;
83     sa->sin6_family = AF_INET6;
84     sa->sin6_scope_id = msg->ifa_index;
85     memcpy(&sa->sin6_addr, data, len);
86     ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
87   } else {
88     return -1;
89   }
90   return 0;
91 }
make_prefixes(struct ifaddrs * ifaddr,int family,int prefixlen)92 int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
93   char* prefix = NULL;
94   if (family == AF_INET) {
95     sockaddr_in* mask = new sockaddr_in;
96     mask->sin_family = AF_INET;
97     memset(&mask->sin_addr, 0, sizeof(in_addr));
98     ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
99     if (prefixlen > 32) {
100       prefixlen = 32;
101     }
102     prefix = reinterpret_cast<char*>(&mask->sin_addr);
103   } else if (family == AF_INET6) {
104     sockaddr_in6* mask = new sockaddr_in6;
105     mask->sin6_family = AF_INET6;
106     memset(&mask->sin6_addr, 0, sizeof(in6_addr));
107     ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
108     if (prefixlen > 128) {
109       prefixlen = 128;
110     }
111     prefix = reinterpret_cast<char*>(&mask->sin6_addr);
112   } else {
113     return -1;
114   }
115   for (int i = 0; i < (prefixlen / 8); i++) {
116     *prefix++ = 0xFF;
117   }
118   char remainder = 0xff;
119   remainder <<= (8 - prefixlen % 8);
120   *prefix = remainder;
121   return 0;
122 }
populate_ifaddrs(struct ifaddrs * ifaddr,ifaddrmsg * msg,void * bytes,size_t len)123 int populate_ifaddrs(struct ifaddrs* ifaddr, ifaddrmsg* msg, void* bytes,
124                      size_t len) {
125   if (set_ifname(ifaddr, msg->ifa_index) != 0) {
126     return -1;
127   }
128   if (set_flags(ifaddr) != 0) {
129     return -1;
130   }
131   if (set_addresses(ifaddr, msg, bytes, len) != 0) {
132     return -1;
133   }
134   if (make_prefixes(ifaddr, msg->ifa_family, msg->ifa_prefixlen) != 0) {
135     return -1;
136   }
137   return 0;
138 }
getifaddrs(struct ifaddrs ** result)139 int getifaddrs(struct ifaddrs** result) {
140   int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
141   if (fd < 0) {
142     return -1;
143   }
144   netlinkrequest ifaddr_request;
145   memset(&ifaddr_request, 0, sizeof(ifaddr_request));
146   ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
147   ifaddr_request.header.nlmsg_type = RTM_GETADDR;
148   ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg));
149   ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
150   if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) {
151     close(fd);
152     return -1;
153   }
154   struct ifaddrs* start = NULL;
155   struct ifaddrs* current = NULL;
156   char buf[kMaxReadSize];
157   ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
158   while (amount_read > 0) {
159     nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]);
160     size_t header_size = static_cast<size_t>(amount_read);
161     for ( ; NLMSG_OK(header, header_size);
162           header = NLMSG_NEXT(header, header_size)) {
163       switch (header->nlmsg_type) {
164         case NLMSG_DONE:
165           // Success. Return.
166           *result = start;
167           close(fd);
168           return 0;
169         case NLMSG_ERROR:
170           close(fd);
171           freeifaddrs(start);
172           return -1;
173         case RTM_NEWADDR: {
174           ifaddrmsg* address_msg =
175               reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(header));
176           rtattr* rta = IFA_RTA(address_msg);
177           ssize_t payload_len = IFA_PAYLOAD(header);
178           while (RTA_OK(rta, payload_len)) {
179             if (rta->rta_type == IFA_ADDRESS) {
180               int family = address_msg->ifa_family;
181               if (family == AF_INET || family == AF_INET6) {
182                 ifaddrs* newest = new ifaddrs;
183                 memset(newest, 0, sizeof(ifaddrs));
184                 if (current) {
185                   current->ifa_next = newest;
186                 } else {
187                   start = newest;
188                 }
189                 if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
190                                      RTA_PAYLOAD(rta)) != 0) {
191                   freeifaddrs(start);
192                   *result = NULL;
193                   return -1;
194                 }
195                 current = newest;
196               }
197             }
198             rta = RTA_NEXT(rta, payload_len);
199           }
200           break;
201         }
202       }
203     }
204     amount_read = recv(fd, &buf, kMaxReadSize, 0);
205   }
206   close(fd);
207   freeifaddrs(start);
208   return -1;
209 }
freeifaddrs(struct ifaddrs * addrs)210 void freeifaddrs(struct ifaddrs* addrs) {
211   struct ifaddrs* last = NULL;
212   struct ifaddrs* cursor = addrs;
213   while (cursor) {
214     delete[] cursor->ifa_name;
215     delete cursor->ifa_addr;
216     delete cursor->ifa_netmask;
217     last = cursor;
218     cursor = cursor->ifa_next;
219     delete last;
220   }
221 }
222