blob: 6474fb7244ec76f70d620b06d3c6a990aeb68fb7 [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)
Steve Anton10542f22019-01-11 09:11:00 -080012#include "rtc_base/ifaddrs_android.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020013
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include <errno.h>
15#include <linux/netlink.h>
16#include <linux/rtnetlink.h>
kjellandere96c45b2017-06-30 10:45:21 -070017#include <net/if.h>
18#include <netinet/in.h>
19#include <stdlib.h>
20#include <string.h>
21#include <sys/ioctl.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <sys/utsname.h>
25#include <unistd.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Danil Chapovalove6106102022-02-16 12:29:02 +010027#include "absl/cleanup/cleanup.h"
28
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000029namespace {
30
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031struct netlinkrequest {
32 nlmsghdr header;
33 ifaddrmsg msg;
34};
35
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036const int kMaxReadSize = 4096;
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000037
38} // namespace
39
40namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
42int set_ifname(struct ifaddrs* ifaddr, int interface) {
43 char buf[IFNAMSIZ] = {0};
44 char* name = if_indextoname(interface, buf);
deadbeef37f5ecf2017-02-27 14:06:41 -080045 if (name == nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 return -1;
47 }
48 ifaddr->ifa_name = new char[strlen(name) + 1];
49 strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
50 return 0;
51}
52
53int set_flags(struct ifaddrs* ifaddr) {
54 int fd = socket(AF_INET, SOCK_DGRAM, 0);
55 if (fd == -1) {
56 return -1;
57 }
58 ifreq ifr;
59 memset(&ifr, 0, sizeof(ifr));
60 strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
61 int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
62 close(fd);
63 if (rc == -1) {
64 return -1;
65 }
66 ifaddr->ifa_flags = ifr.ifr_flags;
67 return 0;
68}
69
Yves Gerey665174f2018-06-19 15:03:05 +020070int set_addresses(struct ifaddrs* ifaddr,
71 ifaddrmsg* msg,
72 void* data,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 size_t len) {
74 if (msg->ifa_family == AF_INET) {
75 sockaddr_in* sa = new sockaddr_in;
76 sa->sin_family = AF_INET;
77 memcpy(&sa->sin_addr, data, len);
78 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
79 } else if (msg->ifa_family == AF_INET6) {
80 sockaddr_in6* sa = new sockaddr_in6;
81 sa->sin6_family = AF_INET6;
82 sa->sin6_scope_id = msg->ifa_index;
83 memcpy(&sa->sin6_addr, data, len);
84 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
85 } else {
86 return -1;
87 }
88 return 0;
89}
90
91int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
deadbeef37f5ecf2017-02-27 14:06:41 -080092 char* prefix = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 if (family == AF_INET) {
94 sockaddr_in* mask = new sockaddr_in;
95 mask->sin_family = AF_INET;
96 memset(&mask->sin_addr, 0, sizeof(in_addr));
97 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
98 if (prefixlen > 32) {
99 prefixlen = 32;
100 }
101 prefix = reinterpret_cast<char*>(&mask->sin_addr);
102 } else if (family == AF_INET6) {
103 sockaddr_in6* mask = new sockaddr_in6;
104 mask->sin6_family = AF_INET6;
105 memset(&mask->sin6_addr, 0, sizeof(in6_addr));
106 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
107 if (prefixlen > 128) {
108 prefixlen = 128;
109 }
110 prefix = reinterpret_cast<char*>(&mask->sin6_addr);
111 } else {
112 return -1;
113 }
114 for (int i = 0; i < (prefixlen / 8); i++) {
115 *prefix++ = 0xFF;
116 }
117 char remainder = 0xff;
118 remainder <<= (8 - prefixlen % 8);
119 *prefix = remainder;
120 return 0;
121}
122
Yves Gerey665174f2018-06-19 15:03:05 +0200123int populate_ifaddrs(struct ifaddrs* ifaddr,
124 ifaddrmsg* msg,
125 void* bytes,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 size_t len) {
127 if (set_ifname(ifaddr, msg->ifa_index) != 0) {
128 return -1;
129 }
130 if (set_flags(ifaddr) != 0) {
131 return -1;
132 }
133 if (set_addresses(ifaddr, msg, bytes, len) != 0) {
134 return -1;
135 }
136 if (make_prefixes(ifaddr, msg->ifa_family, msg->ifa_prefixlen) != 0) {
137 return -1;
138 }
139 return 0;
140}
141
142int getifaddrs(struct ifaddrs** result) {
Danil Chapovalove6106102022-02-16 12:29:02 +0100143 *result = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
145 if (fd < 0) {
146 return -1;
147 }
Danil Chapovalove6106102022-02-16 12:29:02 +0100148 absl::Cleanup close_file = [fd] { close(fd); };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149
150 netlinkrequest ifaddr_request;
151 memset(&ifaddr_request, 0, sizeof(ifaddr_request));
152 ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
153 ifaddr_request.header.nlmsg_type = RTM_GETADDR;
154 ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg));
155
156 ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
157 if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 return -1;
159 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800160 struct ifaddrs* start = nullptr;
Danil Chapovalove6106102022-02-16 12:29:02 +0100161 absl::Cleanup cleanup_start = [&start] { freeifaddrs(start); };
deadbeef37f5ecf2017-02-27 14:06:41 -0800162 struct ifaddrs* current = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 char buf[kMaxReadSize];
164 ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
165 while (amount_read > 0) {
166 nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]);
167 size_t header_size = static_cast<size_t>(amount_read);
Yves Gerey665174f2018-06-19 15:03:05 +0200168 for (; NLMSG_OK(header, header_size);
169 header = NLMSG_NEXT(header, header_size)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000170 switch (header->nlmsg_type) {
171 case NLMSG_DONE:
Danil Chapovalove6106102022-02-16 12:29:02 +0100172 // Success. Return `start`. Cancel `start` cleanup because it
173 // becomes callers responsibility.
174 std::move(cleanup_start).Cancel();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 *result = start;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 return 0;
177 case NLMSG_ERROR:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 return -1;
179 case RTM_NEWADDR: {
180 ifaddrmsg* address_msg =
181 reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(header));
182 rtattr* rta = IFA_RTA(address_msg);
183 ssize_t payload_len = IFA_PAYLOAD(header);
184 while (RTA_OK(rta, payload_len)) {
Yongje Leeebd9abc2018-04-27 09:47:07 +0900185 if ((address_msg->ifa_family == AF_INET &&
Yves Gerey665174f2018-06-19 15:03:05 +0200186 rta->rta_type == IFA_LOCAL) ||
Yongje Leeebd9abc2018-04-27 09:47:07 +0900187 (address_msg->ifa_family == AF_INET6 &&
188 rta->rta_type == IFA_ADDRESS)) {
189 ifaddrs* newest = new ifaddrs;
190 memset(newest, 0, sizeof(ifaddrs));
191 if (current) {
192 current->ifa_next = newest;
193 } else {
194 start = newest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 }
Yongje Leeebd9abc2018-04-27 09:47:07 +0900196 if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
197 RTA_PAYLOAD(rta)) != 0) {
Yongje Leeebd9abc2018-04-27 09:47:07 +0900198 return -1;
199 }
200 current = newest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201 }
202 rta = RTA_NEXT(rta, payload_len);
203 }
204 break;
205 }
206 }
207 }
208 amount_read = recv(fd, &buf, kMaxReadSize, 0);
209 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 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)