libnetd_client: support hooking sendto/sendmsg/sendmmsg too.

Samsung has asked for this for KNOX.

Test: treehugger
Change-Id: Iffaace9f8cb265ce8c911472989c9829cbf91a42
diff --git a/libc/Android.bp b/libc/Android.bp
index 0aa6aa0..7b817c6 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1013,7 +1013,6 @@
         "bionic/__libc_current_sigrtmin.cpp",
         "bionic/abort.cpp",
         "bionic/accept.cpp",
-        "bionic/accept4.cpp",
         "bionic/access.cpp",
         "bionic/arpa_inet.cpp",
         "bionic/assert.cpp",
@@ -1034,7 +1033,6 @@
         "bionic/clock_getcpuclockid.cpp",
         "bionic/clock_nanosleep.cpp",
         "bionic/clone.cpp",
-        "bionic/connect.cpp",
         "bionic/ctype.cpp",
         "bionic/dirent.cpp",
         "bionic/dup2.cpp",
@@ -1127,7 +1125,6 @@
         "bionic/sigaction.cpp",
         "bionic/signal.cpp",
         "bionic/sigprocmask.cpp",
-        "bionic/socket.cpp",
         "bionic/spawn.cpp",
         "bionic/stat.cpp",
         "bionic/statvfs.cpp",
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index e2ea065..0d4d53e 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -246,15 +246,15 @@
 int           __accept4:accept4(int, struct sockaddr*, socklen_t*, int)  arm,arm64,mips,mips64,x86_64
 int           getsockname(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           getpeername(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
-int           sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  arm,arm64,mips,mips64,x86_64
+int           __sendto:sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  arm,arm64,mips,mips64,x86_64
 int           recvfrom(int, void*, size_t, unsigned int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           shutdown(int, int)  arm,arm64,mips,mips64,x86_64
 int           setsockopt(int, int, int, const void*, socklen_t)  arm,arm64,mips,mips64,x86_64
 int           getsockopt(int, int, int, void*, socklen_t*)    arm,arm64,mips,mips64,x86_64
 ssize_t       recvmsg(int, struct msghdr*, unsigned int)   arm,arm64,mips,mips64,x86_64
-ssize_t       sendmsg(int, const struct msghdr*, unsigned int)  arm,arm64,mips,mips64,x86_64
+ssize_t       __sendmsg:sendmsg(int, const struct msghdr*, unsigned int)  arm,arm64,mips,mips64,x86_64
 int           recvmmsg(int, struct mmsghdr*, unsigned int, int, const struct timespec*)   arm,arm64,mips,mips64,x86_64
-int           sendmmsg(int, struct mmsghdr*, unsigned int, int)   arm,arm64,mips,mips64,x86_64
+int           __sendmmsg:sendmmsg(int, struct mmsghdr*, unsigned int, int)   arm,arm64,mips,mips64,x86_64
 
 # sockets for x86. These are done as an "indexed" call to socketcall syscall.
 int           __socket:socketcall:1(int, int, int) x86
@@ -264,16 +264,16 @@
 int           getsockname:socketcall:6(int, struct sockaddr*, socklen_t*)  x86
 int           getpeername:socketcall:7(int, struct sockaddr*, socklen_t*)  x86
 int           socketpair:socketcall:8(int, int, int, int*)    x86
-int           sendto:socketcall:11(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  x86
+int           __sendto:socketcall:11(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  x86
 int           recvfrom:socketcall:12(int, void*, size_t, unsigned int, struct sockaddr*, socklen_t*)  x86
 int           shutdown:socketcall:13(int, int)  x86
 int           setsockopt:socketcall:14(int, int, int, const void*, socklen_t)  x86
 int           getsockopt:socketcall:15(int, int, int, void*, socklen_t*)    x86
-int           sendmsg:socketcall:16(int, const struct msghdr*, unsigned int)  x86
+int           __sendmsg:socketcall:16(int, const struct msghdr*, unsigned int)  x86
 int           recvmsg:socketcall:17(int, struct msghdr*, unsigned int)   x86
 int           __accept4:socketcall:18(int, struct sockaddr*, socklen_t*, int)  x86
 int           recvmmsg:socketcall:19(int, struct mmsghdr*, unsigned int, int, const struct timespec*)   x86
-int           sendmmsg:socketcall:20(int, struct mmsghdr*, unsigned int, int)   x86
+int           __sendmmsg:socketcall:20(int, struct mmsghdr*, unsigned int, int)   x86
 
 # scheduler & real-time
 int sched_setscheduler(pid_t pid, int policy, const struct sched_param* param)  all
diff --git a/libc/bionic/NetdClient.cpp b/libc/bionic/NetdClient.cpp
index 87eec27..d9debbf 100644
--- a/libc/bionic/NetdClient.cpp
+++ b/libc/bionic/NetdClient.cpp
@@ -31,7 +31,7 @@
 static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) {
     typedef void (*InitFunctionType)(FunctionType*);
     InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol));
-    if (initFunction != NULL) {
+    if (initFunction != nullptr) {
         initFunction(function);
     }
 }
@@ -45,20 +45,23 @@
         return;
     }
 
-    void* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW);
-    if (netdClientHandle == NULL) {
+    void* handle = dlopen("libnetd_client.so", RTLD_NOW);
+    if (handle == nullptr) {
         // If the library is not available, it's not an error. We'll just use
         // default implementations of functions that it would've overridden.
         return;
     }
-    netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
-                           &__netdClientDispatch.accept4);
-    netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
-                           &__netdClientDispatch.connect);
-    netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
+
+    netdClientInitFunction(handle, "netdClientInitAccept4", &__netdClientDispatch.accept4);
+    netdClientInitFunction(handle, "netdClientInitConnect", &__netdClientDispatch.connect);
+    netdClientInitFunction(handle, "netdClientInitSendmmsg", &__netdClientDispatch.sendmmsg);
+    netdClientInitFunction(handle, "netdClientInitSendmsg", &__netdClientDispatch.sendmsg);
+    netdClientInitFunction(handle, "netdClientInitSendto", &__netdClientDispatch.sendto);
+    netdClientInitFunction(handle, "netdClientInitSocket", &__netdClientDispatch.socket);
+
+    netdClientInitFunction(handle, "netdClientInitNetIdForResolv",
                            &__netdClientDispatch.netIdForResolv);
