summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ken Chen <cken@google.com> 2022-01-06 10:29:27 +0800
committer Ken Chen <cken@google.com> 2022-01-13 08:52:27 +0800
commita9b7f039c257c32947056a9bc0625adaaf9a427b (patch)
treece769d36dd81e7388d15d76d77dbb0e38e8d081c
parentf82a5a92033f3d5e6d86890ee2b11e185063b0cc (diff)
Add tagSocket/untagSocket NDK APIs
Provide native APIs to tag the various sockets used for network transfer, which helps apps tracking down data usage inside their app. We've had equivalent Java APIs since API level 14. https://developer.android.com/reference/android/net/TrafficStats.html#tagSocket(java.net.Socket) https://developer.android.com/reference/android/net/TrafficStats.html#untagSocket(java.net.Socket) Given that the Java APIs implementation are in native side, providing NDK directly will be easier for native users to use it. Bug: 29761778 Bug: 202086915 Test: Tests in change Ic6554d9b98cef9b41b7eae06315d284c574c0a45 Change-Id: I884522be1dc40e8e959684442b3d736b5dcfdf5c
-rw-r--r--include/android/multinetwork.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/include/android/multinetwork.h b/include/android/multinetwork.h
index 509ee0e49b..4c83a14728 100644
--- a/include/android/multinetwork.h
+++ b/include/android/multinetwork.h
@@ -216,6 +216,61 @@ int android_res_nresult(int fd,
*/
void android_res_cancel(int nsend_fd) __INTRODUCED_IN(29);
+/*
+ * Set the socket tag and owning UID for traffic statistics on the specified
+ * socket.
+ *
+ * Subsequent calls always replace any existing parameters. The socket tag and
+ * uid (if set) are kept when the socket is sent to another process using binder
+ * IPCs or other mechanisms such as UNIX socket fd passing. Any app can accept
+ * blame for future traffic performed on a socket originally created by another
+ * app by calling this method with its own UID (or calling
+ * android_tag_socket(int sockfd, int tag)). However, only apps holding the
+ * android.Manifest.permission#UPDATE_DEVICE_STATS permission may assign blame
+ * to another UIDs. If unset (default) the socket tag is 0, and the uid is the
+ * socket creator's uid.
+ *
+ * Returns 0 on success, or a negative POSIX error code (see errno.h) on
+ * failure.
+ *
+ * Available since API level 33.
+ */
+int android_tag_socket_with_uid(int sockfd, int tag, uid_t uid) __INTRODUCED_IN(33);
+
+/*
+ * Set the socket tag for traffic statistics on the specified socket.
+ *
+ * This function tags the socket with the caller's UID (accepting blame for
+ * future traffic performed on this socket) even if the socket was originally
+ * opened by another UID or was previously tagged by another UID. Subsequent
+ * calls always replace any existing parameters. The socket tag is kept when the
+ * socket is sent to another process using binder IPCs or other mechanisms such
+ * as UNIX socket fd passing.
+ *
+ * Returns 0 on success, or a negative POSIX error code (see errno.h) on
+ * failure.
+ *
+ * Available since API level 33.
+ */
+int android_tag_socket(int sockfd, int tag) __INTRODUCED_IN(33);
+
+/*
+ * Untag a network socket.
+ *
+ * Future traffic on this socket will no longer be associated with any
+ * previously configured tag and uid. If the socket was created by another UID
+ * or was previously tagged by another UID, calling this function will clear the
+ * statistics parameters, and thus the UID blamed for traffic on the socket will
+ * be the UID that originally created the socket, even if the socket was
+ * subsequently tagged by a different UID.
+ *
+ * Returns 0 on success, or a negative POSIX error code (see errno.h) on
+ * failure.
+ *
+ * Available since API level 33.
+ */
+int android_untag_socket(int sockfd) __INTRODUCED_IN(33);
+
__END_DECLS
#endif // ANDROID_MULTINETWORK_H