From 4025fad5aa3a1d5100570d8d63426097770b3cd8 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 15 Jun 2014 01:07:05 +0100 Subject: [PATCH] libuuid is used to generate client ids, where it is available, when an MQTT v3.1.1 client connects with a zero length client id. --- ChangeLog.txt | 2 ++ config.mk | 3 ++- src/CMakeLists.txt | 7 +++++++ src/read_handle_server.c | 24 +++++++++++++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index f698cd09..d910c3b7 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -29,6 +29,8 @@ Broker: - When a durable client reconnects, its queued messages are now checked against ACLs in case of a change in username/ACL state since it last connected. +- libuuid is used to generate client ids, where it is available, when an MQTT + v3.1.1 client connects with a zero length client id. Clients: - Both clients can now load default configuration options from a file. diff --git a/config.mk b/config.mk index 4b9f5827..0d6a4ab1 100644 --- a/config.mk +++ b/config.mk @@ -117,8 +117,9 @@ LIB_LIBS:= PASSWD_LIBS:= ifeq ($(UNAME),Linux) - BROKER_LIBS:=$(BROKER_LIBS) -lrt + BROKER_LIBS:=$(BROKER_LIBS) -lrt -luuid LIB_LIBS:=$(LIB_LIBS) -lrt + BROKER_CFLAGS:=$(BROKER_CFLAGS) -DWITH_UUID endif CLIENT_LDFLAGS:=$(LDFLAGS) -L../lib ../lib/libmosquitto.so.${SOVERSION} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ce8bc194..fffcc1f9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -98,6 +98,13 @@ if (${WITH_WEBSOCKETS} STREQUAL ON) set (MOSQ_LIBS ${MOSQ_LIBS} websockets) endif (${WITH_WEBSOCKETS} STREQUAL ON) +# Simple detect libuuid +FIND_PATH(UUID_HEADER uuid/uuid.h) +if (UUID_HEADER) + add_definitions(-DWITH_UUID) + set (MOSQ_LIBS ${MOSQ_LIBS} uuid) +endif (UUID_HEADER) + target_link_libraries(mosquitto ${MOSQ_LIBS}) install(TARGETS mosquitto RUNTIME DESTINATION ${SBINDIR} LIBRARY DESTINATION ${LIBDIR}) diff --git a/src/read_handle_server.c b/src/read_handle_server.c index c0af4973..36e0abc6 100644 --- a/src/read_handle_server.c +++ b/src/read_handle_server.c @@ -27,6 +27,10 @@ Contributors: #include #include +#ifdef WITH_UUID +# include +#endif + #ifdef WITH_SYS_TREE extern unsigned int g_connection_count; #endif @@ -34,17 +38,35 @@ extern unsigned int g_connection_count; static char *client_id_gen(struct mosquitto_db *db) { char *client_id; +#ifdef WITH_UUID + uuid_t uuid; +#else int i; +#endif +#ifdef WITH_UUID + client_id = (char *)_mosquitto_calloc(37 + db->config->auto_id_prefix_len, sizeof(char)); + if(!client_id){ + return NULL; + } + if(db->config->auto_id_prefix){ + memcpy(client_id, db->config->auto_id_prefix, db->config->auto_id_prefix_len); + } + uuid_generate_random(uuid); + uuid_unparse_lower(uuid, &client_id[db->config->auto_id_prefix_len]); +#else client_id = (char *)_mosquitto_calloc(65 + db->config->auto_id_prefix_len, sizeof(char)); if(!client_id){ return NULL; } - memcpy(client_id, db->config->auto_id_prefix, db->config->auto_id_prefix_len); + if(db->config->auto_id_prefix){ + memcpy(client_id, db->config->auto_id_prefix, db->config->auto_id_prefix_len); + } for(i=0; i<64; i++){ client_id[i+db->config->auto_id_prefix_len] = (rand()%73)+48; } client_id[i] = '\0'; +#endif return client_id; }