Tizen 2.1 base
[sdk/target/sdbd.git] / src / sockets.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
24 #ifdef HAVE_WINSOCK
25 #include <winsock2.h>
26 typedef int  socklen_t;
27 #elif HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
29 #endif
30
31 #define ANDROID_SOCKET_ENV_PREFIX       "ANDROID_SOCKET_"
32 #define ANDROID_SOCKET_DIR              "/dev/socket"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /*
39  * android_get_control_socket - simple helper function to get the file
40  * descriptor of our init-managed Unix domain socket. `name' is the name of the
41  * socket, as given in init.rc. Returns -1 on error.
42  *
43  * This is inline and not in libcutils proper because we want to use this in
44  * third-party daemons with minimal modification.
45  */
46 static inline int android_get_control_socket(const char *name)
47 {
48         char key[64] = ANDROID_SOCKET_ENV_PREFIX;
49         const char *val;
50         int fd;
51
52         /* build our environment variable, counting cycles like a wolf ... */
53 #if HAVE_STRLCPY
54         strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
55                 name,
56                 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
57 #else   /* for the host, which may lack the almightly strncpy ... */
58         strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
59                 name,
60                 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
61         key[sizeof(key)-1] = '\0';
62 #endif
63
64         val = getenv(key);
65         if (!val)
66                 return -1;
67
68         errno = 0;
69         fd = strtol(val, NULL, 10);
70         if (errno)
71                 return -1;
72
73         return fd;
74 }
75
76 /*
77  * See also android.os.LocalSocketAddress.Namespace
78  */
79 // Linux "abstract" (non-filesystem) namespace
80 #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0
81 // Android "reserved" (/dev/socket) namespace
82 #define ANDROID_SOCKET_NAMESPACE_RESERVED 1
83 // Normal filesystem namespace
84 #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2
85
86 extern int socket_loopback_client(int port, int type);
87 extern int socket_network_client(const char *host, int port, int type);
88 extern int socket_loopback_server(int port, int type);
89 extern int socket_local_server(const char *name, int namespaceId, int type);
90 extern int socket_local_server_bind(int s, const char *name, int namespaceId);
91 extern int socket_local_client_connect(int fd, 
92         const char *name, int namespaceId, int type);
93 extern int socket_local_client(const char *name, int namespaceId, int type);
94 extern int socket_inaddr_any_server(int port, int type);
95     
96 #ifdef __cplusplus
97 }
98 #endif
99
100 #endif /* __CUTILS_SOCKETS_H */