| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 const int kMaxReadSize = 4096; | 33 const int kMaxReadSize = 4096; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 namespace rtc { | 37 namespace rtc { |
| 38 | 38 |
| 39 int set_ifname(struct ifaddrs* ifaddr, int interface) { | 39 int set_ifname(struct ifaddrs* ifaddr, int interface) { |
| 40 char buf[IFNAMSIZ] = {0}; | 40 char buf[IFNAMSIZ] = {0}; |
| 41 char* name = if_indextoname(interface, buf); | 41 char* name = if_indextoname(interface, buf); |
| 42 if (name == NULL) { | 42 if (name == nullptr) { |
| 43 return -1; | 43 return -1; |
| 44 } | 44 } |
| 45 ifaddr->ifa_name = new char[strlen(name) + 1]; | 45 ifaddr->ifa_name = new char[strlen(name) + 1]; |
| 46 strncpy(ifaddr->ifa_name, name, strlen(name) + 1); | 46 strncpy(ifaddr->ifa_name, name, strlen(name) + 1); |
| 47 return 0; | 47 return 0; |
| 48 } | 48 } |
| 49 | 49 |
| 50 int set_flags(struct ifaddrs* ifaddr) { | 50 int set_flags(struct ifaddrs* ifaddr) { |
| 51 int fd = socket(AF_INET, SOCK_DGRAM, 0); | 51 int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 52 if (fd == -1) { | 52 if (fd == -1) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 77 sa->sin6_scope_id = msg->ifa_index; | 77 sa->sin6_scope_id = msg->ifa_index; |
| 78 memcpy(&sa->sin6_addr, data, len); | 78 memcpy(&sa->sin6_addr, data, len); |
| 79 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa); | 79 ifaddr->ifa_addr = reinterpret_cast<sockaddr*>(sa); |
| 80 } else { | 80 } else { |
| 81 return -1; | 81 return -1; |
| 82 } | 82 } |
| 83 return 0; | 83 return 0; |
| 84 } | 84 } |
| 85 | 85 |
| 86 int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) { | 86 int make_prefixes(struct ifaddrs* ifaddr, int family, int prefixlen) { |
| 87 char* prefix = NULL; | 87 char* prefix = nullptr; |
| 88 if (family == AF_INET) { | 88 if (family == AF_INET) { |
| 89 sockaddr_in* mask = new sockaddr_in; | 89 sockaddr_in* mask = new sockaddr_in; |
| 90 mask->sin_family = AF_INET; | 90 mask->sin_family = AF_INET; |
| 91 memset(&mask->sin_addr, 0, sizeof(in_addr)); | 91 memset(&mask->sin_addr, 0, sizeof(in_addr)); |
| 92 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask); | 92 ifaddr->ifa_netmask = reinterpret_cast<sockaddr*>(mask); |
| 93 if (prefixlen > 32) { | 93 if (prefixlen > 32) { |
| 94 prefixlen = 32; | 94 prefixlen = 32; |
| 95 } | 95 } |
| 96 prefix = reinterpret_cast<char*>(&mask->sin_addr); | 96 prefix = reinterpret_cast<char*>(&mask->sin_addr); |
| 97 } else if (family == AF_INET6) { | 97 } else if (family == AF_INET6) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 memset(&ifaddr_request, 0, sizeof(ifaddr_request)); | 142 memset(&ifaddr_request, 0, sizeof(ifaddr_request)); |
| 143 ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST; | 143 ifaddr_request.header.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST; |
| 144 ifaddr_request.header.nlmsg_type = RTM_GETADDR; | 144 ifaddr_request.header.nlmsg_type = RTM_GETADDR; |
| 145 ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg)); | 145 ifaddr_request.header.nlmsg_len = NLMSG_LENGTH(sizeof(ifaddrmsg)); |
| 146 | 146 |
| 147 ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0); | 147 ssize_t count = send(fd, &ifaddr_request, ifaddr_request.header.nlmsg_len, 0); |
| 148 if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) { | 148 if (static_cast<size_t>(count) != ifaddr_request.header.nlmsg_len) { |
| 149 close(fd); | 149 close(fd); |
| 150 return -1; | 150 return -1; |
| 151 } | 151 } |
| 152 struct ifaddrs* start = NULL; | 152 struct ifaddrs* start = nullptr; |
| 153 struct ifaddrs* current = NULL; | 153 struct ifaddrs* current = nullptr; |
| 154 char buf[kMaxReadSize]; | 154 char buf[kMaxReadSize]; |
| 155 ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0); | 155 ssize_t amount_read = recv(fd, &buf, kMaxReadSize, 0); |
| 156 while (amount_read > 0) { | 156 while (amount_read > 0) { |
| 157 nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]); | 157 nlmsghdr* header = reinterpret_cast<nlmsghdr*>(&buf[0]); |
| 158 size_t header_size = static_cast<size_t>(amount_read); | 158 size_t header_size = static_cast<size_t>(amount_read); |
| 159 for ( ; NLMSG_OK(header, header_size); | 159 for ( ; NLMSG_OK(header, header_size); |
| 160 header = NLMSG_NEXT(header, header_size)) { | 160 header = NLMSG_NEXT(header, header_size)) { |
| 161 switch (header->nlmsg_type) { | 161 switch (header->nlmsg_type) { |
| 162 case NLMSG_DONE: | 162 case NLMSG_DONE: |
| 163 // Success. Return. | 163 // Success. Return. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 180 ifaddrs* newest = new ifaddrs; | 180 ifaddrs* newest = new ifaddrs; |
| 181 memset(newest, 0, sizeof(ifaddrs)); | 181 memset(newest, 0, sizeof(ifaddrs)); |
| 182 if (current) { | 182 if (current) { |
| 183 current->ifa_next = newest; | 183 current->ifa_next = newest; |
| 184 } else { | 184 } else { |
| 185 start = newest; | 185 start = newest; |
| 186 } | 186 } |
| 187 if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta), | 187 if (populate_ifaddrs(newest, address_msg, RTA_DATA(rta), |
| 188 RTA_PAYLOAD(rta)) != 0) { | 188 RTA_PAYLOAD(rta)) != 0) { |
| 189 freeifaddrs(start); | 189 freeifaddrs(start); |
| 190 *result = NULL; | 190 *result = nullptr; |
| 191 return -1; | 191 return -1; |
| 192 } | 192 } |
| 193 current = newest; | 193 current = newest; |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 rta = RTA_NEXT(rta, payload_len); | 196 rta = RTA_NEXT(rta, payload_len); |
| 197 } | 197 } |
| 198 break; | 198 break; |
| 199 } | 199 } |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 amount_read = recv(fd, &buf, kMaxReadSize, 0); | 202 amount_read = recv(fd, &buf, kMaxReadSize, 0); |
| 203 } | 203 } |
| 204 close(fd); | 204 close(fd); |
| 205 freeifaddrs(start); | 205 freeifaddrs(start); |
| 206 return -1; | 206 return -1; |
| 207 } | 207 } |
| 208 | 208 |
| 209 void freeifaddrs(struct ifaddrs* addrs) { | 209 void freeifaddrs(struct ifaddrs* addrs) { |
| 210 struct ifaddrs* last = NULL; | 210 struct ifaddrs* last = nullptr; |
| 211 struct ifaddrs* cursor = addrs; | 211 struct ifaddrs* cursor = addrs; |
| 212 while (cursor) { | 212 while (cursor) { |
| 213 delete[] cursor->ifa_name; | 213 delete[] cursor->ifa_name; |
| 214 delete cursor->ifa_addr; | 214 delete cursor->ifa_addr; |
| 215 delete cursor->ifa_netmask; | 215 delete cursor->ifa_netmask; |
| 216 last = cursor; | 216 last = cursor; |
| 217 cursor = cursor->ifa_next; | 217 cursor = cursor->ifa_next; |
| 218 delete last; | 218 delete last; |
| 219 } | 219 } |
| 220 } | 220 } |
| 221 | 221 |
| 222 } // namespace rtc | 222 } // namespace rtc |
| 223 #endif // defined(WEBRTC_ANDROID) | 223 #endif // defined(WEBRTC_ANDROID) |
| OLD | NEW |