implement auto gateway Ip auto detection (#25)

Reviewed-on: #25
implemented #19
This commit was merged in pull request #25.
This commit is contained in:
Koda committed 2026-07-14 19:32:02 +01:00
1 parent 74de317ecb
commit a08cd30853
5 files changed
+119 -12

No files matched your search

+22 -6
View File
@@ -1,11 +1,13 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "config.h"
void config_load(openuf_config_t *cfg)
{
/* Defaults */
memset(cfg, 0, sizeof(*cfg));
strncpy(cfg->controller_ip, DEFAULT_CONTROLLER_IP, sizeof(cfg->controller_ip) - 1);
strncpy(cfg->lan_if, DEFAULT_LAN_IF, sizeof(cfg->lan_if) - 1);
strncpy(cfg->ufmodel, DEFAULT_UFMODEL, sizeof(cfg->ufmodel) - 1);
@@ -21,15 +23,29 @@ void config_load(openuf_config_t *cfg)
char line[256];
while (fgets(line, sizeof(line), f)) {
/* Strip newline */
char *key, *val, *eq, *end;
line[strcspn(line, "\r\n")] = '\0';
/* Skip comments / empty */
if (line[0] == '#' || line[0] == '\0') continue;
key = line;
while (isspace((unsigned char)*key)) key++;
if (*key == '#' || *key == '\0') continue;
char key[64] = {0}, val[192] = {0};
if (sscanf(line, " %63[^= ] = %191s", key, val) != 2) continue;
eq = strchr(key, '=');
if (!eq) continue;
*eq = '\0';
end = eq;
while (end > key && isspace((unsigned char)end[-1])) *--end = '\0';
if (!strcmp(key, "controller_ip")) strncpy(cfg->controller_ip, val, sizeof(cfg->controller_ip) - 1);
val = eq + 1;
while (isspace((unsigned char)*val)) val++;
end = val;
while (*end && !isspace((unsigned char)*end) && *end != '#') end++;
*end = '\0';
if (!strcmp(key, "controller_ip")) {
strncpy(cfg->controller_ip, val, sizeof(cfg->controller_ip) - 1);
cfg->controller_ip[sizeof(cfg->controller_ip) - 1] = '\0';
}
else if (!strcmp(key, "lan_if")) strncpy(cfg->lan_if, val, sizeof(cfg->lan_if) - 1);
else if (!strcmp(key, "ufmodel")) strncpy(cfg->ufmodel, val, sizeof(cfg->ufmodel) - 1);
else if (!strcmp(key, "inform_interval")) cfg->inform_interval = atoi(val);