Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/dns/address_sorter.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <netinet/in.h> | |
|
mmenke
2012/07/30 15:37:00
nit: Not in alphabetical order.
szym
2012/07/30 16:28:45
"stdint.h" is needed for "netinet/in.h", but it se
| |
| 9 | |
| 10 #if defined(OS_MACOSX) || defined(OS_BSD) | |
| 11 #include <sys/socket.h> // Must be included before ifaddrs.h. | |
| 12 #include <ifaddrs.h> | |
| 13 #include <net/if.h> | |
| 14 #include <netinet/in_var.h> | |
| 15 #include <string.h> | |
|
mmenke
2012/07/30 15:37:00
Why is this needed?
szym
2012/07/30 16:28:45
memcpy in line 453. But now I see it's included in
| |
| 16 #include <sys/ioctl.h> | |
| 17 #endif | |
| 18 | |
| 19 #include <algorithm> | |
| 20 #include <map> | |
| 21 #include <vector> | |
| 22 | |
| 23 #include "base/eintr_wrapper.h" | |
| 24 #include "base/logging.h" | |
| 25 #include "base/memory/scoped_vector.h" | |
| 26 #include "net/base/address_list.h" | |
| 27 #include "net/base/net_errors.h" | |
| 28 #include "net/base/net_util.h" | |
| 29 #include "net/base/network_change_notifier.h" | |
| 30 #include "net/socket/client_socket_factory.h" | |
| 31 #include "net/udp/datagram_client_socket.h" | |
| 32 | |
| 33 #if defined(OS_LINUX) | |
| 34 #include "net/base/address_tracker_linux.h" | |
| 35 #endif | |
| 36 | |
| 37 namespace net { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // Address sorting is performed according to RFC3484 with revisions. | |
| 42 // http://tools.ietf.org/html/draft-ietf-6man-rfc3484bis-03 | |
|
mmenke
2012/07/30 15:37:00
They're up to http://tools.ietf.org/html/draft-iet
| |
| 43 // The default policy has been updated as in Linux: | |
| 44 // http://www.akkadia.org/drepper/linux-rfc3484.html | |
|
mmenke
2012/07/30 19:40:34
Is this still needed? Looks like the latest 3484b
szym
2012/07/30 20:55:52
You're right. All problems raised there are alread
| |
| 45 // Precedence and label are separate to support override through /etc/gai.conf. | |
| 46 | |
| 47 // Generic policy entry. | |
| 48 struct PolicyEntry { | |
| 49 // IPv4 addresses must be mapped to IPv6. | |
| 50 unsigned char prefix[kIPv6AddressSize]; | |
| 51 unsigned prefix_length; | |
| 52 unsigned value; | |
| 53 }; | |
| 54 | |
| 55 typedef std::vector<PolicyEntry> PolicyTable; | |
| 56 | |
| 57 // Returns true if |p1| should precede |p2| in the table. | |
| 58 // Sorts table by decreasing prefix size to allow longest prefix matching. | |
| 59 bool ComparePolicy(const PolicyEntry& p1, const PolicyEntry& p2) { | |
| 60 return p1.prefix_length > p2.prefix_length; | |
| 61 } | |
| 62 | |
| 63 // Creates sorted PolicyTable from |table| with |size| entries. | |
| 64 PolicyTable LoadPolicy(PolicyEntry* table, size_t size) { | |
| 65 PolicyTable result(table, table + size); | |
| 66 std::sort(result.begin(), result.end(), ComparePolicy); | |
| 67 return result; | |
| 68 } | |
| 69 | |
| 70 // Search |table| for matching prefix of |address|. |table| must be sorted by | |
| 71 // descending prefix (prefix of another prefix must be later in table). | |
| 72 unsigned GetPolicyValue(const PolicyTable& table, | |
| 73 const IPAddressNumber& address) { | |
| 74 if (address.size() == kIPv4AddressSize) | |
| 75 return GetPolicyValue(table, ConvertIPv4NumberToIPv6Number(address)); | |
|
mmenke
2012/07/30 15:37:00
I think converting everything up front may be a li
szym
2012/07/30 16:28:45
I agree, although converting everything up front w
| |
| 76 for (unsigned i = 0; i < table.size(); ++i) { | |
| 77 const PolicyEntry& entry = table[i]; | |
| 78 IPAddressNumber prefix(entry.prefix, entry.prefix + kIPv6AddressSize); | |
| 79 if (IPNumberMatchesPrefix(address, prefix, entry.prefix_length)) | |
| 80 return entry.value; | |
| 81 } | |
| 82 NOTREACHED(); | |
| 83 // The last entry is the least restrictive, so assume it's default. | |
| 84 return table.back().value; | |
| 85 } | |
| 86 | |
| 87 enum AddressScope { | |
| 88 SCOPE_NODELOCAL = 1, | |
| 89 SCOPE_LINKLOCAL = 2, | |
| 90 SCOPE_SITELOCAL = 5, | |
| 91 SCOPE_ORGLOCAL = 8, | |
| 92 SCOPE_GLOBAL = 14, | |
| 93 }; | |
| 94 | |
| 95 bool IsMulticast(const IPAddressNumber& address) { | |
| 96 return address[0] == 0xFF; | |
| 97 } | |
| 98 | |
| 99 AddressScope GetMulticastScope(const IPAddressNumber& address) { | |
| 100 return static_cast<AddressScope>(address[1] & 0x0F); | |
| 101 } | |
| 102 | |
| 103 bool IsIPv6Loopback(const IPAddressNumber& address) { | |
| 104 // IN6_IS_ADDR_LOOPBACK | |
| 105 unsigned char kLoopback[kIPv6AddressSize] = { | |
| 106 0, 0, 0, 0, 0, 0, 0, 0, | |
| 107 0, 0, 0, 0, 0, 0, 0, 1, | |
| 108 }; | |
| 109 return address == IPAddressNumber(kLoopback, kLoopback + kIPv6AddressSize); | |
| 110 } | |
| 111 | |
| 112 bool IsLinkLocal(const IPAddressNumber& address) { | |
| 113 // IN6_IS_ADDR_LINKLOCAL | |
| 114 return (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80); | |
| 115 } | |
| 116 | |
| 117 bool IsSiteLocal(const IPAddressNumber& address) { | |
| 118 // IN6_IS_ADDR_SITELOCAL | |
| 119 return (address[0] == 0xFE) && ((address[1] & 0xC0) == 0xC0); | |
| 120 } | |
| 121 | |
| 122 AddressScope GetScope(const PolicyTable& table, | |
| 123 const IPAddressNumber& address) { | |
| 124 if (address.size() == kIPv6AddressSize) { | |
|
mmenke
2012/07/30 15:37:00
Should we handle IPv4 mapped addresses here? If s
szym
2012/07/30 16:28:45
Good question. Now that I think about it, I wonder
| |
| 125 if (IsMulticast(address)) { | |
| 126 return GetMulticastScope(address); | |
| 127 } else if (IsIPv6Loopback(address) || IsLinkLocal(address)) { | |
| 128 return SCOPE_LINKLOCAL; | |
| 129 } else if (IsSiteLocal(address)) { | |
| 130 return SCOPE_SITELOCAL; | |
| 131 } else { | |
| 132 return SCOPE_GLOBAL; | |
| 133 } | |
| 134 } else if (address.size() == kIPv4AddressSize) { | |
| 135 return static_cast<AddressScope>(GetPolicyValue(table, address)); | |
| 136 } else { | |
| 137 NOTREACHED(); | |
| 138 return SCOPE_NODELOCAL; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 // Default policy table. RFC 3484, Section 2.1. Updated for glibc. | |
| 143 PolicyEntry kDefaultPrecedenceTable[] = { | |
| 144 // ::1/128 -- loopback | |
| 145 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 128, 60 }, | |
| 146 // fc00::/7 -- multicast | |
|
szym
2012/07/30 20:55:52
not "multicast", but "unique local address"
| |
| 147 { { 0xFC }, 7, 50 }, | |
| 148 // ::/0 -- any | |
| 149 { { }, 0, 40 }, | |
| 150 // ::ffff:0:0/96 -- IPv4 mapped | |
| 151 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }, 96, 30 }, | |
| 152 // 2002::/16 -- 6to4 | |
| 153 { { 0x20, 0x02, }, 17, 20 }, | |
| 154 // 2001::/32 -- Teredo | |
| 155 { { 0x20, 0x01, 0, 0 }, 32, 10 }, | |
|
mmenke
2012/07/30 19:40:34
The tables look a bit different from the tables in
szym
2012/07/30 20:55:52
Ah. As bionic, this is following:
http://tools.iet
| |
| 156 // ::/96 -- IPv4 compatible | |
| 157 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 96, 1 }, | |
| 158 // fec0::/16 -- unique local address | |
| 159 { { 0xFE, 0xC0 }, 16, 1 }, | |
|
mmenke
2012/07/30 19:40:34
This looks wrong.
fc00::/7 is unique local.
fec0:
| |
| 160 // 3ffe::/16 -- 6bone | |
| 161 { { 0x3F, 0xFE }, 16, 1 }, | |
| 162 }; | |
| 163 | |
| 164 PolicyEntry kDefaultLabelTable[] = { | |
| 165 // ::1/128 -- loopback | |
| 166 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 128, 0 }, | |
| 167 // fc00::/7 -- multicast | |
| 168 { { 0xFC }, 7, 1 }, | |
| 169 // ::/0 -- any | |
| 170 { { }, 0, 2 }, | |
| 171 // ::ffff:0:0/96 -- IPv4 mapped | |
| 172 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }, 96, 3 }, | |
| 173 // 2002::/16 -- 6to4 | |
| 174 { { 0x20, 0x02, }, 17, 4 }, | |
| 175 // 2001::/32 -- Teredo | |
| 176 { { 0x20, 0x01, 0, 0 }, 32, 5 }, | |
| 177 // ::/96 -- IPv4 compatible | |
| 178 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 96, 10 }, | |
| 179 // fec0::/16 -- unique local address | |
| 180 { { 0xFE, 0xC0 }, 16, 11 }, | |
| 181 // 3ffe::/16 -- 6bone | |
| 182 { { 0x3F, 0xFE }, 16, 12 }, | |
| 183 }; | |
| 184 | |
| 185 // Default mapping of IPv4 addresses to scope. | |
| 186 PolicyEntry kDefaultScopeTable[] = { | |
|
mmenke
2012/07/30 15:37:00
Think this name is very confusing. May be worth j
szym
2012/07/30 16:28:45
The reason why this exists (and has 'Default' in n
mmenke
2012/07/30 19:40:34
Curiously, man gai.conf didn't mention the field,
szym
2012/07/30 20:55:52
My "man gai.conf" includes:
====
scopev4 mask valu
| |
| 187 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0x7F }, 104, | |
| 188 SCOPE_LINKLOCAL }, | |
| 189 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xA9, 0xFE }, 112, | |
| 190 SCOPE_LINKLOCAL }, | |
| 191 { { }, 0, SCOPE_GLOBAL }, | |
| 192 }; | |
| 193 | |
| 194 // Returns number of matching initial bits between the addresses |a1| and |a2|. | |
| 195 unsigned CommonPrefixLength(const IPAddressNumber& a1, | |
| 196 const IPAddressNumber& a2) { | |
| 197 DCHECK_EQ(a1.size(), a2.size()); | |
| 198 for (size_t i = 0; i < a1.size(); ++i) { | |
| 199 unsigned diff = a1[i] ^ a2[i]; | |
| 200 if (!diff) | |
| 201 continue; | |
| 202 for (unsigned j = 0; j < CHAR_BIT; ++j) { | |
| 203 if (diff & (1 << (CHAR_BIT - 1))) | |
| 204 return i * CHAR_BIT + j; | |
| 205 diff <<= 1; | |
| 206 } | |
| 207 } | |
| 208 return a1.size() * CHAR_BIT; | |
| 209 } | |
| 210 | |
| 211 // Computes the number of leading 1-bits in |addr|. | |
| 212 unsigned PrefixLength(const IPAddressNumber& addr) { | |
|
mmenke
2012/07/30 19:40:34
Suggest you rename this MaskPrefixLength(addr_mask
| |
| 213 IPAddressNumber all_ones(addr.size(), 0xFF); | |
| 214 return CommonPrefixLength(addr, all_ones); | |
| 215 } | |
| 216 | |
| 217 struct SourceAddressInfo { | |
| 218 AddressScope scope; | |
| 219 unsigned label; | |
| 220 // Values below matter only if more than one source address in the list. | |
| 221 unsigned prefix_length; | |
| 222 bool deprecated; // vs. preferred RFC4862 | |
| 223 bool home; // vs. care-of RFC6275 | |
| 224 bool native; | |
| 225 }; | |
| 226 | |
| 227 typedef std::map<IPAddressNumber, SourceAddressInfo> SourceAddressMap; | |
| 228 | |
| 229 struct SortElement { | |
| 230 IPAddressNumber address; | |
| 231 AddressScope scope; | |
| 232 unsigned precedence; | |
| 233 unsigned label; | |
| 234 const SourceAddressInfo* src; | |
| 235 unsigned common_prefix_length; | |
| 236 }; | |
| 237 | |
| 238 // Returns true iff |a1| should precede |a2| in the address list. | |
| 239 // RFC 3484, section 6. | |
| 240 bool CompareElements(const SortElement* a1, const SortElement* a2) { | |
|
mmenke
2012/07/30 19:40:34
Think a1 / a2 could be named better. Suggest dest
| |
| 241 // Rule 1: Avoid unusable destinations. | |
| 242 // Unusable destinations are already filtered out. | |
| 243 DCHECK(a1->src); | |
| 244 DCHECK(a2->src); | |
| 245 | |
| 246 // Rule 2: Prefer matching scope. | |
| 247 bool scope_match1 = (a1->src->scope == a1->scope); | |
| 248 bool scope_match2 = (a2->src->scope == a2->scope); | |
| 249 if (scope_match1 != scope_match2) | |
| 250 return scope_match1; | |
| 251 | |
| 252 // Rule 3: Avoid deprecated addresses. | |
| 253 if (a1->src->deprecated != a2->src->deprecated) | |
| 254 return !a1->src->deprecated; | |
| 255 | |
| 256 // Rule 4: Prefer home addresses. | |
| 257 if (a1->src->home != a2->src->home) | |
| 258 return !a1->src->home; | |
|
mmenke
2012/07/30 19:40:34
Shouldn't this be "return a1->src->home;"? (No neg
szym
2012/07/30 20:55:52
Yes.
| |
| 259 | |
| 260 // Rule 5: Prefer matching label. | |
| 261 bool label_match1 = (a1->src->label == a1->label); | |
| 262 bool label_match2 = (a2->src->label == a2->label); | |
| 263 if (label_match1 != label_match2) | |
| 264 return label_match1; | |
| 265 | |
| 266 // Rule 6: Prefer higher precedence. | |
| 267 if (a1->precedence != a2->precedence) | |
| 268 return a1->precedence > a2->precedence; | |
| 269 | |
| 270 // Rule 7: Prefer native transport. | |
| 271 if (a1->src->native != a2->src->native) | |
| 272 return !a1->src->native; | |
|
mmenke
2012/07/30 19:40:34
Believe the negation here is also wrong.
szym
2012/07/30 20:55:52
Yes.
| |
| 273 | |
| 274 // Rule 8: Prefer smaller scope. | |
| 275 if (a1->scope != a2->scope) | |
| 276 return a1->scope < a2->scope; | |
| 277 | |
| 278 // Rule 9: Use longest matching prefix. Only for matching address families. | |
| 279 if (a1->address.size() == a2->address.size()) { | |
| 280 if (a1->common_prefix_length != a2->common_prefix_length) | |
| 281 return a1->common_prefix_length > a2->common_prefix_length; | |
| 282 } | |
| 283 | |
| 284 // Rule 10: Leave the order unchanged. | |
| 285 // stable_sort takes care of that. | |
| 286 return false; | |
| 287 } | |
| 288 | |
| 289 class AddressSorterPosix : public AddressSorter, | |
| 290 public NetworkChangeNotifier::IPAddressObserver { | |
| 291 public: | |
| 292 explicit AddressSorterPosix(ClientSocketFactory* socket_factory); | |
| 293 virtual ~AddressSorterPosix(); | |
| 294 | |
| 295 virtual bool Sort(AddressList* list) const OVERRIDE; | |
| 296 | |
| 297 private: | |
| 298 // NetworkChangeNotifier::IPAddressObserver: | |
| 299 virtual void OnIPAddressChanged() OVERRIDE; | |
| 300 | |
| 301 SourceAddressMap source_info_; | |
| 302 // For the cases when the source address is not in |source_info_|. | |
| 303 // TODO(szym): Consider simply falling back to getaddrinfo in that case. | |
| 304 SourceAddressInfo mock_source_info_; | |
| 305 | |
| 306 ClientSocketFactory* socket_factory_; | |
| 307 PolicyTable precedence_table_; | |
| 308 PolicyTable label_table_; | |
| 309 PolicyTable scope_table_; | |
| 310 | |
| 311 DISALLOW_COPY_AND_ASSIGN(AddressSorterPosix); | |
| 312 }; | |
| 313 | |
| 314 AddressSorterPosix::AddressSorterPosix(ClientSocketFactory* socket_factory) | |
| 315 : socket_factory_(socket_factory), | |
| 316 precedence_table_(LoadPolicy(kDefaultPrecedenceTable, | |
| 317 arraysize(kDefaultPrecedenceTable))), | |
| 318 label_table_(LoadPolicy(kDefaultLabelTable, | |
| 319 arraysize(kDefaultLabelTable))), | |
| 320 scope_table_(LoadPolicy(kDefaultScopeTable, | |
| 321 arraysize(kDefaultScopeTable))) { | |
| 322 mock_source_info_.scope = SCOPE_GLOBAL; | |
| 323 mock_source_info_.deprecated = false; | |
| 324 mock_source_info_.home = false; | |
| 325 mock_source_info_.native = false; | |
| 326 mock_source_info_.label = static_cast<unsigned>(-1); | |
| 327 mock_source_info_.prefix_length = 0; | |
| 328 NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 329 } | |
| 330 | |
| 331 AddressSorterPosix::~AddressSorterPosix() { | |
| 332 NetworkChangeNotifier::RemoveIPAddressObserver(this); | |
| 333 } | |
| 334 | |
| 335 bool AddressSorterPosix::Sort(AddressList* list) const { | |
| 336 ScopedVector<SortElement> sort_list; | |
| 337 | |
| 338 for (size_t i = 0; i < list->size(); ++i) { | |
| 339 scoped_ptr<SortElement> el(new SortElement()); | |
|
mmenke
2012/07/30 19:40:34
Think using "el" for a variable name violates Goog
| |
| 340 el->address = (*list)[i].address(); | |
| 341 el->scope = GetScope(scope_table_, el->address); | |
| 342 el->precedence = GetPolicyValue(precedence_table_, el->address); | |
| 343 el->label = GetPolicyValue(label_table_, el->address); | |
| 344 | |
| 345 // Each socket can only be bound once. | |
| 346 scoped_ptr<DatagramClientSocket> socket( | |
| 347 socket_factory_->CreateDatagramClientSocket( | |
| 348 DatagramSocket::DEFAULT_BIND, | |
| 349 RandIntCallback(), | |
| 350 NULL /* NetLog */, | |
| 351 NetLog::Source())); | |
| 352 | |
| 353 // Even though no packets are sent, cannot use port 0 in Connect. | |
| 354 IPEndPoint dest(el->address, 80 /* port */); | |
| 355 int rv = socket->Connect(dest); | |
| 356 if (rv != OK) { | |
| 357 LOG(WARNING) << "Could not connect to " << dest.ToStringWithoutPort() | |
| 358 << " reason " << rv; | |
| 359 continue; | |
| 360 } | |
| 361 // Filter out unsable destinations. | |
|
mmenke
2012/07/30 19:40:34
nit: unuseable
| |
| 362 IPEndPoint src; | |
| 363 rv = socket->GetLocalAddress(&src); | |
| 364 if (rv != OK) { | |
| 365 LOG(WARNING) << "Could not get local address for " | |
| 366 << dest.ToStringWithoutPort() << " reason " << rv; | |
| 367 continue; | |
| 368 } | |
| 369 | |
| 370 SourceAddressMap::const_iterator it = source_info_.find(src.address()); | |
| 371 if (it != source_info_.end()) { | |
| 372 el->src = &(it->second); | |
| 373 } else { | |
| 374 // If |source_info_| is out of date, we still want to sort, although | |
| 375 // the HostCache will need to be cleared once it is updated. | |
| 376 el->src = &mock_source_info_; | |
|
mmenke
2012/07/30 19:40:34
Wonder if this case could occur often enough that
szym
2012/07/30 20:55:52
If the AddressTrackerLinux implementation is trust
mmenke
2012/07/30 21:07:17
That's probably more effort than it's worth... As
szym
2012/08/06 23:22:20
I figured the code looks cleaner without mock_sour
| |
| 377 } | |
| 378 | |
| 379 if (el->address.size() == src.address().size()) { | |
| 380 el->common_prefix_length = std::min( | |
| 381 CommonPrefixLength(el->address, src.address()), | |
| 382 el->src->prefix_length); | |
| 383 } | |
| 384 sort_list.push_back(el.release()); | |
| 385 } | |
| 386 | |
| 387 std::stable_sort(sort_list.begin(), sort_list.end(), CompareElements); | |
| 388 | |
| 389 list->clear(); | |
| 390 for (size_t i = 0; i < sort_list.size(); ++i) | |
| 391 list->push_back(IPEndPoint(sort_list[i]->address, 0 /* port */)); | |
| 392 | |
| 393 return true; | |
| 394 } | |
| 395 | |
| 396 void AddressSorterPosix::OnIPAddressChanged() { | |
| 397 #if defined(OS_LINUX) | |
| 398 const internal::AddressTrackerLinux* tracker = | |
| 399 NetworkChangeNotifier::GetAddressTracker(); | |
| 400 if (!tracker) | |
| 401 return; | |
|
szym
2012/07/30 16:28:45
There's a potential race during shutdown of Networ
| |
| 402 typedef internal::AddressTrackerLinux::AddressMap AddressMap; | |
| 403 AddressMap map = tracker->GetAddressMap(); | |
| 404 source_info_.clear(); | |
| 405 for (AddressMap::const_iterator it = map.begin(); it != map.end(); ++it) { | |
| 406 const IPAddressNumber& address = it->first; | |
| 407 const struct ifaddrmsg& msg = it->second; | |
| 408 SourceAddressInfo& info = source_info_[address]; | |
| 409 info.native = false; // TODO(szym): obtain this via netlink. | |
| 410 info.deprecated = msg.ifa_flags & IFA_F_DEPRECATED; | |
| 411 info.home = msg.ifa_flags & IFA_F_HOMEADDRESS; | |
| 412 info.prefix_length = msg.ifa_prefixlen; | |
| 413 info.label = GetPolicyValue(label_table_, address); | |
| 414 info.scope = GetScope(scope_table_, address); | |
| 415 } | |
| 416 #elif defined(OS_MACOSX) || defined(OS_BSD) | |
| 417 // It's not clear we will receive notification when deprecated flag changes. | |
| 418 source_info_.clear(); | |
| 419 // Socket for ioctl. | |
| 420 int ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0); | |
| 421 if (ioctl_socket < 0) { | |
| 422 perror("ioctl_socket"); | |
| 423 return; | |
| 424 } | |
| 425 struct ifaddrs* addrs; | |
| 426 int rv = getifaddrs(&addrs); | |
| 427 if (rv < 0) { | |
| 428 perror("getifaddrs"); | |
| 429 close(ioctl_socket); | |
| 430 return; | |
| 431 } | |
| 432 | |
| 433 for (struct ifaddrs* ifa = addrs; ifa != NULL; ifa = ifa->ifa_next) { | |
| 434 IPEndPoint src; | |
| 435 int rv = src.FromSockAddr(ifa->ifa_addr, ifa->ifa_addr->sa_len); | |
| 436 if (rv != OK) { | |
| 437 LOG(WARNING) << "could not ToSockAddr " << rv; | |
|
mmenke
2012/07/30 15:37:00
FromSockAddr?
| |
| 438 continue; | |
| 439 } | |
| 440 IPEndPoint netmask; | |
| 441 rv = netmask.FromSockAddr(ifa->ifa_netmask, ifa->ifa_addr->sa_len); | |
| 442 if (rv != OK) { | |
| 443 LOG(WARNING) << "could not ToSockAddr " << rv; | |
|
mmenke
2012/07/30 15:37:00
FromSockAddr?
| |
| 444 continue; | |
| 445 } | |
| 446 SourceAddressInfo& info = source_info_[src.address()]; | |
| 447 // Note: no known way to fill in |native| and |home|. | |
| 448 info.native = info.home = info.deprecated = false; | |
| 449 if (ifa->ifa_addr->sa_family == AF_INET6) { | |
| 450 struct in6_ifreq ifr = {}; | |
| 451 strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name) - 1); | |
| 452 DCHECK_LE(ifa->ifa_addr->sa_len, sizeof(ifr.ifr_ifru.ifru_addr)); | |
| 453 memcpy(&ifr.ifr_ifru.ifru_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); | |
| 454 rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); | |
| 455 if (rv < 0) { | |
| 456 perror("ioctl failed "); | |
| 457 continue; | |
| 458 } | |
| 459 info.deprecated = ifr.ifr_ifru.ifru_flags & IN6_IFF_DEPRECATED; | |
| 460 } | |
| 461 info.prefix_length = PrefixLength(netmask.address()); | |
| 462 info.label = GetPolicyValue(label_table_, src.address()); | |
| 463 info.scope = GetScope(scope_table_, src.address()); | |
| 464 } | |
| 465 freeifaddrs(addrs); | |
| 466 close(ioctl_socket); | |
| 467 #endif | |
| 468 } | |
| 469 | |
| 470 } // namespace | |
| 471 | |
| 472 // static | |
| 473 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | |
| 474 return scoped_ptr<AddressSorter>( | |
| 475 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); | |
| 476 } | |
| 477 | |
| 478 } // namespace net | |
| 479 | |
| OLD | NEW |