blob: cc6cdab12c4c224ea4675548199dd3589c6f6d7a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#if defined(WEBRTC_ANDROID)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "rtc_base/ifaddrs-android.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <errno.h>
14#include <linux/netlink.h>
15#include <linux/rtnetlink.h>
kjellandere96c45b2017-06-30 10:45:21 -070016#include <net/if.h>
17#include <netinet/in.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/ioctl.h>
21#include <sys/socket.h>
22#include <sys/types.h>
23#include <sys/utsname.h>
24#include <unistd.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000026namespace {
27
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028struct netlinkrequest {
29 nlmsghdr header;
30 ifaddrmsg msg;
31};
32
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033const int kMaxReadSize = 4096;
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000034
35} // namespace
36
37namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
39int set_ifname(struct ifaddrs* ifaddr, int interface) {
40 char buf[IFNAMSIZ] = {0};
41 char* name = if_indextoname(interface, buf);
deadbeef37f5ecf2017-02-27 14:06:41 -080042 if (name == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 return -1;
44 }
45 ifaddr->ifa_name = new char[strlen(name) + 1];
46 strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
47 return 0;
48}
49
50int set_flags(struct ifaddrs* ifaddr) {
51 int fd = socket(AF_INET, SOCK_DGRAM, 0);
52 if (fd == -1) {
53 return -1;
54 }
55 ifreq ifr;
56 memset(&ifr, 0, sizeof(ifr));
57 strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
58 int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
59 close(fd);
60 if (rc == -1) {
61 return -1;
62 }
63 ifaddr->ifa_flags = ifr.ifr_flags;
64 return 0;
65}
66
Yves Gerey665174f2018-06-19 15:03:05 +020067int set_addresses(struct ifaddrs* ifaddr,
68 ifaddrmsg* msg,
69 void* data,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 size_t len) {
71 if (msg->ifa_family == AF_INET) {
72 sockaddr_in* sa = new sockaddr_in;
73 sa->sin_family = AF_INET;
74 memcpy(&sa->sin_addr, data, len);
75 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
76 } else if (msg->ifa_family == AF_INET6) {
77 sockaddr_in6* sa = new sockaddr_in6;
78 sa->sin6_family = AF_INET6;
79 sa->sin6_scope_id = msg->ifa_index;
80 memcpy(&sa->sin6_addr, data, len);
81 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
82 } else {
83 return -1;
84 }
85 return 0;
86}
87
88int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
deadbeef37f5ecf2017-02-27 14:06:41 -080089 char* prefix = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 if (family == AF_INET) {
91 sockaddr_in* mask = new sockaddr_in;
92 mask->sin_family = AF_INET;
93 memset(&mask->sin_addr, 0, sizeof(in_addr));
94 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
95 if (prefixlen > 32) {
96 prefixlen = 32;
97 }
98 prefix = reinterpret_cast<char*>(&mask->sin_addr);
99 } else if (family == AF_INET6) {
100 sockaddr_in6* mask = new sockaddr_in6;
101 mask->sin6_family = AF_INET6;
102 memset(&mask->sin6_addr, 0, sizeof(in6_addr));
103 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
104 if (prefixlen > 128) {
105 prefixlen = 128;
106 }
107 prefix = reinterpret_cast<char*>(&mask->sin6_addr);
108 } else {
109 return -1;
110 }
111 for (int i = 0; i < (prefixlen / 8); i++) {
112 *prefix++ = 0xFF;
113 }
114 char remainder = 0xff;
115 remainder <<= (8 - prefixlen % 8);
116 *prefix = remainder;
117 return 0;
118}
119
Yves Gerey665174f2018-06-19 15:03:05 +0200120int populate_ifaddrs(struct ifaddrs* ifaddr,
121 ifaddrmsg* msg,
122 void* bytes,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 size_t len) {
124 if (set_ifname(ifaddr, msg->ifa_index) != 0) {
125 return -1;
126 }
127 if (set_flags(ifaddr) != 0) {
128 return -1;
129 }
130 if (set_addresses(ifaddr, msg, bytes, len) != 0) {
131 return -1;
132 }
133 if (make_prefixes(ifaddr, msg->ifa_family, msg->ifa_prefixlen) != 0) {
134 return -1;
135 }
136 return 0;
137}
138
139int getifaddrs(struct ifaddrs** result) {
140 int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
141 if (fd < 0) {
142 return -1;
143 }
144
145 netlinkrequest ifaddr_request;
146 memset(&ifaddr_request, 0, sizeof(ifaddr_request));
147 ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
148 ifaddr_request.header.nlmsg_type = RTM_GETADDR;
149 ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg));
150
151 ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
152 if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) {
153 close(fd);
154 return -1;
155 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800156 struct ifaddrs* start = nullptr;
157 struct ifaddrs* current = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 char buf[kMaxReadSize];
159 ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
160 while (amount_read > 0) {
161 nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]);
162 size_t header_size = static_cast<size_t>(amount_read);
Yves Gerey665174f2018-06-19 15:03:05 +0200163 for (; NLMSG_OK(header, header_size);
164 header = NLMSG_NEXT(header, header_size)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165 switch (header->nlmsg_type) {
166 case NLMSG_DONE:
167 // Success. Return.
168 *result = start;
169 close(fd);
170 return 0;
171 case NLMSG_ERROR:
172 close(fd);
173 freeifaddrs(start);
174 return -1;
175 case RTM_NEWADDR: {
176 ifaddrmsg* address_msg =
177 reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(header));
178 rtattr* rta = IFA_RTA(address_msg);
179 ssize_t payload_len = IFA_PAYLOAD(header);
180 while (RTA_OK(rta, payload_len)) {
Yongje Leeebd9abc2018-04-27 09:47:07 +0900181 if ((address_msg->ifa_family == AF_INET &&
Yves Gerey665174f2018-06-19 15:03:05 +0200182 rta->rta_type == IFA_LOCAL) ||
Yongje Leeebd9abc2018-04-27 09:47:07 +0900183 (address_msg->ifa_family == AF_INET6 &&
184 rta->rta_type == IFA_ADDRESS)) {
185 ifaddrs* newest = new ifaddrs;
186 memset(newest, 0, sizeof(ifaddrs));
187 if (current) {
188 current->ifa_next = newest;
189 } else {
190 start = newest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 }
Yongje Leeebd9abc2018-04-27 09:47:07 +0900192 if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
193 RTA_PAYLOAD(rta)) != 0) {
194 freeifaddrs(start);
195 *result = nullptr;
196 return -1;
197 }
198 current = newest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 }
200 rta = RTA_NEXT(rta, payload_len);
201 }
202 break;
203 }
204 }
205 }
206 amount_read = recv(fd, &buf, kMaxReadSize, 0);
207 }
208 close(fd);
209 freeifaddrs(start);
210 return -1;
211}
212
213void freeifaddrs(struct ifaddrs* addrs) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800214 struct ifaddrs* last = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215 struct ifaddrs* cursor = addrs;
216 while (cursor) {
217 delete[] cursor->ifa_name;
218 delete cursor->ifa_addr;
219 delete cursor->ifa_netmask;
220 last = cursor;
221 cursor = cursor->ifa_next;
222 delete last;
223 }
224}
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +0000225
226} // namespace rtc
henrik.lundin@webrtc.org18584fc2014-08-27 10:17:22 +0000227#endif // defined(WEBRTC_ANDROID)