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