network_interfaces_getifaddrs.h 2.4 KB

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