-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmosquitto_config.c
1789 lines (1544 loc) · 67.6 KB
/
mosquitto_config.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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2012, Anthony Minessale II <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <[email protected]>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Norm Brandinger <[email protected]>
*
* mod_mosquitto: Interface to an MQTT broker using Mosquitto
* Implements a Publish/Subscribe (pub/sub) messaging pattern using the Mosquitto API library
* Publishes FreeSWITCH events to one more more MQTT brokers
* Subscribes to topics located on one more more MQTT brokers
*
* MQTT http://mqtt.org/
* Mosquitto https://mosquitto.org/
*
*/
#include <switch.h>
#ifdef HAVE_OPENSSL
#include <openssl/ssl.h>
#endif
#include "mod_mosquitto.h"
#include "mosquitto_config.h"
#include "mosquitto_events.h"
#include "mosquitto_mosq.h"
#include "mosquitto_utils.h"
static mosquitto_profile_t *add_profile(const char *name);
static mosquitto_connection_t *add_connection(mosquitto_profile_t *profile, const char *name);
static mosquitto_publisher_t *add_publisher(mosquitto_profile_t *profile, const char *name);
static mosquitto_subscriber_t *add_subscriber(mosquitto_profile_t *profile, const char *name);
static mosquitto_topic_t *add_publisher_topic(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
const char *name);
static switch_status_t parse_settings(switch_xml_t cfg);
static switch_status_t parse_profiles(switch_xml_t cfg);
static switch_status_t parse_connections(switch_xml_t xconnection, mosquitto_profile_t *profile);
static switch_status_t parse_publishers(switch_xml_t xpublisher, mosquitto_profile_t *profile);
static switch_status_t parse_subscribers(switch_xml_t xsubscriber, mosquitto_profile_t *profile);
static switch_status_t parse_publisher_topics(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
switch_xml_t xpublisher);
static switch_status_t parse_subscriber_topics(mosquitto_profile_t *profile, mosquitto_subscriber_t *subscriber,
switch_xml_t xsubscriber);
static mosquitto_event_t *add_publisher_topic_event(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
mosquitto_topic_t *topic, const char *name,
switch_event_types_t event_type);
static void rand_str(char *dest, size_t length);
static switch_status_t parse_connection_tls(mosquitto_profile_t *profile, mosquitto_connection_t *connection,
switch_xml_t xconnection);
static switch_status_t parse_connection_will(mosquitto_profile_t *profile, mosquitto_connection_t *connection,
switch_xml_t xconnection);
/**
* @brief This function is used to add a new profile to the hash
*
* @details This function will allocate memory for a profile hash entry, initialize generic data types
* such as mutexes and create hashes for publishers, subscribers, connections associated with the new profile.
*
* @param[in] *name Name of the profile being created
*
* @retval pointer to the newly defined profile hash entry or NULL if memory could not be allocated
*/
static mosquitto_profile_t *add_profile(const char *name)
{
mosquitto_profile_t *profile = NULL;
switch_memory_pool_t *pool;
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile name not passed to add_profile()\n");
return profile;
}
if (!(profile = (mosquitto_profile_t *)switch_core_alloc(mosquitto_globals.pool, sizeof(*profile)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from pool for profile %s\n", name);
return profile;
}
switch_core_new_memory_pool(&pool);
profile = (mosquitto_profile_t *)switch_core_alloc(pool, sizeof(*profile));
profile->pool = pool;
profile->name = switch_core_strdup(profile->pool, name);
switch_mutex_init(&profile->mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_thread_rwlock_create(&profile->rwlock, profile->pool);
profile->log = (mosquitto_log_t *)switch_core_alloc(pool, sizeof(*profile->log));
switch_mutex_init(&profile->log->mutex, SWITCH_MUTEX_DEFAULT, profile->pool);
switch_mutex_init(&profile->connections_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&profile->connections);
switch_mutex_init(&profile->publishers_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&profile->publishers);
switch_mutex_init(&profile->subscribers_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&profile->subscribers);
switch_core_hash_insert_locked(mosquitto_globals.profiles, profile->name, profile,
mosquitto_globals.profiles_mutex);
log(SWITCH_LOG_INFO, "Profile %s successfully added\n", name);
return profile;
}
/**
* @brief This function is used to locate a profile by name
*
* @details This function searches the global profiles hash in an attempt to find a profile with a matching name.
* The name is used as a key, so must be unique (no two profiles can have the same name)
*
* @param[in] *name Name of the profile being searched for
*
* @retval pointer to the hash of the profile or NULL
*/
mosquitto_profile_t *locate_profile(const char *name)
{
mosquitto_profile_t *profile = NULL;
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile name not passed to locate_profile()\n");
return profile;
}
if (!(profile = (mosquitto_profile_t *)switch_core_hash_find_locked(mosquitto_globals.profiles, name,
mosquitto_globals.profiles_mutex))) {
// Not finding a profile is expected when starting up or when a new profile is being added
// The caller needs to handle the case when not finding a profile is a problem
// log(SWITCH_LOG_WARNING, "Unable to locate profile %s\n", name);
}
return profile;
}
/**
* @brief This function is used to add a new connection to the hash (within a profile hash)
*
* @details This function will allocate memory for a connection hash entry and initialize generic data types
* such as mutexes.
*
* @param[in] *profile Pointer to the profile hash where the new connection should be added
* @param[in] *name Name of the connection being created
*
* @retval pointer to the newly defined connection hash entry or NULL
*/
static mosquitto_connection_t *add_connection(mosquitto_profile_t *profile, const char *name)
{
mosquitto_connection_t *connection = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to add_connection()\n");
return connection;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s connection name not passed to add_connection()\n", profile->name);
return connection;
}
if (!(connection = (mosquitto_connection_t *)switch_core_alloc(profile->pool, sizeof(*connection)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from pool for connection %s\n", name);
return connection;
}
connection->name = switch_core_strdup(profile->pool, name);
connection->profile_name = switch_core_strdup(profile->pool, profile->name);
switch_mutex_init(&connection->mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_thread_rwlock_create(&connection->rwlock, profile->pool);
switch_core_hash_insert_locked(profile->connections, connection->name, connection, profile->connections_mutex);
log(SWITCH_LOG_INFO, "Profile %s connection: %s successfully added\n", profile->name, name);
return connection;
}
/**
* @brief This function is used to locate a connection by name
*
* @details This function searches the connections hash associated with the profile hash entry in an attempt
* to find a connection with a matching name. the name is used as a key, so must be unique
* (two connections within the same profile cannot have the same name)
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *name Name of the connection to locate
*
* @retval pointer to the hash entry of the connection or NULL
*/
mosquitto_connection_t *locate_connection(mosquitto_profile_t *profile, const char *name)
{
mosquitto_connection_t *connection = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to locate_connection()\n");
return connection;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s connection name not passed to locate_connection()\n", profile->name);
return connection;
}
if (!(connection = (mosquitto_connection_t *)switch_core_hash_find_locked(profile->connections, name,
profile->connections_mutex))) {
// Not finding a connection is expected when starting up or when a new connection is being added
// The caller needs to handle the case when not finding a connection is a problem
// log(SWITCH_LOG_WARNING, "Unable to locate connection: %s for profile %s\n", name, profile->name);
}
return connection;
}
/**
* @brief This function is used to remove a profile by name
*
* @details This function searches the profiles hash for a matching name and if found, removes all
* publishers, subscribers, connections, topics, etc associated with this profile. Once the profile
* has been removed, its memory pool will be destroyed (allowing the system to reclaim memory).
*
* @param[in] *profile Name of the profile to remove
*
* @retval SWITCH_STATUS_GENERR if the profile could not be found
*/
switch_status_t remove_profile(const char *name)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
mosquitto_profile_t *profile = NULL;
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile name not passed to remove_profile()\n");
return SWITCH_STATUS_GENERR;
}
profile = locate_profile(name);
if (!profile) {
log(SWITCH_LOG_WARNING, "Cannot remove unknown profile %s\n", name);
return SWITCH_STATUS_GENERR;
}
log(SWITCH_LOG_NOTICE, "Profile %s shutting down publishers\n", profile->name);
switch_mutex_lock(profile->publishers_mutex);
for (switch_hash_index_t *publishers_hi = switch_core_hash_first(profile->publishers); publishers_hi;
publishers_hi = switch_core_hash_next(&publishers_hi)) {
mosquitto_publisher_t *publisher = NULL;
void *val;
switch_core_hash_this(publishers_hi, NULL, NULL, &val);
publisher = (mosquitto_publisher_t *)val;
switch_mutex_lock(publisher->topics_mutex);
for (switch_hash_index_t *topics_hi = switch_core_hash_first(publisher->topics); topics_hi;
topics_hi = switch_core_hash_next(&topics_hi)) {
mosquitto_topic_t *topic = NULL;
void *val;
switch_core_hash_this(topics_hi, NULL, NULL, &val);
topic = (mosquitto_topic_t *)val;
log(SWITCH_LOG_NOTICE, "shutting down publisher:%s topic: %s\n", publisher->name, topic->name);
switch_mutex_lock(topic->events_mutex);
for (switch_hash_index_t *events_hi = switch_core_hash_first(topic->events); events_hi;
events_hi = switch_core_hash_next(&events_hi)) {
mosquitto_event_t *event = NULL;
void *val;
switch_core_hash_this(events_hi, NULL, NULL, &val);
event = (mosquitto_event_t *)val;
switch_safe_free(event->userdata);
log(SWITCH_LOG_NOTICE, "Profile %s publisher %s topic %s event %s unbind %d\n", profile->name,
publisher->name, topic->name, event->name, status);
}
switch_core_hash_destroy(&topic->events);
switch_mutex_unlock(topic->events_mutex);
switch_core_hash_delete(publisher->topics, topic->name);
}
switch_core_hash_destroy(&publisher->topics);
switch_mutex_unlock(publisher->topics_mutex);
log(SWITCH_LOG_NOTICE, "deleting publisher name:%s from profile hash\n", publisher->name);
switch_core_hash_delete(profile->publishers, publisher->name);
}
switch_core_hash_destroy(&profile->publishers);
switch_mutex_unlock(profile->publishers_mutex);
log(SWITCH_LOG_NOTICE, "shutting down subscribers\n");
switch_mutex_lock(profile->subscribers_mutex);
for (switch_hash_index_t *subscribers_hi = switch_core_hash_first(profile->subscribers); subscribers_hi;
subscribers_hi = switch_core_hash_next(&subscribers_hi)) {
mosquitto_subscriber_t *subscriber = NULL;
void *val;
switch_core_hash_this(subscribers_hi, NULL, NULL, &val);
subscriber = (mosquitto_subscriber_t *)val;
switch_mutex_lock(subscriber->topics_mutex);
for (switch_hash_index_t *topics_hi = switch_core_hash_first(subscriber->topics); topics_hi;
topics_hi = switch_core_hash_next(&topics_hi)) {
mosquitto_topic_t *topic = NULL;
void *val;
switch_core_hash_this(topics_hi, NULL, NULL, &val);
topic = (mosquitto_topic_t *)val;
log(SWITCH_LOG_WARNING, "shutting down subscriber topic: %s\n", topic->name);
subscriber_topic_deactivate(profile, subscriber, topic);
switch_core_hash_delete(subscriber->topics, topic->name);
}
switch_core_hash_destroy(&subscriber->topics);
switch_mutex_unlock(subscriber->topics_mutex);
log(SWITCH_LOG_NOTICE, "deleting subscriber name:%s from profile hash\n", subscriber->name);
switch_core_hash_delete(profile->subscribers, subscriber->name);
}
switch_core_hash_destroy(&profile->subscribers);
switch_mutex_unlock(profile->subscribers_mutex);
log(SWITCH_LOG_NOTICE, "Profile %s shutting down connections\n", profile->name);
switch_mutex_lock(profile->connections_mutex);
for (switch_hash_index_t *connections_hi = switch_core_hash_first(profile->connections); connections_hi;
connections_hi = switch_core_hash_next(&connections_hi)) {
mosquitto_connection_t *connection = NULL;
switch_bool_t force_loop_stop = SWITCH_TRUE;
void *val;
switch_core_hash_this(connections_hi, NULL, NULL, &val);
connection = (mosquitto_connection_t *)val;
log(SWITCH_LOG_NOTICE, "Profile %s connection %s being disconnected\n", profile->name, connection->name);
mosq_disconnect(connection, force_loop_stop);
mosq_destroy(connection);
switch_core_hash_delete(profile->connections, connection->name);
}
switch_core_hash_destroy(&profile->connections);
switch_mutex_unlock(profile->connections_mutex);
switch_mutex_lock(profile->log->mutex);
if (zstr(profile->log->name)) {
log(SWITCH_LOG_ERROR, "Unable to close log file for profile %s name is NULL\n", profile->name);
} else if (profile->log->logfile == NULL) {
log(SWITCH_LOG_ERROR, "Unable to close log file for profile %s name %s file handle is NULL\n", profile->name,
profile->log->name);
} else if ((status = switch_file_close(profile->log->logfile)) != SWITCH_STATUS_SUCCESS) {
log(SWITCH_LOG_ERROR, "Failed to close log file for profile %s name %s\n", profile->name, profile->log->name);
}
switch_mutex_unlock(profile->log->mutex);
switch_mutex_destroy(profile->log->mutex);
status = SWITCH_STATUS_SUCCESS;
switch_core_hash_delete(mosquitto_globals.profiles, profile->name);
switch_core_destroy_memory_pool(&profile->pool);
return status;
}
/**
* @brief This function is used to remove a connection by name
*
* @details This function searches the connections hash associated with a specific profile for a matching name and if
*found, removes it from the hash. If any connections to brokers are active, they are first disconnected.
*
* @param[in] *profile Pointer to the profile containing the connections hash
* @param[in] *name Name of the connection to be removed
*
* @retval SWITCH_STATUS_GENERR if the connection could could not be found
*/
switch_status_t remove_connection(mosquitto_profile_t *profile, const char *name)
{
switch_status_t status = SWITCH_STATUS_FALSE;
mosquitto_connection_t *connection = NULL;
switch_bool_t force_loop_stop = SWITCH_TRUE;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to remove_connection()\n");
return SWITCH_STATUS_GENERR;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s connection name not passed to remove_connection()\n", profile->name);
return SWITCH_STATUS_GENERR;
}
if (!(connection = locate_connection(profile, name))) {
log(SWITCH_LOG_WARNING, "Cannot remove unknown connection: %s from profile %s\n", name, profile->name);
return status;
}
status = mosq_disconnect(connection, force_loop_stop);
mosq_destroy(connection);
switch_core_hash_delete_locked(profile->connections, connection->name, profile->connections_mutex);
return status;
}
/**
* @brief This function is used to remove a publisher by name
*
* @details This function searches the publishers hash associated with a specific profile for a matching name and if
*found, removes it from the hash.
*
* @param[in] *profile Pointer to the profile containing the publishers hash
* @param[in] *name Name of the publisher to be removed
*
* @retval SWITCH_STATUS_GENERR if the publisher could could not be found
*/
switch_status_t remove_publisher(mosquitto_profile_t *profile, const char *name)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
mosquitto_publisher_t *publisher = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to remove_publisher()\n");
return SWITCH_STATUS_GENERR;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher name not passed to remove_publisher()\n", profile->name);
return SWITCH_STATUS_GENERR;
}
if (!(publisher = locate_publisher(profile, name))) {
log(SWITCH_LOG_WARNING, "Cannot remove unknown publisher: %s from profile %s\n", name, profile->name);
return SWITCH_STATUS_GENERR;
}
switch_core_hash_delete_locked(profile->publishers, publisher->name, profile->publishers_mutex);
log(SWITCH_LOG_INFO, "Removed publisher: %s from profile %s\n", name, profile->name);
return status;
}
/**
* @brief This function is used to remove a subscriber by name
*
* @details This function searches the subscribers hash associated with a specific profile for a matching name and if
*found, removes it from the hash.
*
* @param[in] *profile Pointer to the profile containing the subscribers hash
* @param[in] *name Name of the subscriber to be removed
*
* @retval SWITCH_STATUS_GENERR if the subscriber could could not be found
*/
switch_status_t remove_subscriber(mosquitto_profile_t *profile, const char *name)
{
switch_status_t status = SWITCH_STATUS_FALSE;
mosquitto_subscriber_t *subscriber = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to remove_subscriber()\n");
return SWITCH_STATUS_GENERR;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s subscriber name not passed to remove_subscriber()\n", profile->name);
return SWITCH_STATUS_GENERR;
}
if (!(subscriber = locate_subscriber(profile, name))) {
log(SWITCH_LOG_WARNING, "Cannot remove unknown subscriber: %s from profile %s\n", name, profile->name);
return SWITCH_STATUS_GENERR;
}
switch_core_hash_delete_locked(profile->subscribers, subscriber->name, profile->subscribers_mutex);
log(SWITCH_LOG_INFO, "Removed subscriber: %s from profile %s\n", name, profile->name);
status = SWITCH_STATUS_SUCCESS;
return status;
}
/**
* @brief This function is used to add a publisher to a profile
*
* @details This function created and adds a publisher has to the specified profile hash.
*
* @param[in] *profile Pointer to the profile containing the publishers hash
* @param[in] *name Name of the publisher to be added
*
* @retval Pointer to the newly added publisher or NULL
*/
static mosquitto_publisher_t *add_publisher(mosquitto_profile_t *profile, const char *name)
{
mosquitto_publisher_t *publisher = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to add_publisher()\n");
return NULL;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher name not passed to add_publisher()\n", profile->name);
return NULL;
}
if ((publisher = locate_publisher(profile, name))) {
log(SWITCH_LOG_ERROR, "Publisher name: %s is already associated with profile %s and cannot be added\n", name,
profile->name);
return NULL;
}
if (!(publisher = (mosquitto_publisher_t *)switch_core_alloc(profile->pool, sizeof(*publisher)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from pool for profile %s publisher: %s\n", profile->name,
name);
return publisher;
}
publisher->name = switch_core_strdup(profile->pool, name);
publisher->profile_name = switch_core_strdup(profile->pool, profile->name);
switch_mutex_init(&publisher->mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_thread_rwlock_create(&publisher->rwlock, profile->pool);
switch_mutex_init(&publisher->topics_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&publisher->topics);
switch_core_hash_insert_locked(profile->publishers, publisher->name, publisher, profile->publishers_mutex);
log(SWITCH_LOG_INFO, "Profile %s publisher:%s successfully added\n", profile->name, name);
return publisher;
}
/**
* @brief This function is used to locate a publisher by name
*
* @details This function searches the publishers hash associated with the profile hash entry in an attempt
* to find a publisher with a matching name. the name is used as a key, so must be unique
* (two publishers within the same profile cannot have the same name)
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *name Name of the publisher to locate
*
* @retval pointer to the hash entry of the publisher or NULL
*/
mosquitto_publisher_t *locate_publisher(mosquitto_profile_t *profile, const char *name)
{
mosquitto_publisher_t *publisher = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to locate_publisher()\n");
return publisher;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher name not passed to locate_publisher()\n", profile->name);
return publisher;
}
if (!(publisher = (mosquitto_publisher_t *)switch_core_hash_find_locked(profile->publishers, name,
profile->publishers_mutex))) {
// Not finding a publisher is expected when starting up or when a new publisher is being added
// The caller needs to handle the case when not finding a publisher is a problem
// log(SWITCH_LOG_WARNING, "Unable to locate publisher: %s for provider: %s\n", name, profile->name);
}
return publisher;
}
/**
* @brief This function is used to add a subscriber to a profile
*
* @details This function created and adds a subscriber has to the specified profile hash.
*
* @param[in] *profile Pointer to the profile containing the subscribers hash
* @param[in] *name Name of the subscriber to be added
*
* @retval Pointer to the newly added subscriber or NULL
*/
static mosquitto_subscriber_t *add_subscriber(mosquitto_profile_t *profile, const char *name)
{
mosquitto_subscriber_t *subscriber = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to add_subscriber()\n");
return subscriber;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s subscriber name not passed to add_subscriber()\n", profile->name);
return subscriber;
}
if ((subscriber = locate_subscriber(profile, name))) {
log(SWITCH_LOG_ERROR, "Subscriber name: %s is already associated with profile %s and cannot be added\n", name,
profile->name);
return NULL;
}
if (!(subscriber = (mosquitto_subscriber_t *)switch_core_alloc(profile->pool, sizeof(*subscriber)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from pool for profile %s subscriber:%s\n", profile->name,
name);
return subscriber;
}
subscriber->name = switch_core_strdup(profile->pool, name);
subscriber->profile_name = switch_core_strdup(profile->pool, profile->name);
switch_mutex_init(&subscriber->mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_thread_rwlock_create(&subscriber->rwlock, profile->pool);
switch_mutex_init(&subscriber->topics_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&subscriber->topics);
switch_core_hash_insert_locked(profile->subscribers, subscriber->name, subscriber, profile->subscribers_mutex);
log(SWITCH_LOG_INFO, "Profile %s subscriber: %s successfully added\n", profile->name, name);
return subscriber;
}
/**
* @brief This function is used to locate a subscriber by name
*
* @details This function searches the subscribers hash associated with the profile hash entry in an attempt
* to find a subscriber with a matching name. the name is used as a key, so must be unique
* (two subscribers within the same profile cannot have the same name)
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *name Name of the subscriber to locate
*
* @retval pointer to the hash entry of the subscriber or NULL
*/
mosquitto_subscriber_t *locate_subscriber(mosquitto_profile_t *profile, const char *name)
{
mosquitto_subscriber_t *subscriber = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to locate_subscriber()\n");
return subscriber;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s subscriber name not passed to locate_subscriber()\n", profile->name);
return subscriber;
}
if (!(subscriber = (mosquitto_subscriber_t *)switch_core_hash_find_locked(profile->subscribers, name,
profile->subscribers_mutex))) {
// Not finding a subscriber is expected when starting up or when a new subscriber is being added
// The caller needs to handle the case when not finding a subscriber is a problem
// log(SWITCH_LOG_WARNING, "Profile %s unable to locate subscriber: %s\n", profile->name, name);
}
return subscriber;
}
/**
* @brief This function is used to parse the subscribers from the configuration file
*
* @details Subscribers are located within the profile section of the configuration file
*
* @param[in] xprofile Profile section of the configuration file
* @param[in] *profile Pointer to the profile hash that the newly parsed subscribers will be added to
*
* @retval SWITCH_STATUS_SUCCESS indicates this routine completed
*/
static switch_status_t parse_subscribers(switch_xml_t xprofile, mosquitto_profile_t *profile)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_xml_t xsubscribers, xsubscriber, param;
if ((xsubscribers = switch_xml_child(xprofile, "subscribers"))) {
for (xsubscriber = switch_xml_child(xsubscribers, "subscriber"); xsubscriber; xsubscriber = xsubscriber->next) {
mosquitto_subscriber_t *subscriber;
const char *name = switch_xml_attr(xsubscriber, "name");
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Required field name missing\n");
continue;
}
subscriber = add_subscriber(profile, name);
subscriber->enable = SWITCH_FALSE;
for (param = switch_xml_child(xsubscriber, "param"); param; param = param->next) {
char *var = NULL;
char *val = NULL;
var = (char *)switch_xml_attr_soft(param, "name");
val = (char *)switch_xml_attr_soft(param, "value");
if (!strncasecmp(var, "enable", 6) && !zstr(val)) { subscriber->enable = switch_true(val); }
}
parse_subscriber_topics(profile, subscriber, xsubscriber);
}
}
return status;
}
/**
* @brief This function is used to parse the publishers from the configuration file
*
* @details Publishers are located within the profile section of the configuration file
*
* @param[in] xprofile Profile section of the configuration file
* @param[in] *profile Pointer to the profile hash that the newly parsed publishers will be added to
*
* @retval SWITCH_STATUS_SUCCESS indicates this routine completed
*/
static switch_status_t parse_publishers(switch_xml_t xprofile, mosquitto_profile_t *profile)
{
switch_xml_t xpublisher, xpublishers, param;
switch_status_t status = SWITCH_STATUS_SUCCESS;
if ((xpublishers = switch_xml_child(xprofile, "publishers"))) {
for (xpublisher = switch_xml_child(xpublishers, "publisher"); xpublisher; xpublisher = xpublisher->next) {
mosquitto_publisher_t *publisher;
const char *name = switch_xml_attr(xpublisher, "name");
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Required field name missing\n");
continue;
}
publisher = add_publisher(profile, name);
publisher->enable = SWITCH_FALSE;
for (param = switch_xml_child(xpublisher, "param"); param; param = param->next) {
char *var = NULL;
char *val = NULL;
var = (char *)switch_xml_attr_soft(param, "name");
val = (char *)switch_xml_attr_soft(param, "value");
if (!strncasecmp(var, "enable", 6) && !zstr(val)) {
publisher->enable = switch_true(val);
} else if (!strncasecmp(var, "event", 5) && !zstr(val)) {
} else {
}
}
parse_publisher_topics(profile, publisher, xpublisher);
}
}
return status;
}
/**
* @brief This function is used to add a new topic to an existing publisher
*
* @details Each publisher can have one or more associated topics
*
* @param[in] *profile Pointer to the profile that the publisher belongs to
* @param[in] *publisher Pointer to the publisher that the will have the topic added
* @param[in] name Name of the topic to be added to the publisher
*
* @retval Address of the newly added topic or NULL
*/
static mosquitto_topic_t *add_publisher_topic(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
const char *name)
{
mosquitto_topic_t *topic = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to add_publisher_topic()\n");
return NULL;
}
if (!publisher) {
log(SWITCH_LOG_ERROR, "Profile %s publisher not passed to add_publisher_topic()\n", profile->name);
return NULL;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic name not passed to add_publisher_topic()\n", profile->name,
publisher->name);
return NULL;
}
if ((topic = locate_publisher_topic(profile, publisher, name))) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic: %s exists and cannot be added\n", profile->name,
publisher->name, name);
return NULL;
}
if (!(topic = (mosquitto_topic_t *)switch_core_alloc(profile->pool, sizeof(*topic)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from profile %s publisher %s for topic %s\n", profile->name,
publisher->name, name);
return topic;
}
topic->name = switch_core_strdup(profile->pool, name);
topic->publisher_name = switch_core_strdup(profile->pool, publisher->name);
switch_mutex_init(&topic->mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_thread_rwlock_create(&topic->rwlock, profile->pool);
switch_mutex_init(&topic->events_mutex, SWITCH_MUTEX_NESTED, profile->pool);
switch_core_hash_init(&topic->events);
switch_core_hash_insert_locked(publisher->topics, topic->name, topic, publisher->topics_mutex);
log(SWITCH_LOG_INFO, "Profile %s publisher %s topic %s successfully added\n", profile->name, publisher->name,
topic->name);
return topic;
}
/**
* @brief This function is used to add an event to a publishers topic
*
* @details Each publisher topic can have one or more events bound to it
*
* @param[in] *profile Pointer to the profile that the publisher belongs to
* @param[in] *publisher Pointer to the publisher that the topic belongs to
* @param[in] *topic Pointer to the topic that will have the event added
* @param[in] name Name of the event to be added to the topic
*
* @retval Address of the newly added event or NULL
*/
static mosquitto_event_t *add_publisher_topic_event(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
mosquitto_topic_t *topic, const char *name,
switch_event_types_t event_type)
{
mosquitto_event_t *event = NULL;
mosquitto_event_userdata_t *userdata = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to add_publisher_topic_event()\n");
return NULL;
}
if (!publisher) {
log(SWITCH_LOG_ERROR, "Profile %s publisher not passed to add_publisher_topic_event()\n", profile->name);
return NULL;
}
if (!topic) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic not passed to add_publisher_topic_event()\n",
profile->name, publisher->name);
return NULL;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic %s event name not passed to add_publisher()\n",
profile->name, publisher->name, topic->name);
return NULL;
}
if (!(event = (mosquitto_event_t *)switch_core_alloc(profile->pool, sizeof(*event)))) {
log(SWITCH_LOG_ERROR, "Failed to allocate memory from profile %s publisher %s for topic %s event %s\n",
profile->name, publisher->name, topic->name, name);
return event;
}
// userdata = (mosquitto_event_userdata_t *)switch_core_alloc(profile->pool, sizeof(*userdata));
switch_malloc(userdata, sizeof(mosquitto_event_userdata_t));
event->userdata = userdata;
event->name = switch_core_strdup(profile->pool, name);
event->event_type = event_type;
switch_core_hash_insert_locked(topic->events, event->name, event, topic->events_mutex);
log(SWITCH_LOG_INFO, "Profile %s publisher %s topic %s event %s successfully added\n", profile->name,
publisher->name, topic->name, event->name);
return event;
}
/**
* @brief This function is used to locate a topic by name given a profile and publisher
*
* @details This function searches the topic hash asssociated with a publisher and profile in an attempt to
* to find a matching name. The name is used as a key, so must be unique
* (two topics within the same profile and publisher cannot have the same name)
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *publisher Pointer to a publisher hash entry
* @param[in] *name Name of the topic name to locate
*
* @retval pointer to the hash entry of the topic or NULL
*/
mosquitto_topic_t *locate_publisher_topic(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
const char *name)
{
mosquitto_topic_t *topic = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to locate_publisher_topic()\n");
return NULL;
}
if (!publisher) {
log(SWITCH_LOG_ERROR, "Profile %s publisher not passed to locate_publisher_topic()\n", profile->name);
return NULL;
}
if (zstr(name)) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic name not passed to locate_publisher_topic()\n",
profile->name, publisher->name);
return NULL;
}
if (!(topic =
(mosquitto_topic_t *)switch_core_hash_find_locked(publisher->topics, name, publisher->topics_mutex))) {
// Not finding a publisher topic is expected when starting up or when a new publisher topic is being added
// The caller needs to handle the case when not finding a publisher topic is a problem
// log(SWITCH_LOG_WARNING, "Profile %s publisher %s topic %s not found\n", publisher->profile_name,
// publisher->name, name);
}
return topic;
}
/**
* @brief This function is used to locate an event by name given a profile, publisher and topic
*
* @details This function searches the event hash asssociated with a publisher, profile and topic in an attempt to
* to find a matching name. The name is used as a key, so must be unique
* (two events within the same profile, publisher and topic cannot have the same name)
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *publisher Pointer to a publisher hash entry
* @param[in] *name Name of the topic name to locate
*
* @retval pointer to the hash entry of the topic or NULL
*/
mosquitto_event_t *locate_publisher_topic_event(mosquitto_profile_t *profile, mosquitto_publisher_t *publisher,
mosquitto_topic_t *topic, const char *name)
{
mosquitto_event_t *event = NULL;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to locate_publisher_topic_event()\n");
return NULL;
}
if (!publisher) {
log(SWITCH_LOG_ERROR, "Profile %s publisher not passed to locate_publisher_topic_event()\n", profile->name);
return NULL;
}
if (!topic) {
log(SWITCH_LOG_ERROR, "Profile %s publisher %s topic not passed to locate_publisher_topic_event()\n",
profile->name, publisher->name);
return NULL;
}
if (zstr(name)) {
log(SWITCH_LOG_WARNING,
"Profile %s publisher %s topic %s event name not passed to locate_publisher_topic_event()\n", profile->name,
publisher->name, topic->name);
return event;
}
if (!(event = (mosquitto_event_t *)switch_core_hash_find_locked(topic->events, name, topic->events_mutex))) {
// Not finding an event is expected when starting up or when a new event is being added
// The caller needs to handle the case when not finding an event is a problem
// log(SWITCH_LOG_INFO, "Profile %s publisher %s topic %s event %s not found\n", profile->name, publisher->name,
// topic->name, name);
}
return event;
}
/**
* @brief This function is used to parse the connection will settings from the configuration file
*
* @details will settings are located within the connection section of the configuration file
*
* @param[in] *profile Pointer to a profile hash entry
* @param[in] *connection Pointer to a connection hash entry
* @param[in] xconnection Connection section of the configuration file
*
* @retval SWITCH_STATUS_SUCCESS indicates this routine completed
*/
static switch_status_t parse_connection_will(mosquitto_profile_t *profile, mosquitto_connection_t *connection,
switch_xml_t xconnection)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_xml_t param;
if (!profile) {
log(SWITCH_LOG_ERROR, "Profile not passed to parse_connection_will()\n");
return SWITCH_STATUS_GENERR;
}
if (!connection) {
log(SWITCH_LOG_ERROR, "Profile %s connection not passed to parse_connection_will()\n", profile->name);
return SWITCH_STATUS_GENERR;
}
/* Set default values for all the possible settings */
connection->will.enable = SWITCH_FALSE;
connection->will.topic = NULL;