OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <iterator> | 10 #include <iterator> |
11 #include <map> | 11 #include <map> |
12 #include <set> | 12 #include <set> |
13 | 13 |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include <windows.h> | 17 #include <windows.h> |
18 #include <iphlpapi.h> | 18 #include <iphlpapi.h> |
19 #include <winsock2.h> | 19 #include <winsock2.h> |
20 #include <ws2bth.h> | |
20 #pragma comment(lib, "iphlpapi.lib") | 21 #pragma comment(lib, "iphlpapi.lib") |
21 #elif defined(OS_POSIX) | 22 #elif defined(OS_POSIX) |
22 #include <fcntl.h> | 23 #include <fcntl.h> |
23 #include <netdb.h> | 24 #include <netdb.h> |
24 #include <netinet/in.h> | 25 #include <netinet/in.h> |
25 #if !defined(OS_NACL) | 26 #if !defined(OS_NACL) |
26 #include <net/if.h> | 27 #include <net/if.h> |
27 #if !defined(OS_ANDROID) | 28 #if !defined(OS_ANDROID) |
28 #include <ifaddrs.h> | 29 #include <ifaddrs.h> |
29 #endif // !defined(OS_NACL) | 30 #endif // !defined(OS_NACL) |
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1159 return false; | 1160 return false; |
1160 size_t width = host_addr.size() + 1; | 1161 size_t width = host_addr.size() + 1; |
1161 for (size_t i = 0; i < array_size; ++i, array += width) { | 1162 for (size_t i = 0; i < array_size; ++i, array += width) { |
1162 if (IPNumberPrefixCheck(host_addr, array, array[width-1])) | 1163 if (IPNumberPrefixCheck(host_addr, array, array[width-1])) |
1163 return true; | 1164 return true; |
1164 } | 1165 } |
1165 return false; | 1166 return false; |
1166 } | 1167 } |
1167 | 1168 |
1168 // Extracts the address and port portions of a sockaddr. | 1169 // Extracts the address and port portions of a sockaddr. |
1169 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, | 1170 bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, |
wtc
2014/04/24 23:09:31
I don't see any call to GetIPAddressFromSockAddr i
xiyuan
2014/04/25 20:29:29
It is indirectly called in ip_end_point.FromSockAd
| |
1170 socklen_t sock_addr_len, | 1171 socklen_t sock_addr_len, |
1171 const uint8** address, | 1172 const uint8** address, |
1172 size_t* address_len, | 1173 size_t* address_len, |
1173 uint16* port) { | 1174 uint16* port) { |
1174 if (sock_addr->sa_family == AF_INET) { | 1175 if (sock_addr->sa_family == AF_INET) { |
1175 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) | 1176 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) |
1176 return false; | 1177 return false; |
1177 const struct sockaddr_in* addr = | 1178 const struct sockaddr_in* addr = |
1178 reinterpret_cast<const struct sockaddr_in*>(sock_addr); | 1179 reinterpret_cast<const struct sockaddr_in*>(sock_addr); |
1179 *address = reinterpret_cast<const uint8*>(&addr->sin_addr); | 1180 *address = reinterpret_cast<const uint8*>(&addr->sin_addr); |
1180 *address_len = kIPv4AddressSize; | 1181 *address_len = kIPv4AddressSize; |
1181 if (port) | 1182 if (port) |
1182 *port = base::NetToHost16(addr->sin_port); | 1183 *port = base::NetToHost16(addr->sin_port); |
1183 return true; | 1184 return true; |
1184 } | 1185 } |
1185 | 1186 |
1186 if (sock_addr->sa_family == AF_INET6) { | 1187 if (sock_addr->sa_family == AF_INET6) { |
1187 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) | 1188 if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) |
1188 return false; | 1189 return false; |
1189 const struct sockaddr_in6* addr = | 1190 const struct sockaddr_in6* addr = |
1190 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); | 1191 reinterpret_cast<const struct sockaddr_in6*>(sock_addr); |
1191 *address = reinterpret_cast<const unsigned char*>(&addr->sin6_addr); | 1192 *address = reinterpret_cast<const unsigned char*>(&addr->sin6_addr); |
wtc
2014/04/24 23:09:31
Nit: could you change "unsigned char" to "uint8" h
xiyuan
2014/04/25 20:29:29
Done.
| |
1192 *address_len = kIPv6AddressSize; | 1193 *address_len = kIPv6AddressSize; |
1193 if (port) | 1194 if (port) |
1194 *port = base::NetToHost16(addr->sin6_port); | 1195 *port = base::NetToHost16(addr->sin6_port); |
1195 return true; | 1196 return true; |
1196 } | 1197 } |
1197 | 1198 |
1199 #if defined(OS_WIN) | |
1200 // Bluetooth address size. Windows bluetooth is supported via winsock. | |
wtc
2014/04/24 23:09:31
Nit: capitalize "bluetooth".
xiyuan
2014/04/25 20:29:29
Done.
| |
1201 const size_t kBtAddressSize = 6; | |
wtc
2014/04/24 23:09:31
1. Nit: it seems better to use "Bth" instead of "B
xiyuan
2014/04/25 20:29:29
Done.
| |
1202 if (sock_addr->sa_family == AF_BTH) { | |
1203 if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) | |
1204 return false; | |
1205 const SOCKADDR_BTH* addr = | |
1206 reinterpret_cast<const SOCKADDR_BTH*>(sock_addr); | |
1207 *address = reinterpret_cast<const uint8*>(&addr->btAddr); | |
1208 *address_len = kBtAddressSize; | |
1209 if (port) | |
1210 *port = addr->port; | |
wtc
2014/04/24 23:09:31
This doesn't extract the serviceClassId member of
xiyuan
2014/04/25 20:29:29
serviceClassId is a way to map to a port(aka Bt ch
| |
1211 return true; | |
1212 } | |
1213 #endif | |
1214 | |
1198 return false; // Unrecognized |sa_family|. | 1215 return false; // Unrecognized |sa_family|. |
1199 } | 1216 } |
1200 | 1217 |
1201 std::string IPAddressToString(const uint8* address, | 1218 std::string IPAddressToString(const uint8* address, |
1202 size_t address_len) { | 1219 size_t address_len) { |
1203 std::string str; | 1220 std::string str; |
1204 url_canon::StdStringCanonOutput output(&str); | 1221 url_canon::StdStringCanonOutput output(&str); |
1205 | 1222 |
1206 if (address_len == kIPv4AddressSize) { | 1223 if (address_len == kIPv4AddressSize) { |
1207 url_canon::AppendIPv4Address(address, &output); | 1224 url_canon::AppendIPv4Address(address, &output); |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1833 } | 1850 } |
1834 return a1.size() * CHAR_BIT; | 1851 return a1.size() * CHAR_BIT; |
1835 } | 1852 } |
1836 | 1853 |
1837 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 1854 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
1838 IPAddressNumber all_ones(mask.size(), 0xFF); | 1855 IPAddressNumber all_ones(mask.size(), 0xFF); |
1839 return CommonPrefixLength(mask, all_ones); | 1856 return CommonPrefixLength(mask, all_ones); |
1840 } | 1857 } |
1841 | 1858 |
1842 } // namespace net | 1859 } // namespace net |
OLD | NEW |