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