-    netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
-    netdClientInitFunction(netdClientHandle, "netdClientInitDnsOpenProxy",
+    netdClientInitFunction(handle, "netdClientInitDnsOpenProxy",
                            &__netdClientDispatch.dnsOpenProxy);
 }
 
diff --git a/libc/bionic/NetdClientDispatch.cpp b/libc/bionic/NetdClientDispatch.cpp
index a873173..463ef36 100644
--- a/libc/bionic/NetdClientDispatch.cpp
+++ b/libc/bionic/NetdClientDispatch.cpp
@@ -16,6 +16,8 @@
 
 #include "private/NetdClientDispatch.h"
 
+#include <sys/socket.h>
+
 #ifdef __i386__
 #define __socketcall __attribute__((__cdecl__))
 #else
@@ -24,6 +26,9 @@
 
 extern "C" __socketcall int __accept4(int, sockaddr*, socklen_t*, int);
 extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t);
+extern "C" __socketcall int __sendmmsg(int, const mmsghdr*, unsigned int, int);
+extern "C" __socketcall ssize_t __sendmsg(int, const msghdr*, unsigned int);
+extern "C" __socketcall int __sendto(int, const void*, size_t, int, const sockaddr*, socklen_t);
 extern "C" __socketcall int __socket(int, int, int);
 
 static unsigned fallBackNetIdForResolv(unsigned netId) {
@@ -39,7 +44,35 @@
 __LIBC_HIDDEN__ NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
     __accept4,
     __connect,
+    __sendmmsg,
+    __sendmsg,
+    __sendto,
     __socket,
     fallBackNetIdForResolv,
     fallBackDnsOpenProxy,
 };
