mirror of
https://github.com/eclipse-mosquitto/mosquitto.git
synced 2026-09-21 07:13:46 +08:00
packet__read_string() returns the string length.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+9
-8
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user