diff --git a/lib/connect.c b/lib/connect.c index 6fbc776e..0de00322 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -33,9 +33,27 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address) { + int i; + if(!mosq) return MOSQ_ERR_INVAL; if(!host || port <= 0) return MOSQ_ERR_INVAL; + if(mosq->id == NULL && (mosq->protocol == mosq_p_mqtt31 || mosq->protocol == mosq_p_mqtt311)){ + mosq->id = (char *)mosquitto__calloc(24, sizeof(char)); + if(!mosq->id){ + return MOSQ_ERR_NOMEM; + } + mosq->id[0] = 'm'; + mosq->id[1] = 'o'; + mosq->id[2] = 's'; + mosq->id[3] = 'q'; + mosq->id[4] = '/'; + + for(i=5; i<23; i++){ + mosq->id[i] = (random()%73)+48; + } + } + mosquitto__free(mosq->host); mosq->host = mosquitto__strdup(host); if(!mosq->host) return MOSQ_ERR_NOMEM; diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 688312d3..92c4e9ea 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -104,8 +104,6 @@ struct mosquitto *mosquitto_new(const char *id, bool clean_start, void *userdata int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_start, void *userdata) { - int i; - if(!mosq) return MOSQ_ERR_INVAL; if(clean_start == false && id == NULL){ @@ -134,20 +132,6 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st return MOSQ_ERR_MALFORMED_UTF8; } mosq->id = mosquitto__strdup(id); - }else{ - mosq->id = (char *)mosquitto__calloc(24, sizeof(char)); - if(!mosq->id){ - return MOSQ_ERR_NOMEM; - } - mosq->id[0] = 'm'; - mosq->id[1] = 'o'; - mosq->id[2] = 's'; - mosq->id[3] = 'q'; - mosq->id[4] = '/'; - - for(i=5; i<23; i++){ - mosq->id[i] = (rand()%73)+48; - } } mosq->in_packet.payload = NULL; packet__cleanup(&mosq->in_packet); diff --git a/lib/send_connect.c b/lib/send_connect.c index a052a398..5b50e2c7 100644 --- a/lib/send_connect.c +++ b/lib/send_connect.c @@ -44,7 +44,8 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session int proplen, varbytes; assert(mosq); - assert(mosq->id); + + if(mosq->protocol == mosq_p_mqtt31 && !mosq->id) return MOSQ_ERR_PROTOCOL; #if defined(WITH_BROKER) && defined(WITH_BRIDGE) if(mosq->bridge){ @@ -81,7 +82,11 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet)); if(!packet) return MOSQ_ERR_NOMEM; - payloadlen = 2+strlen(clientid); + if(clientid){ + payloadlen = 2+strlen(clientid); + }else{ + payloadlen = 2; + } if(mosq->will){ will = 1; assert(mosq->will->msg.topic); @@ -140,7 +145,11 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session } /* Payload */ - packet__write_string(packet, clientid, strlen(clientid)); + if(clientid){ + packet__write_string(packet, clientid, strlen(clientid)); + }else{ + packet__write_uint16(packet, 0); + } if(will){ if(mosq->protocol == mosq_p_mqtt5){ /* Write will properties */ diff --git a/test/broker/01-connect-invalid-id-missing.py b/test/broker/01-connect-invalid-id-missing.py index 32188a12..63273e93 100755 --- a/test/broker/01-connect-invalid-id-missing.py +++ b/test/broker/01-connect-invalid-id-missing.py @@ -6,13 +6,14 @@ from mosq_test_helper import * rc = 1 keepalive = 10 -connect_packet = mosq_test.gen_connect(None, keepalive=keepalive) +connect_packet = mosq_test.gen_connect(None, keepalive=keepalive, proto_ver=3) +connack_packet = mosq_test.gen_connack(rc=2) 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, "", port=port) + sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) sock.close() rc = 0 finally: diff --git a/test/broker/12-prop-assigned-client-identifier.py b/test/broker/12-prop-assigned-client-identifier.py new file mode 100755 index 00000000..fac50d11 --- /dev/null +++ b/test/broker/12-prop-assigned-client-identifier.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Test whether sending a non zero session expiry interval in DISCONNECT after +# having sent a zero session expiry interval is treated correctly in MQTT v5. + +from mosq_test_helper import * + +rc = 1 + +keepalive = 10 + + +connect_packet = mosq_test.gen_connect(None, proto_ver=5, keepalive=keepalive) + +props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_ASSIGNED_CLIENT_IDENTIFIER, "auto-00000000-0000-0000-0000-000000000000") +connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props) + +props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 1) +props = mqtt5_props.prop_finalise(props) +disconnect_client_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props) + +disconnect_server_packet = mosq_test.gen_disconnect(proto_ver=5, reason_code=130) + +port = mosq_test.get_port() +broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) + +try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(10) + sock.connect(("localhost", port)) + + sock.send(connect_packet) + connack_recvd = sock.recv(len(connack_packet)) + + if connack_recvd[0:12] == connack_packet[0:12]: + # FIXME - this test could be tightened up a lot + rc = 0 + + sock.close() +finally: + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde) + +exit(rc) + diff --git a/test/broker/Makefile b/test/broker/Makefile index f19566ea..db0b971a 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -139,3 +139,4 @@ endif ./12-prop-session-expiry-invalid.py ./12-prop-subpub-payload-format.py ./12-prop-subpub-content-type.py + ./12-prop-assigned-client-identifier.py diff --git a/test/broker/ptest.py b/test/broker/ptest.py index 5d9fb0a3..ff2984fd 100755 --- a/test/broker/ptest.py +++ b/test/broker/ptest.py @@ -108,6 +108,7 @@ tests = [ (1, './12-prop-session-expiry-invalid.py'), (1, './12-prop-subpub-payload-format.py'), (1, './12-prop-subpub-content-type.py'), + (1, './12-prop-assigned-client-identifier.py'), ] minport = 1888 diff --git a/test/mosq_test.py b/test/mosq_test.py index cc3e9264..8d29d1a5 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -308,6 +308,8 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass if client_id != None: remaining_length = remaining_length + 2+len(client_id) + else: + remaining_length = remaining_length + 2 connect_flags = 0 @@ -347,6 +349,8 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass if client_id != None: packet = packet + struct.pack("!H"+str(len(client_id))+"s", len(client_id), client_id) + else: + packet = packet + struct.pack("!H", 0) if will_topic != None: packet = packet + struct.pack("!H"+str(len(will_topic))+"s", len(will_topic), will_topic)