Skip to content

Commit

Permalink
packet__read_string() returns the string length.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Feb 11, 2018
1 parent ff55499 commit 3066f89
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
5 changes: 3 additions & 2 deletions lib/handle_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int handle__publish(struct mosquitto *mosq)
struct mosquitto_message_all *message;
int rc = 0;
uint16_t mid;
int slen;

assert(mosq);

Expand All @@ -45,12 +46,12 @@ int handle__publish(struct mosquitto *mosq)
message->msg.qos = (header & 0x06)>>1;
message->msg.retain = (header & 0x01);

rc = packet__read_string(&mosq->in_packet, &message->msg.topic);
rc = packet__read_string(&mosq->in_packet, &message->msg.topic, &slen);
if(rc){
message__cleanup(&message);
return rc;
}
if(!strlen(message->msg.topic)){
if(!slen){
message__cleanup(&message);
return MOSQ_ERR_PROTOCOL;
}
Expand Down
17 changes: 9 additions & 8 deletions lib/packet_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,27 @@ void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, ui
}


int packet__read_string(struct mosquitto__packet *packet, char **str)
int packet__read_string(struct mosquitto__packet *packet, char **str, int *length)
{
uint16_t len;
uint16_t slen;
int rc;

assert(packet);
rc = packet__read_uint16(packet, &len);
rc = packet__read_uint16(packet, &slen);
if(rc) return rc;

if(packet->pos+len > packet->remaining_length) return MOSQ_ERR_PROTOCOL;
if(packet->pos+slen > packet->remaining_length) return MOSQ_ERR_PROTOCOL;

*str = mosquitto__malloc(len+1);
*str = mosquitto__malloc(slen+1);
if(*str){
memcpy(*str, &(packet->payload[packet->pos]), len);
(*str)[len] = '\0';
packet->pos += len;
memcpy(*str, &(packet->payload[packet->pos]), slen);
(*str)[slen] = '\0';
packet->pos += slen;
}else{
return MOSQ_ERR_NOMEM;
}

*length = slen;
return MOSQ_ERR_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/packet_mosq.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet);

int packet__read_byte(struct mosquitto__packet *packet, uint8_t *byte);
int packet__read_bytes(struct mosquitto__packet *packet, void *bytes, uint32_t count);
int packet__read_string(struct mosquitto__packet *packet, char **str);
int packet__read_string(struct mosquitto__packet *packet, char **str, int *length);
int packet__read_uint16(struct mosquitto__packet *packet, uint16_t *word);

void packet__write_byte(struct mosquitto__packet *packet, uint8_t byte);
Expand Down
17 changes: 8 additions & 9 deletions src/handle_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
goto handle_connect_error;
}

