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. 4 5 #include "net/base/net_util.h" 6 7 #ifndef ANDROID 8 #include <ifaddrs.h> 9 #endif 10 #include <sys/types.h> 11 12 #include "base/eintr_wrapper.h" 13 #include "base/file_path.h" 14 #include "base/logging.h" 15 #include "base/string_util.h" 16 #include "base/threading/thread_restrictions.h" 17 #include "googleurl/src/gurl.h" 18 #include "net/base/escape.h" 19 #include "net/base/ip_endpoint.h" 20 #include "net/base/net_errors.h" 21 22 namespace net { 23 24 bool FileURLToFilePath(const GURL& url, FilePath* path) { 25 *path = FilePath(); 26 std::string& file_path_str = const_cast<std::string&>(path->value()); 27 file_path_str.clear(); 28 29 if (!url.is_valid()) 30 return false; 31 32 // Firefox seems to ignore the "host" of a file url if there is one. That is, 33 // file://foo/bar.txt maps to /bar.txt. 34 // TODO(dhg): This should probably take into account UNCs which could 35 // include a hostname other than localhost or blank 36 std::string old_path = url.path(); 37 38 if (old_path.empty()) 39 return false; 40 41 // GURL stores strings as percent-encoded 8-bit, this will undo if possible. 42 old_path = UnescapeURLComponent(old_path, 43 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); 44 45 // Collapse multiple path slashes into a single path slash. 46 std::string new_path; 47 do { 48 new_path = old_path; 49 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); 50 old_path.swap(new_path); 51 } while (new_path != old_path); 52 53 file_path_str.assign(old_path); 54 55 return !file_path_str.empty(); 56 } 57 58 #ifndef ANDROID 59 bool GetNetworkList(NetworkInterfaceList* networks) { 60 // getifaddrs() may require IO operations. 61 base::ThreadRestrictions::AssertIOAllowed(); 62 63 ifaddrs *ifaddr; 64 if (getifaddrs(&ifaddr) < 0) { 65 PLOG(ERROR) << "getifaddrs"; 66 return false; 67 } 68 69 for (ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { 70 int family = ifa->ifa_addr->sa_family; 71 if (family == AF_INET || family == AF_INET6) { 72 IPEndPoint address; 73 std::string name = ifa->ifa_name; 74 if (address.FromSockAddr(ifa->ifa_addr, 75 sizeof(ifa->ifa_addr)) && 76 name.substr(0, 2) != "lo") { 77 networks->push_back(NetworkInterface(name, address.address())); 78 } 79 } 80 } 81 82 freeifaddrs(ifaddr); 83 84 return true; 85 } 86 #endif 87 88 } // namespace net 89