diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-19 00:36:39 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-19 00:36:39 +0000 |
commit | 611cc90419870d40c0048c01ce72def49b8b5dd3 (patch) | |
tree | 264a4f257f9d2906f1d7d75e47154b014312af7f /net/base/net_util_posix.cc | |
parent | 35d94587215848ac4ec0dbeceaa4e86a7657f7ae (diff) | |
download | chromium_src-611cc90419870d40c0048c01ce72def49b8b5dd3.zip chromium_src-611cc90419870d40c0048c01ce72def49b8b5dd3.tar.gz chromium_src-611cc90419870d40c0048c01ce72def49b8b5dd3.tar.bz2 |
Add GetNetworkList() in net_utils.
BUG=None
TEST=Unittests.
Review URL: http://codereview.chromium.org/6676027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78780 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/net_util_posix.cc')
-rw-r--r-- | net/base/net_util_posix.cc | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/net/base/net_util_posix.cc b/net/base/net_util_posix.cc index 651be1b..af3d5d1 100644 --- a/net/base/net_util_posix.cc +++ b/net/base/net_util_posix.cc @@ -1,13 +1,21 @@ -// 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 "net/base/net_util.h" +#include <ifaddrs.h> +#include <sys/types.h> + +#include "base/eintr_wrapper.h" #include "base/file_path.h" +#include "base/logging.h" #include "base/string_util.h" +#include "base/threading/thread_restrictions.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 { @@ -45,4 +53,32 @@ bool FileURLToFilePath(const GURL& url, FilePath* path) { return !file_path_str.empty(); } +bool GetNetworkList(NetworkInterfaceList* networks) { + // getifaddrs() may require IO operations. + base::ThreadRestrictions::AssertIOAllowed(); + + ifaddrs *ifaddr; + if (getifaddrs(&ifaddr) < 0) { + PLOG(ERROR) << "getifaddrs"; + return false; + } + + for (ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + int family = ifa->ifa_addr->sa_family; + if (family == AF_INET || family == AF_INET6) { + IPEndPoint address; + std::string name = ifa->ifa_name; + if (address.FromSockAddr(ifa->ifa_addr, + sizeof(ifa->ifa_addr)) && + name.substr(0, 2) != "lo") { + networks->push_back(NetworkInterface(name, address.address())); + } + } + } + + freeifaddrs(ifaddr); + + return true; +} + } // namespace net |