The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | #ifndef __CUTILS_SOCKETS_H |
| 18 | #define __CUTILS_SOCKETS_H |
| 19 | |
| 20 | #include <errno.h> |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 24 | #include <stdbool.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Elliott Hughes | adbf442 | 2015-07-29 17:45:24 -0700 | [diff] [blame] | 26 | #if defined(_WIN32) |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 27 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <winsock2.h> |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 29 | #include <ws2tcpip.h> |
| 30 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | typedef int socklen_t; |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 32 | typedef SOCKET cutils_socket_t; |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 33 | typedef WSABUF cutils_socket_buffer_t; |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 34 | |
Elliott Hughes | 3906c85 | 2015-01-09 12:21:51 -0800 | [diff] [blame] | 35 | #else |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <sys/socket.h> |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 38 | #include <sys/uio.h> |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 39 | |
| 40 | typedef int cutils_socket_t; |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 41 | typedef struct iovec cutils_socket_buffer_t; |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 42 | #define INVALID_SOCKET (-1) |
| 43 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" |
| 47 | #define ANDROID_SOCKET_DIR "/dev/socket" |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * android_get_control_socket - simple helper function to get the file |
| 55 | * descriptor of our init-managed Unix domain socket. `name' is the name of the |
| 56 | * socket, as given in init.rc. Returns -1 on error. |
| 57 | * |
| 58 | * This is inline and not in libcutils proper because we want to use this in |
| 59 | * third-party daemons with minimal modification. |
| 60 | */ |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 61 | static inline int android_get_control_socket(const char* name) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | { |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 63 | char key[64]; |
| 64 | snprintf(key, sizeof(key), ANDROID_SOCKET_ENV_PREFIX "%s", name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 66 | const char* val = getenv(key); |
| 67 | if (!val) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | return -1; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 69 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | |
| 71 | errno = 0; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 72 | int fd = strtol(val, NULL, 10); |
| 73 | if (errno) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | return -1; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 75 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | |
| 77 | return fd; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * See also android.os.LocalSocketAddress.Namespace |
| 82 | */ |
| 83 | // Linux "abstract" (non-filesystem) namespace |
| 84 | #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0 |
| 85 | // Android "reserved" (/dev/socket) namespace |
| 86 | #define ANDROID_SOCKET_NAMESPACE_RESERVED 1 |
| 87 | // Normal filesystem namespace |
| 88 | #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2 |
| 89 | |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 90 | /* |
| 91 | * Functions to create sockets for some common usages. |
| 92 | * |
| 93 | * All these functions are implemented for Unix, but only a few are implemented |
| 94 | * for Windows. Those which are can be identified by the cutils_socket_t |
| 95 | * return type. The idea is to be able to use this return value with the |
| 96 | * standard Unix socket functions on any platform. |
| 97 | * |
| 98 | * On Unix the returned cutils_socket_t is a standard int file descriptor and |
| 99 | * can always be used as normal with all file descriptor functions. |
| 100 | * |
| 101 | * On Windows utils_socket_t is an unsigned int pointer, and is only valid |
| 102 | * with functions that specifically take a socket, e.g. send(), sendto(), |
| 103 | * recv(), and recvfrom(). General file descriptor functions such as read(), |
| 104 | * write(), and close() will not work with utils_socket_t and will require |
| 105 | * special handling. |
| 106 | * |
| 107 | * These functions return INVALID_SOCKET (-1) on failure for all platforms. |
| 108 | */ |
| 109 | int socket_loopback_client(int port, int type); |
| 110 | cutils_socket_t socket_network_client(const char* host, int port, int type); |
| 111 | int socket_network_client_timeout(const char* host, int port, int type, |
| 112 | int timeout, int* getaddrinfo_error); |
| 113 | int socket_loopback_server(int port, int type); |
| 114 | int socket_local_server(const char* name, int namespaceId, int type); |
| 115 | int socket_local_server_bind(int s, const char* name, int namespaceId); |
| 116 | int socket_local_client_connect(int fd, const char *name, int namespaceId, |
| 117 | int type); |
| 118 | int socket_local_client(const char* name, int namespaceId, int type); |
| 119 | cutils_socket_t socket_inaddr_any_server(int port, int type); |
| 120 | |
| 121 | /* |
| 122 | * Closes a cutils_socket_t. Windows doesn't allow calling close() on a socket |
| 123 | * so this is a cross-platform way to close a cutils_socket_t. |
| 124 | * |
| 125 | * Returns 0 on success. |
| 126 | */ |
| 127 | int socket_close(cutils_socket_t sock); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 128 | |
| 129 | /* |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 130 | * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0 |
| 131 | * disables receive timeouts. |
| 132 | * |
| 133 | * Return 0 on success. |
| 134 | */ |
| 135 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms); |
| 136 | |
| 137 | /* |
David Pursell | 756e1c8 | 2016-01-29 10:39:41 -0800 | [diff] [blame] | 138 | * Returns the local port the socket is bound to or -1 on error. |
| 139 | */ |
| 140 | int socket_get_local_port(cutils_socket_t sock); |
| 141 | |
| 142 | /* |
David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 143 | * Sends to a socket from multiple buffers; wraps writev() on Unix or WSASend() |
| 144 | * on Windows. This can give significant speedup compared to calling send() |
| 145 | * multiple times. |
| 146 | * |
| 147 | * Because Unix and Windows use different structs to hold buffers, we also |
| 148 | * need a generic function to set up the buffers. |
| 149 | * |
| 150 | * Example usage: |
| 151 | * cutils_socket_buffer_t buffers[2] = { |
| 152 | * make_cutils_socket_buffer(data0, len0), |
| 153 | * make_cutils_socket_buffer(data1, len1) |
| 154 | * }; |
| 155 | * socket_send_buffers(sock, buffers, 2); |
| 156 | * |
| 157 | * Returns the number of bytes written or -1 on error. |
| 158 | */ |
| 159 | cutils_socket_buffer_t make_cutils_socket_buffer(void* data, size_t length); |
| 160 | ssize_t socket_send_buffers(cutils_socket_t sock, |
| 161 | cutils_socket_buffer_t* buffers, |
| 162 | size_t num_buffers); |
| 163 | |
| 164 | /* |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 165 | * socket_peer_is_trusted - Takes a socket which is presumed to be a |
| 166 | * connected local socket (e.g. AF_LOCAL) and returns whether the peer |
| 167 | * (the userid that owns the process on the other end of that socket) |
| 168 | * is one of the two trusted userids, root or shell. |
| 169 | * |
| 170 | * Note: This only works as advertised on the Android OS and always |
| 171 | * just returns true when called on other operating systems. |
| 172 | */ |
| 173 | extern bool socket_peer_is_trusted(int fd); |
| 174 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 179 | #endif /* __CUTILS_SOCKETS_H */ |