• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
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 "build/build_config.h"
17 #include "net/base/net_export.h"
18 #include "net/base/network_interfaces.h"
19 
20 struct ifaddrs;
21 
22 namespace net::internal {
23 
24 class NET_EXPORT_PRIVATE IPAttributesGetter {
25  public:
26   IPAttributesGetter() = default;
27   IPAttributesGetter(const IPAttributesGetter&) = delete;
28   IPAttributesGetter& operator=(const IPAttributesGetter&) = delete;
29   virtual ~IPAttributesGetter() = default;
30   virtual bool IsInitialized() const = 0;
31 
32   // Returns false if the interface must be skipped. Otherwise sets |attributes|
33   // and returns true.
34   virtual bool GetAddressAttributes(const ifaddrs* if_addr,
35                                     int* attributes) = 0;
36 
37   // Returns interface type for the given interface.
38   virtual NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
39       const ifaddrs* if_addr) = 0;
40 };
41 
42 // Converts ifaddrs list returned by getifaddrs() to NetworkInterfaceList. Also
43 // filters the list interfaces according to |policy| (see
44 // HostAddressSelectionPolicy).
45 NET_EXPORT_PRIVATE bool IfaddrsToNetworkInterfaceList(
46     int policy,
47     const ifaddrs* interfaces,
48     IPAttributesGetter* ip_attributes_getter,
49     NetworkInterfaceList* networks);
50 
51 #if BUILDFLAG(IS_ANDROID)
52 // A version of GetNetworkList() that uses getifaddrs(). Only callable on
53 // Android N+ where getifaddrs() was available.
54 // Also, some devices are with buggy getifaddrs(). To work around,
55 // Use Chromium's own getifaddrs() implementation if
56 // use_alternative_getifaddrs is true.
57 bool GetNetworkListUsingGetifaddrs(NetworkInterfaceList* networks,
58                                    int policy,
59                                    bool use_alternative_getifaddrs);
60 #endif
61 
62 }  // namespace net::internal
63 
64 #endif  // NET_BASE_NETWORK_INTERFACES_GETIFADDRS_H_
65