[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 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_ANDROID_NETWORK_LIBRARY_H_ |
| 6 | #define NET_ANDROID_NETWORK_LIBRARY_H_ |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 7 | |
Stefano Duo | b385bfc0 | 2022-02-04 14:19:08 | [diff] [blame] | 8 | #include <android/multinetwork.h> |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 9 | #include <jni.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 10 | #include <stddef.h> |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 11 | #include <stdint.h> |
Paul Jensen | ec3c1112 | 2017-11-28 16:48:38 | [diff] [blame] | 12 | #include <sys/types.h> |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Eric Orth | f18db28c9 | 2021-02-09 01:54:35 | [diff] [blame] | 17 | #include "base/callback.h" |
David Benjamin | a63f1d1 | 2018-09-06 17:27:17 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
[email protected] | 71f4b27 | 2013-02-13 19:13:49 | [diff] [blame] | 19 | #include "net/android/cert_verify_result_android.h" |
pauljensen | f3ef7a58 | 2017-01-10 16:14:50 | [diff] [blame] | 20 | #include "net/base/ip_endpoint.h" |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 21 | #include "net/base/mime_util.h" |
[email protected] | be363b2 | 2012-11-01 17:38:47 | [diff] [blame] | 22 | #include "net/base/net_export.h" |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 23 | #include "net/base/network_handle.h" |
Paul Jensen | ec3c1112 | 2017-11-28 16:48:38 | [diff] [blame] | 24 | #include "net/socket/socket_descriptor.h" |
Lei Zhang | 0effbd7d | 2021-05-18 18:41:36 | [diff] [blame] | 25 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 26 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 27 | namespace net::android { |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 28 | |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 29 | // |cert_chain| is DER encoded chain of certificates, with the server's own |
| 30 | // certificate listed first. |
[email protected] | b47feba | 2012-04-24 01:34:41 | [diff] [blame] | 31 | // |auth_type| is as per the Java X509Certificate.checkServerTrusted method. |
[email protected] | 23073f9 | 2014-01-17 22:52:17 | [diff] [blame] | 32 | void VerifyX509CertChain(const std::vector<std::string>& cert_chain, |
David Benjamin | a63f1d1 | 2018-09-06 17:27:17 | [diff] [blame] | 33 | base::StringPiece auth_type, |
| 34 | base::StringPiece host, |
[email protected] | 23073f9 | 2014-01-17 22:52:17 | [diff] [blame] | 35 | CertVerifyStatusAndroid* status, |
| 36 | bool* is_issued_by_known_root, |
| 37 | std::vector<std::string>* verified_chain); |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 38 | |
[email protected] | 87ccb99 | 2012-12-12 01:31:55 | [diff] [blame] | 39 | // Adds a certificate as a root trust certificate to the trust manager. |
| 40 | // |cert| is DER encoded certificate, |len| is its length in bytes. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 41 | void AddTestRootCertificate(const uint8_t* cert, size_t len); |
[email protected] | 87ccb99 | 2012-12-12 01:31:55 | [diff] [blame] | 42 | |
| 43 | // Removes all root certificates added by |AddTestRootCertificate| calls. |
| 44 | void ClearTestRootCertificates(); |
| 45 | |
mgersh | d21d6d14 | 2016-12-14 23:06:36 | [diff] [blame] | 46 | // Returns true if cleartext traffic to |host| is allowed by the app. Always |
| 47 | // true on L and older. |
| 48 | bool IsCleartextPermitted(const std::string& host); |
| 49 | |
[email protected] | c470e1b | 2012-06-15 07:46:39 | [diff] [blame] | 50 | // Returns true if it can determine that only loopback addresses are configured. |
| 51 | // i.e. if only 127.0.0.1 and ::1 are routable. |
| 52 | // Also returns false if it cannot determine this. |
| 53 | bool HaveOnlyLoopbackAddresses(); |
| 54 | |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 55 | // Get the mime type (if any) that is associated with the file extension. |
| 56 | // Returns true if a corresponding mime type exists. |
| 57 | bool GetMimeTypeFromExtension(const std::string& extension, |
| 58 | std::string* result); |
| 59 | |
[email protected] | 74d9c524 | 2014-05-01 05:10:03 | [diff] [blame] | 60 | // Returns MCC+MNC (mobile country code + mobile network code) as |
Simon Pelchat | 1a7675a1 | 2021-11-04 22:00:39 | [diff] [blame] | 61 | // the numeric name of the current registered operator. This function |
| 62 | // potentially blocks the thread, so use with care. |
[email protected] | 74d9c524 | 2014-05-01 05:10:03 | [diff] [blame] | 63 | NET_EXPORT std::string GetTelephonyNetworkOperator(); |
| 64 | |
tbansal | a2e9f20c | 2015-09-03 20:05:08 | [diff] [blame] | 65 | // Returns true if the device is roaming on the currently active network. When |
| 66 | // true, it suggests that use of data may incur extra costs. |
| 67 | NET_EXPORT bool GetIsRoaming(); |
| 68 | |
tbansal | a7581d8 | 2016-11-24 18:00:33 | [diff] [blame] | 69 | // Returns true if the system's captive portal probe was blocked for the current |
| 70 | // default data network. The method will return false if the captive portal |
| 71 | // probe was not blocked, the login process to the captive portal has been |
| 72 | // successfully completed, or if the captive portal status can't be determined. |
| 73 | // Requires ACCESS_NETWORK_STATE permission. Only available on Android |
| 74 | // Marshmallow and later versions. Returns false on earlier versions. |
| 75 | NET_EXPORT bool GetIsCaptivePortal(); |
| 76 | |
Tarun Bansal | ed90e4518 | 2018-04-06 03:07:17 | [diff] [blame] | 77 | // Gets the SSID of the currently associated WiFi access point if there is one, |
| 78 | // and it is available. SSID may not be available if the app does not have |
| 79 | // permissions to access it. On Android M+, the app accessing SSID needs to have |
| 80 | // ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. If there is no WiFi access |
| 81 | // point or its SSID is unavailable, an empty string is returned. |
tbansal | c5a1c91 | 2016-07-01 22:11:56 | [diff] [blame] | 82 | NET_EXPORT_PRIVATE std::string GetWifiSSID(); |
| 83 | |
Yoichi Osato | 1f347a7 | 2022-05-16 06:43:27 | [diff] [blame] | 84 | // Call WifiManager.setWifiEnabled. |
| 85 | NET_EXPORT_PRIVATE void SetWifiEnabledForTesting(bool enabled); |
| 86 | |
Tarun Bansal | da8b4f7c | 2019-05-13 16:49:28 | [diff] [blame] | 87 | // Returns the signal strength level (between 0 and 4, both inclusive) of the |
| 88 | // currently registered Wifi connection. If the value is unavailable, an |
| 89 | // empty value is returned. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 90 | NET_EXPORT_PRIVATE absl::optional<int32_t> GetWifiSignalLevel(); |
Tarun Bansal | da8b4f7c | 2019-05-13 16:49:28 | [diff] [blame] | 91 | |
Stefano Duo | c138b69 | 2022-04-05 15:56:43 | [diff] [blame] | 92 | // Gets the DNS servers for the current default network and puts them in |
| 93 | // `dns_servers`. Sets `dns_over_tls_active` and `dns_over_tls_hostname` based |
| 94 | // on the private DNS settings. `dns_over_tls_hostname` will only be non-empty |
| 95 | // if `dns_over_tls_active` is true. |
pauljensen | f3ef7a58 | 2017-01-10 16:14:50 | [diff] [blame] | 96 | // Only callable on Marshmallow and newer releases. |
Eric Orth | f7ebdde | 2021-01-08 22:48:14 | [diff] [blame] | 97 | // Returns false when a valid server config could not be read. |
Stefano Duo | c138b69 | 2022-04-05 15:56:43 | [diff] [blame] | 98 | NET_EXPORT_PRIVATE bool GetCurrentDnsServers( |
Eric Orth | 3ee77d78 | 2021-02-09 21:08:09 | [diff] [blame] | 99 | std::vector<IPEndPoint>* dns_servers, |
| 100 | bool* dns_over_tls_active, |
| 101 | std::string* dns_over_tls_hostname, |
| 102 | std::vector<std::string>* search_suffixes); |
Eric Orth | f18db28c9 | 2021-02-09 01:54:35 | [diff] [blame] | 103 | using DnsServerGetter = |
| 104 | base::RepeatingCallback<bool(std::vector<IPEndPoint>* dns_servers, |
| 105 | bool* dns_over_tls_active, |
Eric Orth | 3ee77d78 | 2021-02-09 21:08:09 | [diff] [blame] | 106 | std::string* dns_over_tls_hostname, |
| 107 | std::vector<std::string>* search_suffixes)>; |
pauljensen | f3ef7a58 | 2017-01-10 16:14:50 | [diff] [blame] | 108 | |
Stefano Duo | c138b69 | 2022-04-05 15:56:43 | [diff] [blame] | 109 | // Works as GetCurrentDnsServers but gets info specific to `network` instead |
| 110 | // of the current default network. |
| 111 | // Only callable on Pie and newer releases. |
| 112 | // Returns false when a valid server config could not be read. |
| 113 | NET_EXPORT_PRIVATE bool GetDnsServersForNetwork( |
| 114 | std::vector<IPEndPoint>* dns_servers, |
| 115 | bool* dns_over_tls_active, |
| 116 | std::string* dns_over_tls_hostname, |
| 117 | std::vector<std::string>* search_suffixes, |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 118 | handles::NetworkHandle network); |
Stefano Duo | c138b69 | 2022-04-05 15:56:43 | [diff] [blame] | 119 | |
Ken Rockot | 16610e4 | 2020-07-08 05:05:36 | [diff] [blame] | 120 | // Reports to the framework that the current default network appears to have |
| 121 | // connectivity issues. This may serve as a signal for the OS to consider |
| 122 | // switching to a different default network. Returns |true| if successfully |
| 123 | // reported to the OS, or |false| if not supported. |
| 124 | NET_EXPORT_PRIVATE bool ReportBadDefaultNetwork(); |
| 125 | |
Paul Jensen | ec3c1112 | 2017-11-28 16:48:38 | [diff] [blame] | 126 | // Apply TrafficStats tag |tag| and UID |uid| to |socket|. Future network |
| 127 | // traffic used by |socket| will be attributed to |uid| and |tag|. |
| 128 | NET_EXPORT_PRIVATE void TagSocket(SocketDescriptor socket, |
| 129 | uid_t uid, |
| 130 | int32_t tag); |
| 131 | |
Stefano Duo | 157e10c2 | 2022-01-25 12:58:34 | [diff] [blame] | 132 | // Binds this socket to `network`. All data traffic on the socket will be sent |
| 133 | // and received via `network`. This call will fail if `network` has |
| 134 | // disconnected. Communication using this socket will fail if `network` |
| 135 | // disconnects. |
| 136 | // Returns a net error code. |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 137 | NET_EXPORT_PRIVATE int BindToNetwork(SocketDescriptor socket, |
| 138 | handles::NetworkHandle network); |
Stefano Duo | 157e10c2 | 2022-01-25 12:58:34 | [diff] [blame] | 139 | |
Stefano Duo | b385bfc0 | 2022-02-04 14:19:08 | [diff] [blame] | 140 | // Perform hostname resolution via the DNS servers associated with `network`. |
| 141 | // All arguments are used identically as those passed to Android NDK API |
| 142 | // android_getaddrinfofornetwork: |
| 143 | // https://developer.android.com/ndk/reference/group/networking#group___networking_1ga0ae9e15612e6411855e295476a98ceee |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 144 | NET_EXPORT_PRIVATE int GetAddrInfoForNetwork(handles::NetworkHandle network, |
| 145 | const char* node, |
| 146 | const char* service, |
| 147 | const struct addrinfo* hints, |
| 148 | struct addrinfo** res); |
Stefano Duo | b385bfc0 | 2022-02-04 14:19:08 | [diff] [blame] | 149 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 150 | } // namespace net::android |
[email protected] | 6dbdaa8 | 2011-08-11 16:05:56 | [diff] [blame] | 151 | |
| 152 | #endif // NET_ANDROID_NETWORK_LIBRARY_H_ |