Index: chrome/browser/net/discovery_network_list_posix.cc |
diff --git a/chrome/browser/net/discovery_network_list_posix.cc b/chrome/browser/net/discovery_network_list_posix.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7ff465f86edbbd09eff540f663a289fd06cffac7 |
--- /dev/null |
+++ b/chrome/browser/net/discovery_network_list_posix.cc |
@@ -0,0 +1,80 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/net/discovery_network_list.h" |
+ |
+#include <ifaddrs.h> |
+#include <linux/if_packet.h> |
+#include <net/if.h> |
+#include <netinet/in.h> |
+#include <string.h> |
+#include <sys/ioctl.h> |
+#include <sys/socket.h> |
+#include <sys/types.h> |
+ |
+#include "base/logging.h" |
+#include "chrome/browser/net/discovery_network_list_wifi.h" |
+ |
+namespace { |
+ |
+char ConvertNumberToAsciiHex(unsigned char value) { |
+ if (value < 10) { |
+ return '0' + value; |
+ } |
+ return 'A' + (value - 10); |
+} |
+ |
+std::string GetMacAddressAsString(unsigned char* addr, unsigned char length) { |
+ std::string address_string(length * 4 - 2, ':'); |
+ |
+ for (unsigned char i = 0; i < length; ++i) { |
+ address_string[i * 4 + 0] = ConvertNumberToAsciiHex(addr[i] >> 4); |
+ address_string[i * 4 + 1] = ConvertNumberToAsciiHex(addr[i] & 0xf); |
+ } |
+ |
+ return address_string; |
+} |
+ |
+void GetDiscoveryNetworkIdListImpl( |
+ struct ifaddrs* if_list, |
mark a. foltz
2017/03/18 19:58:21
const
btolsch
2017/04/03 10:16:35
Done.
|
+ std::vector<DiscoveryNetworkId>* network_ids) { |
+ std::string ssid; |
+ 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
|
+ 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.
|
+ std::string name(if_list->ifa_name); |
+ if (name.size() == 0 || addr->sa_family != AF_PACKET) { |
+ continue; |
+ } |
+ |
+ 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
|
+ network_ids->push_back({name, ssid}); |
+ continue; |
+ } |
+ |
+ 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.
|
+ if (ll_addr->sll_halen == 0) { |
+ continue; |
+ } |
+ |
+ network_ids->push_back( |
+ {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.
|
+ } |
+} |
+ |
+} // namespace |
+ |
+std::vector<DiscoveryNetworkId> GetDiscoveryNetworkIdList() { |
+ std::vector<DiscoveryNetworkId> network_ids; |
+ |
+ struct ifaddrs* if_list; |
+ 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.
|
+ return network_ids; |
+ } |
+ |
+ GetDiscoveryNetworkIdListImpl(if_list, &network_ids); |
+ |
+ freeifaddrs(if_list); |
+ |
+ return network_ids; |
+} |