/
janus_mqttevh.c
1274 lines (1082 loc) · 46.4 KB
/
janus_mqttevh.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file janus_mqttevh.c
* \author Olle E. Johansson <oej@edvina.net>
* \copyright GNU General Public License v3
* \brief Janus MQTTEventHandler plugin
* \details This is an MQTT event handler plugin for Janus. It is a
* refactoring of the original effort contributed by Olle E. Johansson
* (see https://github.com/meetecho/janus-gateway/pull/1185), which was
* based on the MQTT transport by Andrei Nesterov and the RabbitMQ event
* plugin by Piter Konstantinov.
*
* \ingroup eventhandlers
* \ref eventhandlers
*/
#include "eventhandler.h"
#include <MQTTAsync.h>
#include "../debug.h"
#include "../config.h"
#include "../utils.h"
#include "../events.h"
/* Plugin information */
#define JANUS_MQTTEVH_VERSION 1
#define JANUS_MQTTEVH_VERSION_STRING "0.1.0"
#define JANUS_MQTTEVH_DESCRIPTION "An MQTT event handler plugin for Janus."
#define JANUS_MQTTEVH_NAME "JANUS MQTTEventHandler plugin"
#define JANUS_MQTTEVH_AUTHOR "Olle E. Johansson, Edvina AB"
#define JANUS_MQTTEVH_PACKAGE "janus.eventhandler.mqttevh"
/* Plugin methods */
janus_eventhandler *create(void);
static int janus_mqttevh_init(const char *config_path);
static void janus_mqttevh_destroy(void);
static int janus_mqttevh_get_api_compatibility(void);
static int janus_mqttevh_get_version(void);
static const char *janus_mqttevh_get_version_string(void);
static const char *janus_mqttevh_get_description(void);
static const char *janus_mqttevh_get_name(void);
static const char *janus_mqttevh_get_author(void);
static const char *janus_mqttevh_get_package(void);
static void janus_mqttevh_incoming_event(json_t *event);
json_t *janus_mqttevh_handle_request(json_t *request);
static int janus_mqttevh_send_message(void *context, const char *topic, json_t *message);
static void *janus_mqttevh_handler(void *data);
/* Event handler setup */
static janus_eventhandler janus_mqttevh =
JANUS_EVENTHANDLER_INIT (
.init = janus_mqttevh_init,
.destroy = janus_mqttevh_destroy,
.get_api_compatibility = janus_mqttevh_get_api_compatibility,
.get_version = janus_mqttevh_get_version,
.get_version_string = janus_mqttevh_get_version_string,
.get_description = janus_mqttevh_get_description,
.get_name = janus_mqttevh_get_name,
.get_author = janus_mqttevh_get_author,
.get_package = janus_mqttevh_get_package,
.incoming_event = janus_mqttevh_incoming_event,
.handle_request = janus_mqttevh_handle_request,
.events_mask = JANUS_EVENT_TYPE_NONE
);
/* Fix an exit event */
static json_t exit_event;
/* Destruction of events */
static void janus_mqttevh_event_free(json_t *event) {
if(!event || event == &exit_event)
return;
json_decref(event);
}
/* Queue of events to handle */
static GAsyncQueue *events = NULL;
/* Plugin creator */
janus_eventhandler *create(void) {
JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_MQTTEVH_NAME);
return &janus_mqttevh;
};
/* API flags */
static gboolean janus_mqtt_evh_enabled = FALSE;
static GThread *handler_thread;
static volatile gint initialized = 0, stopping = 0;
/* JSON serialization options */
#define JANUS_MQTTEVH_DEFAULT_ADDPLUGIN 1
#define JANUS_MQTTEVH_DEFAULT_ADDEVENT 1
#define JANUS_MQTTEVH_DEFAULT_KEEPALIVE 30
#define JANUS_MQTTEVH_DEFAULT_CLEANSESSION 0 /* Off */
#define JANUS_MQTTEVH_DEFAULT_MAX_INFLIGHT 10
#define JANUS_MQTTEVH_DEFAULT_MAX_BUFFERED 100
#define JANUS_MQTTEVH_DEFAULT_TIMEOUT 30
#define JANUS_MQTTEVH_DEFAULT_DISCONNECT_TIMEOUT 100
#define JANUS_MQTTEVH_DEFAULT_QOS 0
#define JANUS_MQTTEVH_DEFAULT_RETAIN 0
#define JANUS_MQTTEVH_DEFAULT_CONNECT_STATUS "{\"event\": \"connected\", \"eventhandler\": \""JANUS_MQTTEVH_PACKAGE"\"}"
#define JANUS_MQTTEVH_DEFAULT_DISCONNECT_STATUS "{\"event\": \"disconnected\"}"
#define JANUS_MQTTEVH_DEFAULT_WILL_RETAIN 1
#define JANUS_MQTTEVH_DEFAULT_WILL_QOS 0
#define JANUS_MQTTEVH_DEFAULT_BASETOPIC "/janus/events"
#define JANUS_MQTTEVH_DEFAULT_MQTTURL "tcp://localhost:1883"
#define JANUS_MQTTEVH_DEFAULT_JSON_FORMAT JSON_INDENT(3) | JSON_PRESERVE_ORDER
#define JANUS_MQTTEVH_DEFAULT_TLS_ENABLE FALSE
#define JANUS_MQTTEVH_DEFAULT_TLS_VERIFY_PEER FALSE
#define JANUS_MQTTEVH_DEFAULT_TLS_VERIFY_HOST FALSE
#define JANUS_MQTTEVH_VERSION_3_1 "3.1"
#define JANUS_MQTTEVH_VERSION_3_1_1 "3.1.1"
#define JANUS_MQTTEVH_VERSION_5 "5"
#define JANUS_MQTTEVH_VERSION_DEFAULT JANUS_MQTTEVH_VERSION_3_1_1
static size_t json_format = JANUS_MQTTEVH_DEFAULT_JSON_FORMAT;
/* Parameter validation (for tweaking via Admin API) */
static struct janus_json_parameter request_parameters[] = {
{"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter tweak_parameters[] = {
{"events", JSON_STRING, 0}
};
/* Error codes (for the tweaking via Admin API */
#define JANUS_MQTTEVH_ERROR_INVALID_REQUEST 411
#define JANUS_MQTTEVH_ERROR_MISSING_ELEMENT 412
#define JANUS_MQTTEVH_ERROR_INVALID_ELEMENT 413
#define JANUS_MQTTEVH_ERROR_UNKNOWN_ERROR 499
/* Special topics postfix */
#define JANUS_MQTTEVH_STATUS_TOPIC "status"
/* MQTT client context */
typedef struct janus_mqttevh_context {
/* THe Paho MQTT client data structure */
MQTTAsync client;
int addplugin;
int addevent;
/* Connection data - authentication and url */
struct {
int mqtt_version;
int keep_alive_interval;
int cleansession;
int max_inflight;
int max_buffered;
char *client_id;
char *username;
char *password;
char *url;
} connect;
struct {
int timeout;
} disconnect;
/* Data for publishing events */
struct {
char *topic;
char *connect_status;
char *disconnect_status;
int qos;
int retain;
#ifdef MQTTVERSION_5
GArray *add_user_properties;
#endif
} publish;
/* If we loose connection, the will is our last publish */
struct {
gboolean enabled;
char *topic;
int qos;
int retain;
} will;
/* TLS connection data */
struct {
gboolean enable;
char *cacert_file;
char *cert_file;
char *key_file;
gboolean verify_peer;
gboolean verify_host;
} tls;
} janus_mqttevh_context;
#ifdef MQTTVERSION_5
/* MQTT 5 specific types */
typedef struct janus_mqttevh_set_add_user_property_user_data {
GArray *acc;
janus_config *config;
} janus_mqttevh_set_add_user_property_user_data;
#endif
/* Event handler methods */
static void janus_mqttevh_client_connection_lost(void *context, char *cause);
static int janus_mqttevh_client_connect(janus_mqttevh_context *ctx);
static int janus_mqttevh_client_disconnect(janus_mqttevh_context *ctx);
static void janus_mqttevh_client_destroy_context(janus_mqttevh_context **ctx);
/* MQTT v3.x interface callbacks */
static void janus_mqttevh_client_connect_success(void *context, MQTTAsync_successData *response);
static void janus_mqttevh_client_connect_failure(void *context, MQTTAsync_failureData *response);
static void janus_mqttevh_client_disconnect_success(void *context, MQTTAsync_successData *response);
static void janus_mqttevh_client_disconnect_failure(void *context, MQTTAsync_failureData *response);
static void janus_mqttevh_client_publish_message_success(void *context, MQTTAsync_successData *response);
static void janus_mqttevh_client_publish_message_failure(void *context, MQTTAsync_failureData *response);
static int janus_mqttevh_client_publish_message(janus_mqttevh_context *ctx, const char *topic, int retain, char *payload);
int janus_mqttevh_client_get_response_code(MQTTAsync_failureData *response);
#ifdef MQTTVERSION_5
/* MQTT v5 interface callbacks */
static void janus_mqttevh_client_connect_success5(void *context, MQTTAsync_successData5 *response);
static void janus_mqttevh_client_connect_failure5(void *context, MQTTAsync_failureData5 *response);
static void janus_mqttevh_client_disconnect_success5(void *context, MQTTAsync_successData5 *response);
static void janus_mqttevh_client_disconnect_failure5(void *context, MQTTAsync_failureData5 *response);
static void janus_mqttevh_client_publish_message_success5(void *context, MQTTAsync_successData5 *response);
static void janus_mqttevh_client_publish_message_failure5(void *context, MQTTAsync_failureData5 *response);
static int janus_mqttevh_client_publish_message5(janus_mqttevh_context *ctx, const char *topic, int retain, char *payload, MQTTProperties *properties);
int janus_mqttevh_client_get_response_code5(MQTTAsync_failureData5 *response);
#endif
/* MQTT version independent callback implementations */
static void janus_mqttevh_client_connect_success_impl(void *context);
static void janus_mqttevh_client_connect_failure_impl(void *context, int rc);
static void janus_mqttevh_client_disconnect_success_impl(void *context);
static void janus_mqttevh_client_disconnect_failure_impl(void *context, int rc);
static void janus_mqttevh_client_publish_message_success_impl(void *context);
static void janus_mqttevh_client_publish_message_failure_impl(void *context, int rc);
int janus_mqttevh_client_publish_message_wrap(void *context, const char *topic, int retain, char *payload);
#ifdef MQTTVERSION_5
/* MQTT 5 specific functions */
void janus_mqttevh_add_properties(GArray *user_properties, MQTTProperties *properties);
void janus_mqttevh_set_add_user_property(gpointer item_ptr, gpointer user_data_ptr);
#endif
/* We only handle a single connection */
static janus_mqttevh_context *context = NULL;
/* Janus API methods */
static int janus_mqttevh_get_api_compatibility(void) {
/* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
return JANUS_EVENTHANDLER_API_VERSION;
}
static int janus_mqttevh_get_version(void) {
return JANUS_MQTTEVH_VERSION;
}
static const char *janus_mqttevh_get_version_string(void) {
return JANUS_MQTTEVH_VERSION_STRING;
}
static const char *janus_mqttevh_get_description(void) {
return JANUS_MQTTEVH_DESCRIPTION;
}
static const char *janus_mqttevh_get_name(void) {
return JANUS_MQTTEVH_NAME;
}
static const char *janus_mqttevh_get_author(void) {
return JANUS_MQTTEVH_AUTHOR;
}
static const char *janus_mqttevh_get_package(void) {
return JANUS_MQTTEVH_PACKAGE;
}
/* Send an JSON message to a MQTT topic */
static int janus_mqttevh_send_message(void *context, const char *topic, json_t *message) {
char *payload = NULL;
int rc = 0;
janus_mqttevh_context *ctx;
if(message == NULL) {
return -1;
}
if(context == NULL) {
/* We have no context, so skip and move on */
json_decref(message);
return -1;
}
JANUS_LOG(LOG_HUGE, "About to send message to %s\n", topic);
#ifdef SKREP
if(payload != NULL) {
/* Free previous message */
free(payload);
payload = NULL;
}
#endif
ctx = (janus_mqttevh_context *)context;
payload = json_dumps(message, json_format);
if(payload == NULL) {
JANUS_LOG(LOG_ERR, "Can't convert message to string format\n");
json_decref(message);
return 0;
}
JANUS_LOG(LOG_HUGE, "Converted message to JSON for %s\n", topic);
/* Ok, lets' get rid of the message */
json_decref(message);
rc = janus_mqttevh_client_publish_message_wrap(context, topic, ctx->publish.retain, payload);
if(rc != MQTTASYNC_SUCCESS) {
JANUS_LOG(LOG_WARN, "Can't publish to MQTT topic: %s, return code: %d\n", ctx->publish.topic, rc);
}
free(payload);
JANUS_LOG(LOG_HUGE, "Done with message to JSON for %s\n", topic);
return 0;
}
int janus_mqttevh_client_publish_message_wrap(void *context, const char *topic, int retain, char *payload) {
int rc = 0;
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
#ifdef MQTTVERSION_5
if(ctx->connect.mqtt_version == MQTTVERSION_5) {
MQTTProperties properties = MQTTProperties_initializer;
janus_mqttevh_add_properties(ctx->publish.add_user_properties, &properties);
rc = janus_mqttevh_client_publish_message5(ctx, topic, retain, payload, &properties);
MQTTProperties_free(&properties);
} else {
rc = janus_mqttevh_client_publish_message(ctx, topic, retain, payload);
}
#else
rc = janus_mqttevh_client_publish_message(ctx, topic, retain, payload);
#endif
return rc;
}
static void janus_mqttevh_client_connection_lost(void *context, char *cause) {
/* Notify handlers about this transport being gone */
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
JANUS_LOG(LOG_WARN, "MQTT EVH connection %s lost cause of %s. Reconnecting...\n", ctx->connect.url, cause);
}
/* Set up connection to MQTT broker */
static int janus_mqttevh_client_connect(janus_mqttevh_context *ctx) {
MQTTAsync_connectOptions options = MQTTAsync_connectOptions_initializer;
#ifdef MQTTVERSION_5
if(ctx->connect.mqtt_version == MQTTVERSION_5) {
MQTTAsync_connectOptions options5 = MQTTAsync_connectOptions_initializer5;
options = options5;
options.cleanstart = ctx->connect.cleansession;
options.onSuccess5 = janus_mqttevh_client_connect_success5;
options.onFailure5 = janus_mqttevh_client_connect_failure5;
} else {
options.cleansession = ctx->connect.cleansession;
options.onSuccess = janus_mqttevh_client_connect_success;
options.onFailure = janus_mqttevh_client_connect_failure;
}
#else
options.cleansession = ctx->connect.cleansession;
options.onSuccess = janus_mqttevh_client_connect_success;
options.onFailure = janus_mqttevh_client_connect_failure;
#endif
options.MQTTVersion = ctx->connect.mqtt_version;
options.username = ctx->connect.username;
options.password = ctx->connect.password;
options.automaticReconnect = TRUE;
options.keepAliveInterval = ctx->connect.keep_alive_interval;
options.maxInflight = ctx->connect.max_inflight;
MQTTAsync_SSLOptions ssl_opts = MQTTAsync_SSLOptions_initializer;
if(ctx->tls.enable) {
ssl_opts.trustStore = ctx->tls.cacert_file;
ssl_opts.keyStore = ctx->tls.cert_file;
ssl_opts.privateKey = ctx->tls.key_file;
ssl_opts.enableServerCertAuth = ctx->tls.verify_peer;
options.ssl = &ssl_opts;
}
MQTTAsync_willOptions willOptions = MQTTAsync_willOptions_initializer;
if(ctx->will.enabled) {
willOptions.topicName = ctx->will.topic;
willOptions.message = ctx->publish.disconnect_status;
willOptions.retained = ctx->will.retain;
willOptions.qos = ctx->will.qos;
options.will = &willOptions;
}
options.context = ctx;
return MQTTAsync_connect(ctx->client, &options);
}
/* Callback for successful connection to MQTT broker */
static void janus_mqttevh_client_connect_success(void *context, MQTTAsync_successData *response) {
janus_mqttevh_client_connect_success_impl(context);
}
#ifdef MQTTVERSION_5
static void janus_mqttevh_client_connect_success5(void *context, MQTTAsync_successData5 *response) {
janus_mqttevh_client_connect_success_impl(context);
}
#endif
static void janus_mqttevh_client_connect_success_impl(void *context) {
JANUS_LOG(LOG_INFO, "MQTT EVH client has been successfully connected to the broker\n");
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
char topicbuf[512];
snprintf(topicbuf, sizeof(topicbuf), "%s/%s", ctx->publish.topic, JANUS_MQTTEVH_STATUS_TOPIC);
/* Using LWT's retain for initial status message because
* we need to ensure we overwrite LWT if it's retained.
*/
int rc = janus_mqttevh_client_publish_message_wrap(context, topicbuf, ctx->will.retain, ctx->publish.connect_status);
if(rc != MQTTASYNC_SUCCESS) {
JANUS_LOG(LOG_WARN, "Can't publish to MQTT topic: %s, return code: %d\n", topicbuf, rc);
}
}
/* Callback for MQTT broker connection failure */
static void janus_mqttevh_client_connect_failure(void *context, MQTTAsync_failureData *response) {
int rc = janus_mqttevh_client_get_response_code(response);
janus_mqttevh_client_connect_failure_impl(context, rc);
}
#ifdef MQTTVERSION_5
static void janus_mqttevh_client_connect_failure5(void *context, MQTTAsync_failureData5 *response) {
int rc = janus_mqttevh_client_get_response_code5(response);
janus_mqttevh_client_connect_failure_impl(context, rc);
}
#endif
static void janus_mqttevh_client_connect_failure_impl(void *contexts, int rc) {
/* Notify handlers about this transport failure */
JANUS_LOG(LOG_ERR, "MQTT EVH client has failed connecting to the broker, return code: %d. Reconnecting...\n", rc);
}
/* Disconnect from MQTT broker */
static int janus_mqttevh_client_disconnect(janus_mqttevh_context *ctx) {
char topicbuf[512];
snprintf(topicbuf, sizeof(topicbuf), "%s/%s", ctx->publish.topic, JANUS_MQTTEVH_STATUS_TOPIC);
/* Using LWT's retain for disconnect status message because
* we need to ensure we overwrite LWT if it's retained.
*/
int rc = janus_mqttevh_client_publish_message_wrap(context, topicbuf, 1, ctx->publish.disconnect_status);
if(rc != MQTTASYNC_SUCCESS) {
JANUS_LOG(LOG_WARN, "Can't publish to MQTT topic: %s, return code: %d\n", topicbuf, rc);
}
MQTTAsync_disconnectOptions options = MQTTAsync_disconnectOptions_initializer;
options.context = ctx;
options.timeout = ctx->disconnect.timeout;
#ifdef MQTTVERSION_5
if(ctx->connect.mqtt_version == MQTTVERSION_5) {
options.onSuccess5 = janus_mqttevh_client_disconnect_success5;
options.onFailure5 = janus_mqttevh_client_disconnect_failure5;
} else {
options.onSuccess = janus_mqttevh_client_disconnect_success;
options.onFailure = janus_mqttevh_client_disconnect_failure;
}
#else
options.onSuccess = janus_mqttevh_client_disconnect_success;
options.onFailure = janus_mqttevh_client_disconnect_failure;
#endif
return MQTTAsync_disconnect(ctx->client, &options);
}
/* Callback for successful MQTT disconnect */
static void janus_mqttevh_client_disconnect_success(void *context, MQTTAsync_successData *response) {
janus_mqttevh_client_disconnect_success_impl(context);
}
#ifdef MQTTVERSION_5
static void janus_mqttevh_client_disconnect_success5(void *context, MQTTAsync_successData5 *response) {
janus_mqttevh_client_disconnect_success_impl(context);
}
#endif
static void janus_mqttevh_client_disconnect_success_impl(void *context) {
/* Notify handlers about this transport being gone */
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
JANUS_LOG(LOG_INFO, "MQTT EVH client has been successfully disconnected from %s. Destroying the client...\n", ctx->connect.url);
janus_mqttevh_client_destroy_context(&ctx);
}
/* Callback for MQTT disconnect failure */
void janus_mqttevh_client_disconnect_failure(void *context, MQTTAsync_failureData *response) {
int rc = janus_mqttevh_client_get_response_code(response);
janus_mqttevh_client_disconnect_failure_impl(context, rc);
}
#ifdef MQTTVERSION_5
void janus_mqttevh_client_disconnect_failure5(void *context, MQTTAsync_failureData5 *response) {
int rc = janus_mqttevh_client_get_response_code5(response);
janus_mqttevh_client_disconnect_failure_impl(context, rc);
}
#endif
void janus_mqttevh_client_disconnect_failure_impl(void *context, int rc) {
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
JANUS_LOG(LOG_ERR, "Can't disconnect from MQTT EVH broker %s, return code: %d\n", ctx->connect.url, rc);
janus_mqttevh_client_destroy_context(&ctx);
}
/* Publish mqtt message using paho
* Payload is a string. JSON objects should be stringified before calling this function.
*/
static int janus_mqttevh_client_publish_message(janus_mqttevh_context *ctx, const char *topic, int retain, char *payload) {
int rc;
MQTTAsync_message msg = MQTTAsync_message_initializer;
msg.payload = payload;
msg.payloadlen = strlen(payload);
msg.qos = ctx->publish.qos;
msg.retained = retain;
/* TODO: The payload if generated by json_dumps needs to be freed
free(payload);
payload = (char *)NULL;
*/
MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer;
options.context = ctx;
options.onSuccess = janus_mqttevh_client_publish_message_success;
options.onFailure = janus_mqttevh_client_publish_message_failure;
rc = MQTTAsync_sendMessage(ctx->client, topic, &msg, &options);
switch(rc) {
case MQTTASYNC_SUCCESS:
JANUS_LOG(LOG_HUGE, "MQTT EVH message sent to topic %s on %s. Result %d\n", topic, ctx->connect.url, rc);
break;
case MQTTASYNC_OPERATION_INCOMPLETE:
break;
default:
JANUS_LOG(LOG_WARN, "FAILURE: MQTT EVH message probably not sent to topic %s on %s. Result %d\n", topic, ctx->connect.url, rc);
}
return rc;
}
#ifdef MQTTVERSION_5
static int janus_mqttevh_client_publish_message5(janus_mqttevh_context *ctx, const char *topic, int retain, char *payload, MQTTProperties *properties) {
int rc;
MQTTAsync_message msg = MQTTAsync_message_initializer;
msg.payload = payload;
msg.payloadlen = strlen(payload);
msg.qos = ctx->publish.qos;
msg.retained = retain;
msg.properties = MQTTProperties_copy(properties);
MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer;
options.context = ctx;
options.onSuccess5 = janus_mqttevh_client_publish_message_success5;
options.onFailure5 = janus_mqttevh_client_publish_message_failure5;
rc = MQTTAsync_sendMessage(ctx->client, topic, &msg, &options);
switch(rc) {
case MQTTASYNC_SUCCESS:
JANUS_LOG(LOG_HUGE, "MQTT EVH message sent to topic %s on %s. Result %d\n", topic, ctx->connect.url, rc);
break;
case MQTTASYNC_OPERATION_INCOMPLETE:
break;
default:
JANUS_LOG(LOG_WARN, "FAILURE: MQTT EVH message probably not sent to topic %s on %s. Result %d\n", topic, ctx->connect.url, rc);
}
return rc;
}
#endif
/* Callback for successful MQTT publish */
static void janus_mqttevh_client_publish_message_success(void *context, MQTTAsync_successData *response) {
janus_mqttevh_client_publish_message_success_impl(context);
}
#ifdef MQTTVERSION_5
static void janus_mqttevh_client_publish_message_success5(void *context, MQTTAsync_successData5 *response) {
janus_mqttevh_client_publish_message_success_impl(context);
}
#endif
static void janus_mqttevh_client_publish_message_success_impl(void *context) {
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
JANUS_LOG(LOG_HUGE, "MQTT EVH client has successfully published to MQTT base topic: %s\n", ctx->publish.topic);
}
/* Callback for MQTT publish failure
* Should we bring message into queue? Right now, we just drop it.
*/
static void janus_mqttevh_client_publish_message_failure(void *context, MQTTAsync_failureData *response) {
int rc = janus_mqttevh_client_get_response_code(response);
janus_mqttevh_client_publish_message_failure_impl(context, rc);
}
#ifdef MQTTVERSION_5
static void janus_mqttevh_client_publish_message_failure5(void *context, MQTTAsync_failureData5 *response) {
int rc = janus_mqttevh_client_get_response_code5(response);
janus_mqttevh_client_publish_message_failure_impl(context, rc);
}
#endif
static void janus_mqttevh_client_publish_message_failure_impl(void *context, int rc) {
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
switch(rc) {
case MQTTASYNC_OPERATION_INCOMPLETE:
break;
default:
JANUS_LOG(LOG_ERR, "MQTT EVH client has failed publishing to MQTT topic: %s, return code: %d\n", ctx->publish.topic, rc);
}
}
/* Destroy Janus MQTT event handler session context */
static void janus_mqttevh_client_destroy_context(janus_mqttevh_context **ptr) {
JANUS_LOG(LOG_INFO, "About to destroy MQTT EVH context...\n");
janus_mqttevh_context *ctx = (janus_mqttevh_context *)*ptr;
if(ctx) {
MQTTAsync_destroy(&ctx->client);
g_free(ctx->publish.topic);
g_free(ctx->connect.username);
g_free(ctx->connect.password);
g_free(ctx);
*ptr = NULL;
}
JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_MQTTEVH_NAME);
}
/* This is not used here, but required by the api (even though docs says no) */
static int janus_mqttevh_client_message_arrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message) {
janus_mqttevh_context *ctx = (janus_mqttevh_context *)context;
gchar *topic = g_strndup(topicName, topicLen);
//~ const gboolean janus = janus_mqtt_evh_enabled && !strcasecmp(topic, ctx->subscribe.topic);
const gboolean janus = janus_mqtt_evh_enabled ;
g_free(topic);
if(janus && message->payloadlen) {
JANUS_LOG(LOG_HUGE, "MQTT %s: Receiving %s EVH message over MQTT: %s\n",
ctx->connect.url, "Janus", (char *)message->payload);
}
MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName);
return TRUE;
}
static void janus_mqttevh_client_delivery_complete(void *context, MQTTAsync_token token) {
/* If you send with QoS, you get confirmation here */
}
/* Plugin implementation */
static int janus_mqttevh_init(const char *config_path) {
int res = 0;
janus_config_item *url_item;
janus_config_item *username_item, *password_item, *topic_item, *addevent_item;
janus_config_item *keep_alive_interval_item, *cleansession_item, *max_inflight_item, *max_buffered_item, *disconnect_timeout_item, *qos_item, *retain_item, *connect_status_item, *disconnect_status_item;
janus_config_item *will_retain_item, *will_qos_item, *will_enabled_item;
if(g_atomic_int_get(&stopping)) {
/* Still stopping from before */
return -1;
}
if(config_path == NULL) {
/* Invalid arguments */
return -1;
}
/* Read configuration */
char filename[255];
g_snprintf(filename, sizeof(filename), "%s/%s.jcfg", config_path, JANUS_MQTTEVH_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
janus_config *config = janus_config_parse(filename);
if(config == NULL) {
JANUS_LOG(LOG_WARN, "Couldn't find .jcfg configuration file (%s), trying .cfg\n", JANUS_MQTTEVH_PACKAGE);
g_snprintf(filename, sizeof(filename), "%s/%s.cfg", config_path, JANUS_MQTTEVH_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
config = janus_config_parse(filename);
if(config == NULL) {
JANUS_LOG(LOG_FATAL, "Couldn't find .cfg configuration file (%s), giving up\n", JANUS_MQTTEVH_PACKAGE);
return -1;
}
}
janus_config_print(config);
janus_config_category *config_general = janus_config_get_create(config, NULL, janus_config_type_category, "general");
/* Initializing context */
janus_mqttevh_context *ctx = g_malloc0(sizeof(struct janus_mqttevh_context));
context = ctx;
/* Set default values */
/* Strings are set to default values later */
ctx->addplugin = JANUS_MQTTEVH_DEFAULT_ADDPLUGIN;
ctx->addevent = JANUS_MQTTEVH_DEFAULT_ADDEVENT;
ctx->publish.qos = JANUS_MQTTEVH_DEFAULT_QOS;
ctx->publish.retain = JANUS_MQTTEVH_DEFAULT_RETAIN;
ctx->connect.username = NULL;
ctx->connect.password = NULL;
ctx->disconnect.timeout = JANUS_MQTTEVH_DEFAULT_TIMEOUT;
ctx->will.enabled = FALSE;
ctx->will.qos = JANUS_MQTTEVH_DEFAULT_WILL_QOS;
ctx->will.retain = JANUS_MQTTEVH_DEFAULT_WILL_RETAIN;
ctx->tls.enable = JANUS_MQTTEVH_DEFAULT_TLS_ENABLE;
ctx->tls.verify_peer = JANUS_MQTTEVH_DEFAULT_TLS_VERIFY_PEER;
ctx->tls.verify_host = JANUS_MQTTEVH_DEFAULT_TLS_VERIFY_HOST;
/* Setup the event handler, if required */
janus_config_item *item = janus_config_get(config, config_general, janus_config_type_item, "enabled");
if(!item || !item->value || !janus_is_true(item->value)) {
JANUS_LOG(LOG_WARN, "MQTT event handler disabled\n");
goto error;
}
janus_mqtt_evh_enabled = TRUE;
/* MQTT URL */
url_item = janus_config_get(config, config_general, janus_config_type_item, "url");
ctx->connect.url= g_strdup((url_item && url_item->value) ? url_item->value : JANUS_MQTTEVH_DEFAULT_MQTTURL);
janus_config_item *mqtt_version = janus_config_get(config, config_general, janus_config_type_item, "mqtt_version");
const char *mqtt_version_str = (mqtt_version && mqtt_version->value) ? mqtt_version->value : JANUS_MQTTEVH_VERSION_DEFAULT;
if(strcmp(mqtt_version_str, JANUS_MQTTEVH_VERSION_3_1) == 0) {
ctx->connect.mqtt_version = MQTTVERSION_3_1;
} else if(strcmp(mqtt_version_str, JANUS_MQTTEVH_VERSION_3_1_1) == 0) {
ctx->connect.mqtt_version = MQTTVERSION_3_1_1;
} else if(strcmp(mqtt_version_str, JANUS_MQTTEVH_VERSION_5) == 0) {
#ifdef MQTTVERSION_5
ctx->connect.mqtt_version = MQTTVERSION_5;
#else
JANUS_LOG(LOG_FATAL, "Using MQTT v5 requires compilation with Paho >= 1.3.0\n");
goto error;
#endif
} else {
JANUS_LOG(LOG_FATAL, "Unknown MQTT version\n");
goto error;
}
janus_config_item *client_id_item = janus_config_get(config, config_general, janus_config_type_item, "client_id");
ctx->connect.client_id = g_strdup((client_id_item && client_id_item->value) ? client_id_item->value : "guest");
username_item = janus_config_get(config, config_general, janus_config_type_item, "username");
if(username_item && username_item->value) {
ctx->connect.username = g_strdup(username_item->value);
} else {
ctx->connect.username = NULL;
}
password_item = janus_config_get(config, config_general, janus_config_type_item, "password");
if(password_item && password_item->value) {
ctx->connect.password = g_strdup(password_item->value);
} else {
ctx->connect.password = NULL;
}
janus_config_item *json_item = janus_config_get(config, config_general, janus_config_type_item, "json");
if(json_item && json_item->value) {
/* Check how we need to format/serialize the JSON output */
if(!strcasecmp(json_item->value, "indented")) {
/* Default: indented, we use three spaces for that */
json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
} else if(!strcasecmp(json_item->value, "plain")) {
/* Not indented and no new lines, but still readable */
json_format = JSON_INDENT(0) | JSON_PRESERVE_ORDER;
} else if(!strcasecmp(json_item->value, "compact")) {
/* Compact, so no spaces between separators */
json_format = JSON_COMPACT | JSON_PRESERVE_ORDER;
} else {
JANUS_LOG(LOG_WARN, "Unsupported JSON format option '%s', using default (indented)\n", json_item->value);
json_format = JANUS_MQTTEVH_DEFAULT_JSON_FORMAT;
}
}
/* Which events should we subscribe to? */
item = janus_config_get(config, config_general, janus_config_type_item, "events");
if(item && item->value)
janus_events_edit_events_mask(item->value, &janus_mqttevh.events_mask);
/* Connect configuration */
keep_alive_interval_item = janus_config_get(config, config_general, janus_config_type_item, "keep_alive_interval");
ctx->connect.keep_alive_interval = (keep_alive_interval_item && keep_alive_interval_item->value) ?
atoi(keep_alive_interval_item->value) : JANUS_MQTTEVH_DEFAULT_KEEPALIVE;
if(ctx->connect.keep_alive_interval < 0) {
JANUS_LOG(LOG_ERR, "Invalid keep-alive value: %s (falling back to default)\n", keep_alive_interval_item->value);
ctx->connect.keep_alive_interval = JANUS_MQTTEVH_DEFAULT_KEEPALIVE;
}
cleansession_item = janus_config_get(config, config_general, janus_config_type_item, "cleansession");
ctx->connect.cleansession = (cleansession_item && cleansession_item->value) ?
atoi(cleansession_item->value) : JANUS_MQTTEVH_DEFAULT_CLEANSESSION;
if(ctx->connect.cleansession < 0) {
JANUS_LOG(LOG_ERR, "Invalid clean-session value: %s (falling back to default)\n", cleansession_item->value);
ctx->connect.cleansession = JANUS_MQTTEVH_DEFAULT_CLEANSESSION;
}
max_inflight_item = janus_config_get(config, config_general, janus_config_type_item, "max_inflight");
ctx->connect.max_inflight = (max_inflight_item && max_inflight_item->value) ?
atoi(max_inflight_item->value) : JANUS_MQTTEVH_DEFAULT_MAX_INFLIGHT;
if(ctx->connect.max_inflight < 0) {
JANUS_LOG(LOG_ERR, "Invalid max-inflight value: %s (falling back to default)\n", max_inflight_item->value);
ctx->connect.max_inflight = JANUS_MQTTEVH_DEFAULT_MAX_INFLIGHT;
}
max_buffered_item = janus_config_get(config, config_general, janus_config_type_item, "max_buffered");
ctx->connect.max_buffered = (max_buffered_item && max_buffered_item->value) ?
atoi(max_buffered_item->value) : JANUS_MQTTEVH_DEFAULT_MAX_BUFFERED;
if(ctx->connect.max_buffered < 0) {
JANUS_LOG(LOG_ERR, "Invalid max-buffered value: %s (falling back to default)\n", max_buffered_item->value);
ctx->connect.max_buffered = JANUS_MQTTEVH_DEFAULT_MAX_BUFFERED;
}
/* Disconnect configuration */
disconnect_timeout_item = janus_config_get(config, config_general, janus_config_type_item, "disconnect_timeout");
ctx->disconnect.timeout = (disconnect_timeout_item && disconnect_timeout_item->value) ?
atoi(disconnect_timeout_item->value) : JANUS_MQTTEVH_DEFAULT_DISCONNECT_TIMEOUT;
if(ctx->disconnect.timeout < 0) {
JANUS_LOG(LOG_ERR, "Invalid disconnect-timeout value: %s (falling back to default)\n", disconnect_timeout_item->value);
ctx->disconnect.timeout = JANUS_MQTTEVH_DEFAULT_DISCONNECT_TIMEOUT;
}
topic_item = janus_config_get(config, config_general, janus_config_type_item, "topic");
if(!topic_item || !topic_item->value) {
ctx->publish.topic = g_strdup(JANUS_MQTTEVH_DEFAULT_BASETOPIC);
} else {
ctx->publish.topic = g_strdup(topic_item->value);
}
addevent_item = janus_config_get(config, config_general, janus_config_type_item, "addevent");
if(addevent_item && addevent_item->value && janus_is_true(addevent_item->value)) {
ctx->addevent = TRUE;
}
retain_item = janus_config_get(config, config_general, janus_config_type_item, "retain");
if(retain_item && retain_item->value && janus_is_true(retain_item->value)) {
ctx->publish.retain = atoi(retain_item->value);;
if(ctx->publish.retain < 0) {
JANUS_LOG(LOG_ERR, "Invalid publish-retain value: %s (disabling)\n", retain_item->value);
ctx->publish.retain = 0;
}
}
qos_item = janus_config_get(config, config_general, janus_config_type_item, "qos");
ctx->publish.qos = (qos_item && qos_item->value) ? atoi(qos_item->value) : 1;
if(ctx->publish.qos < 0) {
JANUS_LOG(LOG_ERR, "Invalid publish-qos value: %s (falling back to default)\n", qos_item->value);
ctx->publish.qos = 1;
}
connect_status_item = janus_config_get(config, config_general, janus_config_type_item, "connect_status");
if(connect_status_item && connect_status_item->value) {
ctx->publish.connect_status = g_strdup(connect_status_item->value);
} else {
ctx->publish.connect_status = g_strdup(JANUS_MQTTEVH_DEFAULT_CONNECT_STATUS);
}
disconnect_status_item = janus_config_get(config, config_general, janus_config_type_item, "disconnect_status");
if(disconnect_status_item && disconnect_status_item->value) {
ctx->publish.disconnect_status = g_strdup(disconnect_status_item->value);
} else {
ctx->publish.disconnect_status = g_strdup(JANUS_MQTTEVH_DEFAULT_DISCONNECT_STATUS);
}
/* LWT config */
will_enabled_item = janus_config_get(config, config_general, janus_config_type_item, "will_enabled");
if(will_enabled_item && will_enabled_item->value && janus_is_true(will_enabled_item->value)) {
ctx->will.enabled = TRUE;
will_retain_item = janus_config_get(config, config_general, janus_config_type_item, "will_retain");
if(will_retain_item && will_retain_item->value && janus_is_true(will_retain_item->value)) {
ctx->will.retain = 1;
}
will_qos_item = janus_config_get(config, config_general, janus_config_type_item, "will_qos");
if(will_qos_item && will_qos_item->value) {
ctx->will.qos = atoi(will_qos_item->value);
if(ctx->will.qos < 0) {
JANUS_LOG(LOG_ERR, "Invalid will-qos value: %s (setting to 0)\n", will_qos_item->value);
ctx->will.qos = 0;
}
}
/* Using the topic for LWT as configured for publish and suffixed with JANUS_MQTTEVH_STATUS_TOPIC. */
char will_topic_buf[512];
snprintf(will_topic_buf, sizeof(will_topic_buf), "%s/%s", ctx->publish.topic, JANUS_MQTTEVH_STATUS_TOPIC);
ctx->will.topic = g_strdup(will_topic_buf);
}
/* TLS config*/
item = janus_config_get(config, config_general, janus_config_type_item, "tls_enable");
/* for old people */
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enable");
}
if(!item || !item->value || !janus_is_true(item->value)) {
JANUS_LOG(LOG_INFO, "MQTTEventHandler: MQTT TLS support disabled\n");
} else {
ctx->tls.enable = TRUE;
item = janus_config_get(config, config_general, janus_config_type_item, "tls_cacert");
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_cacert");
}
if(item && item->value) {
ctx->tls.cacert_file = janus_make_absolute_path(config_path, item->value);
}
item = janus_config_get(config, config_general, janus_config_type_item, "tls_client_cert");
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_client_cert");
}
if(item && item->value) {
ctx->tls.cert_file = janus_make_absolute_path(config_path, item->value);
}
item = janus_config_get(config, config_general, janus_config_type_item, "tls_client_key");
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_client_key");
}
if(item && item->value) {
ctx->tls.key_file = janus_make_absolute_path(config_path, item->value);
}
item = janus_config_get(config, config_general, janus_config_type_item, "tls_verify_peer");
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_peer");
}
if(item && item->value && janus_is_true(item->value)) {
ctx->tls.verify_peer = TRUE;
}
item = janus_config_get(config, config_general, janus_config_type_item, "tls_verify_hostname");
if(!item) {
item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_hostname");
}
if(item && item->value && janus_is_true(item->value)) {
ctx->tls.verify_host = TRUE;
}
}
#ifdef MQTTVERSION_5
if (ctx->connect.mqtt_version == MQTTVERSION_5) {
/* MQTT 5 specific configuration */
janus_config_array *add_user_properties_array = janus_config_get(config, config_general, janus_config_type_array, "add_user_properties");
if(add_user_properties_array) {
GList *add_user_properties_array_items = janus_config_get_arrays(config, add_user_properties_array);
if(add_user_properties_array_items != NULL) {
int add_user_properties_array_len = g_list_length(add_user_properties_array_items);
if(add_user_properties_array_len > 0) {
ctx->publish.add_user_properties = g_array_sized_new(FALSE, FALSE, sizeof(MQTTProperty), add_user_properties_array_len);
janus_mqttevh_set_add_user_property_user_data user_data = {
ctx->publish.add_user_properties,
config
};
g_list_foreach(
add_user_properties_array_items,
(GFunc)janus_mqttevh_set_add_user_property,
(gpointer)&user_data
);
}
}
}
}
#endif
if(!janus_mqtt_evh_enabled) {
JANUS_LOG(LOG_WARN, "MQTT event handler support disabled, giving up\n");
goto error;
}
/* Create a MQTT client */
MQTTAsync_createOptions create_options = MQTTAsync_createOptions_initializer;
#ifdef MQTTVERSION_5
if (ctx->connect.mqtt_version == MQTTVERSION_5) {
create_options.MQTTVersion = MQTTVERSION_5;
}
#endif
create_options.maxBufferedMessages = ctx->connect.max_buffered;