2 * Copyright (C) 2015-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include "afb-socket.h"
44 /******************************************************************************/
61 * Structure for known entries
65 /** the known prefix */
68 /** the type of the entry */
71 /** should not set SO_REUSEADDR for servers */
72 unsigned noreuseaddr: 1;
74 /** should not call listen for servers */
79 * The known entries with the default one at the first place
81 static struct entry entries[] = {
99 * It is possible to set explicit api name instead of using the
102 static const char as_api[] = "?as-api=";
104 /******************************************************************************/
107 * open a unix domain socket for client or server
109 * @param spec the specification of the path (prefix with @ for abstract)
110 * @param server 0 for client, server otherwise
112 * @return the file descriptor number of the socket or -1 in case of error
114 static int open_unix(const char *spec, int server)
116 int fd, rc, abstract;
117 struct sockaddr_un addr;
120 abstract = spec[0] == '@';
122 /* check the length */
123 length = strlen(spec);
125 errno = ENAMETOOLONG;
129 /* remove the file on need */
130 if (server && !abstract)
133 /* create a socket */
134 fd = socket(AF_UNIX, SOCK_STREAM, 0);
138 /* prepare address */
139 memset(&addr, 0, sizeof addr);
140 addr.sun_family = AF_UNIX;
141 strcpy(addr.sun_path, spec);
143 addr.sun_path[0] = 0; /* implement abstract sockets */
146 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
148 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
158 * open a tcp socket for client or server
160 * @param spec the specification of the host:port/...
161 * @param server 0 for client, server otherwise
163 * @return the file descriptor number of the socket or -1 in case of error
165 static int open_tcp(const char *spec, int server, int reuseaddr)
168 const char *service, *host, *tail;
169 struct addrinfo hint, *rai, *iai;
172 tail = strchrnul(spec, '/');
173 service = strchr(spec, ':');
174 if (tail == NULL || service == NULL || tail < service) {
178 host = strndupa(spec, service++ - spec);
179 service = strndupa(service, tail - service);
182 memset(&hint, 0, sizeof hint);
183 hint.ai_family = AF_INET;
184 hint.ai_socktype = SOCK_STREAM;
186 hint.ai_flags = AI_PASSIVE;
187 if (host[0] == 0 || (host[0] == '*' && host[1] == 0))
190 rc = getaddrinfo(host, service, &hint, &rai);
198 while (iai != NULL) {
199 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
204 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
206 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
208 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
223 * open a systemd socket for server
225 * @param spec the specification of the systemd name
227 * @return the file descriptor number of the socket or -1 in case of error
229 static int open_systemd(const char *spec)
232 return systemd_fds_for(spec);
234 errno = EAFNOSUPPORT;
239 /******************************************************************************/
242 * Get the entry of the uri by searching to its prefix
244 * @param uri the searched uri
245 * @param offset where to store the prefix length
247 * @return the found entry or the default one
249 static struct entry *get_entry(const char *uri, int *offset)
251 int l, i = (int)(sizeof entries / sizeof * entries);
259 l = (int)strlen(entries[i].prefix);
260 if (!strncmp(uri, entries[i].prefix, l))
269 * open socket for client or server
271 * @param uri the specification of the socket
272 * @param server 0 for client, server otherwise
274 * @return the file descriptor number of the socket or -1 in case of error
276 static int open_uri(const char *uri, int server)
282 /* search for the entry */
283 e = get_entry(uri, &offset);
287 api = strstr(uri, as_api);
289 uri = strndupa(uri, api - uri);
291 /* open the socket */
294 fd = open_unix(uri, server);
297 fd = open_tcp(uri, server, !e->noreuseaddr);
301 fd = open_systemd(uri);
308 errno = EAFNOSUPPORT;
316 fcntl(fd, F_SETFD, FD_CLOEXEC);
317 fcntl(fd, F_SETFL, O_NONBLOCK);
326 * open socket for client or server
328 * @param uri the specification of the socket
329 * @param server 0 for client, server otherwise
331 * @return the file descriptor number of the socket or -1 in case of error
333 int afb_socket_open(const char *uri, int server)
335 int fd = open_uri(uri, server);
337 ERROR("can't open %s socket for %s", server ? "server" : "client", uri);
342 * open socket for client or server
344 * @param uri the specification of the socket
345 * @param server 0 for client, server otherwise
347 * @return the fdev of the socket or NULL in case of error
349 struct fdev *afb_socket_open_fdev(const char *uri, int server)
354 fd = afb_socket_open(uri, server);
358 fdev = afb_fdev_create(fd);
361 ERROR("can't make %s socket for %s", server ? "server" : "client", uri);
368 * Get the api name of the uri
370 * @param uri the specification of the socket
372 * @return the api name or NULL if none can be deduced
374 const char *afb_socket_api(const char *uri)
380 entry = get_entry(uri, &offset);
382 uri += (entry->type == Type_Unix && *uri == '@');
383 api = strstr(uri, as_api);
385 api += sizeof as_api - 1;
387 api = strrchr(uri, '/');
392 if (strchr(api, ':'))