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..23061c90c946c4d90a344df07a36d1c083901030 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/discovery/discovery_network_list_posix.cc |
| @@ -0,0 +1,73 @@ |
| +// 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 GetDiscoveryNetworkInfoListImpl( |
| + const struct ifaddrs* if_list, |
| + std::vector<DiscoveryNetworkInfo>* network_info_list) { |
| + std::string ssid; |
| + for (; if_list != NULL; if_list = if_list->ifa_next) { |
| + if ((if_list->ifa_flags & IFF_RUNNING) == 0 || |
| + (if_list->ifa_flags & IFF_UP) == 0) { |
| + continue; |
| + } |
| + |
| + const struct sockaddr* addr = if_list->ifa_addr; |
| + std::string name(if_list->ifa_name); |
| + 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.
|
| + 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) { |
|
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.
|
| + continue; |
| + } |
| + |
| + if (MaybeGetWifiSSID(name, &ssid)) { |
| + network_info_list->push_back({name, ssid}); |
| + continue; |
| + } |
| + |
| + if (ll_addr->sll_halen == 0) { |
| + continue; |
| + } |
| + |
| + network_info_list->push_back( |
| + {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)}); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() { |
| + std::vector<DiscoveryNetworkInfo> network_ids; |
| + |
| + struct ifaddrs* if_list; |
| + if (getifaddrs(&if_list)) { |
| + DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno); |
| + return network_ids; |
| + } |
| + |
| + GetDiscoveryNetworkInfoListImpl(if_list, &network_ids); |
| + freeifaddrs(if_list); |
| + return network_ids; |
| +} |