| 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) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <winsock2.h> | 
|  | 28 | typedef int  socklen_t; | 
| Elliott Hughes | 3906c85 | 2015-01-09 12:21:51 -0800 | [diff] [blame] | 29 | #else | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | #include <sys/socket.h> | 
|  | 31 | #endif | 
|  | 32 |  | 
|  | 33 | #define ANDROID_SOCKET_ENV_PREFIX	"ANDROID_SOCKET_" | 
|  | 34 | #define ANDROID_SOCKET_DIR		"/dev/socket" | 
|  | 35 |  | 
|  | 36 | #ifdef __cplusplus | 
|  | 37 | extern "C" { | 
|  | 38 | #endif | 
|  | 39 |  | 
|  | 40 | /* | 
|  | 41 | * android_get_control_socket - simple helper function to get the file | 
|  | 42 | * descriptor of our init-managed Unix domain socket. `name' is the name of the | 
|  | 43 | * socket, as given in init.rc. Returns -1 on error. | 
|  | 44 | * | 
|  | 45 | * This is inline and not in libcutils proper because we want to use this in | 
|  | 46 | * third-party daemons with minimal modification. | 
|  | 47 | */ | 
|  | 48 | static inline int android_get_control_socket(const char *name) | 
|  | 49 | { | 
| Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 50 | char key[64]; | 
|  | 51 | 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] | 52 |  | 
| Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 53 | const char* val = getenv(key); | 
|  | 54 | if (!val) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | return -1; | 
| Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 56 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 |  | 
|  | 58 | errno = 0; | 
| Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 59 | int fd = strtol(val, NULL, 10); | 
|  | 60 | if (errno) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | return -1; | 
| Elliott Hughes | edc49d7 | 2015-04-02 17:42:56 -0700 | [diff] [blame] | 62 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 |  | 
|  | 64 | return fd; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | /* | 
|  | 68 | * See also android.os.LocalSocketAddress.Namespace | 
|  | 69 | */ | 
|  | 70 | // Linux "abstract" (non-filesystem) namespace | 
|  | 71 | #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0 | 
|  | 72 | // Android "reserved" (/dev/socket) namespace | 
|  | 73 | #define ANDROID_SOCKET_NAMESPACE_RESERVED 1 | 
|  | 74 | // Normal filesystem namespace | 
|  | 75 | #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2 | 
|  | 76 |  | 
|  | 77 | extern int socket_loopback_client(int port, int type); | 
|  | 78 | extern int socket_network_client(const char *host, int port, int type); | 
| Ken Lierman | aecc6a6 | 2013-06-20 09:27:13 -0700 | [diff] [blame] | 79 | extern int socket_network_client_timeout(const char *host, int port, int type, | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 80 | int timeout, int* getaddrinfo_error); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | extern int socket_loopback_server(int port, int type); | 
|  | 82 | extern int socket_local_server(const char *name, int namespaceId, int type); | 
|  | 83 | extern int socket_local_server_bind(int s, const char *name, int namespaceId); | 
|  | 84 | extern int socket_local_client_connect(int fd, | 
|  | 85 | const char *name, int namespaceId, int type); | 
|  | 86 | extern int socket_local_client(const char *name, int namespaceId, int type); | 
|  | 87 | extern int socket_inaddr_any_server(int port, int type); | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | /* | 
|  | 90 | * socket_peer_is_trusted - Takes a socket which is presumed to be a | 
|  | 91 | * connected local socket (e.g. AF_LOCAL) and returns whether the peer | 
|  | 92 | * (the userid that owns the process on the other end of that socket) | 
|  | 93 | * is one of the two trusted userids, root or shell. | 
|  | 94 | * | 
|  | 95 | * Note: This only works as advertised on the Android OS and always | 
|  | 96 | * just returns true when called on other operating systems. | 
|  | 97 | */ | 
|  | 98 | extern bool socket_peer_is_trusted(int fd); | 
|  | 99 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | #ifdef __cplusplus | 
|  | 101 | } | 
|  | 102 | #endif | 
|  | 103 |  | 
|  | 104 | #endif /* __CUTILS_SOCKETS_H */ |