Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: net/base/net_util_posix.cc

Issue 23726043: Added NetworkInterface::network_prefix (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/net_util.cc ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 } while (new_path != old_path); 60 } while (new_path != old_path);
60 61
61 file_path_str.assign(old_path); 62 file_path_str.assign(old_path);
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, "\n");
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, "\t");
73 std::string name; 74 CHECK(network_tokenizer.GetNext());
74 if (!network_tokenizer.GetNext()) 75 std::string name = network_tokenizer.token();
75 continue;
76 name = network_tokenizer.token();
77 76
78 std::string literal_address; 77 CHECK(network_tokenizer.GetNext());
79 if (!network_tokenizer.GetNext()) 78 std::string interface_address = network_tokenizer.token();
80 continue; 79
81 literal_address = network_tokenizer.token(); 80 base::StringTokenizer address_tokenizer(interface_address, "/");
81
82 CHECK(address_tokenizer.GetNext());
83 std::string literal_address = address_tokenizer.token();
84
85 CHECK(address_tokenizer.GetNext());
86 std::string network_prefix = address_tokenizer.token();
82 87
83 IPAddressNumber address; 88 IPAddressNumber address;
84 if (!ParseIPLiteralToNumber(literal_address, &address)) 89 CHECK(ParseIPLiteralToNumber(literal_address, &address));
85 continue; 90
86 networks->push_back(NetworkInterface(name, address)); 91 unsigned net_mask = 0;
92 CHECK(base::StringToUint(network_prefix, &net_mask));
93 CHECK_LE(net_mask, address.size() * 8);
94
95 networks->push_back(
96 NetworkInterface(name, address, static_cast<uint8>(net_mask)));
87 } 97 }
88 return true; 98 return true;
89 #else 99 #else
90 // getifaddrs() may require IO operations. 100 // getifaddrs() may require IO operations.
91 base::ThreadRestrictions::AssertIOAllowed(); 101 base::ThreadRestrictions::AssertIOAllowed();
92 102
93 ifaddrs *interfaces; 103 ifaddrs *interfaces;
94 if (getifaddrs(&interfaces) < 0) { 104 if (getifaddrs(&interfaces) < 0) {
95 PLOG(ERROR) << "getifaddrs"; 105 PLOG(ERROR) << "getifaddrs";
96 return false; 106 return false;
(...skipping 29 matching lines...) Expand all
126 reinterpret_cast<struct sockaddr_in*>(addr); 136 reinterpret_cast<struct sockaddr_in*>(addr);
127 addr_size = sizeof(*addr_in); 137 addr_size = sizeof(*addr_in);
128 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK || 138 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
129 addr_in->sin_addr.s_addr == 0) { 139 addr_in->sin_addr.s_addr == 0) {
130 continue; 140 continue;
131 } 141 }
132 } else { 142 } else {
133 // Skip non-IP addresses. 143 // Skip non-IP addresses.
134 continue; 144 continue;
135 } 145 }
146
136 IPEndPoint address; 147 IPEndPoint address;
137 std::string name = interface->ifa_name; 148 std::string name = interface->ifa_name;
138 if (address.FromSockAddr(addr, addr_size)) { 149 if (address.FromSockAddr(addr, addr_size)) {
139 networks->push_back(NetworkInterface(name, address.address())); 150 uint8 net_mask = 0;
151 if (interface->ifa_netmask) {
152 IPEndPoint netmask;
153 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
154 net_mask = MaskPrefixLength(netmask.address());
155 }
156 }
157
158 networks->push_back(NetworkInterface(name, address.address(), net_mask));
140 } 159 }
141 } 160 }
142 161
143 freeifaddrs(interfaces); 162 freeifaddrs(interfaces);
144 163
145 return true; 164 return true;
146 #endif 165 #endif
147 } 166 }
148 167
149 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { 168 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
150 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; 169 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
151 } 170 }
152 171
153 } // namespace net 172 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.cc ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698