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 <string.h> | |
13 #include <sys/ioctl.h> | |
14 #include <sys/socket.h> | |
15 #include <sys/types.h> | |
16 | |
17 #include "base/logging.h" | |
18 #include "base/strings/string_number_conversions.h" | |
19 #include "base/strings/string_util.h" | |
20 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" | |
21 #include "net/base/net_errors.h" | |
22 | |
23 namespace { | |
24 | |
25 void GetDiscoveryNetworkIdListImpl( | |
26 const struct ifaddrs* if_list, | |
27 std::vector<DiscoveryNetworkId>* network_ids) { | |
28 std::string ssid; | |
29 for (; if_list != NULL; if_list = if_list->ifa_next) { | |
30 const struct sockaddr* addr = if_list->ifa_addr; | |
31 std::string name(if_list->ifa_name); | |
32 if (name.size() == 0 || addr->sa_family != AF_PACKET) { | |
33 continue; | |
34 } | |
35 | |
36 if (addr->sa_family != ARPHRD_ETHER && | |
mark a. foltz
2017/04/18 20:37:00
Won't this always be AF_PACKET per line 32?
From
btolsch
2017/04/20 04:03:49
Yes, I messed up the field; it should be sll_hatyp
| |
37 MaybeGetWifiSSID(name.data(), &ssid)) { | |
38 network_ids->push_back({name, ssid}); | |
mark a. foltz
2017/04/18 20:37:00
Is MaybeGetWifiSSID guaranteed to return a result?
btolsch
2017/04/20 04:03:50
Yes, if it returns true then ssid will be non-empt
| |
39 continue; | |
40 } | |
41 | |
42 const struct sockaddr_ll* ll_addr = | |
43 reinterpret_cast<const struct sockaddr_ll*>(addr); | |
mark a. foltz
2017/04/18 20:37:00
Can you add a comment about why |addr| is guarante
btolsch
2017/04/20 04:03:49
Done.
| |
44 if (ll_addr->sll_halen == 0) { | |
45 continue; | |
46 } | |
47 | |
48 network_ids->push_back({name, base::ToLowerASCII(base::HexEncode( | |
49 ll_addr->sll_addr, ll_addr->sll_halen))}); | |
50 } | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 std::vector<DiscoveryNetworkId> GetDiscoveryNetworkIdList() { | |
56 std::vector<DiscoveryNetworkId> network_ids; | |
57 | |
58 struct ifaddrs* if_list; | |
59 if (getifaddrs(&if_list)) { | |
60 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); | |
61 return network_ids; | |
62 } | |
63 | |
64 GetDiscoveryNetworkIdListImpl(if_list, &network_ids); | |
65 | |
mark a. foltz
2017/04/18 20:37:00
Extra newline
btolsch
2017/04/20 04:03:49
Done.
| |
66 freeifaddrs(if_list); | |
67 | |
mark a. foltz
2017/04/18 20:37:00
Ditto
btolsch
2017/04/20 04:03:50
Done.
| |
68 return network_ids; | |
69 } | |
OLD | NEW |