2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2018-04-11 14:24:29 +00:00
|
|
|
Copyright (c) 2009-2018 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
2018-04-05 23:33:22 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mosquitto.h"
|
2016-07-08 09:10:04 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mosquitto_internal.h"
|
|
|
|
#include "net_mosq.h"
|
|
|
|
#include "memory_mosq.h"
|
2015-04-29 20:23:59 +00:00
|
|
|
#include "packet_mosq.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "send_mosq.h"
|
|
|
|
#include "time_mosq.h"
|
|
|
|
#include "tls_mosq.h"
|
|
|
|
#include "util_mosq.h"
|
|
|
|
#include "will_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_BRIDGE
|
|
|
|
|
2015-05-16 18:03:12 +00:00
|
|
|
int bridge__new(struct mosquitto_db *db, struct mosquitto__bridge *bridge)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *new_context = NULL;
|
2014-11-18 23:34:54 +00:00
|
|
|
struct mosquitto **bridges;
|
2015-06-09 22:18:13 +00:00
|
|
|
char *local_id;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(db);
|
|
|
|
assert(bridge);
|
|
|
|
|
2015-06-09 22:18:13 +00:00
|
|
|
local_id = mosquitto__strdup(bridge->local_clientid);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_FIND(hh_id, db->contexts_by_id, local_id, strlen(local_id), new_context);
|
|
|
|
if(new_context){
|
|
|
|
/* (possible from persistent db) */
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(local_id);
|
2014-06-23 16:57:35 +00:00
|
|
|
}else{
|
2014-05-07 22:27:00 +00:00
|
|
|
/* id wasn't found, so generate a new context */
|
2015-05-16 14:24:24 +00:00
|
|
|
new_context = context__init(db, -1);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!new_context){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(local_id);
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2014-07-03 20:55:25 +00:00
|
|
|
new_context->id = local_id;
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, new_context->id, strlen(new_context->id), new_context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
new_context->bridge = bridge;
|
|
|
|
new_context->is_bridge = true;
|
|
|
|
|
2014-08-16 20:31:12 +00:00
|
|
|
new_context->username = new_context->bridge->remote_username;
|
|
|
|
new_context->password = new_context->bridge->remote_password;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_TLS
|
|
|
|
new_context->tls_cafile = new_context->bridge->tls_cafile;
|
|
|
|
new_context->tls_capath = new_context->bridge->tls_capath;
|
|
|
|
new_context->tls_certfile = new_context->bridge->tls_certfile;
|
|
|
|
new_context->tls_keyfile = new_context->bridge->tls_keyfile;
|
2014-05-08 21:48:13 +00:00
|
|
|
new_context->tls_cert_reqs = SSL_VERIFY_PEER;
|
2014-05-07 22:27:00 +00:00
|
|
|
new_context->tls_version = new_context->bridge->tls_version;
|
|
|
|
new_context->tls_insecure = new_context->bridge->tls_insecure;
|
|
|
|
#ifdef REAL_WITH_TLS_PSK
|
|
|
|
new_context->tls_psk_identity = new_context->bridge->tls_psk_identity;
|
|
|
|
new_context->tls_psk = new_context->bridge->tls_psk;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bridge->try_private_accepted = true;
|
2015-01-27 17:12:36 +00:00
|
|
|
new_context->protocol = bridge->protocol_version;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
bridges = mosquitto__realloc(db->bridges, (db->bridge_count+1)*sizeof(struct mosquitto *));
|
2014-11-18 23:34:54 +00:00
|
|
|
if(bridges){
|
|
|
|
db->bridges = bridges;
|
|
|
|
db->bridge_count++;
|
|
|
|
db->bridges[db->bridge_count-1] = new_context;
|
|
|
|
}else{
|
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2014-06-23 16:57:35 +00:00
|
|
|
|
2017-02-08 22:30:00 +00:00
|
|
|
#if defined(__GLIBC__) && defined(WITH_ADNS)
|
2017-02-06 22:39:39 +00:00
|
|
|
new_context->bridge->restart_t = 1; /* force quick restart of bridge */
|
2017-03-06 21:19:53 +00:00
|
|
|
return bridge__connect_step1(db, new_context);
|
2017-02-06 22:39:39 +00:00
|
|
|
#else
|
2015-05-16 14:24:24 +00:00
|
|
|
return bridge__connect(db, new_context);
|
2017-02-06 22:39:39 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:41:48 +00:00
|
|
|
#if defined(__GLIBC__) && defined(WITH_ADNS)
|
2017-03-06 21:19:53 +00:00
|
|
|
int bridge__connect_step1(struct mosquitto_db *db, struct mosquitto *context)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
char *notification_topic;
|
|
|
|
int notification_topic_len;
|
|
|
|
uint8_t notification_payload;
|
|
|
|
|
|
|
|
if(!context || !context->bridge) return MOSQ_ERR_INVAL;
|
|
|
|
|
|
|
|
context->state = mosq_cs_new;
|
2015-03-29 09:43:08 +00:00
|
|
|
context->sock = INVALID_SOCKET;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->last_msg_in = mosquitto_time();
|
2016-02-28 17:24:43 +00:00
|
|
|
context->next_msg_out = mosquitto_time() + context->bridge->keepalive;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->keepalive = context->bridge->keepalive;
|
|
|
|
context->clean_session = context->bridge->clean_session;
|
|
|
|
context->in_packet.payload = NULL;
|
|
|
|
context->ping_t = 0;
|
|
|
|
context->bridge->lazy_reconnect = false;
|
|
|
|
mqtt3_bridge_packet_cleanup(context);
|
2014-11-17 23:46:02 +00:00
|
|
|
mqtt3_db_message_reconnect_reset(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(context->clean_session){
|
2014-11-17 23:46:02 +00:00
|
|
|
mqtt3_db_messages_delete(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete all local subscriptions even for clean_session==false. We don't
|
|
|
|
* remove any messages and the next loop carries out the resubscription
|
|
|
|
* anyway. This means any unwanted subs will be removed.
|
|
|
|
*/
|
2014-10-09 22:28:16 +00:00
|
|
|
mqtt3_subs_clean_session(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
for(i=0; i<context->bridge->topic_count; i++){
|
|
|
|
if(context->bridge->topics[i].direction == bd_out || context->bridge->topics[i].direction == bd_both){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "Bridge %s doing local SUBSCRIBE on topic %s", context->id, context->bridge->topics[i].local_topic);
|
|
|
|
if(mqtt3_sub_add(db, context, context->bridge->topics[i].local_topic, context->bridge->topics[i].qos, &db->subs)) return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(context->bridge->notifications){
|
|
|
|
if(context->bridge->notification_topic){
|
2015-05-16 11:02:18 +00:00
|
|
|
if(!context->bridge->initial_notification_done){
|
|
|
|
notification_payload = '0';
|
|
|
|
mqtt3_db_messages_easy_queue(db, context, context->bridge->notification_topic, 1, 1, ¬ification_payload, 1);
|
|
|
|
context->bridge->initial_notification_done = true;
|
|
|
|
}
|
2014-09-17 21:40:49 +00:00
|
|
|
notification_payload = '0';
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = _mosquitto_will_set(context, context->bridge->notification_topic, 1, ¬ification_payload, 1, true);
|
|
|
|
if(rc != MOSQ_ERR_SUCCESS){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}else{
|
2015-04-17 19:42:54 +00:00
|
|
|
notification_topic_len = strlen(context->bridge->remote_clientid)+strlen("$SYS/broker/connection//state");
|
2014-05-07 22:27:00 +00:00
|
|
|
notification_topic = _mosquitto_malloc(sizeof(char)*(notification_topic_len+1));
|
|
|
|
if(!notification_topic) return MOSQ_ERR_NOMEM;
|
|
|
|
|
2015-04-17 19:42:54 +00:00
|
|
|
snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid);
|
|
|
|
|
2015-05-16 11:02:18 +00:00
|
|
|
if(!context->bridge->initial_notification_done){
|
|
|
|
notification_payload = '0';
|
|
|
|
mqtt3_db_messages_easy_queue(db, context, notification_topic, 1, 1, ¬ification_payload, 1);
|
|
|
|
context->bridge->initial_notification_done = true;
|
|
|
|
}
|
2014-08-16 22:14:41 +00:00
|
|
|
|
2014-09-17 21:40:49 +00:00
|
|
|
notification_payload = '0';
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = _mosquitto_will_set(context, notification_topic, 1, ¬ification_payload, 1, true);
|
2014-08-16 22:14:41 +00:00
|
|
|
_mosquitto_free(notification_topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc != MOSQ_ERR_SUCCESS){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:39:39 +00:00
|
|
|
_mosquitto_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 = _mosquitto_try_connect_step1(context, context->bridge->addresses[context->bridge->cur_address].address);
|
|
|
|
if(rc > 0 ){
|
|
|
|
if(rc == MOSQ_ERR_TLS){
|
|
|
|
_mosquitto_socket_close(db, context);
|
|
|
|
return rc; /* Error already printed */
|
|
|
|
}else if(rc == MOSQ_ERR_ERRNO){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-06 21:19:53 +00:00
|
|
|
int bridge__connect_step2(struct mosquitto_db *db, struct mosquitto *context)
|
2017-02-06 22:39:39 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if(!context || !context->bridge) return MOSQ_ERR_INVAL;
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
_mosquitto_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);
|
2017-02-06 23:10:43 +00:00
|
|
|
rc = _mosquitto_try_connect_step2(context, context->bridge->addresses[context->bridge->cur_address].port, &context->sock);
|
|
|
|
if(rc > 0 ){
|
|
|
|
if(rc == MOSQ_ERR_TLS){
|
|
|
|
_mosquitto_socket_close(db, context);
|
|
|
|
return rc; /* Error already printed */
|
|
|
|
}else if(rc == MOSQ_ERR_ERRNO){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = _mosquitto_socket_connect_step3(context, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port, NULL, false);
|
2014-10-24 20:28:24 +00:00
|
|
|
if(rc > 0 ){
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc == MOSQ_ERR_TLS){
|
2015-10-12 13:48:52 +00:00
|
|
|
_mosquitto_socket_close(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc; /* Error already printed */
|
|
|
|
}else if(rc == MOSQ_ERR_ERRNO){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context);
|
2014-10-24 20:28:24 +00:00
|
|
|
|
|
|
|
if(rc == MOSQ_ERR_CONN_PENDING){
|
|
|
|
context->state = mosq_cs_connect_pending;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
rc = _mosquitto_send_connect(context, context->keepalive, context->clean_session);
|
|
|
|
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){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
|
|
|
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
|
|
|
}
|
2014-06-23 16:57:35 +00:00
|
|
|
_mosquitto_socket_close(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:41:48 +00:00
|
|
|
#else
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int bridge__connect(struct mosquitto_db *db, struct mosquitto *context)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
char *notification_topic;
|
|
|
|
int notification_topic_len;
|
|
|
|
uint8_t notification_payload;
|
|
|
|
|
|
|
|
if(!context || !context->bridge) return MOSQ_ERR_INVAL;
|
|
|
|
|
|
|
|
context->state = mosq_cs_new;
|
2015-03-29 09:43:08 +00:00
|
|
|
context->sock = INVALID_SOCKET;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->last_msg_in = mosquitto_time();
|
2016-02-28 17:24:43 +00:00
|
|
|
context->next_msg_out = mosquitto_time() + context->bridge->keepalive;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->keepalive = context->bridge->keepalive;
|
|
|
|
context->clean_session = context->bridge->clean_session;
|
|
|
|
context->in_packet.payload = NULL;
|
|
|
|
context->ping_t = 0;
|
|
|
|
context->bridge->lazy_reconnect = false;
|
2015-05-16 14:24:24 +00:00
|
|
|
bridge__packet_cleanup(context);
|
|
|
|
db__message_reconnect_reset(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(context->clean_session){
|
2015-05-16 14:24:24 +00:00
|
|
|
db__messages_delete(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete all local subscriptions even for clean_session==false. We don't
|
|
|
|
* remove any messages and the next loop carries out the resubscription
|
|
|
|
* anyway. This means any unwanted subs will be removed.
|
|
|
|
*/
|
2015-05-18 08:29:22 +00:00
|
|
|
sub__clean_session(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
for(i=0; i<context->bridge->topic_count; i++){
|
|
|
|
if(context->bridge->topics[i].direction == bd_out || context->bridge->topics[i].direction == bd_both){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Bridge %s doing local SUBSCRIBE on topic %s", context->id, context->bridge->topics[i].local_topic);
|
2015-05-18 08:29:22 +00:00
|
|
|
if(sub__add(db, context, context->bridge->topics[i].local_topic, context->bridge->topics[i].qos, &db->subs)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(context->bridge->notifications){
|
|
|
|
if(context->bridge->notification_topic){
|
2015-05-16 11:02:18 +00:00
|
|
|
if(!context->bridge->initial_notification_done){
|
|
|
|
notification_payload = '0';
|
2015-09-03 16:46:55 +00:00
|
|
|
db__messages_easy_queue(db, context, context->bridge->notification_topic, 1, 1, ¬ification_payload, 1);
|
2015-05-16 11:02:18 +00:00
|
|
|
context->bridge->initial_notification_done = true;
|
|
|
|
}
|
2016-12-04 21:47:38 +00:00
|
|
|
|
|
|
|
if (!context->bridge->notifications_local_only) {
|
|
|
|
notification_payload = '0';
|
|
|
|
rc = will__set(context, context->bridge->notification_topic, 1, ¬ification_payload, 1, true);
|
|
|
|
if(rc != MOSQ_ERR_SUCCESS){
|
|
|
|
return rc;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}else{
|
2015-04-17 19:42:54 +00:00
|
|
|
notification_topic_len = strlen(context->bridge->remote_clientid)+strlen("$SYS/broker/connection//state");
|
2015-04-19 21:10:59 +00:00
|
|
|
notification_topic = mosquitto__malloc(sizeof(char)*(notification_topic_len+1));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!notification_topic) return MOSQ_ERR_NOMEM;
|
|
|
|
|
2014-08-16 22:14:41 +00:00
|
|
|
snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid);
|
2015-04-17 19:42:54 +00:00
|
|
|
|
2015-05-16 11:02:18 +00:00
|
|
|
if(!context->bridge->initial_notification_done){
|
|
|
|
notification_payload = '0';
|
2015-09-03 16:46:55 +00:00
|
|
|
db__messages_easy_queue(db, context, notification_topic, 1, 1, ¬ification_payload, 1);
|
2015-05-16 11:02:18 +00:00
|
|
|
context->bridge->initial_notification_done = true;
|
|
|
|
}
|
2014-08-16 22:14:41 +00:00
|
|
|
|
2016-12-04 21:47:38 +00:00
|
|
|
if (!context->bridge->notifications_local_only) {
|
|
|
|
notification_payload = '0';
|
|
|
|
rc = will__set(context, notification_topic, 1, ¬ification_payload, 1, true);
|
|
|
|
mosquitto__free(notification_topic);
|
|
|
|
if(rc != MOSQ_ERR_SUCCESS){
|
|
|
|
return rc;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 07:53:21 +00:00
|
|
|
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);
|
2015-05-18 08:29:22 +00:00
|
|
|
rc = net__socket_connect(context, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port, NULL, false);
|
2014-10-24 20:28:24 +00:00
|
|
|
if(rc > 0 ){
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc == MOSQ_ERR_TLS){
|
2016-02-09 23:03:26 +00:00
|
|
|
net__socket_close(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc; /* Error already printed */
|
|
|
|
}else if(rc == MOSQ_ERR_ERRNO){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
2014-05-07 22:27:00 +00:00
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:57:35 +00:00
|
|
|
HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context);
|
2014-10-24 20:28:24 +00:00
|
|
|
|
|
|
|
if(rc == MOSQ_ERR_CONN_PENDING){
|
|
|
|
context->state = mosq_cs_connect_pending;
|
|
|
|
}
|
2015-05-16 14:24:24 +00:00
|
|
|
rc = send__connect(context, context->keepalive, context->clean_session);
|
2014-05-07 22:27:00 +00:00
|
|
|
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){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));
|
2014-05-07 22:27:00 +00:00
|
|
|
}else if(rc == MOSQ_ERR_EAI){
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", gai_strerror(errno));
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-05-18 08:29:22 +00:00
|
|
|
net__socket_close(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 16:41:48 +00:00
|
|
|
#endif
|
2017-02-06 22:39:39 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
void bridge__packet_cleanup(struct mosquitto *context)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__packet *packet;
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return;
|
|
|
|
|
2014-09-22 23:32:34 +00:00
|
|
|
if(context->current_out_packet){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(context->current_out_packet);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context->current_out_packet);
|
2014-09-22 23:32:34 +00:00
|
|
|
context->current_out_packet = NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
while(context->out_packet){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(context->out_packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
packet = context->out_packet;
|
|
|
|
context->out_packet = context->out_packet->next;
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-09-22 23:32:34 +00:00
|
|
|
context->out_packet = NULL;
|
|
|
|
context->out_packet_last = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(&(context->in_packet));
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|