Chromium Code Reviews| Index: chrome/browser/media/router/discovery/discovery_network_list_posix.cc |
| diff --git a/chrome/browser/media/router/discovery/discovery_network_list_posix.cc b/chrome/browser/media/router/discovery/discovery_network_list_posix.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..884ca7ef3ad173fc15b0680cfaaba3aa2b59c7b0 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/discovery/discovery_network_list_posix.cc |
| @@ -0,0 +1,68 @@ |
| +// 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/media/router/discovery/discovery_network_list.h" |
| + |
| +#include <ifaddrs.h> |
| +#include <net/if.h> |
| +#include <net/if_arp.h> |
| +#include <netinet/in.h> |
| +#include <netpacket/packet.h> |
| +#include <sys/socket.h> |
| +#include <sys/types.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h" |
| +#include "net/base/net_errors.h" |
| + |
| +namespace { |
| + |
| +void GetDiscoveryNetworkIdListImpl( |
| + const struct ifaddrs* if_list, |
| + std::vector<DiscoveryNetworkId>* network_ids) { |
| + std::string ssid; |
| + for (; if_list != NULL; if_list = if_list->ifa_next) { |
| + const struct sockaddr* addr = if_list->ifa_addr; |
| + std::string name(if_list->ifa_name); |
| + if (name.size() == 0 || addr->sa_family != AF_PACKET) { |
|
mark a. foltz
2017/05/04 23:16:39
As a future improvement, we could interrogate the
btolsch
2017/05/23 08:55:02
Acknowledged.
|
| + continue; |
| + } |
| + |
| + // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET. |
| + const struct sockaddr_ll* ll_addr = |
| + reinterpret_cast<const struct sockaddr_ll*>(addr); |
| + if (ll_addr->sll_hatype != ARPHRD_ETHER) { |
|
mark a. foltz
2017/05/04 23:16:39
It would be interesting to see if we could detect
btolsch
2017/05/23 08:55:02
AFAIK, another ioctl is required either way. We c
|
| + continue; |
| + } |
| + |
| + if (MaybeGetWifiSSID(name.data(), &ssid)) { |
|
mark a. foltz
2017/05/04 23:16:38
Just pass |name|.
btolsch
2017/05/23 08:55:02
Done.
|
| + network_ids->push_back({name, ssid}); |
| + continue; |
| + } |
| + |
| + if (ll_addr->sll_halen == 0) { |
| + continue; |
| + } |
| + |
| + network_ids->push_back( |
| + {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)}); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +std::vector<DiscoveryNetworkId> GetDiscoveryNetworkIdList() { |
| + std::vector<DiscoveryNetworkId> network_ids; |
| + |
| + struct ifaddrs* if_list; |
| + if (getifaddrs(&if_list)) { |
| + DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); |
| + return network_ids; |
| + } |
| + |
| + GetDiscoveryNetworkIdListImpl(if_list, &network_ids); |
| + freeifaddrs(if_list); |
| + return network_ids; |
| +} |