1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "private/NetdClientDispatch.h"
18 
19 #include <sys/socket.h>
20 
21 #include "private/bionic_fdtrack.h"
22 
23 #ifdef __i386__
24 #define __socketcall __attribute__((__cdecl__))
25 #else
26 #define __socketcall
27 #endif
28 
29 extern "C" __socketcall int __accept4(int, sockaddr*, socklen_t*, int);
30 extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t);
31 extern "C" __socketcall int __sendmmsg(int, const mmsghdr*, unsigned int, int);
32 extern "C" __socketcall ssize_t __sendmsg(int, const msghdr*, unsigned int);
33 extern "C" __socketcall int __sendto(int, const void*, size_t, int, const sockaddr*, socklen_t);
34 extern "C" __socketcall int __socket(int, int, int);
35 
fallBackNetIdForResolv(unsigned netId)36 static unsigned fallBackNetIdForResolv(unsigned netId) {
37     return netId;
38 }
39 
fallBackDnsOpenProxy()40 static int fallBackDnsOpenProxy() {
41     return -1;
42 }
43 
44 // This structure is modified only at startup (when libc.so is loaded) and never
45 // afterwards, so it's okay that it's read later at runtime without a lock.
46 __LIBC_HIDDEN__ NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
47     __accept4,
48     __connect,
49     __sendmmsg,
50     __sendmsg,
51     __sendto,
52     __socket,
53     fallBackNetIdForResolv,
54     fallBackDnsOpenProxy,
55 };
56 
accept4(int fd,sockaddr * addr,socklen_t * addr_length,int flags)57 int accept4(int fd, sockaddr* addr, socklen_t* addr_length, int flags) {
58   return FDTRACK_CREATE(__netdClientDispatch.accept4(fd, addr, addr_length, flags));
59 }
60 
connect(int fd,const sockaddr * addr,socklen_t addr_length)61 int connect(int fd, const sockaddr* addr, socklen_t addr_length) {
62     return __netdClientDispatch.connect(fd, addr, addr_length);
63 }
64 
sendmmsg(int fd,const struct mmsghdr * msgs,unsigned int msg_count,int flags)65 int sendmmsg(int fd, const struct mmsghdr* msgs, unsigned int msg_count, int flags) {
66     return __netdClientDispatch.sendmmsg(fd, msgs, msg_count, flags);
67 }
68 
sendmsg(int fd,const struct msghdr * msg,int flags)69 ssize_t sendmsg(int fd, const struct msghdr* msg, int flags) {
70     return __netdClientDispatch.sendmsg(fd, msg, flags);
71 }
72 
sendto(int fd,const void * buf,size_t n,int flags,const struct sockaddr * dst_addr,socklen_t dst_addr_length)73 ssize_t sendto(int fd, const void* buf, size_t n, int flags,
74                const struct sockaddr* dst_addr, socklen_t dst_addr_length) {
75     return __netdClientDispatch.sendto(fd, buf, n, flags, dst_addr, dst_addr_length);
76 }
77 
socket(int domain,int type,int protocol)78 int socket(int domain, int type, int protocol) {
79   return FDTRACK_CREATE(__netdClientDispatch.socket(domain, type, protocol));
80 }
81