clientID configurable from config file

This commit is contained in:
Jan-Piet Mens
2016-02-25 18:59:48 +01:00
parent 21bed4b184
commit c4daf5c523
5 changed files with 32 additions and 8 deletions
+1
View File
@@ -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
+6
View File
@@ -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)
#
+9
View File
@@ -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) {
+15 -8
View File
@@ -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);
+1
View File
@@ -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 */