Skip to content

Commit

Permalink
Fix dropping oversize messages for QoS>0.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Feb 21, 2019
1 parent 1d17ced commit c3c8c99
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ int db__message_release(struct mosquitto_db *db, struct mosquitto *context, uint
int db__message_write(struct mosquitto_db *db, struct mosquitto *context)
{
int rc;
struct mosquitto_client_msg *tail, *last = NULL;
struct mosquitto_client_msg *tail, *last = NULL, *tmp;
uint16_t mid;
int retries;
int retain;
Expand Down Expand Up @@ -959,13 +959,16 @@ int db__message_write(struct mosquitto_db *db, struct mosquitto *context)
tail->timestamp = mosquitto_time();
tail->dup = 1; /* Any retry attempts are a duplicate. */
tail->state = mosq_ms_wait_for_puback;

last = tail;
tail = tail->next;
}else if(rc == MOSQ_ERR_OVERSIZE_PACKET){
tmp = tail->next;
db__message_remove(db, context, &tail, last);
tail = tmp;
}else{
return rc;
}
last = tail;
tail = tail->next;
break;

case mosq_ms_publish_qos2:
Expand All @@ -974,13 +977,16 @@ int db__message_write(struct mosquitto_db *db, struct mosquitto *context)
tail->timestamp = mosquitto_time();
tail->dup = 1; /* Any retry attempts are a duplicate. */
tail->state = mosq_ms_wait_for_pubrec;

last = tail;
tail = tail->next;
}else if(rc == MOSQ_ERR_OVERSIZE_PACKET){
tmp = tail->next;
db__message_remove(db, context, &tail, last);
tail = tmp;
}else{
return rc;
}
last = tail;
tail = tail->next;
break;

case mosq_ms_send_pubrec:
Expand Down
54 changes: 54 additions & 0 deletions test/broker/12-prop-maximum-packet-size-publish-qos1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python

# Test whether maximum packet size is honoured on a PUBLISH to a client
# MQTTv5

from mosq_test_helper import *

rc = 1

keepalive = 10
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_MAXIMUM_PACKET_SIZE, 20)
connect_packet = mosq_test.gen_connect("test", proto_ver=5, keepalive=keepalive, properties=props)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)

mid = 1
subscribe_packet = mosq_test.gen_subscribe(mid, "test/topic", 1, proto_ver=5)
suback_packet = mosq_test.gen_suback(mid, 1, proto_ver=5)

mid=1
publish1_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=1, payload="12345678901234567890", proto_ver=5)
puback1_packet = mosq_test.gen_puback(mid, proto_ver=5)

mid=2
publish2_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=1, payload="7890", proto_ver=5)
puback2_packet = mosq_test.gen_puback(mid, proto_ver=5)

pingreq_packet = mosq_test.gen_pingreq()
pingresp_packet = mosq_test.gen_pingresp()

port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet)

mosq_test.do_send_receive(sock, publish1_packet, puback1_packet, "puback 1")

# We shouldn't receive the publish here because it is > MAXIMUM_PACKET_SIZE
mosq_test.do_send_receive(sock, pingreq_packet, pingresp_packet)

mosq_test.do_send_receive(sock, publish2_packet, puback2_packet, "puback 2")

if mosq_test.expect_packet(sock, "publish2", publish2_packet):
rc = 0
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)

exit(rc)

60 changes: 60 additions & 0 deletions test/broker/12-prop-maximum-packet-size-publish-qos2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Test whether maximum packet size is honoured on a PUBLISH to a client
# MQTTv5

from mosq_test_helper import *

rc = 1

keepalive = 10
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_MAXIMUM_PACKET_SIZE, 20)
connect_packet = mosq_test.gen_connect("test", proto_ver=5, keepalive=keepalive, properties=props)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)

mid = 1
subscribe_packet = mosq_test.gen_subscribe(mid, "test/topic", 2, proto_ver=5)
suback_packet = mosq_test.gen_suback(mid, 2, proto_ver=5)

mid=1
publish1_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=2, payload="12345678901234567890", proto_ver=5)
pubrec1_packet = mosq_test.gen_pubrec(mid, proto_ver=5)
pubrel1_packet = mosq_test.gen_pubrel(mid, proto_ver=5)
pubcomp1_packet = mosq_test.gen_pubcomp(mid, proto_ver=5)

mid=2
publish2_packet = mosq_test.gen_publish(topic="test/topic", mid=mid, qos=2, payload="7890", proto_ver=5)
pubrec2_packet = mosq_test.gen_pubrec(mid, proto_ver=5)
pubrel2_packet = mosq_test.gen_pubrel(mid, proto_ver=5)
pubcomp2_packet = mosq_test.gen_pubcomp(mid, proto_ver=5)

pingreq_packet = mosq_test.gen_pingreq()
pingresp_packet = mosq_test.gen_pingresp()

port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)

try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet)

mosq_test.do_send_receive(sock, publish1_packet, pubrec1_packet, "pubrec 1")
mosq_test.do_send_receive(sock, pubrel1_packet, pubcomp1_packet, "pubcomp 1")

# We shouldn't receive the publish here because it is > MAXIMUM_PACKET_SIZE
mosq_test.do_send_receive(sock, pingreq_packet, pingresp_packet)

mosq_test.do_send_receive(sock, publish2_packet, pubrec2_packet, "pubrec 2")
mosq_test.do_send_receive(sock, pubrel2_packet, pubcomp2_packet, "pubcomp 2")

if mosq_test.expect_packet(sock, "publish2", publish2_packet):
rc = 0
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)

exit(rc)

2 changes: 2 additions & 0 deletions test/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,5 @@ endif
./12-prop-maximum-packet-size-broker.py
./12-prop-maximum-packet-size-connect.py
./12-prop-maximum-packet-size-publish.py
./12-prop-maximum-packet-size-publish-qos1.py
./12-prop-maximum-packet-size-publish-qos2.py
2 changes: 2 additions & 0 deletions test/broker/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
(1, './12-prop-maximum-packet-size-broker.py'),
(1, './12-prop-maximum-packet-size-connect.py'),
(1, './12-prop-maximum-packet-size-publish.py'),
(1, './12-prop-maximum-packet-size-publish-qos1.py'),
(1, './12-prop-maximum-packet-size-publish-qos2.py'),
]

ptest.run_tests(tests)

0 comments on commit c3c8c99

Please sign in to comment.