diff options
Diffstat (limited to 'net/base/net_util_win.cc')
-rw-r--r-- | net/base/net_util_win.cc | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/net/base/net_util_win.cc b/net/base/net_util_win.cc index dac93a9..ee6a5af 100644 --- a/net/base/net_util_win.cc +++ b/net/base/net_util_win.cc @@ -1,18 +1,24 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <algorithm> - #include "net/base/net_util.h" +#include <iphlpapi.h> + +#include <algorithm> + #include "base/file_path.h" +#include "base/scoped_ptr.h" #include "base/string_piece.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" +#include "base/threading/thread_restrictions.h" #include "base/utf_string_conversions.h" #include "googleurl/src/gurl.h" #include "net/base/escape.h" +#include "net/base/ip_endpoint.h" +#include "net/base/net_errors.h" namespace net { @@ -69,4 +75,52 @@ bool FileURLToFilePath(const GURL& url, FilePath* file_path) { return true; } +bool GetNetworkList(NetworkInterfaceList* networks) { + // GetAdaptersAddresses() may require IO operations. + base::ThreadRestrictions::AssertIOAllowed(); + + IP_ADAPTER_ADDRESSES info_temp; + ULONG len = 0; + + // First get number of networks. + ULONG result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, &info_temp, &len); + if (result != ERROR_BUFFER_OVERFLOW) { + // There are 0 networks. + return true; + } + + scoped_array<char> buf(new char[len]); + IP_ADAPTER_ADDRESSES *adapters = + reinterpret_cast<IP_ADAPTER_ADDRESSES *>(buf.get()); + result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &len); + if (result != NO_ERROR) { + LOG(ERROR) << "GetAdaptersAddresses failed: " << result; + return false; + } + + for (IP_ADAPTER_ADDRESSES *adapter = adapters; adapter != NULL; + adapter = adapter->Next) { + // Ignore the loopback device. + if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { + continue; + } + + IP_ADAPTER_UNICAST_ADDRESS* address; + for (address = adapter->FirstUnicastAddress; address != NULL; + address = address->Next) { + int family = address->Address.lpSockaddr->sa_family; + if (family == AF_INET || family == AF_INET6) { + IPEndPoint endpoint; + if (endpoint.FromSockAddr(address->Address.lpSockaddr, + address->Address.iSockaddrLength)) { + std::string name = adapter->AdapterName; + networks->push_back(NetworkInterface(name, endpoint.address())); + } + } + } + } + + return true; +} + } // namespace net |