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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 if (!network_tokenizer.GetNext()) |
75 continue; | 76 continue; |
76 name = network_tokenizer.token(); | 77 name = network_tokenizer.token(); |
77 | 78 |
78 std::string literal_address; | 79 std::string literal_address; |
79 if (!network_tokenizer.GetNext()) | 80 if (!network_tokenizer.GetNext()) |
80 continue; | 81 continue; |
szym
2013/09/23 17:26:55
I don't quite understand why the code here silentl
| |
81 literal_address = network_tokenizer.token(); | 82 literal_address = network_tokenizer.token(); |
82 | 83 |
84 uint8 net_mask = 0; | |
85 if (network_tokenizer.GetNext()) { | |
86 unsigned int_token = 0; | |
87 if (base::StringToUint(network_tokenizer.token(), &int_token) && | |
88 int_token <= 255) { | |
89 net_mask = int_token; | |
90 } else { | |
91 NOTREACHED(); | |
szym
2013/09/23 17:26:55
This should be consistent with the rest of the cod
| |
92 } | |
93 } | |
94 | |
83 IPAddressNumber address; | 95 IPAddressNumber address; |
84 if (!ParseIPLiteralToNumber(literal_address, &address)) | 96 if (!ParseIPLiteralToNumber(literal_address, &address)) |
85 continue; | 97 continue; |
86 networks->push_back(NetworkInterface(name, address)); | 98 networks->push_back(NetworkInterface(name, address, net_mask)); |
87 } | 99 } |
88 return true; | 100 return true; |
89 #else | 101 #else |
90 // getifaddrs() may require IO operations. | 102 // getifaddrs() may require IO operations. |
91 base::ThreadRestrictions::AssertIOAllowed(); | 103 base::ThreadRestrictions::AssertIOAllowed(); |
92 | 104 |
93 ifaddrs *interfaces; | 105 ifaddrs *interfaces; |
94 if (getifaddrs(&interfaces) < 0) { | 106 if (getifaddrs(&interfaces) < 0) { |
95 PLOG(ERROR) << "getifaddrs"; | 107 PLOG(ERROR) << "getifaddrs"; |
96 return false; | 108 return false; |
(...skipping 29 matching lines...) Expand all Loading... | |
126 reinterpret_cast<struct sockaddr_in*>(addr); | 138 reinterpret_cast<struct sockaddr_in*>(addr); |
127 addr_size = sizeof(*addr_in); | 139 addr_size = sizeof(*addr_in); |
128 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK || | 140 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK || |
129 addr_in->sin_addr.s_addr == 0) { | 141 addr_in->sin_addr.s_addr == 0) { |
130 continue; | 142 continue; |
131 } | 143 } |
132 } else { | 144 } else { |
133 // Skip non-IP addresses. | 145 // Skip non-IP addresses. |
134 continue; | 146 continue; |
135 } | 147 } |
148 | |
136 IPEndPoint address; | 149 IPEndPoint address; |
137 std::string name = interface->ifa_name; | 150 std::string name = interface->ifa_name; |
138 if (address.FromSockAddr(addr, addr_size)) { | 151 if (address.FromSockAddr(addr, addr_size)) { |
139 networks->push_back(NetworkInterface(name, address.address())); | 152 uint8 net_mask = 0; |
153 if (interface->ifa_netmask) { | |
154 IPEndPoint netmask; | |
155 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) { | |
156 net_mask = MaskPrefixLength(netmask.address()); | |
157 } | |
158 } | |
159 | |
160 networks->push_back(NetworkInterface(name, address.address(), net_mask)); | |
140 } | 161 } |
141 } | 162 } |
142 | 163 |
143 freeifaddrs(interfaces); | 164 freeifaddrs(interfaces); |
144 | 165 |
145 return true; | 166 return true; |
146 #endif | 167 #endif |
147 } | 168 } |
148 | 169 |
149 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 170 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
150 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 171 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
151 } | 172 } |
152 | 173 |
153 } // namespace net | 174 } // namespace net |
OLD | NEW |