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 #include <ifaddrs.h> | |
8 #include <net/if.h> | |
9 #include <net/if_arp.h> | |
10 #include <netinet/in.h> | |
11 #include <netpacket/packet.h> | |
12 #include <sys/socket.h> | |
13 #include <sys/types.h> | |
14 | |
15 #include "base/logging.h" | |
16 #include "base/strings/string_number_conversions.h" | |
17 #include "base/strings/string_util.h" | |
18 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" | |
19 #include "net/base/net_errors.h" | |
20 | |
21 namespace { | |
22 | |
23 void GetDiscoveryNetworkIdListImpl( | |
24 const struct ifaddrs* if_list, | |
25 std::vector<DiscoveryNetworkId>* network_ids) { | |
26 std::string ssid; | |
27 for (; if_list != NULL; if_list = if_list->ifa_next) { | |
28 const struct sockaddr* addr = if_list->ifa_addr; | |
29 std::string name(if_list->ifa_name); | |
30 if (name.size() == 0 || addr->sa_family != AF_PACKET) { | |
31 continue; | |
32 } | |
33 | |
34 // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET. | |
35 const struct sockaddr_ll* ll_addr = | |
36 reinterpret_cast<const struct sockaddr_ll*>(addr); | |
37 if (ll_addr->sll_hatype != ARPHRD_ETHER) { | |
38 continue; | |
39 } | |
40 | |
41 if (MaybeGetWifiSSID(name.data(), &ssid)) { | |
42 network_ids->push_back({name, ssid}); | |
43 continue; | |
44 } | |
45 | |
46 if (ll_addr->sll_halen == 0) { | |
47 continue; | |
48 } | |
49 | |
50 network_ids->push_back({name, base::ToLowerASCII(base::HexEncode( | |
mark a. foltz
2017/04/26 22:25:11
Is it necessary to convert the hex bytes to lower
btolsch
2017/05/03 22:03:09
No it's not. Removed.
| |
51 ll_addr->sll_addr, ll_addr->sll_halen))}); | |
52 } | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 std::vector<DiscoveryNetworkId> GetDiscoveryNetworkIdList() { | |
58 std::vector<DiscoveryNetworkId> network_ids; | |
59 | |
60 struct ifaddrs* if_list; | |
61 if (getifaddrs(&if_list)) { | |
62 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); | |
63 return network_ids; | |
64 } | |
65 | |
66 GetDiscoveryNetworkIdListImpl(if_list, &network_ids); | |
67 freeifaddrs(if_list); | |
68 return network_ids; | |
69 } | |
OLD | NEW |