diff --git a/README.md b/README.md index ba07340..4dd480d 100644 --- a/README.md +++ b/README.md @@ -387,6 +387,7 @@ The following configuration settings may be applied (a `Y` in column `$` means a | `OTR_USER` | Y | | MQTT username | `OTR_PASS` | Y | | MQTT password | `OTR_QOS` | | `2` | MQTT QoS +| `OTR_CLIENTID` | | hostname+pid | MQTT ClientID (override with -i) | `OTR_HTTPHOST` | | `localhost` | Address for the HTTP module to bind to | `OTR_HTTPPORT` | | `8083` | Port number of the HTTP module to bind to | `OTR_LUASCRIPT` | | | Path to the Lua script diff --git a/etc/ot-recorder.default b/etc/ot-recorder.default index 34502d2..86a466b 100644 --- a/etc/ot-recorder.default +++ b/etc/ot-recorder.default @@ -40,6 +40,12 @@ # OTR_QOS=2 +# ----------------------------------------------------- +# MQTT clientid (default is constant+hostname+pid) +# + +# OTR_CLIENTID="" + # ----------------------------------------------------- # Address for the HTTP module to bind to (default: localhost) # diff --git a/misc.c b/misc.c index ac584a9..6deff8e 100644 --- a/misc.c +++ b/misc.c @@ -125,6 +125,11 @@ void get_defaults(char *filename, struct udata *ud) if (config_lookup_string(cf, "OTR_STORAGEDIR", &value) != CONFIG_FALSE) strcpy(STORAGEDIR, value); + + if (ud == NULL) { + /* being invoked by ocat; return */ + return; + } #if WITH_MQTT if (config_lookup_string(cf, "OTR_HOST", &value) != CONFIG_FALSE) { if (ud->hostname) free(ud->hostname); @@ -144,6 +149,10 @@ void get_defaults(char *filename, struct udata *ud) if (config_lookup_int(cf, "OTR_QOS", &ival) != CONFIG_FALSE) { ud->qos = ival; } + if (config_lookup_string(cf, "OTR_CLIENTID", &value) != CONFIG_FALSE) { + if (ud->clientid) free(ud->clientid); + ud->clientid = strdup(value); + } /* Topics is a blank-separated string of words; split and add to JSON array */ if (config_lookup_string(cf, "OTR_TOPICS", &value) != CONFIG_FALSE) { diff --git a/recorder.c b/recorder.c index 5c3dc47..978a2f1 100644 --- a/recorder.c +++ b/recorder.c @@ -1136,6 +1136,7 @@ int main(int argc, char **argv) udata.password = NULL; udata.hostname = strdup("localhost"); udata.port = 1883; + udata.clientid = NULL; udata.topics = NULL; #endif udata.ignoreretained = TRUE; @@ -1161,6 +1162,15 @@ int main(int argc, char **argv) openlog("ot-recorder", LOG_PID | LOG_PERROR, syslog_facility_code(logfacility)); + utstring_new(clientid); + utstring_printf(clientid, "ot-recorder"); + if (uname(&uts) == 0) { + utstring_printf(clientid, "-%s", uts.nodename); + } + utstring_printf(clientid, "-%d", getpid()); + + ud->clientid = strdup(UB(clientid)); + get_defaults(CONFIGFILE, &udata); @@ -1190,12 +1200,6 @@ int main(int argc, char **argv) ud->password = strdup(p); } - utstring_new(clientid); - utstring_printf(clientid, "ot-recorder"); - if (uname(&uts) == 0) { - utstring_printf(clientid, "-%s", uts.nodename); - } - utstring_printf(clientid, "-%d", getpid()); #endif while (1) { @@ -1267,6 +1271,8 @@ int main(int argc, char **argv) case 'i': utstring_clear(clientid); utstring_printf(clientid, "%s", optarg); + free(ud->clientid); + ud->clientid = strdup(UB(clientid)); break; case 'P': udata.pubprefix = strdup(optarg); /* TODO: do we want this? */ @@ -1467,7 +1473,7 @@ int main(int argc, char **argv) #ifdef WITH_MQTT mosquitto_lib_init(); - mosq = mosquitto_new(UB(clientid), CLEAN_SESSION, (void *)&udata); + mosq = mosquitto_new(ud->clientid, CLEAN_SESSION, (void *)&udata); if (!mosq) { fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); @@ -1515,7 +1521,7 @@ int main(int argc, char **argv) olog(LOG_INFO, "connecting to MQTT on %s:%d as clientID %s %s TLS", ud->hostname, ud->port, - UB(clientid), + ud->clientid, (cafile) ? "with" : "without"); rc = mosquitto_connect(mosq, ud->hostname, ud->port, 60); @@ -1613,6 +1619,7 @@ int main(int argc, char **argv) mosquitto_destroy(mosq); mosquitto_lib_cleanup(); free(ud->hostname); + free(ud->clientid); #endif return (0); diff --git a/udata.h b/udata.h index 4033187..f325b6d 100644 --- a/udata.h +++ b/udata.h @@ -19,6 +19,7 @@ struct udata { int port; /* MQTT port */ char *username; /* MQTT user */ char *password; /* MQTT password */ + char *clientid; /* MQTT clientid */ #endif int skipdemo; /* True if _demo users are to be skipped */ int revgeo; /* True (default) if we should do reverse Geo lookups */