xref: /aosp_15_r20/bionic/libc/bionic/NetdClientDispatch.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
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 extern "C" int __accept4(int, sockaddr*, socklen_t*, int);
24 extern "C" int __connect(int, const sockaddr*, socklen_t);
25 extern "C" int __sendmmsg(int, const mmsghdr*, unsigned int, int);
26 extern "C" ssize_t __sendmsg(int, const msghdr*, unsigned int);
27 extern "C" int __sendto(int, const void*, size_t, int, const sockaddr*, socklen_t);
28 extern "C" int __socket(int, int, int);
29 
fallBackNetIdForResolv(unsigned netId)30 static unsigned fallBackNetIdForResolv(unsigned netId) {
31     return netId;
32 }
33 
fallBackDnsOpenProxy()34 static int fallBackDnsOpenProxy() {
35     return -1;
36 }
37 
38 // This structure is modified only at startup (when libc.so is loaded) and never
39 // afterwards, so it's okay that it's read later at runtime without a lock.
40 __LIBC_HIDDEN__ NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
41     __accept4,
42     __connect,
43     __sendmmsg,
44     __sendmsg,
45     __sendto,
46     __socket,
47     fallBackNetIdForResolv,
48     fallBackDnsOpenProxy,
49 };
50 
accept4(int fd,sockaddr * addr,socklen_t * addr_length,int flags)51 int accept4(int fd, sockaddr* addr, socklen_t* addr_length, int flags) {
52   return FDTRACK_CREATE(__netdClientDispatch.accept4(fd, addr, addr_length, flags));
53 }
54 
connect(int fd,const sockaddr * addr,socklen_t addr_length)55 int connect(int fd, const sockaddr* addr, socklen_t addr_length) {
56     return __netdClientDispatch.connect(fd, addr, addr_length);
57 }
58 
sendmmsg(int fd,const struct mmsghdr * msgs,unsigned int msg_count,int flags)59 int sendmmsg(int fd, const struct mmsghdr* msgs, unsigned int msg_count, int flags) {
60     return __netdClientDispatch.sendmmsg(fd, msgs, msg_count, flags);
61 }
62 
sendmsg(int fd,const struct msghdr * msg,int flags)63 ssize_t sendmsg(int fd, const struct msghdr* msg, int flags) {
64     return __netdClientDispatch.sendmsg(fd, msg, flags);
65 }
66 
sendto(int fd,const void * buf,size_t n,int flags,const struct sockaddr * dst_addr,socklen_t dst_addr_length)67 ssize_t sendto(int fd, const void* buf, size_t n, int flags,
68                const struct sockaddr* dst_addr, socklen_t dst_addr_length) {
69     return __netdClientDispatch.sendto(fd, buf, n, flags, dst_addr, dst_addr_length);
70 }
71 
socket(int domain,int type,int protocol)72 int socket(int domain, int type, int protocol) {
73   return FDTRACK_CREATE(__netdClientDispatch.socket(domain, type, protocol));
74 }
75