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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
9 #include "base/eintr_wrapper.h"
10 #include "base/file_path.h"
11 #include "base/logging.h"
12 #include "base/string_util.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/base/escape.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h"
19 #if !defined(OS_ANDROID)
23 #include <netinet/in.h>
27 bool FileURLToFilePath(const GURL
& url
, FilePath
* path
) {
29 std::string
& file_path_str
= const_cast<std::string
&>(path
->value());
30 file_path_str
.clear();
35 // Firefox seems to ignore the "host" of a file url if there is one. That is,
36 // file://foo/bar.txt maps to /bar.txt.
37 // TODO(dhg): This should probably take into account UNCs which could
38 // include a hostname other than localhost or blank
39 std::string old_path
= url
.path();
44 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
45 old_path
= UnescapeURLComponent(old_path
,
46 UnescapeRule::SPACES
| UnescapeRule::URL_SPECIAL_CHARS
);
48 // Collapse multiple path slashes into a single path slash.
52 ReplaceSubstringsAfterOffset(&new_path
, 0, "//", "/");
53 old_path
.swap(new_path
);
54 } while (new_path
!= old_path
);
56 file_path_str
.assign(old_path
);
58 return !file_path_str
.empty();
61 bool GetNetworkList(NetworkInterfaceList
* networks
) {
62 #if defined(OS_ANDROID)
63 // TODO: Android API doesn't support ifaddrs. This method was only used by
64 // P2PMessage. Consider to implement it until really needed. The possible
65 // approach is implementing the similar feature by
66 // java.net.NetworkInterface through JNI.
70 // getifaddrs() may require IO operations.
71 base::ThreadRestrictions::AssertIOAllowed();
74 if (getifaddrs(&interfaces
) < 0) {
75 PLOG(ERROR
) << "getifaddrs";
79 // Enumerate the addresses assigned to network interfaces which are up.
80 for (ifaddrs
*interface
= interfaces
;
82 interface
= interface
->ifa_next
) {
83 // Skip loopback interfaces, and ones which are down.
84 if (!(IFF_UP
& interface
->ifa_flags
))
86 if (IFF_LOOPBACK
& interface
->ifa_flags
)
88 // Skip interfaces with no address configured.
89 struct sockaddr
* addr
= interface
->ifa_addr
;
92 // Skip loopback addresses configured on non-loopback interfaces.
94 if (addr
->sa_family
== AF_INET6
) {
95 struct sockaddr_in6
* addr_in6
=
96 reinterpret_cast<struct sockaddr_in6
*>(addr
);
97 struct in6_addr
* sin6_addr
= &addr_in6
->sin6_addr
;
98 addr_size
= sizeof(*addr_in6
);
99 if (IN6_IS_ADDR_LOOPBACK(sin6_addr
))
101 } else if (addr
->sa_family
== AF_INET
) {
102 struct sockaddr_in
* addr_in
=
103 reinterpret_cast<struct sockaddr_in
*>(addr
);
104 addr_size
= sizeof(*addr_in
);
105 if (addr_in
->sin_addr
.s_addr
== INADDR_LOOPBACK
)
108 // Skip non-IP addresses.
112 std::string name
= interface
->ifa_name
;
113 if (address
.FromSockAddr(addr
, addr_size
)) {
114 networks
->push_back(NetworkInterface(name
, address
.address()));
118 freeifaddrs(interfaces
);