| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 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 |  | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 17 | #include "private/NetdClientDispatch.h" | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 18 |  | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 19 | #include <sys/socket.h> | 
|  | 20 |  | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 21 | #include "private/bionic_fdtrack.h" | 
|  | 22 |  | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 23 | #ifdef __i386__ | 
|  | 24 | #define __socketcall __attribute__((__cdecl__)) | 
|  | 25 | #else | 
|  | 26 | #define __socketcall | 
|  | 27 | #endif | 
|  | 28 |  | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 29 | extern "C" __socketcall int __accept4(int, sockaddr*, socklen_t*, int); | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 30 | extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t); | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 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); | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 34 | extern "C" __socketcall int __socket(int, int, int); | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 35 |  | 
| Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 36 | static unsigned fallBackNetIdForResolv(unsigned netId) { | 
|  | 37 | return netId; | 
|  | 38 | } | 
|  | 39 |  | 
| Luke Huang | e3ed892 | 2018-11-19 16:59:08 +0800 | [diff] [blame] | 40 | static int fallBackDnsOpenProxy() { | 
|  | 41 | return -1; | 
|  | 42 | } | 
|  | 43 |  | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 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))) = { | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 47 | __accept4, | 
| Sreeram Ramachandran | 8f0cd8a | 2014-05-13 15:40:26 -0700 | [diff] [blame] | 48 | __connect, | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 49 | __sendmmsg, | 
|  | 50 | __sendmsg, | 
|  | 51 | __sendto, | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 52 | __socket, | 
| Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 53 | fallBackNetIdForResolv, | 
| Luke Huang | e3ed892 | 2018-11-19 16:59:08 +0800 | [diff] [blame] | 54 | fallBackDnsOpenProxy, | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 55 | }; | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | int accept4(int fd, sockaddr* addr, socklen_t* addr_length, int flags) { | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 58 | return FDTRACK_CREATE(__netdClientDispatch.accept4(fd, addr, addr_length, flags)); | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
|  | 61 | int connect(int fd, const sockaddr* addr, socklen_t addr_length) { | 
|  | 62 | return __netdClientDispatch.connect(fd, addr, addr_length); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 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 |  | 
|  | 69 | ssize_t sendmsg(int fd, const struct msghdr* msg, int flags) { | 
|  | 70 | return __netdClientDispatch.sendmsg(fd, msg, flags); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 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 |  | 
|  | 78 | int socket(int domain, int type, int protocol) { | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 79 | return FDTRACK_CREATE(__netdClientDispatch.socket(domain, type, protocol)); | 
| Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame] | 80 | } |