libnetd_client: support hooking sendto/sendmsg/sendmmsg too.

Samsung has asked for this for KNOX.

Test: treehugger
Change-Id: Iffaace9f8cb265ce8c911472989c9829cbf91a42
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);
-}