From b6c4055a868345ca1cb76e737b5b0494fab8989c Mon Sep 17 00:00:00 2001 From: claes Date: Sat, 10 Jun 2023 22:01:43 +0200 Subject: [PATCH] Make the config file overridable --- mqtt-arp.c | 113 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 20 deletions(-) diff --git a/mqtt-arp.c b/mqtt-arp.c index bc65fbb..024f44c 100644 --- a/mqtt-arp.c +++ b/mqtt-arp.c @@ -293,8 +293,10 @@ int read_config(char *file, struct ma_config *config, int *macs) int i; f = fopen(file, "r"); - if (f == NULL) + if (f == NULL) { + fprintf(stderr, "Could not read config file %s\n", file); return errno; + } #define INT_OPTION(opt, var) \ if (strncmp(line, opt " ", sizeof(opt)) == 0) { \ @@ -341,6 +343,67 @@ int read_config(char *file, struct ma_config *config, int *macs) return 0; } +void override_config(const struct ma_config *source, struct ma_config *target) +{ + int i; + + if (source->mqtt_host != NULL) { + target->mqtt_host = source->mqtt_host; + } + if (source->mqtt_port != 0) { + target->mqtt_port = source->mqtt_port; + } + if (source->mqtt_username != NULL) { + target->mqtt_username = source->mqtt_username; + } + if (source->mqtt_password != NULL) { + target->mqtt_password = source->mqtt_password; + } + if (source->mqtt_topic != NULL) { + target->mqtt_topic = source->mqtt_topic; + } + if (source->location != NULL) { + target->location = source->location; + } + if (source->capath != NULL) { + target->capath = source->capath; + } + for (i = 0; i < MAX_MACS; ++i) { + if (source->macs[i].valid) { + memcpy(&target->macs[i], &source->macs[i], sizeof(struct mac_entry)); + } + } +} + +void print_config(const struct ma_config *config) +{ + int i, j; + + printf("Config:\n"); + printf("mqtt_host: %s\n", config->mqtt_host ? config->mqtt_host : "NULL"); + printf("mqtt_port: %d\n", config->mqtt_port); + printf("mqtt_username: %s\n", config->mqtt_username ? config->mqtt_username : "NULL"); + printf("mqtt_password: %s\n", config->mqtt_password ? config->mqtt_password : "NULL"); + printf("mqtt_topic: %s\n", config->mqtt_topic ? config->mqtt_topic : "NULL"); + printf("location: %s\n", config->location ? config->location : "NULL"); + printf("capath: %s\n", config->capath ? config->capath : "NULL"); + + for (i = 0; i < MAX_MACS; ++i) { + if (config->macs[i].valid) { + printf("macs[%d]: { valid: true, mac: ", i); + for (j = 0; j < 6; ++j) { + printf("%02x", config->macs[i].mac[j]); + if (j < 5) { + printf(":"); + } + } + printf("\n"); + } else { + printf("macs[%d]: { valid: false }\n", i); + } + } +} + struct option long_options[] = { { "capath", required_argument, 0, 'c' }, { "host", required_argument, 0, 'h' }, @@ -351,6 +414,7 @@ struct option long_options[] = { { "topic", required_argument, 0, 't' }, { "username", required_argument, 0, 'u' }, { "verbose", no_argument, 0, 'v' }, + { "configfile", required_argument, 0, 'f' }, { 0, 0, 0, 0 } }; @@ -359,32 +423,34 @@ int main(int argc, char *argv[]) int sock; struct mosquitto *mosq; struct ma_config config; + struct ma_config cmdline_config; int option_index = 0; int macs = 0; int c; + char *config_file = CONFIG_FILE; bzero(&config, sizeof(config)); + bzero(&cmdline_config, sizeof(cmdline_config)); config.mqtt_port = MQTT_PORT; - /* Read config before parsing command line */ - read_config(CONFIG_FILE, &config, &macs); - while (1) { - c = getopt_long(argc, argv, "c:h:l:m:p:P:t:u:v", + c = getopt_long(argc, argv, "c:h:l:m:p:P:t:u:f:v", long_options, &option_index); if (c == -1) break; - switch (c) { + case 'f': + config_file = optarg; + break; case 'c': - config.capath = optarg; + cmdline_config.capath = optarg; break; case 'h': - config.mqtt_host = optarg; + cmdline_config.mqtt_host = optarg; break; case 'l': - config.location = optarg; + cmdline_config.location = optarg; break; case 'm': if (macs >= MAX_MACS) { @@ -394,26 +460,26 @@ int main(int argc, char *argv[]) } sscanf(optarg, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", - &config.macs[macs].mac[0], - &config.macs[macs].mac[1], - &config.macs[macs].mac[2], - &config.macs[macs].mac[3], - &config.macs[macs].mac[4], - &config.macs[macs].mac[5]); - config.macs[macs].valid = true; + &cmdline_config.macs[macs].mac[0], + &cmdline_config.macs[macs].mac[1], + &cmdline_config.macs[macs].mac[2], + &cmdline_config.macs[macs].mac[3], + &cmdline_config.macs[macs].mac[4], + &cmdline_config.macs[macs].mac[5]); + cmdline_config.macs[macs].valid = true; macs++; break; case 'p': - config.mqtt_port = atoi(optarg); + cmdline_config.mqtt_port = atoi(optarg); break; case 'P': - config.mqtt_password = optarg; + cmdline_config.mqtt_password = optarg; break; case 't': - config.mqtt_topic = optarg; + cmdline_config.mqtt_topic = optarg; break; case 'u': - config.mqtt_username = optarg; + cmdline_config.mqtt_username = optarg; break; case 'v': debug = true; @@ -424,6 +490,10 @@ int main(int argc, char *argv[]) } } + read_config(config_file, &config, &macs); + + override_config(&cmdline_config, &config); + if (!config.mqtt_host) config.mqtt_host = MQTT_HOST; if (!config.mqtt_topic) @@ -431,6 +501,9 @@ int main(int argc, char *argv[]) if (!config.location) config.location = LOCATION; + if (debug) + print_config(&config); + signal(SIGTERM, shutdown_request); sock = netlink_init(); -- 2.39.5