blob: cb9b3ff8e097de6c1d66c22eb3c5b7f66de9051c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Hughesedc49d72015-04-02 17:42:56 -070021#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdlib.h>
23#include <string.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070024#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
Elliott Hughesadbf4422015-07-29 17:45:24 -070026#if defined(_WIN32)
David Pursell0eb8e1b2016-01-14 17:18:27 -080027
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <winsock2.h>
David Pursell0eb8e1b2016-01-14 17:18:27 -080029#include <ws2tcpip.h>
30
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031typedef int socklen_t;
David Pursell0eb8e1b2016-01-14 17:18:27 -080032typedef SOCKET cutils_socket_t;
David Pursell8385fb22016-01-29 13:49:25 -080033typedef WSABUF cutils_socket_buffer_t;
David Pursell0eb8e1b2016-01-14 17:18:27 -080034
Elliott Hughes3906c852015-01-09 12:21:51 -080035#else
David Pursell0eb8e1b2016-01-14 17:18:27 -080036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <sys/socket.h>
David Pursell8385fb22016-01-29 13:49:25 -080038#include <sys/uio.h>
David Pursell0eb8e1b2016-01-14 17:18:27 -080039
40typedef int cutils_socket_t;
David Pursell8385fb22016-01-29 13:49:25 -080041typedef struct iovec cutils_socket_buffer_t;
David Pursell0eb8e1b2016-01-14 17:18:27 -080042#define INVALID_SOCKET (-1)
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#endif
45
46#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_"
47#define ANDROID_SOCKET_DIR "/dev/socket"
48
49#ifdef __cplusplus
50extern "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 Pursell0eb8e1b2016-01-14 17:18:27 -080061static inline int android_get_control_socket(const char* name)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062{
Elliott Hughesedc49d72015-04-02 17:42:56 -070063 char key[64];
64 snprintf(key, sizeof(key), ANDROID_SOCKET_ENV_PREFIX "%s", name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Elliott Hughesedc49d72015-04-02 17:42:56 -070066 const char* val = getenv(key);
67 if (!val) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 return -1;
Elliott Hughesedc49d72015-04-02 17:42:56 -070069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 errno = 0;
Elliott Hughesedc49d72015-04-02 17:42:56 -070072 int fd = strtol(val, NULL, 10);
73 if (errno) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 return -1;
Elliott Hughesedc49d72015-04-02 17:42:56 -070075 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
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 Pursell0eb8e1b2016-01-14 17:18:27 -080090/*
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 */
109int socket_loopback_client(int port, int type);
110cutils_socket_t socket_network_client(const char* host, int port, int type);
111int socket_network_client_timeout(const char* host, int port, int type,
112 int timeout, int* getaddrinfo_error);
113int socket_loopback_server(int port, int type);
114int socket_local_server(const char* name, int namespaceId, int type);
115int socket_local_server_bind(int s, const char* name, int namespaceId);
116int socket_local_client_connect(int fd, const char *name, int namespaceId,
117 int type);
118int socket_local_client(const char* name, int namespaceId, int type);
119cutils_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 */
127int socket_close(cutils_socket_t sock);
jeffhao2b8f76c2011-05-05 14:25:36 -0700128
129/*
David Pursell572bce22016-01-15 14:19:56 -0800130 * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0
131 * disables receive timeouts.
132 *
133 * Return 0 on success.
134 */
135int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms);
136
137/*
David Pursell756e1c82016-01-29 10:39:41 -0800138 * Returns the local port the socket is bound to or -1 on error.
139 */
140int socket_get_local_port(cutils_socket_t sock);
141
142/*
David Pursell8385fb22016-01-29 13:49:25 -0800143 * 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 */
159cutils_socket_buffer_t make_cutils_socket_buffer(void* data, size_t length);
160ssize_t socket_send_buffers(cutils_socket_t sock,
161 cutils_socket_buffer_t* buffers,
162 size_t num_buffers);
163
164/*
jeffhao2b8f76c2011-05-05 14:25:36 -0700165 * 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 */
173extern bool socket_peer_is_trusted(int fd);
174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175#ifdef __cplusplus
176}
177#endif
178
David Pursell0eb8e1b2016-01-14 17:18:27 -0800179#endif /* __CUTILS_SOCKETS_H */