#include #include #include #include #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); cfg->inform_interval = DEFAULT_INFORM_INTERVAL; cfg->enable_announce = 1; cfg->enable_inform = 1; /* File logging is opt-in to avoid flash wear and unbounded log growth. */ cfg->enable_logging = 0; cfg->protocol_debug_level = 0; FILE *f = fopen(OPENUF_CONF_FILE, "r"); if (!f) return; char line[256]; while (fgets(line, sizeof(line), f)) { char *key, *val, *eq, *end; line[strcspn(line, "\r\n")] = '\0'; key = line; while (isspace((unsigned char)*key)) key++; if (*key == '#' || *key == '\0') continue; eq = strchr(key, '='); if (!eq) continue; *eq = '\0'; end = eq; while (end > key && isspace((unsigned char)end[-1])) *--end = '\0'; 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); else if (!strcmp(key, "enable_announce")) cfg->enable_announce = atoi(val); else if (!strcmp(key, "enable_inform")) cfg->enable_inform = atoi(val); else if (!strcmp(key, "enable_logging")) cfg->enable_logging = atoi(val); else if (!strcmp(key, "protocol_debug_level")) cfg->protocol_debug_level = atoi(val); } fclose(f); }