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/net/discovery_network_list.h" | |
6 | |
7 #include <ifaddrs.h> | |
8 #include <linux/if_packet.h> | |
9 #include <net/if.h> | |
10 #include <netinet/in.h> | |
11 #include <string.h> | |
12 #include <sys/ioctl.h> | |
13 #include <sys/socket.h> | |
14 #include <sys/types.h> | |
15 | |
16 #include "base/logging.h" | |
17 #include "chrome/browser/net/discovery_network_list_wifi.h" | |
18 | |
19 namespace { | |
20 | |
21 char ConvertNumberToAsciiHex(unsigned char value) { | |
22 if (value < 10) { | |
23 return '0' + value; | |
24 } | |
25 return 'A' + (value - 10); | |
26 } | |
27 | |
28 std::string GetMacAddressAsString(unsigned char* addr, unsigned char length) { | |
29 std::string address_string(length * 4 - 2, ':'); | |
30 | |
31 for (unsigned char i = 0; i < length; ++i) { | |
32 address_string[i * 4 + 0] = ConvertNumberToAsciiHex(addr[i] >> 4); | |
33 address_string[i * 4 + 1] = ConvertNumberToAsciiHex(addr[i] & 0xf); | |
34 } | |
35 | |
36 return address_string; | |
37 } | |
38 | |
39 void GetDiscoveryNetworkIdListImpl( | |
40 struct ifaddrs* if_list, | |
mark a. foltz
2017/03/18 19:58:21
const
btolsch
2017/04/03 10:16:35
Done.
| |
41 std::vector<DiscoveryNetworkId>* network_ids) { | |
42 std::string ssid; | |
43 for (; if_list != NULL; if_list = if_list->ifa_next) { | |
mark a. foltz
2017/03/18 19:58:21
IF these are guaranteed to be returned in the same
btolsch
2017/04/03 10:16:35
Acknowledged. I don't know whether that's guarant
| |
44 struct sockaddr* addr = if_list->ifa_addr; | |
mark a. foltz
2017/03/18 19:58:21
const
btolsch
2017/04/03 10:16:35
Done.
| |
45 std::string name(if_list->ifa_name); | |
46 if (name.size() == 0 || addr->sa_family != AF_PACKET) { | |
47 continue; | |
48 } | |
49 | |
50 if (MaybeGetWifiSSID(name.data(), &ssid)) { | |
mark a. foltz
2017/03/18 19:58:21
Is there any way to further filter on the interfac
btolsch
2017/04/03 10:16:35
We can also check the hardware address to see if i
| |
51 network_ids->push_back({name, ssid}); | |
52 continue; | |
53 } | |
54 | |
55 struct sockaddr_ll* ll_addr = reinterpret_cast<struct sockaddr_ll*>(addr); | |
mark a. foltz
2017/03/18 19:58:21
const
btolsch
2017/04/03 10:16:35
Done.
| |
56 if (ll_addr->sll_halen == 0) { | |
57 continue; | |
58 } | |
59 | |
60 network_ids->push_back( | |
61 {name, GetMacAddressAsString(ll_addr->sll_addr, ll_addr->sll_halen)}); | |
imcheng
2017/03/14 22:45:34
Can this be done with a combination of base::ToLow
btolsch
2017/04/03 10:16:35
Done.
| |
62 } | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 std::vector<DiscoveryNetworkId> GetDiscoveryNetworkIdList() { | |
68 std::vector<DiscoveryNetworkId> network_ids; | |
69 | |
70 struct ifaddrs* if_list; | |
71 if (getifaddrs(&if_list)) { | |
mark a. foltz
2017/03/18 19:58:21
If non-zero, DVLOG(2) the result (possibly using n
btolsch
2017/04/03 10:16:35
Done.
| |
72 return network_ids; | |
73 } | |
74 | |
75 GetDiscoveryNetworkIdListImpl(if_list, &network_ids); | |
76 | |
77 freeifaddrs(if_list); | |
78 | |
79 return network_ids; | |
80 } | |
OLD | NEW |