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