Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: chrome/browser/media/router/discovery/discovery_network_list_posix.cc

Issue 2750453002: Add DiscoveryNetworkMonitor implementation (Closed)
Patch Set: Respond to imcheng's comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <sys/socket.h>
13 #include <sys/types.h>
14
15 #include <algorithm>
16
17 #include "base/logging.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "chrome/browser/media/router/discovery/discovery_network_list_wifi.h"
20 #include "net/base/net_errors.h"
21
22 namespace {
23
24 void GetDiscoveryNetworkInfoListImpl(
25 const struct ifaddrs* if_list,
26 std::vector<DiscoveryNetworkInfo>* network_info_list) {
27 std::string ssid;
28 for (; if_list != NULL; if_list = if_list->ifa_next) {
29 if ((if_list->ifa_flags & IFF_RUNNING) == 0 ||
30 (if_list->ifa_flags & IFF_UP) == 0) {
31 continue;
32 }
33
34 const struct sockaddr* addr = if_list->ifa_addr;
35 if (addr->sa_family != AF_PACKET) {
36 continue;
37 }
38 std::string name(if_list->ifa_name);
39 if (name.size() == 0) {
40 continue;
41 }
42
43 // |addr| will always be sockaddr_ll when |sa_family| == AF_PACKET.
44 const struct sockaddr_ll* ll_addr =
45 reinterpret_cast<const struct sockaddr_ll*>(addr);
46 // ARPHRD_ETHER is used to test for Ethernet, as in IEEE 802.3 MAC protocol.
47 // This spec is used by both wired Ethernet and wireless (e.g. 802.11).
48 if (ll_addr->sll_hatype != ARPHRD_ETHER) {
49 continue;
50 }
51
52 if (MaybeGetWifiSSID(name, &ssid)) {
53 network_info_list->push_back({name, ssid});
54 continue;
55 }
56
57 if (ll_addr->sll_halen == 0) {
58 continue;
59 }
60
61 network_info_list->push_back(
62 {name, base::HexEncode(ll_addr->sll_addr, ll_addr->sll_halen)});
63 }
64 }
65
66 } // namespace
67
68 std::vector<DiscoveryNetworkInfo> GetDiscoveryNetworkInfoList() {
69 std::vector<DiscoveryNetworkInfo> network_ids;
70
71 struct ifaddrs* if_list;
72 if (getifaddrs(&if_list)) {
73 DVLOG(2) << "getifaddrs() error: " << net::ErrorToString(errno);
74 return network_ids;
75 }
76
77 GetDiscoveryNetworkInfoListImpl(if_list, &network_ids);
78 std::stable_sort(
79 network_ids.begin(), network_ids.end(),
80 [](const DiscoveryNetworkInfo& info1, const DiscoveryNetworkInfo& info2) {
81 return info1.network_id < info2.network_id;
82 });
83 freeifaddrs(if_list);
84 return network_ids;
85 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698