recorder/misc.c

230 lines
6.0 KiB
C
Raw Normal View History

2015-09-01 08:19:52 -07:00
/*
2016-02-16 06:09:30 -07:00
* OwnTracks Recorder
* Copyright (C) 2015-2016 Jan-Piet Mens <jpmens@gmail.com>
2015-09-01 08:19:52 -07:00
*
2016-02-16 06:09:30 -07:00
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2015-09-01 08:19:52 -07:00
*
2016-02-16 06:09:30 -07:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2015-09-01 08:19:52 -07:00
*
2016-02-16 06:09:30 -07:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2015-09-01 08:19:52 -07:00
*/
2015-08-14 23:14:34 -07:00
#include <stdio.h>
#include <stdlib.h>
2016-02-09 03:17:59 -07:00
#include <unistd.h>
#include <errno.h>
2016-02-09 03:17:59 -07:00
#include <libconfig.h>
2015-08-14 23:14:34 -07:00
#include "utstring.h"
#include "ctype.h"
2015-08-15 01:46:46 -07:00
#include "udata.h"
2015-08-14 23:14:34 -07:00
#include "misc.h"
2015-09-01 08:15:00 -07:00
#include "util.h"
2015-08-14 23:14:34 -07:00
char *bindump(char *buf, long buflen)
{
static UT_string *out = NULL;
int i, ch;
utstring_renew(out);
for (i = 0; i < buflen; i++) {
ch = buf[i];
if (isprint(ch)) {
utstring_printf(out, "%c", ch);
} else {
utstring_printf(out, " %02X ", ch & 0xFF);
}
}
2015-09-21 07:07:31 -07:00
return (UB(out));
2015-08-14 23:14:34 -07:00
}
2015-08-15 01:42:55 -07:00
/*
* At each received message, the recorder invokes this function with the
* current epoch time and the topic being handled. Use this to update
* a monitoring hoook. If we have Redis, use that exclusively.
2015-08-15 01:42:55 -07:00
*/
2015-08-15 01:46:46 -07:00
void monitorhook(struct udata *userdata, time_t now, char *topic)
2015-08-15 01:42:55 -07:00
{
2015-09-01 04:31:49 -07:00
// struct udata *ud = (struct udata *)userdata;
2015-08-15 01:46:46 -07:00
2015-09-01 04:31:49 -07:00
/* TODO: add monitor hook to a "monitor" key in LMDB ? */
2015-09-01 04:31:49 -07:00
char mpath[BUFSIZ];
static UT_string *us = NULL;
2015-09-01 04:31:49 -07:00
utstring_renew(us);
utstring_printf(us, "%ld %s\n", now, topic);
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
2015-09-21 07:07:31 -07:00
safewrite(mpath, UB(us));
2015-08-15 01:42:55 -07:00
}
/*
* Return a pointer to a static area containing the last monitor entry or NULL.
*/
char *monitor_get()
{
char mpath[BUFSIZ];
static char monitorline[BUFSIZ], *ret = NULL;
FILE *fp;
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
if ((fp = fopen(mpath, "r")) != NULL) {
if (fgets(monitorline, sizeof(monitorline), fp) != NULL) {
char *bp = strchr(monitorline, '\n');
if (bp)
*bp = 0;
ret = monitorline;
}
fclose(fp);
}
return (ret);
}
2016-02-09 03:17:59 -07:00
/*
* Fill in some defaults
*/
void get_defaults(char *filename, struct udata *ud)
{
config_t cfg, *cf;
const char *value;
2016-02-25 09:56:05 -07:00
#if LIBCONFIG_VER_MAJOR == 1
2016-02-25 10:21:48 -07:00
# if LIBCONFIG_VER_MINOR >= 4
int ival;
2016-02-25 09:56:05 -07:00
# endif
# else
long ival;
#endif
2016-02-09 03:17:59 -07:00
if (access(filename, R_OK) == -1) {
olog(LOG_ERR, "Cannot read defaults from %s: %s", filename, strerror(errno));
2016-02-09 03:17:59 -07:00
return;
}
2016-02-09 03:17:59 -07:00
config_init(cf = &cfg);
if (!config_read_file(cf, filename)) {
olog(LOG_ERR, "Syntax error in %s:%d - %s",
filename,
2016-02-09 03:17:59 -07:00
config_error_line(cf),
config_error_text(cf));
config_destroy(cf);
exit(2);
}
if (config_lookup_string(cf, "OTR_STORAGEDIR", &value) != CONFIG_FALSE)
strcpy(STORAGEDIR, value);
2016-02-25 10:59:48 -07:00
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);
2016-10-03 05:58:24 -07:00
ud->hostname = (value) ? strdup(value) : NULL;
}
if (config_lookup_int(cf, "OTR_PORT", &ival) != CONFIG_FALSE) {
ud->port = ival;
}
if (config_lookup_string(cf, "OTR_USER", &value) != CONFIG_FALSE) {
if (ud->username) free(ud->username);
2016-10-03 05:58:24 -07:00
ud->username = (value) ? strdup(value) : NULL;
}
if (config_lookup_string(cf, "OTR_PASS", &value) != CONFIG_FALSE) {
if (ud->password) free(ud->password);
2016-10-03 05:58:24 -07:00
ud->password = (value) ? strdup(value) : NULL;
}
if (config_lookup_int(cf, "OTR_QOS", &ival) != CONFIG_FALSE) {
ud->qos = ival;
}
2016-02-25 10:59:48 -07:00
if (config_lookup_string(cf, "OTR_CLIENTID", &value) != CONFIG_FALSE) {
if (ud->clientid) free(ud->clientid);
2016-10-03 05:58:24 -07:00
ud->clientid = (value) ? strdup(value) : NULL;
2016-02-25 10:59:48 -07:00
}
2016-02-25 23:38:46 -07:00
if (config_lookup_string(cf, "OTR_CAFILE", &value) != CONFIG_FALSE) {
if (ud->cafile) free(ud->cafile);
2016-10-03 05:58:24 -07:00
ud->cafile = (value) ? strdup(value) : NULL;
2016-02-25 23:38:46 -07:00
}
if (config_lookup_string(cf, "OTR_KEYFILE", &value) != CONFIG_FALSE) {
if (ud->keyfile) free(ud->keyfile);
ud->keyfile = (value) ? strdup(value) : NULL;
}
if (config_lookup_string(cf, "OTR_CERTFILE", &value) != CONFIG_FALSE) {
if (ud->certfile) free(ud->certfile);
ud->certfile = (value) ? strdup(value) : NULL;
}
/* Topics is a blank-separated string of words; split and add to JSON array */
if (config_lookup_string(cf, "OTR_TOPICS", &value) != CONFIG_FALSE) {
char *parts[40];
int np, n;
if (ud->topics) json_delete(ud->topics);
if ((np = splitter((char *)value, " ", parts)) < 1) {
olog(LOG_ERR, "Illegal value in OTR_TOPICS");
exit(2);
}
ud->topics = json_mkarray();
for (n = 0; n < np; n++) {
json_append_element(ud->topics, json_mkstring(parts[n]));
}
splitterfree(parts);
}
#endif /* WITH_MQTT */
if (config_lookup_string(cf, "OTR_GEOKEY", &value) != CONFIG_FALSE) {
if (ud->geokey) free(ud->geokey);
2016-10-03 05:58:24 -07:00
ud->geokey = (value) ? strdup(value) : NULL;
}
if (config_lookup_int(cf, "OTR_PRECISION", &ival) != CONFIG_FALSE) {
geohash_setprec(ival);
}
#if WITH_HTTP
if (config_lookup_string(cf, "OTR_HTTPHOST", &value) != CONFIG_FALSE) {
if (ud->http_host) free(ud->http_host);
2016-10-03 05:58:24 -07:00
ud->http_host = (value) ? strdup(value) : NULL;
}
if (config_lookup_int(cf, "OTR_HTTPPORT", &ival) != CONFIG_FALSE) {
ud->http_port = ival;
}
2016-03-03 11:05:44 -07:00
if (config_lookup_string(cf, "OTR_HTTPLOGDIR", &value) != CONFIG_FALSE) {
if (ud->http_logdir) free(ud->http_logdir);
2016-10-03 05:58:24 -07:00
ud->http_logdir = (value) ? strdup(value) : NULL;
2016-03-03 11:05:44 -07:00
}
if (config_lookup_string(cf, "OTR_BROWSERAPIKEY", &value) != CONFIG_FALSE) {
if (ud->browser_apikey) free(ud->browser_apikey);
2016-10-03 05:58:24 -07:00
ud->browser_apikey = (value) ? strdup(value) : NULL;
}
#endif /* WITH_HTTP */
#if WITH_LUA
if (config_lookup_string(cf, "OTR_LUASCRIPT", &value) != CONFIG_FALSE) {
if (ud->luascript) free(ud->luascript);
2016-10-03 05:58:24 -07:00
ud->luascript = (value) ? strdup(value) : NULL;
}
#endif
2016-02-09 03:17:59 -07:00
config_destroy(cf);
}