2 * Copyright (C) 2015, 2016, 2017 "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.
19 #define NO_PLUGIN_VERBOSE_MACRO
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <systemd/sd-event.h>
35 #include "afb-apiset.h"
36 #include "afb-common.h"
37 #include "afb-stub-ws.h"
43 char *path; /* path of the object for the API */
44 char *api; /* api name of the interface */
45 int fd; /* file descriptor */
46 sd_event_source *listensrc; /**< systemd source for server socket */
47 struct afb_apiset *apiset;
50 /******************************************************************************/
53 * create a structure api_ws not connected to the 'path'.
55 static struct api_ws *api_ws_make(const char *path)
60 /* allocates the structure */
61 length = strlen(path);
62 api = calloc(1, sizeof *api + 1 + length);
68 /* path is copied after the struct */
69 api->path = (char*)(api+1);
70 memcpy(api->path, path, length + 1);
72 /* api name is at the end of the path */
73 while (length && path[length - 1] != '/' && path[length - 1] != ':')
75 api->api = &api->path[length];
76 if (api->api == NULL || !afb_api_is_valid_name(api->api)) {
90 static int api_ws_socket_unix(const char *path, int server)
93 struct sockaddr_un addr;
96 length = strlen(path);
102 if (server && path[0] != '@')
105 fd = socket(AF_UNIX, SOCK_STREAM, 0);
109 memset(&addr, 0, sizeof addr);
110 addr.sun_family = AF_UNIX;
111 strcpy(addr.sun_path, path);
112 if (addr.sun_path[0] == '@')
113 addr.sun_path[0] = 0; /* implement abstract sockets */
115 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
117 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
126 static int api_ws_socket_inet(const char *path, int server)
129 const char *service, *host, *api;
130 struct addrinfo hint, *rai, *iai;
133 api = strrchr(path, '/');
134 service = strrchr(path, ':');
135 if (api == NULL || service == NULL || api < service) {
139 host = strndupa(path, service++ - path);
140 service = strndupa(service, api - service);
143 memset(&hint, 0, sizeof hint);
144 hint.ai_family = AF_INET;
145 hint.ai_socktype = SOCK_STREAM;
146 rc = getaddrinfo(host, service, &hint, &rai);
154 while (iai != NULL) {
155 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
158 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
160 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
175 static int api_ws_socket(const char *path, int server)
179 /* check for systemd socket */
180 if (0 == strncmp(path, "sd:", 3))
181 fd = sd_fds_for(path + 3);
183 /* check for unix socket */
184 if (0 == strncmp(path, "unix:", 5))
186 fd = api_ws_socket_unix(path + 5, server);
189 fd = api_ws_socket_inet(path, server);
191 if (fd >= 0 && server) {
193 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
197 /* configure the socket */
199 fcntl(fd, F_SETFD, FD_CLOEXEC);
200 fcntl(fd, F_SETFL, O_NONBLOCK);
205 /**********************************************************************************/
207 int afb_api_ws_add_client(const char *path, struct afb_apiset *apiset)
209 struct api_ws *apiws;
210 struct afb_stub_ws *stubws;
212 /* create the ws client api */
213 apiws = api_ws_make(path);
217 /* connect to the service */
218 apiws->fd = api_ws_socket(apiws->path, 0);
220 ERROR("can't connect to ws service %s", apiws->path);
224 stubws = afb_stub_ws_create_client(apiws->fd, apiws->api, apiset);
226 ERROR("can't setup client ws service to %s", apiws->path);
240 static int api_ws_server_accept_client(struct api_ws *apiws, int fd)
242 return -!afb_stub_ws_create_server(fd, apiws->api, apiws->apiset);
245 static void api_ws_server_accept(struct api_ws *apiws)
248 struct sockaddr addr;
251 lenaddr = (socklen_t)sizeof addr;
252 fd = accept(apiws->fd, &addr, &lenaddr);
254 rc = api_ws_server_accept_client(apiws, fd);
261 static int api_ws_server_connect(struct api_ws *apiws);
263 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
265 struct api_ws *apiws = closure;
267 if ((revents & EPOLLIN) != 0)
268 api_ws_server_accept(apiws);
269 if ((revents & EPOLLHUP) != 0)
270 api_ws_server_connect(apiws);
274 static void api_ws_server_disconnect(struct api_ws *apiws)
276 if (apiws->listensrc != NULL) {
277 sd_event_source_unref(apiws->listensrc);
278 apiws->listensrc = NULL;
280 if (apiws->fd >= 0) {
286 static int api_ws_server_connect(struct api_ws *apiws)
290 /* ensure disconnected */
291 api_ws_server_disconnect(apiws);
293 /* request the service object name */
294 apiws->fd = api_ws_socket(apiws->path, 1);
296 ERROR("can't create socket %s", apiws->path);
298 /* listen for service */
299 rc = sd_event_add_io(afb_common_get_event_loop(),
300 &apiws->listensrc, apiws->fd, EPOLLIN,
301 api_ws_server_listen_callback, apiws);
306 ERROR("can't add ws object %s", apiws->path);
311 /* create the service */
312 int afb_api_ws_add_server(const char *path, struct afb_apiset *apiset)
315 struct api_ws *apiws;
317 /* creates the ws api object */
318 apiws = api_ws_make(path);
322 /* connect for serving */
323 rc = api_ws_server_connect(apiws);
327 apiws->apiset = afb_apiset_addref(apiset);