+
+int accept4(int fd, sockaddr* addr, socklen_t* addr_length, int flags) {
+    return __netdClientDispatch.accept4(fd, addr, addr_length, flags);
+}
+
+int connect(int fd, const sockaddr* addr, socklen_t addr_length) {
+    return __netdClientDispatch.connect(fd, addr, addr_length);
+}
+
+int sendmmsg(int fd, const struct mmsghdr* msgs, unsigned int msg_count, int flags) {
+    return __netdClientDispatch.sendmmsg(fd, msgs, msg_count, flags);
+}
+
+ssize_t sendmsg(int fd, const struct msghdr* msg, int flags) {
+    return __netdClientDispatch.sendmsg(fd, msg, flags);
+}
+
+ssize_t sendto(int fd, const void* buf, size_t n, int flags,
+               const struct sockaddr* dst_addr, socklen_t dst_addr_length) {
+    return __netdClientDispatch.sendto(fd, buf, n, flags, dst_addr, dst_addr_length);
+}
+
+int socket(int domain, int type, int protocol) {
+    return __netdClientDispatch.socket(domain, type, protocol);
+}
diff --git a/libc/bionic/accept4.cpp b/libc/bionic/accept4.cpp
deleted file mode 100644
index 9f58dc1..0000000
--- a/libc/bionic/accept4.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "private/NetdClientDispatch.h"
-
-#include <sys/socket.h>
-
-int accept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags) {
-    return __netdClientDispatch.accept4(sockfd, addr, addrlen, flags);
-}
diff --git a/libc/bionic/connect.cpp b/libc/bionic/connect.cpp
deleted file mode 100644
index 1673f4a..0000000
--- a/libc/bionic/connect.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "private/NetdClientDispatch.h"
-
-#include <sys/socket.h>
-
-int connect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
-    return __netdClientDispatch.connect(sockfd, addr, addrlen);
-}
diff --git a/libc/bionic/socket.cpp b/libc/bionic/socket.cpp
deleted file mode 100644
index 2f9e145..0000000
--- a/libc/bionic/socket.cpp
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "private/NetdClientDispatch.h"
-
-#include <sys/socket.h>
-
-int socket(int domain, int type, int protocol) {
-    return __netdClientDispatch.socket(domain, type, protocol);
-}
diff --git a/libc/private/NetdClientDispatch.h b/libc/private/NetdClientDispatch.h
index 20e7f25..7ebbe0c 100644
--- a/libc/private/NetdClientDispatch.h
+++ b/libc/private/NetdClientDispatch.h
@@ -14,8 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef PRIVATE_NETD_CLIENT_DISPATCH_H
-#define PRIVATE_NETD_CLIENT_DISPATCH_H
+#pragma once
 
 #include <sys/cdefs.h>
 #include <sys/socket.h>
@@ -25,6 +24,9 @@
 struct NetdClientDispatch {
     int (*accept4)(int, struct sockaddr*, socklen_t*, int);
     int (*connect)(int, const struct sockaddr*, socklen_t);
+    int (*sendmmsg)(int, const struct mmsghdr*, unsigned int, int);
+    ssize_t (*sendmsg)(int, const struct msghdr*, unsigned int);
+    int (*sendto)(int, const void*, size_t, int, const struct sockaddr*, socklen_t);
     int (*socket)(int, int, int);
     unsigned (*netIdForResolv)(unsigned);
     int (*dnsOpenProxy)();
@@ -33,5 +35,3 @@
 extern __LIBC_HIDDEN__ struct NetdClientDispatch __netdClientDispatch;
 
 __END_DECLS
-
-#endif  // PRIVATE_NETD_CLIENT_DISPATCH_H