You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #if defined(WEBRTC_ANDROID)
  11. #include "rtc_base/ifaddrs_android.h"
  12. #include <errno.h>
  13. #include <linux/netlink.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/if.h>
  16. #include <netinet/in.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/socket.h>
  21. #include <sys/types.h>
  22. #include <sys/utsname.h>
  23. #include <unistd.h>
  24. namespace {
  25. struct netlinkrequest {
  26. nlmsghdr header;
  27. ifaddrmsg msg;
  28. };
  29. const int kMaxReadSize = 4096;
  30. } // namespace
  31. namespace rtc {
  32. int set_ifname(struct ifaddrs* ifaddr, int interface) {
  33. char buf[IFNAMSIZ] = {0};
  34. char* name = if_indextoname(interface, buf);
  35. if (name == nullptr) {
  36. return -1;
  37. }
  38. ifaddr->ifa_name = new char[strlen(name) + 1];
  39. strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
  40. return 0;
  41. }
  42. int set_flags(struct ifaddrs* ifaddr) {
  43. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  44. if (fd == -1) {
  45. return -1;
  46. }
  47. ifreq ifr;
  48. memset(&ifr, 0, sizeof(ifr));
  49. strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
  50. int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
  51. close(fd);
  52. if (rc == -1) {
  53. return -1;
  54. }
  55. ifaddr->ifa_flags = ifr.ifr_flags;
  56. return 0;
  57. }
  58. int set_addresses(struct ifaddrs* ifaddr,
  59. ifaddrmsg* msg,
  60. void* data,
  61. size_t len) {
  62. if (msg->ifa_family == AF_INET) {
  63. sockaddr_in* sa = new sockaddr_in;
  64. sa->sin_family = AF_INET;
  65. memcpy(&sa->sin_addr, data, len);
  66. ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
  67. } else if (msg->ifa_family == AF_INET6) {
  68. sockaddr_in6* sa = new sockaddr_in6;
  69. sa->sin6_family = AF_INET6;
  70. sa->sin6_scope_id = msg->ifa_index;
  71. memcpy(&sa->sin6_addr, data, len);
  72. ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
  73. } else {
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
  79. char* prefix = nullptr;
  80. if (family == AF_INET) {
  81. sockaddr_in* mask = new sockaddr_in;
  82. mask->sin_family = AF_INET;
  83. memset(&mask->sin_addr, 0, sizeof(in_addr));
  84. ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
  85. if (prefixlen > 32) {
  86. prefixlen = 32;
  87. }
  88. prefix = reinterpret_cast<char*>(&mask->sin_addr);
  89. } else if (family == AF_INET6) {
  90. sockaddr_in6* mask = new sockaddr_in6;
  91. mask->sin6_family = AF_INET6;
  92. memset(&mask->sin6_addr, 0, sizeof(in6_addr));
  93. ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask);
  94. if (prefixlen > 128) {
  95. prefixlen = 128;
  96. }
  97. prefix = reinterpret_cast<char*>(&mask->sin6_addr);
  98. } else {
  99. return -1;
  100. }
  101. for (int i = 0; i < (prefixlen / 8); i++) {
  102. *prefix++ = 0xFF;
  103. }
  104. char remainder = 0xff;
  105. remainder <<= (8 - prefixlen % 8);
  106. *prefix = remainder;
  107. return 0;
  108. }
  109. int populate_ifaddrs(struct ifaddrs* ifaddr,
  110. ifaddrmsg* msg,
  111. void* bytes,
  112. size_t len) {
  113. if (set_ifname(ifaddr, msg->ifa_index) != 0) {
  114. return -1;
  115. }
  116. if (set_flags(ifaddr) != 0) {
  117. return -1;
  118. }
  119. if (set_addresses(ifaddr, msg, bytes, len) != 0) {
  120. return -1;
  121. }
  122. if (make_prefixes(ifaddr, msg->ifa_family, msg->ifa_prefixlen) != 0) {
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. int getifaddrs(struct ifaddrs** result) {
  128. int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  129. if (fd < 0) {
  130. return -1;
  131. }
  132. netlinkrequest ifaddr_request;
  133. memset(&ifaddr_request, 0, sizeof(ifaddr_request));
  134. ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
  135. ifaddr_request.header.nlmsg_type = RTM_GETADDR;
  136. ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg));
  137. ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
  138. if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) {
  139. close(fd);
  140. return -1;
  141. }
  142. struct ifaddrs* start = nullptr;
  143. struct ifaddrs* current = nullptr;
  144. char buf[kMaxReadSize];
  145. ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
  146. while (amount_read > 0) {
  147. nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]);
  148. size_t header_size = static_cast<size_t>(amount_read);
  149. for (; NLMSG_OK(header, header_size);
  150. header = NLMSG_NEXT(header, header_size)) {
  151. switch (header->nlmsg_type) {
  152. case NLMSG_DONE:
  153. // Success. Return.
  154. *result = start;
  155. close(fd);
  156. return 0;
  157. case NLMSG_ERROR:
  158. close(fd);
  159. freeifaddrs(start);
  160. return -1;
  161. case RTM_NEWADDR: {
  162. ifaddrmsg* address_msg =
  163. reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(header));
  164. rtattr* rta = IFA_RTA(address_msg);
  165. ssize_t payload_len = IFA_PAYLOAD(header);
  166. while (RTA_OK(rta, payload_len)) {
  167. if ((address_msg->ifa_family == AF_INET &&
  168. rta->rta_type == IFA_LOCAL) ||
  169. (address_msg->ifa_family == AF_INET6 &&
  170. rta->rta_type == IFA_ADDRESS)) {
  171. ifaddrs* newest = new ifaddrs;
  172. memset(newest, 0, sizeof(ifaddrs));
  173. if (current) {
  174. current->ifa_next = newest;
  175. } else {
  176. start = newest;
  177. }
  178. if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
  179. RTA_PAYLOAD(rta)) != 0) {
  180. freeifaddrs(start);
  181. *result = nullptr;
  182. return -1;
  183. }
  184. current = newest;
  185. }
  186. rta = RTA_NEXT(rta, payload_len);
  187. }
  188. break;
  189. }
  190. }
  191. }
  192. amount_read = recv(fd, &buf, kMaxReadSize, 0);
  193. }
  194. close(fd);
  195. freeifaddrs(start);
  196. return -1;
  197. }
  198. void freeifaddrs(struct ifaddrs* addrs) {
  199. struct ifaddrs* last = nullptr;
  200. struct ifaddrs* cursor = addrs;
  201. while (cursor) {
  202. delete[] cursor->ifa_name;
  203. delete cursor->ifa_addr;
  204. delete cursor->ifa_netmask;
  205. last = cursor;
  206. cursor = cursor->ifa_next;
  207. delete last;
  208. }
  209. }
  210. } // namespace rtc
  211. #endif // defined(WEBRTC_ANDROID)