1 // Copyright 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 #ifndef NET_BASE_NETWORK_INTERFACES_GETIFADDRS_H_
6 #define NET_BASE_NETWORK_INTERFACES_GETIFADDRS_H_
7 
8 // network_interfaces_getaddrs.cc implements GetNetworkList() using getifaddrs()
9 // API. It is a non-standard API, so not all POSIX systems implement it (e.g.
10 // it doesn't exist on Android). It is used on MacOS, iOS and Fuchsia. On Linux
11 // and Android interface is used to implement GetNetworkList(), see
12 // network_interfaces_linux.cc.
13 // This file defines IfaddrsToNetworkInterfaceList() so it can be called in
14 // unittests.
15 
16 #include <string>
17 
18 #include "build/build_config.h"
19 #include "net/base/net_export.h"
20 #include "net/base/network_interfaces.h"
21 
22 struct ifaddrs;
23 
24 namespace net {
25 namespace internal {
26 
27 class NET_EXPORT_PRIVATE IPAttributesGetter {
28  public:
29   IPAttributesGetter() = default;
30   IPAttributesGetter(const IPAttributesGetter&) = delete;
31   IPAttributesGetter& operator=(const IPAttributesGetter&) = delete;
32   virtual ~IPAttributesGetter() = default;
33   virtual bool IsInitialized() const = 0;
34 
35   // Returns false if the interface must be skipped. Otherwise sets |attributes|
36   // and returns true.
37   virtual bool GetAddressAttributes(const ifaddrs* if_addr,
38                                     int* attributes) = 0;
39 
40   // Returns interface type for the given interface.
41   virtual NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
42       const ifaddrs* if_addr) = 0;
43 };
44 
45 // Converts ifaddrs list returned by getifaddrs() to NetworkInterfaceList. Also
46 // filters the list interfaces according to |policy| (see
47 // HostAddressSelectionPolicy).
48 NET_EXPORT_PRIVATE bool IfaddrsToNetworkInterfaceList(
49     int policy,
50     const ifaddrs* interfaces,
51     IPAttributesGetter* ip_attributes_getter,
52     NetworkInterfaceList* networks);
53 
54 #if defined(OS_ANDROID)
55 // A version of GetNetworkList() that uses getifaddrs(). Only callable on
56 // Android N+ where getifaddrs() was available.
57 bool GetNetworkListUsingGetifaddrs(NetworkInterfaceList* networks, int policy);
58 #endif
59 
60 }  // namespace internal
61 }  // namespace net
62 
63 #endif  // NET_BASE_NETWORK_INTERFACES_GETIFADDRS_H_
64