-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathbridge.c
More file actions
1226 lines (1060 loc) · 36.3 KB
/
bridge.c
File metadata and controls
1226 lines (1060 loc) · 36.3 KB
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
/*
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifndef WIN32
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef WIN32
#include <unistd.h>
#else
#include <process.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include "mosquitto.h"
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
#include "sys_tree.h"
#include "tls_mosq.h"
#include "util_mosq.h"
#include "will_mosq.h"
#include "utlist.h"
#ifdef WITH_BRIDGE
static void bridge__update_backoff(struct mosquitto__bridge *bridge);
#if defined(__GLIBC__) && defined(WITH_ADNS)
static int bridge__connect_step1(struct mosquitto *context);
static int bridge__connect_step2(struct mosquitto *context);
#endif
static void bridge__packet_cleanup(struct mosquitto *context);
static void bridge_auth_credentials(struct mosquitto__bridge* bridge)
{
// If an auth bridge plugin is available load username/password
// for bridge authentication.
struct mosquitto__callback *cb_base, *cb_next;
struct mosquitto__callback* load_bridge_cred = db.config->security_options.plugin_callbacks.load_bridge_cred;
DL_FOREACH_SAFE( load_bridge_cred, cb_base, cb_next )
{
struct mosquitto_evt_load_bridge_cred event_data;
memset(&event_data, 0, sizeof(event_data));
event_data.bridge_name = bridge->name;
event_data.bridge_address = bridge->addresses;
int rc = cb_base->cb(MOSQ_EVT_LOAD_BRIDGE_CRED, &event_data, cb_base->userdata);
/* If bridge auth plugin is returning MOSQ_ERR_AUTH_DENIED username/password
* will be removed and set to NULL */
if (rc == MOSQ_ERR_AUTH_DENIED){
mosquitto_free(bridge->remote_username);
mosquitto_free(bridge->remote_password);
bridge->remote_username = NULL;
bridge->remote_password = NULL;
break;
}
/* If username/password are set by the bridge auth plugin we are
* using them. Any already existing username/password will be
* removed and exchanged by the new ones */
if (rc == MOSQ_ERR_SUCCESS && event_data.username && event_data.password){
mosquitto_free(bridge->remote_username);
mosquitto_free(bridge->remote_password);
bridge->remote_username = event_data.username;
bridge->remote_password = event_data.password;
break;
}
/* If bridge auth plugin is returning MOSQ_ERR_NOT_FOUND no further
* bridge auth plugin will be called even when configured, instead
* configured remote_username/remote_password will be used */
if (rc == MOSQ_ERR_NOT_FOUND){
break;
}
/* If bridge auth plugin is returning MOSQ_ERR_PLUGIN_DEFER the
* next plugin will be asked for username/password, if present */
if (rc == MOSQ_ERR_PLUGIN_DEFER) {
continue;
}
/* On all other return values we are leaving the loop */
break;
}
}
static struct mosquitto* bridge__new(struct mosquitto__bridge* bridge)
{
struct mosquitto *new_context = NULL;
struct mosquitto **bridges;
char *local_id;
assert(bridge);
local_id = mosquitto_strdup(bridge->local_clientid);
if(!local_id){
return NULL;
}
HASH_FIND(hh_id, db.contexts_by_id, local_id, strlen(local_id), new_context);
if(new_context){
/* (possible from persistent db) */
mosquitto_FREE(local_id);
}else{
/* id wasn't found, so generate a new context */
new_context = context__init();
if(!new_context){
mosquitto_FREE(local_id);
return NULL;
}
new_context->id = local_id;
context__add_to_by_id(new_context);
}
new_context->transport = mosq_t_tcp;
new_context->bridge = bridge;
new_context->is_bridge = true;
bridge_auth_credentials(bridge);
new_context->username = bridge->remote_username;
new_context->password = bridge->remote_password;
#ifdef WITH_TLS
new_context->tls_cafile = bridge->tls_cafile;
new_context->tls_capath = bridge->tls_capath;
new_context->tls_certfile = bridge->tls_certfile;
new_context->tls_keyfile = bridge->tls_keyfile;
new_context->tls_cert_reqs = SSL_VERIFY_PEER;
new_context->tls_ocsp_required = bridge->tls_ocsp_required;
new_context->tls_version = bridge->tls_version;
new_context->tls_insecure = bridge->tls_insecure;
new_context->tls_alpn = bridge->tls_alpn;
new_context->tls_ciphers = bridge->tls_ciphers;
new_context->tls_13_ciphers = bridge->tls_13_ciphers;
new_context->tls_engine = NULL;
new_context->tls_keyform = mosq_k_pem;
new_context->tls_use_os_certs = bridge->tls_use_os_certs;
new_context->ssl_ctx_defaults = true;
#ifdef FINAL_WITH_TLS_PSK
new_context->tls_psk_identity = bridge->tls_psk_identity;
new_context->tls_psk = bridge->tls_psk;
#endif
#endif
bridge->try_private_accepted = true;
if(bridge->clean_start_local == -1){
/* default to "regular" clean start setting */
bridge->clean_start_local = bridge->clean_start;
}
new_context->retain_available = bridge->outgoing_retain;
new_context->protocol = bridge->protocol_version;
if(!bridge->clean_start_local){
new_context->session_expiry_interval = MQTT_SESSION_EXPIRY_NEVER;
plugin_persist__handle_client_add(new_context);
if(new_context->expiry_list_item){
/* We've restored from persistence and been added to the session
* expiry list, even though we should never be expired */
session_expiry__remove(new_context);
}
}
bridges = mosquitto_realloc(db.bridges, (size_t)(db.bridge_count+1)*sizeof(struct mosquitto *));
if(bridges){
db.bridges = bridges;
db.bridge_count++;
db.bridges[db.bridge_count-1] = new_context;
}else{
return NULL;
}
return new_context;
}
static void bridge__destroy(struct mosquitto *context)
{
send__disconnect(context, MQTT_RC_SUCCESS, NULL);
context__cleanup(context, true);
}
void bridge__start_all(void)
{
for(int i=0; i<db.config->bridge_count; i++){
struct mosquitto *context;
int ret;
context = bridge__new(db.config->bridges[i]);
if(!context){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return;
}
assert(context);
#if defined(__GLIBC__) && defined(WITH_ADNS)
context->bridge->restart_t = 1; /* force quick restart of bridge */
loop__update_next_event(1000);
ret = bridge__connect_step1(context);
#else
ret = bridge__connect(context);
#endif
if(ret && ret != MOSQ_ERR_CONN_PENDING){
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Unable to connect bridge %s.",
context->bridge->name);
}
db.config->bridges[i] = NULL;
}
}
static int bridge__set_tcp_keepalive(struct mosquitto *context)
{
unsigned int idle = context->bridge->tcp_keepalive_idle;
unsigned int interval = context->bridge->tcp_keepalive_interval;
unsigned int counter = context->bridge->tcp_keepalive_counter;
unsigned int enabled = 1;
bool ret;
if(idle == 0 || interval == 0 || counter == 0){
return MOSQ_ERR_SUCCESS;
}
#ifdef WIN32
ret = setsockopt(context->sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&enabled, sizeof(enabled)) ||
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&idle, sizeof(idle)) ||
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&interval, sizeof(interval)) ||
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPCNT, (char *)&counter, sizeof(counter));
#else
ret = setsockopt(context->sock, SOL_SOCKET, SO_KEEPALIVE, (const void *)&enabled, sizeof(enabled)) ||
#ifndef __APPLE__
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPIDLE, (const void *)&idle, sizeof(idle)) ||
#endif
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPINTVL, (const void *)&interval, sizeof(interval)) ||
setsockopt(context->sock, IPPROTO_TCP, TCP_KEEPCNT, (const void *)&counter, sizeof(counter));
#endif
if(ret){
return MOSQ_ERR_UNKNOWN;
}
return MOSQ_ERR_SUCCESS;
}
#ifdef WITH_TCP_USER_TIMEOUT
static int bridge__set_tcp_user_timeout(struct mosquitto *context)
{
int timeout = context->bridge->tcp_user_timeout;
if(timeout >= 0){
if(setsockopt(context->sock, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&timeout, sizeof(timeout))){
return MOSQ_ERR_UNKNOWN;
}
}
return MOSQ_ERR_SUCCESS;
}
#endif
#if defined(__GLIBC__) && defined(WITH_ADNS)
static int bridge__connect_step1(struct mosquitto *context)
{
int rc;
char *notification_topic;
size_t notification_topic_len;
uint8_t notification_payload;
struct mosquitto__bridge_topic *cur_topic;
uint8_t qos;
if(!context || !context->bridge){
return MOSQ_ERR_INVAL;
}
mosquitto__set_state(context, mosq_cs_new);
context->sock = INVALID_SOCKET;
context->last_msg_in = db.now_s;
context->next_msg_out = db.now_s + context->bridge->keepalive;
context->keepalive = context->bridge->keepalive;
context->clean_start = context->bridge->clean_start;
context->in_packet.payload = NULL;
context->ping_t = 0;
context->bridge->lazy_reconnect = false;
context->maximum_packet_size = context->bridge->maximum_packet_size;
bridge__packet_cleanup(context);
db__message_reconnect_reset(context);
db__messages_delete(context, false);
/* Delete all local subscriptions even for clean_start==false. We don't
* remove any messages and the next loop carries out the resubscription
* anyway. This means any unwanted subs will be removed.
*/
sub__clean_session(context);
LL_FOREACH(context->bridge->topics, cur_topic){
if(cur_topic->direction == bd_out || cur_topic->direction == bd_both){
log__printf(NULL, MOSQ_LOG_DEBUG, "Bridge %s doing local SUBSCRIBE on topic %s", context->id, cur_topic->local_topic);
if(cur_topic->qos > context->max_qos){
qos = context->max_qos;
}else{
qos = cur_topic->qos;
}
struct mosquitto_subscription sub;
sub.topic_filter = cur_topic->local_topic;
sub.identifier = 0;
sub.options = MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED | qos;
if(sub__add(context, &sub) > 0){
return 1;
}
retain__queue(context, &sub);
}
}
if(context->bridge->notifications){
if(context->max_qos == 0){
qos = 0;
}else{
qos = 1;
}
if(context->bridge->notification_topic){
if(!context->bridge->initial_notification_done){
notification_payload = '0';
db__messages_easy_queue(context, context->bridge->notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
context->bridge->initial_notification_done = true;
}
notification_payload = '0';
rc = will__set(context, context->bridge->notification_topic, 1, ¬ification_payload, qos, true, NULL);
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}else{
notification_topic_len = strlen(context->bridge->remote_clientid)+strlen("$SYS/broker/connection//state");
notification_topic = mosquitto_malloc(sizeof(char)*(notification_topic_len+1));
if(!notification_topic){
return MOSQ_ERR_NOMEM;
}
snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid);
if(!context->bridge->initial_notification_done){
notification_payload = '0';
db__messages_easy_queue(context, notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
context->bridge->initial_notification_done = true;
}
notification_payload = '0';
rc = will__set(context, notification_topic, 1, ¬ification_payload, qos, true, NULL);
mosquitto_FREE(notification_topic);
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}
}
log__printf(NULL, MOSQ_LOG_NOTICE, "Connecting bridge (step 1) %s (%s:%d)", context->bridge->name, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port);
rc = net__try_connect_step1(context, context->bridge->addresses[context->bridge->cur_address].address);
if(rc > 0){
if(rc == MOSQ_ERR_TLS){
mux__delete(context);
net__socket_close(context);
return rc; /* Error already printed */
}else if(rc == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
return rc;
}
return MOSQ_ERR_SUCCESS;
}
static int bridge__connect_step2(struct mosquitto *context)
{
int rc;
if(!context || !context->bridge){
return MOSQ_ERR_INVAL;
}
log__printf(NULL, MOSQ_LOG_NOTICE, "Connecting bridge (step 2) %s (%s:%d)", context->bridge->name, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port);
rc = net__try_connect_step2(context, context->bridge->addresses[context->bridge->cur_address].port, &context->sock);
if(rc > 0){
if(rc == MOSQ_ERR_TLS){
mux__delete(context);
net__socket_close(context);
return rc; /* Error already printed */
}else if(rc == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
return rc;
}
HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(context->sock), context);
if(rc == MOSQ_ERR_CONN_PENDING){
mosquitto__set_state(context, mosq_cs_connect_pending);
mux__add_out(context);
}
return rc;
}
int bridge__connect_step3(struct mosquitto *context)
{
int rc;
mosquitto_property receive_maximum;
mosquitto_property session_expiry_interval;
mosquitto_property topic_alias_max;
mosquitto_property *properties = NULL;
rc = net__socket_connect_step3(context, context->bridge->addresses[context->bridge->cur_address].address);
if(rc > 0){
if(rc == MOSQ_ERR_TLS){
mux__delete(context);
net__socket_close(context);
return rc; /* Error already printed */
}else if(rc == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
return rc;
}
if(context->bridge->round_robin == false && context->bridge->cur_address != 0){
context->bridge->primary_retry = db.now_s + 5;
loop__update_next_event(5000);
}
if(bridge__set_tcp_keepalive(context) != MOSQ_ERR_SUCCESS){
return MOSQ_ERR_UNKNOWN;
}
#ifdef WITH_TCP_USER_TIMEOUT
if(bridge__set_tcp_user_timeout(context)){
return MOSQ_ERR_UNKNOWN;
}
#endif
if(context->bridge->receive_maximum != 0){
receive_maximum.value.i16 = context->bridge->receive_maximum;
receive_maximum.identifier = MQTT_PROP_RECEIVE_MAXIMUM;
receive_maximum.property_type = MQTT_PROP_TYPE_INT16;
receive_maximum.client_generated = false;
receive_maximum.next = properties;
properties = &receive_maximum;
}
if(context->bridge->session_expiry_interval != MQTT_SESSION_EXPIRY_IMMEDIATE){
session_expiry_interval.value.i32 = context->bridge->session_expiry_interval;
session_expiry_interval.identifier = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
session_expiry_interval.property_type = MQTT_PROP_TYPE_INT32;
session_expiry_interval.client_generated = false;
session_expiry_interval.next = properties;
properties = &session_expiry_interval;
}
if(context->bridge->max_topic_alias != 0){
topic_alias_max.value.i16 = context->bridge->max_topic_alias;
topic_alias_max.identifier = MQTT_PROP_TOPIC_ALIAS_MAXIMUM;
topic_alias_max.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_max.client_generated = false;
topic_alias_max.next = properties;
properties = &topic_alias_max;
}
rc = send__connect(context, context->keepalive, context->clean_start, properties);
if(rc == MOSQ_ERR_SUCCESS){
return MOSQ_ERR_SUCCESS;
}else if(rc == MOSQ_ERR_ERRNO && errno == ENOTCONN){
return MOSQ_ERR_SUCCESS;
}else{
if(rc == MOSQ_ERR_TLS){
return rc; /* Error already printed */
}else if(rc == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
mux__delete(context);
net__socket_close(context);
return rc;
}
}
#else
int bridge__connect(struct mosquitto *context)
{
int rc, rc2;
char *notification_topic = NULL;
size_t notification_topic_len;
uint8_t notification_payload;
struct mosquitto__bridge_topic *cur_topic;
uint8_t qos;
mosquitto_property receive_maximum;
mosquitto_property session_expiry_interval;
mosquitto_property topic_alias_max;
mosquitto_property *properties = NULL;
if(!context || !context->bridge){
return MOSQ_ERR_INVAL;
}
mosquitto__set_state(context, mosq_cs_new);
context->sock = INVALID_SOCKET;
/* coverity[missing_lock] - broker is single threaded, so no lock required */
context->last_msg_in = db.now_s;
/* coverity[missing_lock] - broker is single threaded, so no lock required */
context->next_msg_out = db.now_s + context->bridge->keepalive;
context->keepalive = context->bridge->keepalive;
context->clean_start = context->bridge->clean_start;
context->in_packet.payload = NULL;
context->ping_t = 0;
context->bridge->lazy_reconnect = false;
context->maximum_packet_size = context->bridge->maximum_packet_size;
bridge__packet_cleanup(context);
db__message_reconnect_reset(context);
db__messages_delete(context, false);
/* Delete all local subscriptions even for clean_start==false. We don't
* remove any messages and the next loop carries out the resubscription
* anyway. This means any unwanted subs will be removed.
*/
sub__clean_session(context);
LL_FOREACH(context->bridge->topics, cur_topic){
if(cur_topic->direction == bd_out || cur_topic->direction == bd_both){
log__printf(NULL, MOSQ_LOG_DEBUG, "Bridge %s doing local SUBSCRIBE on topic %s", context->id, cur_topic->local_topic);
if(cur_topic->qos > context->max_qos){
qos = context->max_qos;
}else{
qos = cur_topic->qos;
}
struct mosquitto_subscription sub;
sub.topic_filter = cur_topic->local_topic;
sub.identifier = 0;
sub.options = MQTT_SUB_OPT_NO_LOCAL | MQTT_SUB_OPT_RETAIN_AS_PUBLISHED | qos;
if(sub__add(context, &sub) > 0){
return 1;
}
}
}
if(context->bridge->notifications){
if(context->max_qos == 0){
qos = 0;
}else{
qos = 1;
}
if(context->bridge->notification_topic){
if(!context->bridge->initial_notification_done){
notification_payload = '0';
db__messages_easy_queue(context, context->bridge->notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
context->bridge->initial_notification_done = true;
}
notification_payload = '0';
rc = will__set(context, context->bridge->notification_topic, 1, ¬ification_payload, qos, true, NULL);
if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}else{
notification_topic_len = strlen(context->bridge->remote_clientid)+strlen("$SYS/broker/connection//state");
notification_topic = mosquitto_malloc(sizeof(char)*(notification_topic_len+1));
if(!notification_topic){
return MOSQ_ERR_NOMEM;
}
snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid);
if(!context->bridge->initial_notification_done){
notification_payload = '0';
db__messages_easy_queue(context, notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
context->bridge->initial_notification_done = true;
}
notification_payload = '0';
rc = will__set(context, notification_topic, 1, ¬ification_payload, qos, true, NULL);
if(rc != MOSQ_ERR_SUCCESS){
mosquitto_FREE(notification_topic);
return rc;
}
mosquitto_FREE(notification_topic);
}
}
log__printf(NULL, MOSQ_LOG_NOTICE, "Connecting bridge %s (%s:%d)", context->bridge->name, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port);
rc = net__socket_connect(context,
context->bridge->addresses[context->bridge->cur_address].address,
context->bridge->addresses[context->bridge->cur_address].port,
context->bridge->bind_address,
false);
if(rc > 0){
if(rc == MOSQ_ERR_TLS){
mux__delete(context);
net__socket_close(context);
return rc; /* Error already printed */
}else if(rc == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
return rc;
}else if(rc == MOSQ_ERR_CONN_PENDING){
mosquitto__set_state(context, mosq_cs_connect_pending);
mux__add_out(context);
}
HASH_ADD(hh_sock, db.contexts_by_sock, sock, sizeof(context->sock), context);
if(bridge__set_tcp_keepalive(context) != MOSQ_ERR_SUCCESS){
return MOSQ_ERR_UNKNOWN;
}
#ifdef WITH_TCP_USER_TIMEOUT
if(bridge__set_tcp_user_timeout(context)){
return MOSQ_ERR_UNKNOWN;
}
#endif
if(context->bridge->receive_maximum != 0){
receive_maximum.value.i16 = context->bridge->receive_maximum;
receive_maximum.identifier = MQTT_PROP_RECEIVE_MAXIMUM;
receive_maximum.property_type = MQTT_PROP_TYPE_INT16;
receive_maximum.client_generated = false;
receive_maximum.next = properties;
properties = &receive_maximum;
}
if(context->bridge->session_expiry_interval != MQTT_SESSION_EXPIRY_IMMEDIATE){
session_expiry_interval.value.i32 = context->bridge->session_expiry_interval;
session_expiry_interval.identifier = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
session_expiry_interval.property_type = MQTT_PROP_TYPE_INT32;
session_expiry_interval.client_generated = false;
session_expiry_interval.next = properties;
properties = &session_expiry_interval;
}
if(context->bridge->max_topic_alias != 0){
topic_alias_max.value.i16 = context->bridge->max_topic_alias;
topic_alias_max.identifier = MQTT_PROP_TOPIC_ALIAS_MAXIMUM;
topic_alias_max.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_max.client_generated = false;
topic_alias_max.next = properties;
properties = &topic_alias_max;
}
rc2 = send__connect(context, context->keepalive, context->clean_start, properties);
if(rc2 == MOSQ_ERR_SUCCESS){
return rc;
}else if(rc2 == MOSQ_ERR_ERRNO && errno == ENOTCONN){
return MOSQ_ERR_SUCCESS;
}else{
if(rc2 == MOSQ_ERR_TLS){
return rc2; /* Error already printed */
}else if(rc2 == MOSQ_ERR_ERRNO){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
}else if(rc2 == MOSQ_ERR_EAI){
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
}
mux__delete(context);
net__socket_close(context);
return rc2;
}
}
#endif
int bridge__on_connect(struct mosquitto *context)
{
char *notification_topic;
size_t notification_topic_len;
struct mosquitto__bridge_topic *cur_topic;
int sub_opts;
bool retain = true;
uint8_t qos;
if(context->bridge->notifications){
if(context->max_qos == 0){
qos = 0;
}else{
qos = 1;
}
if(!context->retain_available){
retain = false;
}
char notification_payload = '1';
if(context->bridge->notification_topic){
if(!context->bridge->notifications_local_only){
if(send__real_publish(context, mosquitto__mid_generate(context),
context->bridge->notification_topic, 1, ¬ification_payload, qos, retain, 0, 0, NULL, 0)){
return 1;
}
}
db__messages_easy_queue(context, context->bridge->notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
}else{
notification_topic_len = strlen(context->bridge->remote_clientid)+strlen("$SYS/broker/connection//state");
notification_topic = mosquitto_malloc(sizeof(char)*(notification_topic_len+1));
if(!notification_topic){
return MOSQ_ERR_NOMEM;
}
snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid);
notification_payload = '1';
if(!context->bridge->notifications_local_only){
if(send__real_publish(context, mosquitto__mid_generate(context),
notification_topic, 1, ¬ification_payload, qos, retain, 0, 0, NULL, 0)){
mosquitto_FREE(notification_topic);
return 1;
}
}
db__messages_easy_queue(context, notification_topic, qos, 1, ¬ification_payload, 1, MSG_EXPIRY_INFINITE, NULL);
mosquitto_FREE(notification_topic);
}
}
LL_FOREACH(context->bridge->topics, cur_topic){
if(cur_topic->direction == bd_in || cur_topic->direction == bd_both){
if(cur_topic->qos > context->max_qos){
sub_opts = context->max_qos;
}else{
sub_opts = cur_topic->qos;
}
if(context->bridge->protocol_version == mosq_p_mqtt5){
sub_opts = sub_opts
| MQTT_SUB_OPT_NO_LOCAL
| MQTT_SUB_OPT_RETAIN_AS_PUBLISHED
| MQTT_SUB_OPT_SEND_RETAIN_ALWAYS;
}
if(send__subscribe(context, NULL, 1, &cur_topic->remote_topic, sub_opts, NULL)){
return 1;
}
}else{
if(context->bridge->attempt_unsubscribe){
if(send__unsubscribe(context, NULL, 1, &cur_topic->remote_topic, NULL)){
/* direction = inwards only. This means we should not be subscribed
* to the topic. It is possible that we used to be subscribed to
* this topic so unsubscribe. */
return 1;
}
}
}
}
struct mosquitto_subscription sub;
memset(&sub, 0, sizeof(sub));
LL_FOREACH(context->bridge->topics, cur_topic){
sub.topic_filter = cur_topic->local_topic;
if(cur_topic->direction == bd_out || cur_topic->direction == bd_both){
if(cur_topic->qos > context->max_qos){
sub.options = context->max_qos;
}else{
sub.options = cur_topic->qos;
}
retain__queue(context, &sub);
}
}
context->bridge->connected_at = db.now_s;
return MOSQ_ERR_SUCCESS;
}
int bridge__register_local_connections(void)
{
struct mosquitto *context, *ctxt_tmp = NULL;
HASH_ITER(hh_sock, db.contexts_by_sock, context, ctxt_tmp){
if(context->bridge){
if(mux__new(context)){
log__printf(NULL, MOSQ_LOG_ERR, "Error in initial bridge registration: %s", strerror(errno));
return MOSQ_ERR_UNKNOWN;
}
mux__add_out(context);
}
}
return MOSQ_ERR_SUCCESS;
}
void bridge__reload(void)
{
int i;
int j;
// destroy old bridges that dissappeared
for(i=0; i<db.bridge_count; i++){
for(j=0; j<db.config->bridge_count; j++){
if(!strcmp(db.bridges[i]->bridge->name, db.config->bridges[j]->name)){
break;
}
}
if(j==db.config->bridge_count){
bridge__destroy(db.bridges[i]);
}
}
for(i=0; i<db.config->bridge_count; i++){
for(j=0; j<db.bridge_count; j++){
if(!strcmp(db.config->bridges[i]->name, db.bridges[j]->bridge->name)){
break;
}
}
if(j==db.bridge_count){
// a new bridge was found, create it
bridge__new(db.config->bridges[i]);
db.config->bridges[i] = NULL;
continue;
}
if(db.config->bridges[i]->reload_type == brt_immediate){
// in this case, an existing bridge should match
for(j=0; j<db.bridge_count; j++){
if(!strcmp(db.config->bridges[i]->name, db.bridges[j]->bridge->name)){
break;
}
}
assert(j<db.bridge_count);
db.bridges[j]->will_delay_interval = 0;
bridge__destroy(db.bridges[j]);
bridge__new(db.config->bridges[i]);
db.config->bridges[i] = NULL;
}
}
}
void bridge__db_cleanup(void)
{
int i;
for(i=0; i<db.bridge_count; i++){
if(db.bridges[i]){
context__cleanup(db.bridges[i], true);
}
}
mosquitto_FREE(db.bridges);
}
void bridge__cleanup(struct mosquitto *context)
{
int i;
assert(db.bridge_count > 0);
for(i=0; i<db.bridge_count; i++){
if(db.bridges[i] == context){
db.bridges[i] = db.bridges[db.bridge_count-1];
break;
}
}
db.bridge_count--;
if(db.bridge_count == 0){
mosquitto_FREE(db.bridges);
}else{
db.bridges = mosquitto_realloc(db.bridges, (unsigned)db.bridge_count * sizeof(db.bridges[0]));
}
mosquitto_FREE(context->bridge->name);
mosquitto_FREE(context->bridge->local_clientid);
mosquitto_FREE(context->bridge->local_username);
mosquitto_FREE(context->bridge->local_password);
mosquitto_FREE(context->bridge->tls_certfile);
mosquitto_FREE(context->bridge->tls_keyfile);
if(context->bridge->remote_clientid != context->id){
mosquitto_FREE(context->bridge->remote_clientid);
}
context->bridge->remote_clientid = NULL;
if(context->bridge->remote_username != context->username){
mosquitto_FREE(context->bridge->remote_username);
}
context->bridge->remote_username = NULL;
if(context->bridge->remote_password != context->password){
mosquitto_FREE(context->bridge->remote_password);
}
context->bridge->remote_password = NULL;
#ifdef WITH_TLS
if(context->ssl_ctx){
SSL_CTX_free(context->ssl_ctx);
context->ssl_ctx = NULL;
}
#endif
for(i=0; i<context->bridge->address_count; i++){
mosquitto_FREE(context->bridge->addresses[i].address);
}
mosquitto_FREE(context->bridge->addresses);
config__bridge_cleanup(context->bridge);
context->bridge = NULL;
}
static void bridge__packet_cleanup(struct mosquitto *context)
{
struct mosquitto__packet *packet;
if(!context){
return;
}
while(context->out_packet){
packet = context->out_packet;
context->out_packet = context->out_packet->next;
mosquitto_FREE(packet);
}
context->out_packet = NULL;
context->out_packet_last = NULL;
metrics__int_dec(mosq_gauge_out_packets, context->out_packet_count);
metrics__int_dec(mosq_gauge_out_packet_bytes, context->out_packet_bytes);
context->out_packet_count = 0;
context->out_packet_bytes = 0;
packet__cleanup(&(context->in_packet));
}
static int rand_between(int low, int high)
{
int r;
mosquitto_getrandom(&r, sizeof(int));
return (abs(r) % (high - low)) + low;
}
static void bridge__backoff_step(struct mosquitto__bridge *bridge)
{
/*
“Decorrelated Jitter” calculation, according to:
https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
*/
bridge->restart_timeout = rand_between(bridge->backoff_base, bridge->restart_timeout * 3);
if(bridge->restart_timeout > bridge->backoff_cap){
bridge->restart_timeout = bridge->backoff_cap;
}
}
static void bridge__backoff_reset(struct mosquitto__bridge *bridge)
{
bridge->restart_timeout = bridge->backoff_base;
}
static void bridge__update_backoff(struct mosquitto__bridge *bridge)
{