sockets.h revision 2b8f76cdbb254ac1170087a40dbf30b7627f3516
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>
21#include <stdlib.h>
22#include <string.h>
23#include <stdbool.h>
24
25#ifdef HAVE_WINSOCK
26#include <winsock2.h>
27typedef int  socklen_t;
28#elif HAVE_SYS_SOCKET_H
29#include <sys/socket.h>
30#endif
31
32#define ANDROID_SOCKET_ENV_PREFIX	"ANDROID_SOCKET_"
33#define ANDROID_SOCKET_DIR		"/dev/socket"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/*
40 * android_get_control_socket - simple helper function to get the file
41 * descriptor of our init-managed Unix domain socket. `name' is the name of the
42 * socket, as given in init.rc. Returns -1 on error.
43 *
44 * This is inline and not in libcutils proper because we want to use this in
45 * third-party daemons with minimal modification.
46 */
47static inline int android_get_control_socket(const char *name)
48{
49	char key[64] = ANDROID_SOCKET_ENV_PREFIX;
50	const char *val;
51	int fd;
52
53	/* build our environment variable, counting cycles like a wolf ... */
54#if HAVE_STRLCPY
55	strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
56		name,
57		sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
58#else	/* for the host, which may lack the almightly strncpy ... */
59	strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
60		name,
61		sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
62	key[sizeof(key)-1] = '\0';
63#endif
64
65	val = getenv(key);
66	if (!val)
67		return -1;
68
69	errno = 0;
70	fd = strtol(val, NULL, 10);
71	if (errno)
72		return -1;
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
87extern int socket_loopback_client(int port, int type);
88extern int socket_network_client(const char *host, int port, int type);
89extern int socket_loopback_server(int port, int type);
90extern int socket_local_server(const char *name, int namespaceId, int type);
91extern int socket_local_server_bind(int s, const char *name, int namespaceId);
92extern int socket_local_client_connect(int fd,
93        const char *name, int namespaceId, int type);
94extern int socket_local_client(const char *name, int namespaceId, int type);
95extern int socket_inaddr_any_server(int port, int type);
96
97/*
98 * socket_peer_is_trusted - Takes a socket which is presumed to be a
99 * connected local socket (e.g. AF_LOCAL) and returns whether the peer
100 * (the userid that owns the process on the other end of that socket)
101 * is one of the two trusted userids, root or shell.
102 *
103 * Note: This only works as advertised on the Android OS and always
104 * just returns true when called on other operating systems.
105 */
106extern bool socket_peer_is_trusted(int fd);
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif /* __CUTILS_SOCKETS_H */
113