• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
25 
26 #if defined(_WIN32)
27 
28 #include <winsock2.h>
29 #include <ws2tcpip.h>
30 
31 typedef int  socklen_t;
32 typedef SOCKET cutils_socket_t;
33 
34 #else
35 
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 
39 typedef int cutils_socket_t;
40 #define INVALID_SOCKET (-1)
41 
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.
55  */
56 int android_get_control_socket(const char* name);
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 
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  */
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_local_server(const char* name, int namespaceId, int type);
91 int socket_local_server_bind(int s, const char* name, int namespaceId);
92 int socket_local_client_connect(int fd, const char *name, int namespaceId,
93                                 int type);
94 int socket_local_client(const char* name, int namespaceId, int type);
95 cutils_socket_t socket_inaddr_any_server(int port, int type);
96 
97 /*
98  * Closes a cutils_socket_t. Windows doesn't allow calling close() on a socket
99  * so this is a cross-platform way to close a cutils_socket_t.
100  *
101  * Returns 0 on success.
102  */
103 int socket_close(cutils_socket_t sock);
104 
105 /*
106  * Sets socket receive timeout using SO_RCVTIMEO. Setting |timeout_ms| to 0
107  * disables receive timeouts.
108  *
109  * Return 0 on success.
110  */
111 int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms);
112 
113 /*
114  * Returns the local port the socket is bound to or -1 on error.
115  */
116 int socket_get_local_port(cutils_socket_t sock);
117 
118 /*
119  * Sends to a socket from multiple buffers; wraps writev() on Unix or WSASend()
120  * on Windows. This can give significant speedup compared to calling send()
121  * multiple times.
122  *
123  * Example usage:
124  *   cutils_socket_buffer_t buffers[2] = { {data0, len0}, {data1, len1} };
125  *   socket_send_buffers(sock, buffers, 2);
126  *
127  * If you try to pass more than SOCKET_SEND_BUFFERS_MAX_BUFFERS buffers into
128  * this function it will return -1 without sending anything.
129  *
130  * Returns the number of bytes written or -1 on error.
131  */
132 typedef struct {
133   const void* data;
134   size_t length;
135 } cutils_socket_buffer_t;
136 
137 #define SOCKET_SEND_BUFFERS_MAX_BUFFERS 16
138 
139 ssize_t socket_send_buffers(cutils_socket_t sock,
140                             const cutils_socket_buffer_t* buffers,
141                             size_t num_buffers);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146