1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
#include "aclk_lws_wss_client.h"
#include "libnetdata/libnetdata.h"
static int aclk_lws_wss_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
struct aclk_lws_wss_perconnect_data {
int todo;
};
struct lws_wss_packet_buffer {
unsigned char* data;
size_t data_size;
struct lws_wss_packet_buffer *next;
};
static inline struct lws_wss_packet_buffer *lws_wss_packet_buffer_new(void* data, size_t size)
{
struct lws_wss_packet_buffer *new = callocz(1, sizeof(struct lws_wss_packet_buffer));
if(data) {
new->data = mallocz(LWS_PRE+size);
memcpy(new->data+LWS_PRE, data, size);
new->data_size = size;
}
return new;
}
static inline void lws_wss_packet_buffer_append(struct lws_wss_packet_buffer **list, struct lws_wss_packet_buffer *item)
{
struct lws_wss_packet_buffer *tail = *list;
if(!*list) {
*list = item;
return;
}
while(tail->next) {
tail = tail->next;
}
tail->next = item;
}
static inline struct lws_wss_packet_buffer *lws_wss_packet_buffer_pop(struct lws_wss_packet_buffer **list)
{
struct lws_wss_packet_buffer *ret = *list;
if(ret != NULL)
*list = ret->next;
return ret;
}
static inline void lws_wss_packet_buffer_free(struct lws_wss_packet_buffer *item)
{
freez(item->data);
freez(item);
}
static inline void _aclk_lws_wss_read_buffer_clear(struct lws_ring *ringbuffer)
{
size_t elems = lws_ring_get_count_waiting_elements(ringbuffer, NULL);
if(elems > 0)
lws_ring_consume(ringbuffer, NULL, NULL, elems);
}
static inline void _aclk_lws_wss_write_buffer_clear(struct lws_wss_packet_buffer **list)
{
struct lws_wss_packet_buffer *i;
while((i = lws_wss_packet_buffer_pop(list)) != NULL) {
lws_wss_packet_buffer_free(i);
}
*list = NULL;
}
static inline void aclk_lws_wss_clear_io_buffers(struct aclk_lws_wss_engine_instance *inst)
{
aclk_lws_mutex_lock(&inst->read_buf_mutex);
_aclk_lws_wss_read_buffer_clear(inst->read_ringbuffer);
aclk_lws_mutex_unlock(&inst->read_buf_mutex);
aclk_lws_mutex_lock(&inst->write_buf_mutex);
_aclk_lws_wss_write_buffer_clear(&inst->write_buffer_head);
aclk_lws_mutex_unlock(&inst->write_buf_mutex);
}
static const struct lws_protocols protocols[] = {
{
"aclk-wss",
aclk_lws_wss_callback,
sizeof(struct aclk_lws_wss_perconnect_data),
0, 0, 0, 0
},
{ NULL, NULL, 0, 0, 0, 0, 0 }
};
static void aclk_lws_wss_log_divert(int level, const char *line) {
switch(level){
case LLL_ERR:
error("Libwebsockets Error: %s", line);
break;
case LLL_WARN:
debug(D_ACLK, "Libwebsockets Warn: %s", line);
break;
default:
error("Libwebsockets try to log with unknown log level (%d), msg: %s", level, line);
}
}
struct aclk_lws_wss_engine_instance* aclk_lws_wss_client_init (const struct aclk_lws_wss_engine_callbacks *callbacks, const char *target_hostname, int target_port) {
static int lws_logging_initialized = 0;
struct lws_context_creation_info info;
struct aclk_lws_wss_engine_instance *inst;
if(unlikely(!lws_logging_initialized)) {
lws_set_log_level(LLL_ERR | LLL_WARN, aclk_lws_wss_log_divert);
lws_logging_initialized = 1;
}
if(!callbacks || !target_hostname)
return NULL;
inst = callocz(1, sizeof(struct aclk_lws_wss_engine_instance));
inst->host = target_hostname;
inst->port = target_port;
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.user = inst;
inst->lws_context = lws_create_context(&info);
if(!inst->lws_context)
goto failure_cleanup_2;
inst->callbacks = *callbacks;
aclk_lws_mutex_init(&inst->write_buf_mutex);
aclk_lws_mutex_init(&inst->read_buf_mutex);
inst->read_ringbuffer = lws_ring_create(1, ACLK_LWS_WSS_RECV_BUFF_SIZE_BYTES, NULL);
if(!inst->read_ringbuffer)
goto failure_cleanup;
return inst;
failure_cleanup:
lws_context_destroy(inst->lws_context);
failure_cleanup_2:
freez(inst);
return NULL;
}
void aclk_lws_wss_client_destroy(struct aclk_lws_wss_engine_instance* inst) {
lws_context_destroy(inst->lws_context);
inst->lws_context = NULL;
inst->lws_wsi = NULL;
aclk_lws_wss_clear_io_buffers(inst);
#ifdef ACLK_LWS_MOSQUITTO_IO_CALLS_MULTITHREADED
pthread_mutex_destroy(&inst->write_buf_mutex);
pthread_mutex_destroy(&inst->read_buf_mutex);
#endif
}
void aclk_lws_wss_connect(struct aclk_lws_wss_engine_instance *inst){
struct lws_client_connect_info i;
if(inst->lws_wsi) {
error("Already Connected. Only one connection supported at a time.");
return;
}
memset(&i, 0, sizeof(i));
i.context = inst->lws_context;
i.port = inst->port;
i.address = inst->host;
i.path = "/mqtt";
i.host = inst->host;
i.protocol = "mqtt";
#ifdef ACLK_SSL_ALLOW_SELF_SIGNED
i.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
#else
i.ssl_connection = LCCSCF_USE_SSL;
#endif
lws_client_connect_via_info(&i);
}
static inline int received_data_to_ringbuff(struct lws_ring *buffer, void* data, size_t len) {
if( lws_ring_insert(buffer, data, len) != len ) {
error("ACLK_LWS_WSS_CLIENT: receive buffer full. Closing connection to prevent flooding.");
return 0;
}
return 1;
}
static const char *aclk_lws_callback_name(enum lws_callback_reasons reason)
{
switch(reason)
{
case LWS_CALLBACK_CLIENT_WRITEABLE:
return "LWS_CALLBACK_CLIENT_WRITEABLE";
case LWS_CALLBACK_CLIENT_RECEIVE:
return "LWS_CALLBACK_CLIENT_RECEIVE";
case LWS_CALLBACK_PROTOCOL_INIT:
return "LWS_CALLBACK_PROTOCOL_INIT";
case LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED:
return "LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED";
case LWS_CALLBACK_USER:
return "LWS_CALLBACK_USER";
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
return "LWS_CALLBACK_CLIENT_CONNECTION_ERROR";
case LWS_CALLBACK_CLIENT_CLOSED:
return "LWS_CALLBACK_CLIENT_CLOSED";
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
return "LWS_CALLBACK_WS_PEER_INITIATED_CLOSE";
case LWS_CALLBACK_WSI_DESTROY:
return "LWS_CALLBACK_WSI_DESTROY";
case LWS_CALLBACK_CLIENT_ESTABLISHED:
return "LWS_CALLBACK_CLIENT_ESTABLISHED";
default:
// Not using an internal buffer here for thread-safety with unknown calling context.
error("Unknown LWS callback %u", reason);
return "unknown";
}
}
static int
aclk_lws_wss_callback(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
UNUSED(user);
struct aclk_lws_wss_engine_instance *inst = lws_context_user(lws_get_context(wsi));
struct lws_wss_packet_buffer *data;
int retval =
|