OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <sys/types.h> | 7 #include <sys/types.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
12 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_tokenizer.h" | 13 #include "base/strings/string_tokenizer.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
15 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
16 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 #if !defined(OS_ANDROID) | 21 #if !defined(OS_ANDROID) |
21 #include <ifaddrs.h> | 22 #include <ifaddrs.h> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 63 |
63 return !file_path_str.empty(); | 64 return !file_path_str.empty(); |
64 } | 65 } |
65 | 66 |
66 bool GetNetworkList(NetworkInterfaceList* networks) { | 67 bool GetNetworkList(NetworkInterfaceList* networks) { |
67 #if defined(OS_ANDROID) | 68 #if defined(OS_ANDROID) |
68 std::string network_list = android::GetNetworkList(); | 69 std::string network_list = android::GetNetworkList(); |
69 base::StringTokenizer network_interfaces(network_list, ";"); | 70 base::StringTokenizer network_interfaces(network_list, ";"); |
70 while (network_interfaces.GetNext()) { | 71 while (network_interfaces.GetNext()) { |
71 std::string network_item = network_interfaces.token(); | 72 std::string network_item = network_interfaces.token(); |
72 base::StringTokenizer network_tokenizer(network_item, ","); | 73 base::StringTokenizer network_tokenizer(network_item, "/"); |
73 std::string name; | 74 std::string name; |
74 if (!network_tokenizer.GetNext()) | 75 CHECK(network_tokenizer.GetNext()); |
75 continue; | |
76 name = network_tokenizer.token(); | 76 name = network_tokenizer.token(); |
77 | 77 |
78 std::string literal_address; | 78 std::string literal_address; |
79 if (!network_tokenizer.GetNext()) | 79 CHECK(network_tokenizer.GetNext()); |
80 continue; | |
81 literal_address = network_tokenizer.token(); | 80 literal_address = network_tokenizer.token(); |
82 | 81 |
82 std::string network_prefix; | |
83 CHECK(network_tokenizer.GetNext()); | |
84 network_prefix = network_tokenizer.token(); | |
85 | |
86 unsigned net_mask = 0; | |
87 CHECK(base::StringToUint(network_tokenizer.token(), &net_mask)); | |
88 CHECK(net_mask <= 255); | |
szym
2013/09/23 19:58:42
CHECK_LE
Vitaly Buka (NO REVIEWS)
2013/09/23 20:06:11
Done.
| |
89 | |
83 IPAddressNumber address; | 90 IPAddressNumber address; |
84 if (!ParseIPLiteralToNumber(literal_address, &address)) | 91 CHECK(ParseIPLiteralToNumber(literal_address, &address)); |
85 continue; | 92 networks->push_back( |
86 networks->push_back(NetworkInterface(name, address)); | 93 NetworkInterface(name, address, static_cast<uint8>(net_mask))); |
87 } | 94 } |
88 return true; | 95 return true; |
89 #else | 96 #else |
90 // getifaddrs() may require IO operations. | 97 // getifaddrs() may require IO operations. |
91 base::ThreadRestrictions::AssertIOAllowed(); | 98 base::ThreadRestrictions::AssertIOAllowed(); |
92 | 99 |
93 ifaddrs *interfaces; | 100 ifaddrs *interfaces; |
94 if (getifaddrs(&interfaces) < 0) { | 101 if (getifaddrs(&interfaces) < 0) { |
95 PLOG(ERROR) << "getifaddrs"; | 102 PLOG(ERROR) << "getifaddrs"; |
96 return false; | 103 return false; |
(...skipping 29 matching lines...) Expand all Loading... | |
126 reinterpret_cast<struct sockaddr_in*>(addr); | 133 reinterpret_cast<struct sockaddr_in*>(addr); |
127 addr_size = sizeof(*addr_in); | 134 addr_size = sizeof(*addr_in); |
128 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK || | 135 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK || |
129 addr_in->sin_addr.s_addr == 0) { | 136 addr_in->sin_addr.s_addr == 0) { |
130 continue; | 137 continue; |
131 } | 138 } |
132 } else { | 139 } else { |
133 // Skip non-IP addresses. | 140 // Skip non-IP addresses. |
134 continue; | 141 continue; |
135 } | 142 } |
143 | |
136 IPEndPoint address; | 144 IPEndPoint address; |
137 std::string name = interface->ifa_name; | 145 std::string name = interface->ifa_name; |
138 if (address.FromSockAddr(addr, addr_size)) { | 146 if (address.FromSockAddr(addr, addr_size)) { |
139 networks->push_back(NetworkInterface(name, address.address())); | 147 uint8 net_mask = 0; |
148 if (interface->ifa_netmask) { | |
149 IPEndPoint netmask; | |
150 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { | |
151 net_mask = MaskPrefixLength(netmask.address()); | |
152 } | |
153 } | |
154 | |
155 networks->push_back(NetworkInterface(name, address.address(), net_mask)); | |
140 } | 156 } |
141 } | 157 } |
142 | 158 |
143 freeifaddrs(interfaces); | 159 freeifaddrs(interfaces); |
144 | 160 |
145 return true; | 161 return true; |
146 #endif | 162 #endif |
147 } | 163 } |
148 | 164 |
149 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 165 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
150 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 166 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
151 } | 167 } |
152 | 168 |
153 } // namespace net | 169 } // namespace net |
OLD | NEW |