From 5ba6139990373e77d638f4dd903281673e145e7e Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Wed, 9 Oct 2019 15:00:51 +0200 Subject: [PATCH] Let library generate client id when unset --- mqtt-exec.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/mqtt-exec.c b/mqtt-exec.c index 5c69325..ca585f9 100644 --- a/mqtt-exec.c +++ b/mqtt-exec.c @@ -151,8 +151,7 @@ int main(int argc, char *argv[]) int keepalive = 60; int i, c, rc = 1; struct userdata ud; - char hostname[256]; - static char id[MOSQ_MQTT_ID_MAX_LENGTH+1]; + char *id = NULL; struct mosquitto *mosq = NULL; char *username = NULL; char *password = NULL; @@ -174,9 +173,6 @@ int main(int argc, char *argv[]) memset(&ud, 0, sizeof(ud)); - memset(hostname, 0, sizeof(hostname)); - memset(id, 0, sizeof(id)); - while ((c = getopt_long(argc, argv, "cdh:i:k:p:P:q:t:u:v", opts, &i)) != -1) { switch(c) { case 'c': @@ -194,7 +190,7 @@ int main(int argc, char *argv[]) MOSQ_MQTT_ID_MAX_LENGTH); return 1; } - strncpy(id, optarg, sizeof(id)-1); + id = optarg; break; case 'k': keepalive = atoi(optarg); @@ -276,12 +272,6 @@ int main(int argc, char *argv[]) for (i=0; i <= ud.command_argc; i++) ud.command_argv[i] = optind+i < argc ? argv[optind+i] : NULL; - if (id[0] == '\0') { - /* generate an id */ - gethostname(hostname, sizeof(hostname)-1); - snprintf(id, sizeof(id), "mqttexe/%x-%s", getpid(), hostname); - } - mosquitto_lib_init(); mosq = mosquitto_new(id, clean_session, &ud); if (mosq == NULL) @@ -289,7 +279,8 @@ int main(int argc, char *argv[]) if (debug) { printf("host=%s:%d\nid=%s\ntopic_count=%zu\ncommand=%s\n", - host, port, id, ud.topic_count, ud.command_argv[0]); + host, port, id ? id : "(null)", ud.topic_count, + ud.command_argv[0]); mosquitto_log_callback_set(mosq, log_cb); } -- 2.23.0