Introduce object for tokens
[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 #include "afb-token.h"
38
39 #include "systemd.h"
40 #include "verbose.h"
41 #include "fdev.h"
42
43 /* predeclaration of structures */
44 struct afb_ws_json1;
45 struct afb_wsreq;
46
47 /* predeclaration of websocket callbacks */
48 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1);
49 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
50 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object);
51 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
52
53 /* predeclaration of wsreq callbacks */
54 static void wsreq_destroy(struct afb_xreq *xreq);
55 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
56 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
57 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event);
58
59 /* declaration of websocket structure */
60 struct afb_ws_json1
61 {
62         int refcount;
63         void (*cleanup)(void*);
64         void *cleanup_closure;
65         struct afb_session *session;
66         struct afb_token *token;
67         struct afb_evt_listener *listener;
68         struct afb_wsj1 *wsj1;
69         struct afb_cred *cred;
70         struct afb_apiset *apiset;
71         int new_session;
72 };
73
74 /* declaration of wsreq structure */
75 struct afb_wsreq
76 {
77         struct afb_xreq xreq;
78         struct afb_ws_json1 *aws;
79         struct afb_wsreq *next;
80         struct afb_wsj1_msg *msgj1;
81 };
82
83 /* interface for afb_ws_json1 / afb_wsj1 */
84 static struct afb_wsj1_itf wsj1_itf = {
85         .on_hangup = aws_on_hangup_cb,
86         .on_call = aws_on_call_cb
87 };
88
89 /* interface for xreq */
90 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
91         .reply = wsreq_reply,
92         .subscribe = wsreq_subscribe,
93         .unsubscribe = wsreq_unsubscribe,
94         .unref = wsreq_destroy
95 };
96
97 /* the interface for events */
98 static const struct afb_evt_itf evt_itf = {
99         .broadcast = aws_on_broadcast_cb,
100         .push = aws_on_push_cb
101 };
102
103 /***************************************************************
104 ****************************************************************
105 **
106 **  functions of afb_ws_json1 / afb_wsj1
107 **
108 ****************************************************************
109 ***************************************************************/
110
111 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)
112 {
113         struct afb_ws_json1 *result;
114
115         assert(fdev);
116         assert(context != NULL);
117
118         result = malloc(sizeof * result);
119         if (result == NULL)
120                 goto error;
121
122         result->refcount = 1;
123         result->cleanup = cleanup;
124         result->cleanup_closure = cleanup_closure;
125         result->session = afb_session_addref(context->session);
126         result->token = afb_token_addref(context->token);
127         result->new_session = context->created != 0;
128         if (result->session == NULL)
129                 goto error2;
130
131         result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
132         if (result->wsj1 == NULL)
133                 goto error3;
134
135         result->listener = afb_evt_listener_create(&evt_itf, result);
136         if (result->listener == NULL)
137                 goto error4;
138
139         result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
140         result->apiset = afb_apiset_addref(apiset);
141         return result;
142
143 error4:
144         afb_wsj1_unref(result->wsj1);
145 error3:
146         afb_session_unref(result->session);
147         afb_token_unref(result->token);
148 error2:
149         free(result);
150 error:
151         fdev_unref(fdev);
152         return NULL;
153 }
154
155 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
156 {
157         __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
158         return ws;
159 }
160
161 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
162 {
163         if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
164                 afb_evt_listener_unref(ws->listener);
165                 afb_wsj1_unref(ws->wsj1);
166                 if (ws->cleanup != NULL)
167                         ws->cleanup(ws->cleanup_closure);
168                 afb_token_unref(ws->token);
169                 afb_session_unref(ws->session);
170                 afb_cred_unref(ws->cred);
171                 afb_apiset_unref(ws->apiset);
172                 free(ws);
173         }
174 }
175
176 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
177 {
178         struct afb_ws_json1 *ws = closure;
179         afb_ws_json1_unref(ws);
180 }
181
182 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
183 {
184         struct afb_ws_json1 *ws = closure;
185         struct afb_wsreq *wsreq;
186
187         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
188
189         /* allocate */
190         wsreq = calloc(1, sizeof *wsreq);
191         if (wsreq == NULL) {
192                 afb_wsj1_close(ws->wsj1, 1008, NULL);
193                 return;
194         }
195
196         /* init the context */
197         afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
198         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
199         if (!wsreq->xreq.context.invalidated)
200                 wsreq->xreq.context.validated = 1;
201         if (ws->new_session != 0) {
202                 wsreq->xreq.context.created = 1;
203                 ws->new_session = 0;
204         }
205
206         /* fill and record the request */
207         afb_wsj1_msg_addref(msg);
208         wsreq->msgj1 = msg;
209         wsreq->xreq.cred = afb_cred_addref(ws->cred);
210         wsreq->xreq.request.called_api = api;
211         wsreq->xreq.request.called_verb = verb;
212         wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
213         wsreq->aws = afb_ws_json1_addref(ws);
214
215         /* emits the call */
216         afb_xreq_process(&wsreq->xreq, ws->apiset);
217 }
218
219 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
220 {
221         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
222 }
223
224 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
225 {
226         aws_on_event(closure, event, object);
227 }
228
229 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
230 {
231         aws_on_event(closure, event, afb_msg_json_event(event, object));
232 }
233
234 /***************************************************************
235 ****************************************************************
236 **
237 **  functions of wsreq / afb_req
238 **
239 ****************************************************************
240 ***************************************************************/
241
242 static void wsreq_destroy(struct afb_xreq *xreq)
243 {
244         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
245
246         afb_context_disconnect(&wsreq->xreq.context);
247         afb_wsj1_msg_unref(wsreq->msgj1);
248         afb_cred_unref(wsreq->xreq.cred);
249         afb_ws_json1_unref(wsreq->aws);
250         free(wsreq);
251 }
252
253 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
254 {
255         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
256         int rc;
257         struct json_object *reply;
258
259         /* create the reply */
260         reply = afb_msg_json_reply(object, error, info, &xreq->context);
261
262         rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
263                         wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
264         if (rc)
265                 ERROR("Can't send reply: %m");
266 }
267
268 static int wsreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
269 {
270         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
271
272         return afb_evt_event_x2_add_watch(wsreq->aws->listener, event);
273 }
274
275 static int wsreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
276 {
277         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
278
279         return afb_evt_event_x2_remove_watch(wsreq->aws->listener, event);
280 }
281