network_library.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2012 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_ANDROID_NETWORK_LIBRARY_H_
  5. #define NET_ANDROID_NETWORK_LIBRARY_H_
  6. #include <android/multinetwork.h>
  7. #include <jni.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <sys/types.h>
  11. #include <string>
  12. #include <vector>
  13. #include "base/callback.h"
  14. #include "base/strings/string_piece.h"
  15. #include "net/android/cert_verify_result_android.h"
  16. #include "net/base/ip_endpoint.h"
  17. #include "net/base/mime_util.h"
  18. #include "net/base/net_export.h"
  19. #include "net/base/network_handle.h"
  20. #include "net/socket/socket_descriptor.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. namespace net::android {
  23. // |cert_chain| is DER encoded chain of certificates, with the server's own
  24. // certificate listed first.
  25. // |auth_type| is as per the Java X509Certificate.checkServerTrusted method.
  26. void VerifyX509CertChain(const std::vector<std::string>& cert_chain,
  27. base::StringPiece auth_type,
  28. base::StringPiece host,
  29. CertVerifyStatusAndroid* status,
  30. bool* is_issued_by_known_root,
  31. std::vector<std::string>* verified_chain);
  32. // Adds a certificate as a root trust certificate to the trust manager.
  33. // |cert| is DER encoded certificate, |len| is its length in bytes.
  34. void AddTestRootCertificate(const uint8_t* cert, size_t len);
  35. // Removes all root certificates added by |AddTestRootCertificate| calls.
  36. void ClearTestRootCertificates();
  37. // Returns true if cleartext traffic to |host| is allowed by the app. Always
  38. // true on L and older.
  39. bool IsCleartextPermitted(const std::string& host);
  40. // Returns true if it can determine that only loopback addresses are configured.
  41. // i.e. if only 127.0.0.1 and ::1 are routable.
  42. // Also returns false if it cannot determine this.
  43. bool HaveOnlyLoopbackAddresses();
  44. // Get the mime type (if any) that is associated with the file extension.
  45. // Returns true if a corresponding mime type exists.
  46. bool GetMimeTypeFromExtension(const std::string& extension,
  47. std::string* result);
  48. // Returns MCC+MNC (mobile country code + mobile network code) as
  49. // the numeric name of the current registered operator. This function
  50. // potentially blocks the thread, so use with care.
  51. NET_EXPORT std::string GetTelephonyNetworkOperator();
  52. // Returns true if the device is roaming on the currently active network. When
  53. // true, it suggests that use of data may incur extra costs.
  54. NET_EXPORT bool GetIsRoaming();
  55. // Returns true if the system's captive portal probe was blocked for the current
  56. // default data network. The method will return false if the captive portal
  57. // probe was not blocked, the login process to the captive portal has been
  58. // successfully completed, or if the captive portal status can't be determined.
  59. // Requires ACCESS_NETWORK_STATE permission. Only available on Android
  60. // Marshmallow and later versions. Returns false on earlier versions.
  61. NET_EXPORT bool GetIsCaptivePortal();
  62. // Gets the SSID of the currently associated WiFi access point if there is one,
  63. // and it is available. SSID may not be available if the app does not have
  64. // permissions to access it. On Android M+, the app accessing SSID needs to have
  65. // ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. If there is no WiFi access
  66. // point or its SSID is unavailable, an empty string is returned.
  67. NET_EXPORT_PRIVATE std::string GetWifiSSID();
  68. // Call WifiManager.setWifiEnabled.
  69. NET_EXPORT_PRIVATE void SetWifiEnabledForTesting(bool enabled);
  70. // Returns the signal strength level (between 0 and 4, both inclusive) of the
  71. // currently registered Wifi connection. If the value is unavailable, an
  72. // empty value is returned.
  73. NET_EXPORT_PRIVATE absl::optional<int32_t> GetWifiSignalLevel();
  74. // Gets the DNS servers for the current default network and puts them in
  75. // `dns_servers`. Sets `dns_over_tls_active` and `dns_over_tls_hostname` based
  76. // on the private DNS settings. `dns_over_tls_hostname` will only be non-empty
  77. // if `dns_over_tls_active` is true.
  78. // Only callable on Marshmallow and newer releases.
  79. // Returns false when a valid server config could not be read.
  80. NET_EXPORT_PRIVATE bool GetCurrentDnsServers(
  81. std::vector<IPEndPoint>* dns_servers,
  82. bool* dns_over_tls_active,
  83. std::string* dns_over_tls_hostname,
  84. std::vector<std::string>* search_suffixes);
  85. using DnsServerGetter =
  86. base::RepeatingCallback<bool(std::vector<IPEndPoint>* dns_servers,
  87. bool* dns_over_tls_active,
  88. std::string* dns_over_tls_hostname,
  89. std::vector<std::string>* search_suffixes)>;
  90. // Works as GetCurrentDnsServers but gets info specific to `network` instead
  91. // of the current default network.
  92. // Only callable on Pie and newer releases.
  93. // Returns false when a valid server config could not be read.
  94. NET_EXPORT_PRIVATE bool GetDnsServersForNetwork(
  95. std::vector<IPEndPoint>* dns_servers,
  96. bool* dns_over_tls_active,
  97. std::string* dns_over_tls_hostname,
  98. std::vector<std::string>* search_suffixes,
  99. handles::NetworkHandle network);
  100. // Reports to the framework that the current default network appears to have
  101. // connectivity issues. This may serve as a signal for the OS to consider
  102. // switching to a different default network. Returns |true| if successfully
  103. // reported to the OS, or |false| if not supported.
  104. NET_EXPORT_PRIVATE bool ReportBadDefaultNetwork();
  105. // Apply TrafficStats tag |tag| and UID |uid| to |socket|. Future network
  106. // traffic used by |socket| will be attributed to |uid| and |tag|.
  107. NET_EXPORT_PRIVATE void TagSocket(SocketDescriptor socket,
  108. uid_t uid,
  109. int32_t tag);
  110. // Binds this socket to `network`. All data traffic on the socket will be sent
  111. // and received via `network`. This call will fail if `network` has
  112. // disconnected. Communication using this socket will fail if `network`
  113. // disconnects.
  114. // Returns a net error code.
  115. NET_EXPORT_PRIVATE int BindToNetwork(SocketDescriptor socket,
  116. handles::NetworkHandle network);
  117. // Perform hostname resolution via the DNS servers associated with `network`.
  118. // All arguments are used identically as those passed to Android NDK API
  119. // android_getaddrinfofornetwork:
  120. // https://developer.android.com/ndk/reference/group/networking#group___networking_1ga0ae9e15612e6411855e295476a98ceee
  121. NET_EXPORT_PRIVATE int GetAddrInfoForNetwork(handles::NetworkHandle network,
  122. const char* node,
  123. const char* service,
  124. const struct addrinfo* hints,
  125. struct addrinfo** res);
  126. } // namespace net::android
  127. #endif // NET_ANDROID_NETWORK_LIBRARY_H_