aboutsummaryrefslogtreecommitdiffstats
path: root/main/mqtt-exec/0001-Let-library-generate-client-id-when-unset.patch
blob: bd6411e5e3112354b8e954ec922f7ffe5c510742 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
From 5ba6139990373e77d638f4dd903281673e145e7e Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
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