OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 "chrome/browser/media/router/discovery/discovery_network_list.h" |
| 6 |
| 7 // TODO(btolsch): Remove the preprocessor conditionals when adding Mac version. |
| 8 #include "build/build_config.h" |
| 9 #if defined(OS_LINUX) |
| 10 |
| 11 #include <ifaddrs.h> |
| 12 #include <net/if.h> |
| 13 #include <net/if_arp.h> |
| 14 #include <netinet/in.h> |
| 15 #include <netpacket/packet.h> |
| 16 #include <sys/socket.h> |
| 17 #include <sys/types.h> |
| 18 |
| 19 #include <algorithm> |
| 20 |
| 21 #include "base/logging.h" |
| 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" |
| 24 #include "net/base/net_errors.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 void GetDiscoveryNetworkInfoListImpl( |
| 29 const struct ifaddrs* if_list, |
| 30 std::vector<DiscoveryNetworkInfo>* network_info_list) { |
| 31 std::string ssid; |
| 32 for (; if_list != NULL; if_list = if_list->ifa_next) { |
| 33 if ((if_list->ifa_flags & IFF_RUNNING) == 0 || |
| 34 (if_list->ifa_flags & IFF_UP) == 0) { |
| 35 continue; |
| 36 } |
| 37 |
| 38 const struct sockaddr* addr = if_list->ifa_addr; |
| 39 if (addr->sa_family != AF_PACKET) { |
| 40 continue; |
| 41 } |
| 42 std::string name(if_list->ifa_name); |
| 43 if (name.size() == 0) { |
| 44 continue; |
| 45 } |
| 46 |
| 47 // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET. |
| 48 const struct sockaddr_ll* ll_addr = |
| 49 reinterpret_cast<const struct sockaddr_ll*>(addr); |
| 50 // ARPHRD_ETHER is used to test for Ethernet, as in IEEE 802.3 MAC protocol. |
| 51 // This spec is used by both wired Ethernet and wireless (e.g. 802.11). |
| 52 if (ll_addr->sll_hatype != ARPHRD_ETHER) { |
| 53 continue; |
| 54 } |
| 55 |
| 56 if (MaybeGetWifiSSID(name, &ssid)) { |
| 57 network_info_list->push_back({name, ssid}); |
| 58 continue; |
| 59 } |
| 60 |
| 61 if (ll_addr->sll_halen == 0) { |
| 62 continue; |
| 63 } |
| 64 |
| 65 network_info_list->push_back( |
| 66 {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)}); |
| 67 } |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { |
| 73 std::vector<DiscoveryNetworkInfo> network_ids; |
| 74 |
| 75 struct ifaddrs* if_list; |
| 76 if (getifaddrs(&if_list)) { |
| 77 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); |
| 78 return network_ids; |
| 79 } |
| 80 |
| 81 GetDiscoveryNetworkInfoListImpl(if_list, &network_ids); |
| 82 StableSortDiscoveryNetworkInfo(network_ids.begin(), network_ids.end()); |
| 83 freeifaddrs(if_list); |
| 84 return network_ids; |
| 85 } |
| 86 |
| 87 #else // !defined(OS_LINUX) |
| 88 |
| 89 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { |
| 90 return std::vector<DiscoveryNetworkInfo>(); |
| 91 } |
| 92 |
| 93 #endif |
OLD | NEW |