9d6bfc3ccd897d3a2e68cc45802d48e56a945137
[src/app-framework-binder.git] / src / afb-ws-json1.c
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <json-c/json.h>
27
28 #include "afb-wsj1.h"
29 #include "afb-ws-json1.h"
30 #include "afb-msg-json.h"
31 #include "afb-session.h"
32 #include "afb-cred.h"
33 #include "afb-apiset.h"
34 #include "afb-xreq.h"
35 #include "afb-context.h"
36 #include "afb-evt.h"
37
38 #include "systemd.h"
39 #include "verbose.h"
40 #include "fdev.h"
41
42 /* predeclaration of structures */
43 struct afb_ws_json1;
44 struct afb_wsreq;
45
46 /* predeclaration of websocket callbacks */
47 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1);
48 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
49 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object);
50 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object);
51
52 /* predeclaration of wsreq callbacks */
53 static void wsreq_destroy(struct afb_xreq *xreq);
54 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
55
56 /* declaration of websocket structure */
57 struct afb_ws_json1
58 {
59         int refcount;
60         void (*cleanup)(void*);
61         void *cleanup_closure;
62         struct afb_session *session;
63         struct afb_evt_listener *listener;
64         struct afb_wsj1 *wsj1;
65         struct afb_cred *cred;
66         struct afb_apiset *apiset;
67         int new_session;
68 };
69
70 /* declaration of wsreq structure */
71 struct afb_wsreq
72 {
73         struct afb_xreq xreq;
74         struct afb_ws_json1 *aws;
75         struct afb_wsreq *next;
76         struct afb_wsj1_msg *msgj1;
77 };
78
79 /* interface for afb_ws_json1 / afb_wsj1 */
80 static struct afb_wsj1_itf wsj1_itf = {
81         .on_hangup = aws_on_hangup_cb,
82         .on_call = aws_on_call_cb
83 };
84
85 /* interface for xreq */
86 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
87         .reply = wsreq_reply,
88         .unref = wsreq_destroy
89 };
90
91 /* the interface for events */
92 static const struct afb_evt_itf evt_itf = {
93         .broadcast = aws_on_broadcast_cb,
94         .push = aws_on_push_cb
95 };
96
97 /***************************************************************
98 ****************************************************************
99 **
100 **  functions of afb_ws_json1 / afb_wsj1
101 **
102 ****************************************************************
103 ***************************************************************/
104
105 struct afb_ws_json1 *afb_ws_json1_create(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
106 {
107         struct afb_ws_json1 *result;
108
109         assert(fdev);
110         assert(context != NULL);
111
112         result = malloc(sizeof * result);
113         if (result == NULL)
114                 goto error;
115
116         result->refcount = 1;
117         result->cleanup = cleanup;
118         result->cleanup_closure = cleanup_closure;
119         result->session = afb_session_addref(context->session);
120         result->new_session = context->created != 0;
121         if (result->session == NULL)
122                 goto error2;
123
124         result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
125         if (result->wsj1 == NULL)
126                 goto error3;
127
128         result->listener = afb_evt_listener_create(&evt_itf, result);
129         if (result->listener == NULL)
130                 goto error4;
131
132         result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
133         result->apiset = afb_apiset_addref(apiset);
134         return result;
135
136 error4:
137         afb_wsj1_unref(result->wsj1);
138 error3:
139         afb_session_unref(result->session);
140 error2:
141         free(result);
142 error:
143         fdev_unref(fdev);
144         return NULL;
145 }
146
147 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
148 {
149         __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
150         return ws;
151 }
152
153 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
154 {
155         if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
156                 afb_evt_listener_unref(ws->listener);
157                 afb_wsj1_unref(ws->wsj1);
158                 if (ws->cleanup != NULL)
159                         ws->cleanup(ws->cleanup_closure);
160                 afb_session_unref(ws->session);
161                 afb_cred_unref(ws->cred);
162                 afb_apiset_unref(ws->apiset);
163                 free(ws);
164         }
165 }
166
167 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
168 {
169         struct afb_ws_json1 *ws = closure;
170         afb_ws_json1_unref(ws);
171 }
172
173 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
174 {
175         struct afb_ws_json1 *ws = closure;
176         struct afb_wsreq *wsreq;
177
178         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
179
180         /* allocate */
181         wsreq = calloc(1, sizeof *wsreq);
182         if (wsreq == NULL) {
183                 afb_wsj1_close(ws->wsj1, 1008, NULL);
184                 return;
185         }
186
187         /* init the context */
188         afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
189         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
190         if (!wsreq->xreq.context.invalidated)
191                 wsreq->xreq.context.validated = 1;
192         if (ws->new_session != 0) {
193                 wsreq->xreq.context.created = 1;
194                 ws->new_session = 0;
195         }
196
197         /* fill and record the request */
198         afb_wsj1_msg_addref(msg);
199         wsreq->msgj1 = msg;
200         wsreq->xreq.cred = afb_cred_addref(ws->cred);
201         wsreq->xreq.request.called_api = api;
202         wsreq->xreq.request.called_verb = verb;
203         wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
204         wsreq->aws = afb_ws_json1_addref(ws);
205         wsreq->xreq.listener = wsreq->aws->listener;
206
207         /* emits the call */
208         afb_xreq_process(&wsreq->xreq, ws->apiset);
209 }
210
211 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
212 {
213         aws_on_broadcast_cb(closure, event, object);
214 }
215
216 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object)
217 {
218         struct afb_ws_json1 *aws = closure;
219         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
220 }
221
222 /***************************************************************
223 ****************************************************************
224 **
225 **  functions of wsreq / afb_req
226 **
227 ****************************************************************
228 ***************************************************************/
229
230 static void wsreq_destroy(struct afb_xreq *xreq)
231 {
232         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
233
234         afb_context_disconnect(&wsreq->xreq.context);
235         afb_wsj1_msg_unref(wsreq->msgj1);
236         afb_cred_unref(wsreq->xreq.cred);
237         afb_ws_json1_unref(wsreq->aws);
238         free(wsreq);
239 }
240
241 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
242 {
243         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
244         int rc;
245         struct json_object *reply;
246
247         /* create the reply */
248         reply = afb_msg_json_reply(object, error, info, &xreq->context);
249
250         rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
251                         wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
252         if (rc)
253                 ERROR("Can't send reply: %m");
254 }
255