if(packet__read_string(&context->in_packet, &protocol_name)){
if(packet__read_string(&context->in_packet, &protocol_name, &slen)){
rc = 1;
goto handle_connect_error;
return 1;
Expand Down Expand Up @@ -221,12 +221,11 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
goto handle_connect_error;
}

if(packet__read_string(&context->in_packet, &client_id)){
if(packet__read_string(&context->in_packet, &client_id, &slen)){
rc = 1;
goto handle_connect_error;
}

slen = strlen(client_id);
if(slen == 0){
if(context->protocol == mosq_p_mqtt31){
send__connack(context, 0, CONNACK_REFUSED_IDENTIFIER_REJECTED);
Expand Down Expand Up @@ -259,7 +258,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
}
}

if(mosquitto_validate_utf8(client_id, strlen(client_id)) != MOSQ_ERR_SUCCESS){
if(mosquitto_validate_utf8(client_id, slen) != MOSQ_ERR_SUCCESS){
rc = 1;
goto handle_connect_error;
}
Expand All @@ -270,11 +269,11 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
rc = MOSQ_ERR_NOMEM;
goto handle_connect_error;
}
if(packet__read_string(&context->in_packet, &will_topic)){
if(packet__read_string(&context->in_packet, &will_topic, &slen)){
rc = 1;
goto handle_connect_error;
}
if(STREMPTY(will_topic)){
if(!slen){
rc = 1;
goto handle_connect_error;
}
Expand Down Expand Up @@ -325,15 +324,15 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
}

if(username_flag){
rc = packet__read_string(&context->in_packet, &username);
rc = packet__read_string(&context->in_packet, &username, &slen);
if(rc == MOSQ_ERR_SUCCESS){
if(mosquitto_validate_utf8(username, strlen(username)) != MOSQ_ERR_SUCCESS){
if(mosquitto_validate_utf8(username, slen) != MOSQ_ERR_SUCCESS){
rc = MOSQ_ERR_PROTOCOL;
goto handle_connect_error;
}

if(password_flag){
rc = packet__read_string(&context->in_packet, &password);
rc = packet__read_string(&context->in_packet, &password, &slen);
if(rc == MOSQ_ERR_NOMEM){
rc = MOSQ_ERR_NOMEM;
goto handle_connect_error;
Expand Down
5 changes: 3 additions & 2 deletions src/handle_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context)
int res = 0;
struct mosquitto_msg_store *stored = NULL;
int len;
int slen;
char *topic_mount;
#ifdef WITH_BRIDGE
char *topic_temp;
Expand All @@ -61,8 +62,8 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context)
}
retain = (header & 0x01);

if(packet__read_string(&context->in_packet, &topic)) return 1;
if(STREMPTY(topic)){
if(packet__read_string(&context->in_packet, &topic, &slen)) return 1;
if(!slen){
/* Invalid publish topic, disconnect client. */
mosquitto__free(topic);
return 1;
Expand Down
9 changes: 5 additions & 4 deletions src/handle_subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context)
uint8_t *payload = NULL, *tmp_payload;
uint32_t payloadlen = 0;
int len;
int slen;
char *sub_mount;

if(!context) return MOSQ_ERR_INVAL;
Expand All @@ -50,13 +51,13 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context)

while(context->in_packet.pos < context->in_packet.remaining_length){
sub = NULL;
if(packet__read_string(&context->in_packet, &sub)){
if(packet__read_string(&context->in_packet, &sub, &slen)){
mosquitto__free(payload);
return 1;
}

if(sub){
if(STREMPTY(sub)){
if(!slen){
log__printf(NULL, MOSQ_LOG_INFO,
"Empty subscription string from %s, disconnecting.",
context->address);
Expand All @@ -72,7 +73,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context)
mosquitto__free(payload);
return 1;
}
if(mosquitto_validate_utf8(sub, strlen(sub))){
if(mosquitto_validate_utf8(sub, slen)){
log__printf(NULL, MOSQ_LOG_INFO,
"Malformed UTF-8 in subscription string from %s, disconnecting.",
context->id);
Expand All @@ -94,7 +95,7 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context)
return 1;
}
if(context->listener && context->listener->mount_point){
len = strlen(context->listener->mount_point) + strlen(sub) + 1;
len = strlen(context->listener->mount_point) + slen + 1;
sub_mount = mosquitto__malloc(len+1);
if(!sub_mount){
mosquitto__free(sub);
Expand Down
7 changes: 4 additions & 3 deletions src/handle_unsubscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
{
uint16_t mid;
char *sub;
int slen;

if(!context) return MOSQ_ERR_INVAL;
log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBSCRIBE from %s", context->id);
Expand All @@ -54,12 +55,12 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
}
while(context->in_packet.pos < context->in_packet.remaining_length){
sub = NULL;
if(packet__read_string(&context->in_packet, &sub)){
if(packet__read_string(&context->in_packet, &sub, &slen)){
return 1;
}

if(sub){
if(STREMPTY(sub)){
if(!slen){
log__printf(NULL, MOSQ_LOG_INFO,
"Empty unsubscription string from %s, disconnecting.",
context->id);
Expand All @@ -73,7 +74,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
mosquitto__free(sub);
return 1;
}
if(mosquitto_validate_utf8(sub, strlen(sub))){
if(mosquitto_validate_utf8(sub, slen)){
log__printf(NULL, MOSQ_LOG_INFO,
"Malformed UTF-8 in unsubscription string from %s, disconnecting.",
context->id);
Expand Down

0 comments on commit 3066f89

Please sign in to comment.