• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 <stddef.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/select.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25 
26 #include "osi/include/osi.h"
27 #include "osi/include/socket_utils/socket_local.h"
28 #include "osi/include/socket_utils/sockets.h"
29 
30 #define LISTEN_BACKLOG 4
31 
32 /* Documented in header file. */
osi_socket_make_sockaddr_un(const char * name,int namespaceId,struct sockaddr_un * p_addr,socklen_t * alen)33 int osi_socket_make_sockaddr_un(const char* name, int namespaceId,
34                                 struct sockaddr_un* p_addr, socklen_t* alen) {
35   memset(p_addr, 0, sizeof(*p_addr));
36   size_t namelen;
37 
38   switch (namespaceId) {
39     case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
40 #if defined(__linux__)
41       namelen = strlen(name);
42 
43       // Test with length +1 for the *initial* '\0'.
44       if ((namelen + 1) > sizeof(p_addr->sun_path)) {
45         goto error;
46       }
47 
48       /*
49        * Note: The path in this case is *not* supposed to be
50        * '\0'-terminated. ("man 7 unix" for the gory details.)
51        */
52 
53       p_addr->sun_path[0] = 0;
54       memcpy(p_addr->sun_path + 1, name, namelen);
55 #else
56       /* this OS doesn't have the Linux abstract namespace */
57 
58       namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
59       /* unix_path_max appears to be missing on linux */
60       if (namelen >
61           sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
62         goto error;
63       }
64 
65       strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
66       strcat(p_addr->sun_path, name);
67 #endif
68       break;
69 
70     case ANDROID_SOCKET_NAMESPACE_RESERVED:
71       namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
72       /* unix_path_max appears to be missing on linux */
73       if (namelen >
74           sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
75         goto error;
76       }
77 
78       strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
79       strcat(p_addr->sun_path, name);
80       break;
81 
82     case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
83       namelen = strlen(name);
84       /* unix_path_max appears to be missing on linux */
85       if (namelen >
86           sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
87         goto error;
88       }
89 
90       strcpy(p_addr->sun_path, name);
91       break;
92     default:
93       // invalid namespace id
94       return -1;
95   }
96 
97   p_addr->sun_family = AF_LOCAL;
98   *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
99   return 0;
100 error:
101   return -1;
102 }
103 
104 /**
105  * connect to peer named "name" on fd
106  * returns same fd or -1 on error.
107  * fd is not closed on error. that's your job.
108  *
109  * Used by AndroidSocketImpl
110  */
osi_socket_local_client_connect(int fd,const char * name,int namespaceId,int)111 int osi_socket_local_client_connect(int fd, const char* name, int namespaceId,
112                                     int /* type */) {
113   struct sockaddr_un addr;
114   socklen_t alen;
115   int err;
116 
117   err = osi_socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
118 
119   if (err < 0) {
120     goto error;
121   }
122 
123   OSI_NO_INTR(err = connect(fd, (struct sockaddr*)&addr, alen));
124   if (err < 0) {
125     goto error;
126   }
127 
128   return fd;
129 
130 error:
131   return -1;
132 }
133 
134 /**
135  * connect to peer named "name"
136  * returns fd or -1 on error
137  */
osi_socket_local_client(const char * name,int namespaceId,int type)138 int osi_socket_local_client(const char* name, int namespaceId, int type) {
139   int s;
140 
141   s = socket(AF_LOCAL, type, 0);
142   if (s < 0) return -1;
143 
144   if (0 > osi_socket_local_client_connect(s, name, namespaceId, type)) {
145     close(s);
146     return -1;
147   }
148 
149   return s;
150 }
151