/* * This will result in any outgoing packets going unsent. If we're disconnected * forcefully then it is usually an error condition and shouldn't be a problem, * but it will mean that CONNACK messages will never get sent for bad protocol * versions for example. */ void mqtt3_context_cleanup(mosquitto_db *db, mqtt3_context *context, bool do_free) { struct _mosquitto_packet *packet; mosquitto_client_msg *msg, *next; if(!context) return; if(context->core.sock != -1){ mqtt3_socket_close(context); } if(context->clean_session && !context->duplicate){ mqtt3_subs_clean_session(context, &db->subs); mqtt3_db_messages_delete(context); } if(context->address) _mosquitto_free(context->address); if(context->core.id) _mosquitto_free(context->core.id); _mosquitto_packet_cleanup(&(context->core.in_packet)); while(context->core.out_packet){ _mosquitto_packet_cleanup(context->core.out_packet); packet = context->core.out_packet; context->core.out_packet = context->core.out_packet->next; _mosquitto_free(packet); } if(context->core.will){ if(context->core.will->topic) _mosquitto_free(context->core.will->topic); if(context->core.will->payload) _mosquitto_free(context->core.will->payload); _mosquitto_free(context->core.will); } msg = context->msgs; while(msg){ next = msg->next; msg->store->ref_count--; _mosquitto_free(msg); msg = next; } if(do_free){ _mosquitto_free(context); } }
int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context) { 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; context->sock = INVALID_SOCKET; context->last_msg_in = mosquitto_time(); context->next_msg_out = mosquitto_time() + context->bridge->keepalive; 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); mqtt3_db_message_reconnect_reset(db, context); if(context->clean_session){ mqtt3_db_messages_delete(db, context); } /* 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. */ mqtt3_subs_clean_session(db, context); 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){ 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; } notification_payload = '0'; rc = _mosquitto_will_set(context, context->bridge->notification_topic, 1, ¬ification_payload, 1, true); 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'; mqtt3_db_messages_easy_queue(db, context, notification_topic, 1, 1, ¬ification_payload, 1); context->bridge->initial_notification_done = true; } notification_payload = '0'; rc = _mosquitto_will_set(context, notification_topic, 1, ¬ification_payload, 1, true); _mosquitto_free(notification_topic); if(rc != MOSQ_ERR_SUCCESS){ return rc; } } } _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_socket_connect(context, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port, NULL, false); 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; } HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context); if(rc == MOSQ_ERR_CONN_PENDING){ context->state = mosq_cs_connect_pending; } 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)); } _mosquitto_socket_close(db, context); return rc; } }
/* * This will result in any outgoing packets going unsent. If we're disconnected * forcefully then it is usually an error condition and shouldn't be a problem, * but it will mean that CONNACK messages will never get sent for bad protocol * versions for example. */ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free) { struct _mosquitto_packet *packet; struct mosquitto_client_msg *msg, *next; int i; if(!context) return; if(context->username){ _mosquitto_free(context->username); context->username = NULL; } if(context->password){ _mosquitto_free(context->password); context->password = NULL; } #ifdef WITH_BRIDGE if(context->bridge){ for(i=0; i<db->bridge_count; i++){ if(db->bridges[i] == context){ db->bridges[i] = NULL; } } if(context->bridge->local_clientid){ _mosquitto_free(context->bridge->local_clientid); context->bridge->local_clientid = NULL; } if(context->bridge->remote_username){ context->bridge->remote_username = NULL; } if(context->bridge->remote_password){ context->bridge->remote_password = NULL; } if(context->bridge->local_username){ context->bridge->local_username = NULL; } if(context->bridge->local_password){ context->bridge->local_password = NULL; } if(context->bridge->local_clientid){ context->bridge->local_clientid = NULL; } } #endif _mosquitto_socket_close(db, context); if((do_free || context->clean_session) && db){ mqtt3_subs_clean_session(db, context); mqtt3_db_messages_delete(db, context); } if(context->address){ _mosquitto_free(context->address); context->address = NULL; } if(context->id){ assert(db); /* db can only be NULL here if the client hasn't sent a CONNECT and hence wouldn't have an id. */ HASH_DELETE(hh_id, db->contexts_by_id, context); _mosquitto_free(context->id); context->id = NULL; } _mosquitto_packet_cleanup(&(context->in_packet)); if(context->current_out_packet){ _mosquitto_packet_cleanup(context->current_out_packet); _mosquitto_free(context->current_out_packet); context->current_out_packet = NULL; } while(context->out_packet){ _mosquitto_packet_cleanup(context->out_packet); packet = context->out_packet; context->out_packet = context->out_packet->next; _mosquitto_free(packet); } if(context->will){ if(context->will->topic) _mosquitto_free(context->will->topic); if(context->will->payload) _mosquitto_free(context->will->payload); _mosquitto_free(context->will); context->will = NULL; } if(do_free || context->clean_session){ msg = context->msgs; while(msg){ next = msg->next; mosquitto__db_msg_store_deref(db, &msg->store); _mosquitto_free(msg); msg = next; } context->msgs = NULL; context->last_msg = NULL; } if(do_free){ _mosquitto_free(context); } }
int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context) { 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; context->sock = -1; context->last_msg_in = mosquitto_time(); context->last_msg_out = mosquitto_time(); 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); mqtt3_db_message_reconnect_reset(context); if(context->clean_session){ mqtt3_db_messages_delete(context); } rc = mosquitto_unpwd_check(db, context->bridge->local_username, context->bridge->local_password); switch(rc){ case MOSQ_ERR_SUCCESS: break; case MOSQ_ERR_AUTH: _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Bridge %s failed authentication on local broker.", context->id); return rc; case MOSQ_ERR_UNKNOWN: _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Bridge %s returned application error in authorisation.", context->id); return rc; default: _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Unknown error in authentication for bridge %s.", context->id); return rc; } /* 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. */ mqtt3_subs_clean_session(db, context, &db->subs); 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){ notification_payload = '0'; if(context->bridge->notification_topic){ mqtt3_db_messages_easy_queue(db, context, context->bridge->notification_topic, 1, 1, ¬ification_payload, 1); rc = _mosquitto_will_set(context, context->bridge->notification_topic, 1, ¬ification_payload, 1, true); if(rc != MOSQ_ERR_SUCCESS){ return rc; } }else{ notification_topic_len = strlen(context->id)+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); mqtt3_db_messages_easy_queue(db, context, notification_topic, 1, 1, ¬ification_payload, 1); rc = _mosquitto_will_set(context, notification_topic, 1, ¬ification_payload, 1, true); if(rc != MOSQ_ERR_SUCCESS){ _mosquitto_free(notification_topic); return rc; } _mosquitto_free(notification_topic); } } _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_socket_connect(context, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port, NULL, true); if(rc != MOSQ_ERR_SUCCESS){ 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)); } return rc; } 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)); } _mosquitto_socket_close(context); return rc; } }
/* * This will result in any outgoing packets going unsent. If we're disconnected * forcefully then it is usually an error condition and shouldn't be a problem, * but it will mean that CONNACK messages will never get sent for bad protocol * versions for example. */ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free) { struct _mosquitto_packet *packet; struct mosquitto_client_msg *msg, *next; struct _clientid_index_hash *find_cih; if(!context) return; if(context->username){ _mosquitto_free(context->username); context->username = NULL; } if(context->password){ _mosquitto_free(context->password); context->password = NULL; } #ifdef WITH_BRIDGE if(context->bridge){ if(context->bridge->username){ context->bridge->username = NULL; } if(context->bridge->password){ context->bridge->password = NULL; } } #endif #ifdef WITH_TLS if(context->ssl){ SSL_free(context->ssl); context->ssl = NULL; } #endif if(context->sock != -1){ if(context->listener){ context->listener->client_count--; assert(context->listener->client_count >= 0); } _mosquitto_socket_close(context); context->listener = NULL; } if(context->clean_session && db){ mqtt3_subs_clean_session(db, context, &db->subs); mqtt3_db_messages_delete(context); } if(context->address){ _mosquitto_free(context->address); context->address = NULL; } if(context->id){ assert(db); /* db can only be NULL here if the client hasn't sent a CONNECT and hence wouldn't have an id. */ // Remove the context's ID from the DB hash HASH_FIND_STR(db->clientid_index_hash, context->id, find_cih); if(find_cih){ // FIXME - internal level debug? _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Found id for client \"%s\", their index was %d.", context->id, find_cih->db_context_index); HASH_DEL(db->clientid_index_hash, find_cih); _mosquitto_free(find_cih); }else{ // FIXME - internal level debug? _mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Unable to find id for client \"%s\".", context->id); } _mosquitto_free(context->id); context->id = NULL; } _mosquitto_packet_cleanup(&(context->in_packet)); _mosquitto_packet_cleanup(context->current_out_packet); while(context->out_packet){ _mosquitto_packet_cleanup(context->out_packet); packet = context->out_packet; context->out_packet = context->out_packet->next; _mosquitto_free(packet); } if(context->will){ if(context->will->topic) _mosquitto_free(context->will->topic); if(context->will->payload) _mosquitto_free(context->will->payload); _mosquitto_free(context->will); context->will = NULL; } if(do_free || context->clean_session){ msg = context->msgs; while(msg){ next = msg->next; msg->store->ref_count--; _mosquitto_free(msg); msg = next; } context->msgs = NULL; context->last_msg = NULL; } if(do_free){ _mosquitto_free(context); } }