From eefa7f76047662fd91d16e7f60cde7e03459fec4 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 25 Oct 2018 12:03:22 +0100 Subject: [PATCH] All existing commands read properties. --- lib/handle_connack.c | 23 +++++++++++++++++------ lib/handle_pubackcomp.c | 9 +++++++++ lib/handle_publish.c | 10 ++++++++++ lib/handle_pubrec.c | 9 +++++++++ lib/handle_pubrel.c | 9 +++++++++ lib/handle_suback.c | 10 ++++++++++ lib/handle_unsuback.c | 10 ++++++++++ src/handle_connack.c | 20 +++++++++++++++----- src/handle_connect.c | 13 ++++++++++++- src/handle_subscribe.c | 1 - src/handle_unsubscribe.c | 15 +++++++++------ 11 files changed, 110 insertions(+), 19 deletions(-) diff --git a/lib/handle_connack.c b/lib/handle_connack.c index 9d0a01cd..b3125017 100644 --- a/lib/handle_connack.c +++ b/lib/handle_connack.c @@ -22,35 +22,46 @@ Contributors: #include "logging_mosq.h" #include "memory_mosq.h" #include "messages_mosq.h" +#include "mqtt_protocol.h" #include "net_mosq.h" #include "packet_mosq.h" +#include "property_mosq.h" #include "read_handle.h" int handle__connack(struct mosquitto *mosq) { uint8_t connect_flags; - uint8_t result; + uint8_t reason_code; int rc; + struct mqtt5__property *properties = NULL; assert(mosq); rc = packet__read_byte(&mosq->in_packet, &connect_flags); if(rc) return rc; - rc = packet__read_byte(&mosq->in_packet, &result); + rc = packet__read_byte(&mosq->in_packet, &reason_code); if(rc) return rc; - log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK (%d)", mosq->id, result); + + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(CONNACK, &mosq->in_packet, &properties); + if(rc) return rc; + property__free_all(&properties); + } + property__free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ + + log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK (%d)", mosq->id, reason_code); pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_connect){ mosq->in_callback = true; - mosq->on_connect(mosq, mosq->userdata, result); + mosq->on_connect(mosq, mosq->userdata, reason_code); mosq->in_callback = false; } if(mosq->on_connect_with_flags){ mosq->in_callback = true; - mosq->on_connect_with_flags(mosq, mosq->userdata, result, connect_flags); + mosq->on_connect_with_flags(mosq, mosq->userdata, reason_code, connect_flags); mosq->in_callback = false; } pthread_mutex_unlock(&mosq->callback_mutex); - switch(result){ + switch(reason_code){ case 0: if(mosq->state != mosq_cs_disconnecting){ mosq->state = mosq_cs_connected; diff --git a/lib/handle_pubackcomp.c b/lib/handle_pubackcomp.c index a6586f8f..8f5f0a30 100644 --- a/lib/handle_pubackcomp.c +++ b/lib/handle_pubackcomp.c @@ -44,10 +44,19 @@ int handle__pubackcomp(struct mosquitto *mosq, const char *type) { uint16_t mid; int rc; + struct mqtt5__property *properties = NULL; assert(mosq); rc = packet__read_uint16(&mosq->in_packet, &mid); if(rc) return rc; + + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(PUBACK, &mosq->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with Reason String or User Property at the moment */ + property__free_all(&properties); + } + #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Received %s from %s (Mid: %d)", type, mosq->id, mid); diff --git a/lib/handle_publish.c b/lib/handle_publish.c index f8092f89..5a253aa0 100644 --- a/lib/handle_publish.c +++ b/lib/handle_publish.c @@ -23,8 +23,10 @@ Contributors: #include "mosquitto_internal.h" #include "logging_mosq.h" #include "memory_mosq.h" +#include "mqtt_protocol.h" #include "messages_mosq.h" #include "packet_mosq.h" +#include "property_mosq.h" #include "send_mosq.h" #include "time_mosq.h" @@ -36,6 +38,7 @@ int handle__publish(struct mosquitto *mosq) int rc = 0; uint16_t mid; int slen; + struct mqtt5__property *properties = NULL; assert(mosq); @@ -67,6 +70,13 @@ int handle__publish(struct mosquitto *mosq) message->msg.mid = (int)mid; } + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(PUBLISH, &mosq->in_packet, &properties); + if(rc) return rc; + property__free_all(&properties); + } + property__free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ + message->msg.payloadlen = mosq->in_packet.remaining_length - mosq->in_packet.pos; if(message->msg.payloadlen){ message->msg.payload = mosquitto__calloc(message->msg.payloadlen+1, sizeof(uint8_t)); diff --git a/lib/handle_pubrec.c b/lib/handle_pubrec.c index 3f79d506..227d27be 100644 --- a/lib/handle_pubrec.c +++ b/lib/handle_pubrec.c @@ -39,10 +39,19 @@ int handle__pubrec(struct mosquitto *mosq) { uint16_t mid; int rc; + struct mqtt5__property *properties = NULL; assert(mosq); rc = packet__read_uint16(&mosq->in_packet, &mid); if(rc) return rc; + + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(PUBREC, &mosq->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with Reason String or User Property at the moment */ + property__free_all(&properties); + } + #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREC from %s (Mid: %d)", mosq->id, mid); diff --git a/lib/handle_pubrel.c b/lib/handle_pubrel.c index 4e4bf048..2630f1f2 100644 --- a/lib/handle_pubrel.c +++ b/lib/handle_pubrel.c @@ -43,6 +43,7 @@ int handle__pubrel(struct mosquitto_db *db, struct mosquitto *mosq) struct mosquitto_message_all *message = NULL; #endif int rc; + struct mqtt5__property *properties = NULL; assert(mosq); if(mosq->protocol != mosq_p_mqtt31){ @@ -52,6 +53,14 @@ int handle__pubrel(struct mosquitto_db *db, struct mosquitto *mosq) } rc = packet__read_uint16(&mosq->in_packet, &mid); if(rc) return rc; + + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(PUBREL, &mosq->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with Reason String or User Property at the moment */ + property__free_all(&properties); + } + #ifdef WITH_BROKER log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREL from %s (Mid: %d)", mosq->id, mid); diff --git a/lib/handle_suback.c b/lib/handle_suback.c index d6e3d19c..512415f2 100644 --- a/lib/handle_suback.c +++ b/lib/handle_suback.c @@ -26,7 +26,9 @@ Contributors: #include "mosquitto_internal.h" #include "logging_mosq.h" #include "memory_mosq.h" +#include "mqtt_protocol.h" #include "packet_mosq.h" +#include "property_mosq.h" int handle__suback(struct mosquitto *mosq) @@ -37,6 +39,7 @@ int handle__suback(struct mosquitto *mosq) int qos_count; int i = 0; int rc; + struct mqtt5__property *properties = NULL; assert(mosq); #ifdef WITH_BROKER @@ -47,6 +50,13 @@ int handle__suback(struct mosquitto *mosq) rc = packet__read_uint16(&mosq->in_packet, &mid); if(rc) return rc; + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(SUBACK, &mosq->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with Reason String or User Property at the moment */ + property__free_all(&properties); + } + qos_count = mosq->in_packet.remaining_length - mosq->in_packet.pos; granted_qos = mosquitto__malloc(qos_count*sizeof(int)); if(!granted_qos) return MOSQ_ERR_NOMEM; diff --git a/lib/handle_unsuback.c b/lib/handle_unsuback.c index 357ef5c9..0c403175 100644 --- a/lib/handle_unsuback.c +++ b/lib/handle_unsuback.c @@ -31,6 +31,7 @@ Contributors: #include "mqtt_protocol.h" #include "net_mosq.h" #include "packet_mosq.h" +#include "property_mosq.h" #include "read_handle.h" #include "send_mosq.h" #include "util_mosq.h" @@ -40,6 +41,7 @@ int handle__unsuback(struct mosquitto *mosq) { uint16_t mid; int rc; + struct mqtt5__property *properties = NULL; assert(mosq); #ifdef WITH_BROKER @@ -49,6 +51,14 @@ int handle__unsuback(struct mosquitto *mosq) #endif rc = packet__read_uint16(&mosq->in_packet, &mid); if(rc) return rc; + + if(mosq->protocol == mosq_p_mqtt5){ + rc = property__read_all(UNSUBACK, &mosq->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with Reason String or User Property at the moment */ + property__free_all(&properties); + } + #ifndef WITH_BROKER pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_unsubscribe){ diff --git a/src/handle_connack.c b/src/handle_connack.c index 0124ce60..fd289b5f 100644 --- a/src/handle_connack.c +++ b/src/handle_connack.c @@ -28,20 +28,30 @@ Contributors: int handle__connack(struct mosquitto_db *db, struct mosquitto *context) { - uint8_t byte; - uint8_t rc; + int rc; + uint8_t connect_acknowledge; + uint8_t reason_code; int i; char *notification_topic; int notification_topic_len; char notification_payload; + struct mqtt5__property *properties = NULL; if(!context){ return MOSQ_ERR_INVAL; } log__printf(NULL, MOSQ_LOG_DEBUG, "Received CONNACK on connection %s.", context->id); - if(packet__read_byte(&context->in_packet, &byte)) return 1; // Reserved byte, not used - if(packet__read_byte(&context->in_packet, &rc)) return 1; - switch(rc){ + if(packet__read_byte(&context->in_packet, &connect_acknowledge)) return 1; + if(packet__read_byte(&context->in_packet, &reason_code)) return 1; + + if(context->protocol == mosq_p_mqtt5){ + rc = property__read_all(CONNACK, &context->in_packet, &properties); + if(rc) return rc; + property__free_all(&properties); + } + property__free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ + + switch(reason_code){ case CONNACK_ACCEPTED: if(context->bridge){ if(context->bridge->notifications){ diff --git a/src/handle_connect.c b/src/handle_connect.c index c0e42489..6a4680ae 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -304,7 +304,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) if(protocol_version == PROTOCOL_VERSION_v5){ rc = property__read_all(CMD_WILL, &context->in_packet, &will_struct->properties); if(rc) return rc; - property__free_all(&will_struct->properties); + property__free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ } if(packet__read_string(&context->in_packet, &will_topic, &slen)){ rc = 1; @@ -721,9 +721,20 @@ handle_connect_error: int handle__disconnect(struct mosquitto_db *db, struct mosquitto *context) { + int rc; + struct mqtt5__property *properties = NULL; + if(!context){ return MOSQ_ERR_INVAL; } + + if(context->protocol == mosq_p_mqtt5){ + rc = property__read_all(DISCONNECT, &context->in_packet, &properties); + if(rc) return rc; + property__free_all(&properties); + } + property__free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ + if(context->in_packet.remaining_length != 0){ return MOSQ_ERR_PROTOCOL; } diff --git a/src/handle_subscribe.c b/src/handle_subscribe.c index a134bf55..e592ec83 100644 --- a/src/handle_subscribe.c +++ b/src/handle_subscribe.c @@ -43,7 +43,6 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) if(!context) return MOSQ_ERR_INVAL; log__printf(NULL, MOSQ_LOG_DEBUG, "Received SUBSCRIBE from %s", context->id); - /* FIXME - plenty of potential for memory leaks here */ if(context->protocol != mosq_p_mqtt31){ if((context->in_packet.command&0x0F) != 0x02){ diff --git a/src/handle_unsubscribe.c b/src/handle_unsubscribe.c index 03848d8d..5a130684 100644 --- a/src/handle_unsubscribe.c +++ b/src/handle_unsubscribe.c @@ -24,18 +24,14 @@ Contributors: #include "mqtt_protocol.h" #include "packet_mosq.h" #include "send_mosq.h" -/* -#include "sys_tree.h" -#include "time_mosq.h" -#include "tls_mosq.h" -#include "util_mosq.h" -*/ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) { uint16_t mid; char *sub; int slen; + int rc; + struct mqtt5__property *properties = NULL; if(!context) return MOSQ_ERR_INVAL; log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBSCRIBE from %s", context->id); @@ -47,6 +43,13 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) } if(packet__read_uint16(&context->in_packet, &mid)) return 1; + if(context->protocol == mosq_p_mqtt5){ + rc = property__read_all(UNSUBSCRIBE, &context->in_packet, &properties); + if(rc) return rc; + /* Immediately free, we don't do anything with User Property at the moment */ + property__free_all(&properties); + } + if(context->protocol == mosq_p_mqtt311 || context->protocol == mosq_p_mqtt5){ if(context->in_packet.pos == context->in_packet.remaining_length){ /* No topic specified, protocol error. */