[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 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/base/net_util.h" |
| 6 | |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 7 | #include <sys/types.h> |
| 8 | |
| 9 | #include "base/eintr_wrapper.h" |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 14 | #include "googleurl/src/gurl.h" |
| 15 | #include "net/base/escape.h" |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 16 | #include "net/base/ip_endpoint.h" |
| 17 | #include "net/base/net_errors.h" |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 18 | |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 19 | #if !defined(OS_ANDROID) |
| 20 | #include <ifaddrs.h> |
| 21 | #endif |
[email protected] | 7fc22b1d | 2011-08-16 18:47:07 | [diff] [blame] | 22 | #include <net/if.h> |
| 23 | #include <netinet/in.h> |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 24 | |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 25 | namespace net { |
| 26 | |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 27 | bool FileURLToFilePath(const GURL& url, FilePath* path) { |
| 28 | *path = FilePath(); |
| 29 | std::string& file_path_str = const_cast<std::string&>(path->value()); |
| 30 | file_path_str.clear(); |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 31 | |
| 32 | if (!url.is_valid()) |
| 33 | return false; |
| 34 | |
| 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. |
[email protected] | b4037d13 | 2009-11-11 01:10:45 | [diff] [blame] | 37 | // TODO(dhg): This should probably take into account UNCs which could |
| 38 | // include a hostname other than localhost or blank |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 39 | std::string old_path = url.path(); |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 40 | |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 41 | if (old_path.empty()) |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 42 | return false; |
| 43 | |
| 44 | // GURL stores strings as percent-encoded 8-bit, this will undo if possible. |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 45 | old_path = UnescapeURLComponent(old_path, |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 46 | UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
| 47 | |
| 48 | // Collapse multiple path slashes into a single path slash. |
| 49 | std::string new_path; |
| 50 | do { |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 51 | new_path = old_path; |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 52 | ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 53 | old_path.swap(new_path); |
| 54 | } while (new_path != old_path); |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 55 | |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 56 | file_path_str.assign(old_path); |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 57 | |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 58 | return !file_path_str.empty(); |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 61 | bool GetNetworkList(NetworkInterfaceList* networks) { |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 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. |
| 67 | NOTIMPLEMENTED(); |
| 68 | return false; |
| 69 | #else |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 70 | // getifaddrs() may require IO operations. |
| 71 | base::ThreadRestrictions::AssertIOAllowed(); |
| 72 | |
[email protected] | 7fc22b1d | 2011-08-16 18:47:07 | [diff] [blame] | 73 | ifaddrs *interfaces; |
| 74 | if (getifaddrs(&interfaces) < 0) { |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 75 | PLOG(ERROR) << "getifaddrs"; |
| 76 | return false; |
| 77 | } |
| 78 | |
[email protected] | 7fc22b1d | 2011-08-16 18:47:07 | [diff] [blame] | 79 | // Enumerate the addresses assigned to network interfaces which are up. |
| 80 | for (ifaddrs *interface = interfaces; |
| 81 | interface != NULL; |
| 82 | interface = interface->ifa_next) { |
| 83 | // Skip loopback interfaces, and ones which are down. |
| 84 | if (!(IFF_UP & interface->ifa_flags)) |
| 85 | continue; |
| 86 | if (IFF_LOOPBACK & interface->ifa_flags) |
| 87 | continue; |
| 88 | // Skip interfaces with no address configured. |
| 89 | struct sockaddr* addr = interface->ifa_addr; |
| 90 | if (!addr) |
| 91 | continue; |
| 92 | // Skip loopback addresses configured on non-loopback interfaces. |
| 93 | int addr_size = 0; |
| 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)) |
| 100 | continue; |
| 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) |
| 106 | continue; |
| 107 | } else { |
| 108 | // Skip non-IP addresses. |
| 109 | continue; |
| 110 | } |
| 111 | IPEndPoint address; |
| 112 | std::string name = interface->ifa_name; |
| 113 | if (address.FromSockAddr(addr, addr_size)) { |
| 114 | networks->push_back(NetworkInterface(name, address.address())); |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
[email protected] | 7fc22b1d | 2011-08-16 18:47:07 | [diff] [blame] | 118 | freeifaddrs(interfaces); |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 119 | |
| 120 | return true; |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 121 | #endif |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 844cbe0 | 2008-09-02 18:13:34 | [diff] [blame] | 124 | } // namespace net |