From 580cd722dc952eb246822fb2ceb6c8fd797964df Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sat, 16 Aug 2014 23:14:41 +0100 Subject: [PATCH] Remote/local bridge fixes. --- lib/send_client_mosq.c | 28 +++++++++++++++++----------- src/bridge.c | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/send_client_mosq.c b/lib/send_client_mosq.c index 35cac447..f5ea952a 100644 --- a/lib/send_client_mosq.c +++ b/lib/send_client_mosq.c @@ -37,19 +37,25 @@ int _mosquitto_send_connect(struct mosquitto *mosq, uint16_t keepalive, bool cle uint8_t byte; int rc; uint8_t version = PROTOCOL_VERSION_v31; - char *clientid; + char *clientid, *username, *password; assert(mosq); assert(mosq->id); #if defined(WITH_BROKER) && defined(WITH_BRIDGE) if(mosq->bridge){ - clientid = mosq->bridge->clientid; + clientid = mosq->bridge->remote_clientid; + username = mosq->bridge->remote_username; + password = mosq->bridge->remote_password; }else{ clientid = mosq->id; + username = mosq->username; + password = mosq->password; } #else clientid = mosq->id; + username = mosq->username; + password = mosq->password; #endif packet = _mosquitto_calloc(1, sizeof(struct _mosquitto_packet)); @@ -62,10 +68,10 @@ int _mosquitto_send_connect(struct mosquitto *mosq, uint16_t keepalive, bool cle payloadlen += 2+strlen(mosq->will->topic) + 2+mosq->will->payloadlen; } - if(mosq->username){ - payloadlen += 2+strlen(mosq->username); - if(mosq->password){ - payloadlen += 2+strlen(mosq->password); + if(username){ + payloadlen += 2+strlen(username); + if(password){ + payloadlen += 2+strlen(password); } } @@ -90,7 +96,7 @@ int _mosquitto_send_connect(struct mosquitto *mosq, uint16_t keepalive, bool cle if(will){ byte = byte | ((mosq->will->retain&0x1)<<5) | ((mosq->will->qos&0x3)<<3) | ((will&0x1)<<2); } - if(mosq->username){ + if(username){ byte = byte | 0x1<<7; if(mosq->password){ byte = byte | 0x1<<6; @@ -105,10 +111,10 @@ int _mosquitto_send_connect(struct mosquitto *mosq, uint16_t keepalive, bool cle _mosquitto_write_string(packet, mosq->will->topic, strlen(mosq->will->topic)); _mosquitto_write_string(packet, (const char *)mosq->will->payload, mosq->will->payloadlen); } - if(mosq->username){ - _mosquitto_write_string(packet, mosq->username, strlen(mosq->username)); - if(mosq->password){ - _mosquitto_write_string(packet, mosq->password, strlen(mosq->password)); + if(username){ + _mosquitto_write_string(packet, username, strlen(username)); + if(password){ + _mosquitto_write_string(packet, password, strlen(password)); } } diff --git a/src/bridge.c b/src/bridge.c index 83bb438e..698c4032 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -131,6 +131,7 @@ int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context) char *notification_topic; int notification_topic_len; uint8_t notification_payload; + int lr, ll; if(!context || !context->bridge) return MOSQ_ERR_INVAL; @@ -179,18 +180,25 @@ int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context) return rc; } }else{ - notification_topic_len = strlen(context->id)+strlen("$SYS/broker/connection//state"); + ll = strlen(context->bridge->local_clientid); + lr = strlen(context->bridge->remote_clientid); + if(ll > lr){ + notification_topic_len = ll+strlen("$SYS/broker/connection//state"); + }else{ + notification_topic_len = lr+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->id); + snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->local_clientid); mqtt3_db_messages_easy_queue(db, context, notification_topic, 1, 1, ¬ification_payload, 1); + + snprintf(notification_topic, notification_topic_len+1, "$SYS/broker/connection/%s/state", context->bridge->remote_clientid); rc = _mosquitto_will_set(context, notification_topic, 1, ¬ification_payload, 1, true); + _mosquitto_free(notification_topic); if(rc != MOSQ_ERR_SUCCESS){ - _mosquitto_free(notification_topic); return rc; } - _mosquitto_free(notification_topic); } }