OLD | NEW |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/router/discovery/discovery_network_list.h" | 5 #include "chrome/browser/media/router/discovery/discovery_network_list.h" |
6 | 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> | 7 #include <ifaddrs.h> |
12 #include <net/if.h> | 8 #include <net/if.h> |
13 #include <net/if_arp.h> | 9 #include <net/if_arp.h> |
14 #include <netinet/in.h> | 10 #include <netinet/in.h> |
15 #include <netpacket/packet.h> | |
16 #include <sys/socket.h> | 11 #include <sys/socket.h> |
17 #include <sys/types.h> | 12 #include <sys/types.h> |
18 | 13 |
19 #include <algorithm> | 14 #include <algorithm> |
20 | 15 |
21 #include "base/logging.h" | 16 #include "base/logging.h" |
22 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "build/build_config.h" |
23 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" | 19 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" |
24 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
25 | 21 |
| 22 #if !defined(OS_MACOSX) |
| 23 #include <netpacket/packet.h> |
| 24 #else |
| 25 #include <net/if_dl.h> |
| 26 #endif |
| 27 |
26 namespace { | 28 namespace { |
27 | 29 |
| 30 #if !defined(OS_MACOSX) |
| 31 using sll = struct sockaddr_ll; |
| 32 #define SLL_HATYPE(s) ((s)->sll_hatype) |
| 33 #define SLL_HALEN(s) ((s)->sll_halen) |
| 34 #define SLL_ADDR(s) ((s)->sll_addr) |
| 35 #else // defined(OS_MACOSX) |
| 36 #define AF_PACKET AF_LINK |
| 37 using sll = struct sockaddr_dl; |
| 38 #define SLL_HATYPE(s) ((s)->sdl_type) |
| 39 #define SLL_HALEN(s) ((s)->sdl_alen) |
| 40 #define SLL_ADDR(s) (LLADDR(s)) |
| 41 #endif |
| 42 |
28 void GetDiscoveryNetworkInfoListImpl( | 43 void GetDiscoveryNetworkInfoListImpl( |
29 const struct ifaddrs* if_list, | 44 const struct ifaddrs* if_list, |
30 std::vector<DiscoveryNetworkInfo>* network_info_list) { | 45 std::vector<DiscoveryNetworkInfo>* network_info_list) { |
31 std::string ssid; | 46 std::string ssid; |
32 for (; if_list != NULL; if_list = if_list->ifa_next) { | 47 for (; if_list != NULL; if_list = if_list->ifa_next) { |
33 if ((if_list->ifa_flags & IFF_RUNNING) == 0 || | 48 if ((if_list->ifa_flags & IFF_RUNNING) == 0 || |
34 (if_list->ifa_flags & IFF_UP) == 0) { | 49 (if_list->ifa_flags & IFF_UP) == 0) { |
35 continue; | 50 continue; |
36 } | 51 } |
37 | 52 |
38 const struct sockaddr* addr = if_list->ifa_addr; | 53 const struct sockaddr* addr = if_list->ifa_addr; |
39 if (addr->sa_family != AF_PACKET) { | 54 if (addr->sa_family != AF_PACKET) { |
40 continue; | 55 continue; |
41 } | 56 } |
42 std::string name(if_list->ifa_name); | 57 std::string name(if_list->ifa_name); |
43 if (name.size() == 0) { | 58 if (name.size() == 0) { |
44 continue; | 59 continue; |
45 } | 60 } |
46 | 61 |
47 // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET. | 62 // |addr| will always be sockaddr_ll/sockaddr_dl when |sa_family| == |
48 const struct sockaddr_ll* ll_addr = | 63 // AF_PACKET. |
49 reinterpret_cast<const struct sockaddr_ll*>(addr); | 64 const auto* ll_addr = reinterpret_cast<const sll*>(addr); |
50 // ARPHRD_ETHER is used to test for Ethernet, as in IEEE 802.3 MAC protocol. | 65 // 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). | 66 // This spec is used by both wired Ethernet and wireless (e.g. 802.11). |
52 if (ll_addr->sll_hatype != ARPHRD_ETHER) { | 67 // ARPHRD_IEEE802 is used to test for the 802.2 LLC protocol of Ethernet. |
| 68 if (SLL_HATYPE(ll_addr) != ARPHRD_ETHER && |
| 69 SLL_HATYPE(ll_addr) != ARPHRD_IEEE802) { |
53 continue; | 70 continue; |
54 } | 71 } |
55 | 72 |
56 if (MaybeGetWifiSSID(name, &ssid)) { | 73 if (MaybeGetWifiSSID(name, &ssid)) { |
57 network_info_list->push_back({name, ssid}); | 74 network_info_list->push_back({name, ssid}); |
58 continue; | 75 continue; |
59 } | 76 } |
60 | 77 |
61 if (ll_addr->sll_halen == 0) { | 78 if (SLL_HALEN(ll_addr) == 0) { |
62 continue; | 79 continue; |
63 } | 80 } |
64 | 81 |
65 network_info_list->push_back( | 82 network_info_list->push_back( |
66 {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)}); | 83 {name, base::HexEncode( |
| 84 reinterpret_cast<const unsigned char*>(SLL_ADDR(ll_addr)), |
| 85 SLL_HALEN(ll_addr))}); |
67 } | 86 } |
68 } | 87 } |
69 | 88 |
70 } // namespace | 89 } // namespace |
71 | 90 |
72 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { | 91 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { |
73 std::vector<DiscoveryNetworkInfo> network_ids; | 92 std::vector<DiscoveryNetworkInfo> network_ids; |
74 | 93 |
75 struct ifaddrs* if_list; | 94 struct ifaddrs* if_list; |
76 if (getifaddrs(&if_list)) { | 95 if (getifaddrs(&if_list)) { |
77 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); | 96 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); |
78 return network_ids; | 97 return network_ids; |
79 } | 98 } |
80 | 99 |
81 GetDiscoveryNetworkInfoListImpl(if_list, &network_ids); | 100 GetDiscoveryNetworkInfoListImpl(if_list, &network_ids); |
82 StableSortDiscoveryNetworkInfo(network_ids.begin(), network_ids.end()); | 101 StableSortDiscoveryNetworkInfo(network_ids.begin(), network_ids.end()); |
83 freeifaddrs(if_list); | 102 freeifaddrs(if_list); |
84 return network_ids; | 103 return network_ids; |
85 } | 104 } |
86 | |
87 #else // !defined(OS_LINUX) | |
88 | |
89 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { | |
90 return std::vector<DiscoveryNetworkInfo>(); | |
91 } | |
92 | |
93 #endif | |
OLD | NEW |