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; |
| 33 | |
Elliott Hughes | 3906c85 | 2015-01-09 12:21:51 -0800 | [diff] [blame] | 34 | #else |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <sys/socket.h> |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 37 | |
| 38 | typedef int cutils_socket_t; |
| 39 | #define INVALID_SOCKET (-1) |
| 40 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" |
| 44 | #define ANDROID_SOCKET_DIR "/dev/socket" |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | /* |
| 51 | * android_get_control_socket - simple helper function to get the file |
| 52 | * descriptor of our init-managed Unix domain socket. `name' is the name of the |
| 53 | * socket, as given in init.rc. Returns -1 on error. |
| 54 | * |
| 55 | * This is inline and not in libcutils proper because we want to use this in |
| 56 | * third-party daemons with minimal modification. |
| 57 | */ |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 58 | 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] | 59 | { |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 60 | char key[64]; |
| 61 | 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] | 62 | |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 63 | const char* val = getenv(key); |
| 64 | if (!val) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | return -1; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 66 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | |
| 68 | errno = 0; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 69 | int fd = strtol(val, NULL, 10); |
| 70 | if (errno) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | return -1; |
Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 72 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
| 74 | return fd; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * See also android.os.LocalSocketAddress.Namespace |
| 79 | */ |
| 80 | // Linux "abstract" (non-filesystem) namespace |
| 81 | #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0 |
| 82 | // Android "reserved" (/dev/socket) namespace |
| 83 | #define ANDROID_SOCKET_NAMESPACE_RESERVED 1 |
| 84 | // Normal filesystem namespace |
| 85 | #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2 |
| 86 | |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 87 | /* |
| 88 | * Functions to create sockets for some common usages. |
| 89 | * |
| 90 | * All these functions are implemented for Unix, but only a few are implemented |
| 91 | * for Windows. Those which are can be identified by the cutils_socket_t |
| 92 | * return type. The idea is to be able to use this return value with the |
| 93 | * standard Unix socket functions on any platform. |
| 94 | * |
| 95 | * On Unix the returned cutils_socket_t is a standard int file descriptor and |
| 96 | * can always be used as normal with all file descriptor functions. |
| 97 | * |
| 98 | * On Windows utils_socket_t is an unsigned int pointer, and is only valid |
| 99 | * with functions that specifically take a socket, e.g. send(), sendto(), |
| 100 | * recv(), and recvfrom(). General file descriptor functions such as read(), |
| 101 | * write(), and close() will not work with utils_socket_t and will require |
| 102 | * special handling. |
| 103 | * |
| 104 | * These functions return INVALID_SOCKET (-1) on failure for all platforms. |
| 105 | */ |
| 106 | int socket_loopback_client(int port, int type); |
| 107 | cutils_socket_t socket_network_client(const char* host, int port, int type); |
| 108 | int socket_network_client_timeout(const char* host, int port, int type, |
| 109 | int timeout, int* getaddrinfo_error); |
| 110 | int socket_loopback_server(int port, int type); |
| 111 | int socket_local_server(const char* name, int namespaceId, int type); |
| 112 | int socket_local_server_bind(int s, const char* name, int namespaceId); |
| 113 | int socket_local_client_connect(int fd, const char *name, int namespaceId, |
| 114 | int type); |
| 115 | int socket_local_client(const char* name, int namespaceId, int type); |
| 116 | cutils_socket_t socket_inaddr_any_server(int port, int type); |
| 117 | |
| 118 | /* |
| 119 | * Closes a cutils_socket_t. Windows doesn't allow calling close() on a socket |
| 120 | * so this is a cross-platform way to close a cutils_socket_t. |
| 121 | * |
| 122 | * Returns 0 on success. |
| 123 | */ |
| 124 | int socket_close(cutils_socket_t sock); |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 127 | * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0 |
| 128 | * disables receive timeouts. |
| 129 | * |
| 130 | * Return 0 on success. |
| 131 | */ |
| 132 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms); |
| 133 | |
| 134 | /* |
jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 135 | * socket_peer_is_trusted - Takes a socket which is presumed to be a |
| 136 | * connected local socket (e.g. AF_LOCAL) and returns whether the peer |
| 137 | * (the userid that owns the process on the other end of that socket) |
| 138 | * is one of the two trusted userids, root or shell. |
| 139 | * |
| 140 | * Note: This only works as advertised on the Android OS and always |
| 141 | * just returns true when called on other operating systems. |
| 142 | */ |
| 143 | extern bool socket_peer_is_trusted(int fd); |
| 144 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | #ifdef __cplusplus |
| 146 | } |
| 147 | #endif |
| 148 | |
David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 149 | #endif /* __CUTILS_SOCKETS_H */ |