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