blob: 8ff720f3e43d0e47e4707eecde5a5a36a25892ea [file] [log] [blame]
[email protected]611cc902011-03-19 00:36:391// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]844cbe02008-09-02 18:13:342// 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]611cc902011-03-19 00:36:397#include <sys/types.h>
8
9#include "base/eintr_wrapper.h"
[email protected]498c1a6b22008-11-24 23:37:0410#include "base/file_path.h"
[email protected]611cc902011-03-19 00:36:3911#include "base/logging.h"
[email protected]844cbe02008-09-02 18:13:3412#include "base/string_util.h"
[email protected]611cc902011-03-19 00:36:3913#include "base/threading/thread_restrictions.h"
[email protected]844cbe02008-09-02 18:13:3414#include "googleurl/src/gurl.h"
15#include "net/base/escape.h"
[email protected]611cc902011-03-19 00:36:3916#include "net/base/ip_endpoint.h"
17#include "net/base/net_errors.h"
[email protected]844cbe02008-09-02 18:13:3418
[email protected]39588992011-07-11 19:54:3719#if !defined(OS_ANDROID)
20#include <ifaddrs.h>
21#endif
[email protected]7fc22b1d2011-08-16 18:47:0722#include <net/if.h>
23#include <netinet/in.h>
[email protected]39588992011-07-11 19:54:3724
[email protected]844cbe02008-09-02 18:13:3425namespace net {
26
[email protected]498c1a6b22008-11-24 23:37:0427bool 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]844cbe02008-09-02 18:13:3431
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]b4037d132009-11-11 01:10:4537 // TODO(dhg): This should probably take into account UNCs which could
38 // include a hostname other than localhost or blank
[email protected]498c1a6b22008-11-24 23:37:0439 std::string old_path = url.path();
[email protected]844cbe02008-09-02 18:13:3440
[email protected]498c1a6b22008-11-24 23:37:0441 if (old_path.empty())
[email protected]844cbe02008-09-02 18:13:3442 return false;
43
44 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
[email protected]498c1a6b22008-11-24 23:37:0445 old_path = UnescapeURLComponent(old_path,
[email protected]844cbe02008-09-02 18:13:3446 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]498c1a6b22008-11-24 23:37:0451 new_path = old_path;
[email protected]844cbe02008-09-02 18:13:3452 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
[email protected]498c1a6b22008-11-24 23:37:0453 old_path.swap(new_path);
54 } while (new_path != old_path);
[email protected]844cbe02008-09-02 18:13:3455
[email protected]498c1a6b22008-11-24 23:37:0456 file_path_str.assign(old_path);
[email protected]844cbe02008-09-02 18:13:3457
[email protected]498c1a6b22008-11-24 23:37:0458 return !file_path_str.empty();
[email protected]844cbe02008-09-02 18:13:3459}
60
[email protected]611cc902011-03-19 00:36:3961bool GetNetworkList(NetworkInterfaceList* networks) {
[email protected]39588992011-07-11 19:54:3762#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]611cc902011-03-19 00:36:3970 // getifaddrs() may require IO operations.
71 base::ThreadRestrictions::AssertIOAllowed();
72
[email protected]7fc22b1d2011-08-16 18:47:0773 ifaddrs *interfaces;
74 if (getifaddrs(&interfaces) < 0) {
[email protected]611cc902011-03-19 00:36:3975 PLOG(ERROR) << "getifaddrs";
76 return false;
77 }
78
[email protected]7fc22b1d2011-08-16 18:47:0779 // 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]611cc902011-03-19 00:36:39115 }
116 }
117
[email protected]7fc22b1d2011-08-16 18:47:07118 freeifaddrs(interfaces);
[email protected]611cc902011-03-19 00:36:39119
120 return true;
[email protected]39588992011-07-11 19:54:37121#endif
[email protected]611cc902011-03-19 00:36:39122}
123
[email protected]844cbe02008-09-02 18:13:34124} // namespace net