2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 int socket_local_client(const char *name, int namespaceId, int type)
33 #else /* !HAVE_WINSOCK */
35 #include <sys/socket.h>
37 #include <sys/select.h>
38 #include <sys/types.h>
40 #include "socket_local.h"
42 #define LISTEN_BACKLOG 4
44 /* Documented in header file. */
45 int socket_make_sockaddr_un(const char *name, int namespaceId,
46 struct sockaddr_un *p_addr, socklen_t *alen)
48 memset (p_addr, 0, sizeof (*p_addr));
51 switch (namespaceId) {
52 case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
53 #ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
54 namelen = strlen(name);
56 // Test with length +1 for the *initial* '\0'.
57 if ((namelen + 1) > sizeof(p_addr->sun_path)) {
62 * Note: The path in this case is *not* supposed to be
63 * '\0'-terminated. ("man 7 unix" for the gory details.)
66 p_addr->sun_path[0] = 0;
67 memcpy(p_addr->sun_path + 1, name, namelen);
68 #else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
69 /* this OS doesn't have the Linux abstract namespace */
71 namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
72 /* unix_path_max appears to be missing on linux */
73 if (namelen > sizeof(*p_addr)
74 - offsetof(struct sockaddr_un, sun_path) - 1) {
78 s_strncpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX, strlen(FILESYSTEM_SOCKET_PREFIX));
79 strncat(p_addr->sun_path, name, strlen(name));
80 #endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
83 case ANDROID_SOCKET_NAMESPACE_RESERVED:
84 namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
85 /* unix_path_max appears to be missing on linux */
86 if (namelen > sizeof(*p_addr)
87 - offsetof(struct sockaddr_un, sun_path) - 1) {
91 s_strncpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX, strlen(ANDROID_RESERVED_SOCKET_PREFIX));
92 strncat(p_addr->sun_path, name, strlen(name));
95 case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
96 namelen = strlen(name);
97 /* unix_path_max appears to be missing on linux */
98 if (namelen > sizeof(*p_addr)
99 - offsetof(struct sockaddr_un, sun_path) - 1) {
103 s_strncpy(p_addr->sun_path, name, strlen(name));
106 // invalid namespace id
110 p_addr->sun_family = AF_LOCAL;
111 *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
118 * connect to peer named "name" on fd
119 * returns same fd or -1 on error.
120 * fd is not closed on error. that's your job.
122 * Used by AndroidSocketImpl
124 int socket_local_client_connect(int fd, const char *name, int namespaceId,
127 struct sockaddr_un addr;
131 err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
137 if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
148 * connect to peer named "name"
149 * returns fd or -1 on error
151 int socket_local_client(const char *name, int namespaceId, int type)
155 s = socket(AF_LOCAL, type, 0);
158 if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
166 #endif /* !HAVE_WINSOCK */