blob: 531051674bc4a085de556f84d4d3c7ad22421761 [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#include <cutils/sockets.h>
18
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <errno.h>
23#include <stddef.h>
24
25#ifdef HAVE_WINSOCK
26
27int socket_local_client(const char *name, int namespaceId, int type)
28{
29 errno = ENOSYS;
30 return -1;
31}
32
33#else /* !HAVE_WINSOCK */
34
35#include <sys/socket.h>
36#include <sys/un.h>
37#include <sys/select.h>
38#include <sys/types.h>
39
40#include "socket_local.h"
41
Mark Salyzynba02cd12013-11-22 07:36:45 -080042#define UNUSED __attribute__((unused))
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#define LISTEN_BACKLOG 4
45
46/* Documented in header file. */
47int socket_make_sockaddr_un(const char *name, int namespaceId,
48 struct sockaddr_un *p_addr, socklen_t *alen)
49{
50 memset (p_addr, 0, sizeof (*p_addr));
51 size_t namelen;
52
53 switch (namespaceId) {
54 case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
55#ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
56 namelen = strlen(name);
57
58 // Test with length +1 for the *initial* '\0'.
59 if ((namelen + 1) > sizeof(p_addr->sun_path)) {
60 goto error;
61 }
62
63 /*
64 * Note: The path in this case is *not* supposed to be
65 * '\0'-terminated. ("man 7 unix" for the gory details.)
66 */
67
68 p_addr->sun_path[0] = 0;
69 memcpy(p_addr->sun_path + 1, name, namelen);
70#else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
71 /* this OS doesn't have the Linux abstract namespace */
72
73 namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
74 /* unix_path_max appears to be missing on linux */
75 if (namelen > sizeof(*p_addr)
76 - offsetof(struct sockaddr_un, sun_path) - 1) {
77 goto error;
78 }
79
80 strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
81 strcat(p_addr->sun_path, name);
82#endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
83 break;
84
85 case ANDROID_SOCKET_NAMESPACE_RESERVED:
86 namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
87 /* unix_path_max appears to be missing on linux */
88 if (namelen > sizeof(*p_addr)
89 - offsetof(struct sockaddr_un, sun_path) - 1) {
90 goto error;
91 }
92
93 strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
94 strcat(p_addr->sun_path, name);
95 break;
96
97 case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
98 namelen = strlen(name);
99 /* unix_path_max appears to be missing on linux */
100 if (namelen > sizeof(*p_addr)
101 - offsetof(struct sockaddr_un, sun_path) - 1) {
102 goto error;
103 }
104
105 strcpy(p_addr->sun_path, name);
106 break;
107 default:
108 // invalid namespace id
109 return -1;
110 }
111
112 p_addr->sun_family = AF_LOCAL;
113 *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
114 return 0;
115error:
116 return -1;
117}
118
119/**
120 * connect to peer named "name" on fd
121 * returns same fd or -1 on error.
122 * fd is not closed on error. that's your job.
123 *
124 * Used by AndroidSocketImpl
125 */
126int socket_local_client_connect(int fd, const char *name, int namespaceId,
Mark Salyzynba02cd12013-11-22 07:36:45 -0800127 int type UNUSED)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128{
129 struct sockaddr_un addr;
130 socklen_t alen;
131 size_t namelen;
132 int err;
133
134 err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
135
136 if (err < 0) {
137 goto error;
138 }
139
140 if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
141 goto error;
142 }
143
144 return fd;
145
146error:
147 return -1;
148}
149
150/**
151 * connect to peer named "name"
152 * returns fd or -1 on error
153 */
154int socket_local_client(const char *name, int namespaceId, int type)
155{
156 int s;
157
158 s = socket(AF_LOCAL, type, 0);
159 if(s < 0) return -1;
160
161 if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
162 close(s);
163 return -1;
164 }
165
166 return s;
167}
168
169#endif /* !HAVE_WINSOCK */