blob: 9597e9f1e1ab2dba76285c3778e02981dee1356b [file] [log] [blame]
[email protected]b47feba2012-04-24 01:34:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6dbdaa82011-08-11 16:05:562// 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]6dbdaa82011-08-11 16:05:567
Stefano Duob385bfc02022-02-04 14:19:088#include <android/multinetwork.h>
[email protected]6dbdaa82011-08-11 16:05:569#include <jni.h>
Avi Drissman13fc8932015-12-20 04:40:4610#include <stddef.h>
wtc69f8ea82015-06-04 00:08:1311#include <stdint.h>
Paul Jensenec3c11122017-11-28 16:48:3812#include <sys/types.h>
[email protected]6dbdaa82011-08-11 16:05:5613
14#include <string>
15#include <vector>
16
Eric Orthf18db28c92021-02-09 01:54:3517#include "base/callback.h"
David Benjamina63f1d12018-09-06 17:27:1718#include "base/strings/string_piece.h"
[email protected]71f4b272013-02-13 19:13:4919#include "net/android/cert_verify_result_android.h"
pauljensenf3ef7a582017-01-10 16:14:5020#include "net/base/ip_endpoint.h"
[email protected]3b455502012-12-11 18:22:5821#include "net/base/mime_util.h"
[email protected]be363b22012-11-01 17:38:4722#include "net/base/net_export.h"
Stefano Duo6527ed42022-07-29 09:25:4423#include "net/base/network_handle.h"
Paul Jensenec3c11122017-11-28 16:48:3824#include "net/socket/socket_descriptor.h"
Lei Zhang0effbd7d2021-05-18 18:41:3625#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]6dbdaa82011-08-11 16:05:5626
Tsuyoshi Horo4f516be2022-06-14 11:53:1327namespace net::android {
[email protected]6dbdaa82011-08-11 16:05:5628
[email protected]6dbdaa82011-08-11 16:05:5629// |cert_chain| is DER encoded chain of certificates, with the server's own
30// certificate listed first.
[email protected]b47feba2012-04-24 01:34:4131// |auth_type| is as per the Java X509Certificate.checkServerTrusted method.
[email protected]23073f92014-01-17 22:52:1732void VerifyX509CertChain(const std::vector<std::string>& cert_chain,
David Benjamina63f1d12018-09-06 17:27:1733 base::StringPiece auth_type,
34 base::StringPiece host,
[email protected]23073f92014-01-17 22:52:1735 CertVerifyStatusAndroid* status,
36 bool* is_issued_by_known_root,
37 std::vector<std::string>* verified_chain);
[email protected]6dbdaa82011-08-11 16:05:5638
[email protected]87ccb992012-12-12 01:31:5539// Adds a certificate as a root trust certificate to the trust manager.
40// |cert| is DER encoded certificate, |len| is its length in bytes.
wtc69f8ea82015-06-04 00:08:1341void AddTestRootCertificate(const uint8_t* cert, size_t len);
[email protected]87ccb992012-12-12 01:31:5542
43// Removes all root certificates added by |AddTestRootCertificate| calls.
44void ClearTestRootCertificates();
45
mgershd21d6d142016-12-14 23:06:3646// Returns true if cleartext traffic to |host| is allowed by the app. Always
47// true on L and older.
48bool IsCleartextPermitted(const std::string& host);
49
[email protected]c470e1b2012-06-15 07:46:3950// 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.
53bool HaveOnlyLoopbackAddresses();
54
[email protected]6dbdaa82011-08-11 16:05:5655// Get the mime type (if any) that is associated with the file extension.
56// Returns true if a corresponding mime type exists.
57bool GetMimeTypeFromExtension(const std::string& extension,
58 std::string* result);
59
[email protected]74d9c5242014-05-01 05:10:0360// Returns MCC+MNC (mobile country code + mobile network code) as
Simon Pelchat1a7675a12021-11-04 22:00:3961// the numeric name of the current registered operator. This function
62// potentially blocks the thread, so use with care.
[email protected]74d9c5242014-05-01 05:10:0363NET_EXPORT std::string GetTelephonyNetworkOperator();
64
tbansala2e9f20c2015-09-03 20:05:0865// 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.
67NET_EXPORT bool GetIsRoaming();
68
tbansala7581d82016-11-24 18:00:3369// 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.
75NET_EXPORT bool GetIsCaptivePortal();
76
Tarun Bansaled90e45182018-04-06 03:07:1777// 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.
tbansalc5a1c912016-07-01 22:11:5682NET_EXPORT_PRIVATE std::string GetWifiSSID();
83
Yoichi Osato1f347a72022-05-16 06:43:2784// Call WifiManager.setWifiEnabled.
85NET_EXPORT_PRIVATE void SetWifiEnabledForTesting(bool enabled);
86
Tarun Bansalda8b4f7c2019-05-13 16:49:2887// 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 Bikineev068d2912021-05-15 20:43:5290NET_EXPORT_PRIVATE absl::optional<int32_t> GetWifiSignalLevel();
Tarun Bansalda8b4f7c2019-05-13 16:49:2891
Stefano Duoc138b692022-04-05 15:56:4392// 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.
pauljensenf3ef7a582017-01-10 16:14:5096// Only callable on Marshmallow and newer releases.
Eric Orthf7ebdde2021-01-08 22:48:1497// Returns false when a valid server config could not be read.
Stefano Duoc138b692022-04-05 15:56:4398NET_EXPORT_PRIVATE bool GetCurrentDnsServers(
Eric Orth3ee77d782021-02-09 21:08:0999 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 Orthf18db28c92021-02-09 01:54:35103using DnsServerGetter =
104 base::RepeatingCallback<bool(std::vector<IPEndPoint>* dns_servers,
105 bool* dns_over_tls_active,
Eric Orth3ee77d782021-02-09 21:08:09106 std::string* dns_over_tls_hostname,
107 std::vector<std::string>* search_suffixes)>;
pauljensenf3ef7a582017-01-10 16:14:50108
Stefano Duoc138b692022-04-05 15:56:43109// 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.
113NET_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 Duo6527ed42022-07-29 09:25:44118 handles::NetworkHandle network);
Stefano Duoc138b692022-04-05 15:56:43119
Ken Rockot16610e42020-07-08 05:05:36120// 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.
124NET_EXPORT_PRIVATE bool ReportBadDefaultNetwork();
125
Paul Jensenec3c11122017-11-28 16:48:38126// Apply TrafficStats tag |tag| and UID |uid| to |socket|. Future network
127// traffic used by |socket| will be attributed to |uid| and |tag|.
128NET_EXPORT_PRIVATE void TagSocket(SocketDescriptor socket,
129 uid_t uid,
130 int32_t tag);
131
Stefano Duo157e10c22022-01-25 12:58:34132// 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 Duo6527ed42022-07-29 09:25:44137NET_EXPORT_PRIVATE int BindToNetwork(SocketDescriptor socket,
138 handles::NetworkHandle network);
Stefano Duo157e10c22022-01-25 12:58:34139
Stefano Duob385bfc02022-02-04 14:19:08140// 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 Duo6527ed42022-07-29 09:25:44144NET_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 Duob385bfc02022-02-04 14:19:08149
Tsuyoshi Horo4f516be2022-06-14 11:53:13150} // namespace net::android
[email protected]6dbdaa82011-08-11 16:05:56151
152#endif // NET_ANDROID_NETWORK_LIBRARY_H_