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 "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" | |
18 #include "net/base/net_errors.h" | |
19 | |
20 namespace { | |
21 | |
22 void GetDiscoveryNetworkInfoListImpl( | |
23 const struct ifaddrs* if_list, | |
24 std::vector<DiscoveryNetworkInfo>* network_info_list) { | |
25 std::string ssid; | |
26 for (; if_list != NULL; if_list = if_list->ifa_next) { | |
27 if ((if_list->ifa_flags & IFF_RUNNING) == 0 || | |
28 (if_list->ifa_flags & IFF_UP) == 0) { | |
29 continue; | |
30 } | |
31 | |
32 const struct sockaddr* addr = if_list->ifa_addr; | |
33 std::string name(if_list->ifa_name); | |
34 if (name.size() == 0 || addr->sa_family != AF_PACKET) { | |
imcheng
2017/05/26 23:49:01
nit: Not a big deal, but seems you can check sa_fa
btolsch
2017/05/30 09:54:30
Done.
| |
35 continue; | |
36 } | |
37 | |
38 // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET. | |
39 const struct sockaddr_ll* ll_addr = | |
40 reinterpret_cast<const struct sockaddr_ll*>(addr); | |
41 if (ll_addr->sll_hatype != ARPHRD_ETHER) { | |
imcheng
2017/05/26 23:49:01
Can you add a comment here that ARPHRD_ETHER is us
btolsch
2017/05/30 09:54:30
Done.
| |
42 continue; | |
43 } | |
44 | |
45 if (MaybeGetWifiSSID(name, &ssid)) { | |
46 network_info_list->push_back({name, ssid}); | |
47 continue; | |
48 } | |
49 | |
50 if (ll_addr->sll_halen == 0) { | |
51 continue; | |
52 } | |
53 | |
54 network_info_list->push_back( | |
55 {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)}); | |
56 } | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { | |
62 std::vector<DiscoveryNetworkInfo> network_ids; | |
63 | |
64 struct ifaddrs* if_list; | |
65 if (getifaddrs(&if_list)) { | |
66 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); | |
67 return network_ids; | |
68 } | |
69 | |
70 GetDiscoveryNetworkInfoListImpl(if_list, &network_ids); | |
71 freeifaddrs(if_list); | |
72 return network_ids; | |
73 } | |
OLD | NEW |