blob: 4626e7acd46b4c12672210972250189c1381734d [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>
Colin Crosse8ffa442016-09-23 10:06:28 -070021#include <limits.h>
Elliott Hughesedc49d72015-04-02 17:42:56 -070022#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdlib.h>
24#include <string.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070025#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Elliott Hughesadbf4422015-07-29 17:45:24 -070027#if defined(_WIN32)
David Pursell0eb8e1b2016-01-14 17:18:27 -080028
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <winsock2.h>
David Pursell0eb8e1b2016-01-14 17:18:27 -080030#include <ws2tcpip.h>
31
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032typedef int socklen_t;
David Pursell0eb8e1b2016-01-14 17:18:27 -080033typedef SOCKET cutils_socket_t;
34
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 Pursell0eb8e1b2016-01-14 17:18:27 -080038
39typedef int cutils_socket_t;
40#define INVALID_SOCKET (-1)
41
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#endif
43
44#define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_"
45#define ANDROID_SOCKET_DIR "/dev/socket"
46
47#ifdef __cplusplus
48extern "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 Projectdd7bc332009-03-03 19:32:55 -080055 */
Colin Crosse8ffa442016-09-23 10:06:28 -070056int android_get_control_socket(const char* name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
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 Pursell0eb8e1b2016-01-14 17:18:27 -080068/*
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 Pursell0eb8e1b2016-01-14 17:18:27 -080087cutils_socket_t socket_network_client(const char* host, int port, int type);
88int socket_network_client_timeout(const char* host, int port, int type,
89 int timeout, int* getaddrinfo_error);
90int socket_loopback_server(int port, int type);
Tao Wu7b700762016-09-19 16:50:10 -070091int socket_loopback_server6(int port, int type);
David Pursell0eb8e1b2016-01-14 17:18:27 -080092int socket_local_server(const char* name, int namespaceId, int type);
93int socket_local_server_bind(int s, const char* name, int namespaceId);
94int socket_local_client_connect(int fd, const char *name, int namespaceId,
95 int type);
96int socket_local_client(const char* name, int namespaceId, int type);
97cutils_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 */
105int socket_close(cutils_socket_t sock);
jeffhao2b8f76c2011-05-05 14:25:36 -0700106
107/*
David Pursell572bce22016-01-15 14:19:56 -0800108 * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0
109 * disables receive timeouts.
110 *
111 * Return 0 on success.
112 */
113int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms);
114
115/*
David Pursell756e1c82016-01-29 10:39:41 -0800116 * Returns the local port the socket is bound to or -1 on error.
117 */
118int socket_get_local_port(cutils_socket_t sock);
119
120/*
David Pursell8385fb22016-01-29 13:49:25 -0800121 * 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 Pursell8385fb22016-01-29 13:49:25 -0800125 * Example usage:
David Pursellb34e4a02016-02-01 09:42:09 -0800126 * cutils_socket_buffer_t buffers[2] = { {data0, len0}, {data1, len1} };
David Pursell8385fb22016-01-29 13:49:25 -0800127 * socket_send_buffers(sock, buffers, 2);
128 *
David Pursellb34e4a02016-02-01 09:42:09 -0800129 * 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 Pursell8385fb22016-01-29 13:49:25 -0800132 * Returns the number of bytes written or -1 on error.
133 */
David Pursellb34e4a02016-02-01 09:42:09 -0800134typedef 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 Pursell8385fb22016-01-29 13:49:25 -0800141ssize_t socket_send_buffers(cutils_socket_t sock,
David Pursellb34e4a02016-02-01 09:42:09 -0800142 const cutils_socket_buffer_t* buffers,
David Pursell8385fb22016-01-29 13:49:25 -0800143 size_t num_buffers);
144
145/*
jeffhao2b8f76c2011-05-05 14:25:36 -0700146 * 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 */
154extern bool socket_peer_is_trusted(int fd);
155
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156#ifdef __cplusplus
157}
158#endif
159
David Pursell0eb8e1b2016-01-14 17:18:27 -0800160#endif /* __CUTILS_SOCKETS_H */