ifaddrs-android.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "ifaddrs-android.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <sys/utsname.h>
  33. #include <sys/ioctl.h>
  34. #include <netinet/in.h>
  35. #include <net/if.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <linux/netlink.h>
  39. #include <linux/rtnetlink.h>
  40. struct netlinkrequest {
  41. nlmsghdr header;
  42. ifaddrmsg msg;
  43. };
  44. namespace {
  45. const int kMaxReadSize = 4096;
  46. };
  47. int set_ifname(struct ifaddrs* ifaddr, int interface) {
  48. char buf[IFNAMSIZ] = {0};
  49. char* name = if_indextoname(interface, buf);
  50. if (name == NULL) {
  51. return -1;
  52. }
  53. ifaddr->ifa_name = new char[strlen(name) + 1];
  54. strncpy(ifaddr->ifa_name, name, strlen(name) + 1);
  55. return 0;
  56. }
  57. int set_flags(struct ifaddrs* ifaddr) {
  58. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  59. if (fd == -1) {
  60. return -1;
  61. }
  62. ifreq ifr;
  63. memset(&ifr, 0, sizeof(ifr));
  64. strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
  65. int rc = ioctl(fd, SIOCGIFFLAGS, &ifr);
  66. close(fd);
  67. if (rc == -1) {
  68. return -1;
  69. }
  70. ifaddr->ifa_flags = ifr.ifr_flags;
  71. return 0;
  72. }
  73. int set_addresses(struct ifaddrs* ifaddr, ifaddrmsg* msg, void* data,
  74. size_t len) {
  75. if (msg->ifa_family == AF_INET) {
  76. sockaddr_in* sa = new sockaddr_in;
  77. sa->sin_family = AF_INET;
  78. memcpy(&sa->sin_addr, data, len);
  79. ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
  80. } else if (msg->ifa_family == AF_INET6) {
  81. sockaddr_in6* sa = new sockaddr_in6;
  82. sa->sin6_family = AF_INET6;
  83. sa->sin6_scope_id = msg->ifa_index;
  84. memcpy(&sa->sin6_addr, data, len);
  85. ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa);
  86. } else {
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) {
  92. char* prefix = NULL;
  93. 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. int populate_ifaddrs(struct ifaddrs* ifaddr, ifaddrmsg* msg, void* bytes,
  123. 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. int getifaddrs(struct ifaddrs** result) {
  139. int fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  140. if (fd < 0) {
  141. return -1;
  142. }
  143. netlinkrequest ifaddr_request;
  144. memset(&ifaddr_request, 0, sizeof(ifaddr_request));
  145. ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
  146. ifaddr_request.header.nlmsg_type = RTM_GETADDR;
  147. ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg));
  148. ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0);
  149. if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) {
  150. close(fd);
  151. return -1;
  152. }
  153. struct ifaddrs* start = NULL;
  154. struct ifaddrs* current = NULL;
  155. char buf[kMaxReadSize];
  156. ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0);
  157. while (amount_read > 0) {
  158. nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]);
  159. size_t header_size = static_cast<size_t>(amount_read);
  160. for ( ; NLMSG_OK(header, header_size);
  161. header = NLMSG_NEXT(header, header_size)) {
  162. switch (header->nlmsg_type) {
  163. case NLMSG_DONE:
  164. // Success. Return.
  165. *result = start;
  166. close(fd);
  167. return 0;
  168. case NLMSG_ERROR:
  169. close(fd);
  170. freeifaddrs(start);
  171. return -1;
  172. case RTM_NEWADDR: {
  173. ifaddrmsg* address_msg =
  174. reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(header));
  175. rtattr* rta = IFA_RTA(address_msg);
  176. ssize_t payload_len = IFA_PAYLOAD(header);
  177. while (RTA_OK(rta, payload_len)) {
  178. if (rta->rta_type == IFA_ADDRESS) {
  179. int family = address_msg->ifa_family;
  180. if (family == AF_INET || family == AF_INET6) {
  181. ifaddrs* newest = new ifaddrs;
  182. memset(newest, 0, sizeof(ifaddrs));
  183. if (current) {
  184. current->ifa_next = newest;
  185. } else {
  186. start = newest;
  187. }
  188. if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta),
  189. RTA_PAYLOAD(rta)) != 0) {
  190. freeifaddrs(start);
  191. *result = NULL;
  192. return -1;
  193. }
  194. current = newest;
  195. }
  196. }
  197. rta = RTA_NEXT(rta, payload_len);
  198. }
  199. break;
  200. }
  201. }
  202. }
  203. amount_read = recv(fd, &buf, kMaxReadSize, 0);
  204. }
  205. close(fd);
  206. freeifaddrs(start);
  207. return -1;
  208. }
  209. void freeifaddrs(struct ifaddrs* addrs) {
  210. struct ifaddrs* last = NULL;
  211. struct ifaddrs* cursor = addrs;
  212. while (cursor) {
  213. delete[] cursor->ifa_name;
  214. delete cursor->ifa_addr;
  215. delete cursor->ifa_netmask;
  216. last = cursor;
  217. cursor = cursor->ifa_next;
  218. delete last;
  219. }
  220. }