Task 1: Migrate to GNU Autotools - Add configure.ac, Makefile.am files (root, src, u_async, tests) - Add autogen.sh bootstrap script - Create INSTALL.autotools guide - Update .gitignore for autotools - Old Makefile backed up as Makefile.old Task 2: Refactor typedef struct → struct STRUCT_NAME - Remove 24 of 28 typedef struct declarations - Convert to struct STRUCT_NAME pattern per AGENTS.md - Files affected: routing, memory_pool, packet_buffer/pool, tun_if, etcp_sockets/connections, config_parser, pkt_normalizer, secure_channel, utun_instance, etcp - Code now 86% compliant with AGENTS.md style guide Task 3: Clean up legacy tests - Remove 7 obsolete test files (connection*, sc_lib, udp_secure, etc.) - Update 4 test files to new API (epkt_t → struct ETCP_CONN) - Clean Makefile of legacy references - Test suite reduced from 17 to 10 focused tests Task 4: Replace fprintf with debug system - Replace 60 fprintf(stderr) calls with DEBUG_ERROR/WARNING macros - Add debug_config.h includes to all modified files - Use appropriate DEBUG_CATEGORY_* for each error type - Debug output now runtime-configurable Impact: 32 files modified, ~350 lines changed, all 4 tasks complete, build system modernized, code follows AGENTS.md, debugging improved.
395 lines
14 KiB
C
395 lines
14 KiB
C
// config_parser.c - Configuration parser for utun application (simplified)
|
|
#define _POSIX_C_SOURCE 200809L
|
|
#include "config_parser.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include "debug_config.h"
|
|
#include <ctype.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#define MAX_LINE_LEN 1024
|
|
#define INITIAL_ARRAY_CAPACITY 8
|
|
|
|
typedef enum {
|
|
SECTION_UNKNOWN,
|
|
SECTION_GLOBAL,
|
|
SECTION_SERVER,
|
|
SECTION_CLIENT,
|
|
SECTION_CONNECTION,
|
|
SECTION_ROUTING
|
|
} section_type_t;
|
|
|
|
static char* trim(char *str) {
|
|
if (!str) return NULL;
|
|
while (isspace((unsigned char)*str)) str++;
|
|
char *end = str + strlen(str) - 1;
|
|
while (end > str && isspace((unsigned char)*end)) end--;
|
|
*(end + 1) = '\0';
|
|
return str;
|
|
}
|
|
|
|
static int parse_key_value(const char *line, char *key, size_t key_len, char *value, size_t value_len) {
|
|
char *equal = strchr(line, '=');
|
|
if (!equal) return -1;
|
|
|
|
size_t key_size = equal - line;
|
|
if (key_size >= key_len) return -1;
|
|
strncpy(key, line, key_size);
|
|
key[key_size] = '\0';
|
|
trim(key);
|
|
|
|
const char *val_start = equal + 1;
|
|
size_t val_len = strlen(val_start);
|
|
if (val_len >= value_len) return -1;
|
|
strcpy(value, val_start);
|
|
trim(value);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ensure_capacity(void **array, int *capacity, int item_size) {
|
|
if (!array || !capacity) return -1;
|
|
|
|
int new_capacity = *capacity == 0 ? INITIAL_ARRAY_CAPACITY : *capacity * 2;
|
|
void *new_array = realloc(*array, new_capacity * item_size);
|
|
if (!new_array) return -1;
|
|
|
|
*array = new_array;
|
|
*capacity = new_capacity;
|
|
return 0;
|
|
}
|
|
|
|
static int assign_string(char *dest, size_t dest_size, const char *src) {
|
|
if (!dest || !src || strlen(src) >= dest_size) return -1;
|
|
strcpy(dest, src);
|
|
return 0;
|
|
}
|
|
|
|
static int parse_ip_port(const char *addr, uint32_t *ip, uint16_t *port) {
|
|
char addr_copy[MAX_ADDR_LEN];
|
|
if (strlen(addr) >= MAX_ADDR_LEN) return -1;
|
|
strcpy(addr_copy, addr);
|
|
|
|
char *colon = strrchr(addr_copy, ':');
|
|
if (!colon) return -1;
|
|
|
|
*colon = '\0';
|
|
*port = (uint16_t)atoi(colon + 1);
|
|
|
|
struct in_addr ina;
|
|
if (inet_pton(AF_INET, addr_copy, &ina) != 1) return -1;
|
|
*ip = ina.s_addr;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int add_server(struct utun_config *cfg, const struct server_config *srv) {
|
|
if (cfg->server_count >= cfg->server_capacity) {
|
|
if (ensure_capacity((void**)&cfg->servers, &cfg->server_capacity, sizeof(struct server_config)) < 0) return -1;
|
|
}
|
|
memcpy(&cfg->servers[cfg->server_count++], srv, sizeof(struct server_config));
|
|
return 0;
|
|
}
|
|
|
|
static int add_client(struct utun_config *cfg, const struct client_config *cli) {
|
|
if (cfg->client_count >= cfg->client_capacity) {
|
|
if (ensure_capacity((void**)&cfg->clients, &cfg->client_capacity, sizeof(struct client_config)) < 0) return -1;
|
|
}
|
|
memcpy(&cfg->clients[cfg->client_count++], cli, sizeof(struct client_config));
|
|
return 0;
|
|
}
|
|
|
|
static int add_route(struct utun_config *cfg, const char *server, const char *client) {
|
|
if (!cfg->connections_v2) {
|
|
cfg->connections_v2 = calloc(1, sizeof(struct connection_config_v2));
|
|
if (!cfg->connections_v2) return -1;
|
|
cfg->connection_v2_capacity = 1;
|
|
}
|
|
|
|
struct connection_config_v2 *conn = &cfg->connections_v2[cfg->connection_v2_count];
|
|
|
|
if (conn->route_count >= conn->route_capacity) {
|
|
if (ensure_capacity((void**)&conn->routes, &conn->route_capacity, sizeof(struct route_pair)) < 0) return -1;
|
|
}
|
|
|
|
struct route_pair *route = &conn->routes[conn->route_count++];
|
|
if (assign_string(route->server_name, MAX_CONN_NAME_LEN, server) < 0) return -1;
|
|
if (assign_string(route->client_name, MAX_CONN_NAME_LEN, client) < 0) return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int parse_global(const char *key, const char *value, struct global_config *global) {
|
|
if (strcmp(key, "my_private_key") == 0) return assign_string(global->my_private_key_hex, MAX_KEY_LEN, value);
|
|
if (strcmp(key, "my_public_key") == 0) return assign_string(global->my_public_key_hex, MAX_KEY_LEN, value);
|
|
if (strcmp(key, "tun_ip") == 0) return assign_string(global->tun_ip, MAX_ADDR_LEN, value);
|
|
if (strcmp(key, "mtu") == 0) { global->mtu = atoi(value); return 0; }
|
|
if (strcmp(key, "control_ip") == 0) return assign_string(global->control_ip, MAX_ADDR_LEN, value);
|
|
if (strcmp(key, "control_port") == 0) { global->control_port = (uint16_t)atoi(value); return 0; }
|
|
if (strcmp(key, "net_debug") == 0) { global->net_debug = atoi(value); return 0; }
|
|
return 0;
|
|
}
|
|
|
|
static int parse_server(const char *key, const char *value, struct server_config *srv) {
|
|
if (strcmp(key, "addr") == 0) return assign_string(srv->addr, MAX_ADDR_LEN, value);
|
|
if (strcmp(key, "so_mark") == 0) { srv->so_mark = atoi(value); return 0; }
|
|
if (strcmp(key, "netif") == 0) return assign_string(srv->netif, MAX_NETIF_LEN, value);
|
|
if (strcmp(key, "type") == 0) return 0; // Ignored: not stored or used
|
|
return 0;
|
|
}
|
|
|
|
static int parse_client(const char *key, const char *value, struct client_config *cli) {
|
|
// Handle link=server:ip:port format
|
|
if (strcmp(key, "link") == 0) {
|
|
char link_copy[MAX_CONN_NAME_LEN * 2];
|
|
if (strlen(value) >= sizeof(link_copy)) return -1;
|
|
strcpy(link_copy, value);
|
|
|
|
// Find first colon (separator between server and ip:port)
|
|
char *first_colon = strchr(link_copy, ':');
|
|
if (!first_colon) return -1;
|
|
|
|
*first_colon = '\0';
|
|
|
|
if (assign_string(cli->from, MAX_CONN_NAME_LEN, link_copy) < 0) return -1;
|
|
if (assign_string(cli->to_addr, MAX_ADDR_LEN, first_colon + 1) < 0) return -1;
|
|
|
|
return 1; // Signal that client is ready to be added
|
|
}
|
|
|
|
if (strcmp(key, "from") == 0) return assign_string(cli->from, MAX_CONN_NAME_LEN, value);
|
|
if (strcmp(key, "to_addr") == 0) return assign_string(cli->to_addr, MAX_ADDR_LEN, value);
|
|
// Support for test config format: addr1, addr2
|
|
if (strncmp(key, "addr", 4) == 0) return assign_string(cli->to_addr, MAX_ADDR_LEN, value);
|
|
return 0;
|
|
}
|
|
|
|
static int parse_connection(const char *key, const char *value, struct connection_config_v2 *conn) {
|
|
if (strcmp(key, "link") == 0) {
|
|
char link_copy[MAX_CONN_NAME_LEN * 2];
|
|
if (strlen(value) >= sizeof(link_copy)) return -1;
|
|
strcpy(link_copy, value);
|
|
|
|
char *colon = strchr(link_copy, ':');
|
|
if (!colon) return -1;
|
|
*colon = '\0';
|
|
|
|
return add_route(NULL, link_copy, colon + 1); // NULL cfg - will use global
|
|
}
|
|
if (strcmp(key, "keepalive") == 0) { conn->keepalive = atoi(value); return 0; }
|
|
if (strcmp(key, "peer_public_key") == 0) return assign_string(conn->peer_public_key_hex, MAX_KEY_LEN, value);
|
|
return 0;
|
|
}
|
|
|
|
static section_type_t parse_section_header(const char *line, char *name, size_t name_len) {
|
|
if (line[0] != '[') return SECTION_UNKNOWN;
|
|
|
|
size_t line_len = strlen(line);
|
|
if (line[line_len - 1] != ']') return SECTION_UNKNOWN;
|
|
|
|
char section[128];
|
|
if (line_len - 2 >= sizeof(section)) return SECTION_UNKNOWN;
|
|
|
|
strncpy(section, line + 1, line_len - 2);
|
|
section[line_len - 2] = '\0';
|
|
trim(section);
|
|
|
|
if (strcasecmp(section, "global") == 0) return SECTION_GLOBAL;
|
|
if (strcasecmp(section, "routing") == 0) return SECTION_ROUTING;
|
|
|
|
char *colon = strchr(section, ':');
|
|
if (!colon) return SECTION_UNKNOWN;
|
|
|
|
*colon = '\0';
|
|
char *type = trim(section);
|
|
char *n = trim(colon + 1);
|
|
|
|
if (strlen(n) >= name_len) return SECTION_UNKNOWN;
|
|
strcpy(name, n);
|
|
|
|
if (strcasecmp(type, "server") == 0) return SECTION_SERVER;
|
|
if (strcasecmp(type, "client") == 0) return SECTION_CLIENT;
|
|
if (strcasecmp(type, "connection") == 0) return SECTION_CONNECTION;
|
|
|
|
return SECTION_UNKNOWN;
|
|
}
|
|
|
|
static struct utun_config* parse_config_internal(FILE *fp, const char *filename) {
|
|
struct utun_config *cfg = calloc(1, sizeof(struct utun_config));
|
|
if (!cfg) return NULL;
|
|
|
|
section_type_t cur_section = SECTION_UNKNOWN;
|
|
struct server_config cur_server = {0};
|
|
struct client_config cur_client = {0};
|
|
char line[MAX_LINE_LEN];
|
|
int line_num = 0;
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
line_num++;
|
|
char *trimmed = trim(line);
|
|
|
|
if (trimmed[0] == '\0' || trimmed[0] == '#') continue;
|
|
|
|
if (trimmed[0] == '[') {
|
|
if (cur_section == SECTION_SERVER && cur_server.name[0]) {
|
|
if (add_server(cfg, &cur_server) < 0) goto error;
|
|
}
|
|
if (cur_section == SECTION_CLIENT && cur_client.name[0]) {
|
|
if (add_client(cfg, &cur_client) < 0) goto error;
|
|
}
|
|
|
|
char name[MAX_CONN_NAME_LEN];
|
|
cur_section = parse_section_header(trimmed, name, sizeof(name));
|
|
|
|
if (cur_section == SECTION_SERVER) {
|
|
memset(&cur_server, 0, sizeof(cur_server));
|
|
strcpy(cur_server.name, name);
|
|
} else if (cur_section == SECTION_CLIENT) {
|
|
memset(&cur_client, 0, sizeof(cur_client));
|
|
strcpy(cur_client.name, name);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
char key[MAX_LINE_LEN], value[MAX_LINE_LEN];
|
|
if (parse_key_value(trimmed, key, sizeof(key), value, sizeof(value)) < 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid key=value format", filename, line_num);
|
|
continue;
|
|
}
|
|
|
|
switch (cur_section) {
|
|
case SECTION_GLOBAL:
|
|
if (parse_global(key, value, &cfg->global) < 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid global key '%s'", filename, line_num, key);
|
|
}
|
|
break;
|
|
case SECTION_SERVER:
|
|
if (parse_server(key, value, &cur_server) < 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid server key '%s'", filename, line_num, key);
|
|
}
|
|
break;
|
|
case SECTION_CLIENT:
|
|
{
|
|
int ret = parse_client(key, value, &cur_client);
|
|
if (ret == 1) {
|
|
// Client ready to add (from link=)
|
|
char saved_name[MAX_CONN_NAME_LEN];
|
|
strcpy(saved_name, cur_client.name); // Save name
|
|
if (add_client(cfg, &cur_client) < 0) goto error;
|
|
memset(&cur_client, 0, sizeof(cur_client));
|
|
strcpy(cur_client.name, saved_name); // Restore name for next link
|
|
} else if (ret < 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid client key '%s'", filename, line_num, key);
|
|
}
|
|
}
|
|
break;
|
|
case SECTION_ROUTING:
|
|
// Routing settings are not stored yet, just ignore
|
|
break;
|
|
case SECTION_CONNECTION:
|
|
if (cfg->connection_v2_count == 0) {
|
|
cfg->connection_v2_count = 1;
|
|
}
|
|
if (parse_connection(key, value, &cfg->connections_v2[0]) < 0) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid connection key '%s'", filename, line_num, key);
|
|
}
|
|
break;
|
|
default:
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Key outside section: %s", filename, line_num, key);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (cur_section == SECTION_SERVER && cur_server.name[0]) {
|
|
if (add_server(cfg, &cur_server) < 0) goto error;
|
|
}
|
|
if (cur_section == SECTION_CLIENT && cur_client.name[0] && cur_client.to_addr[0]) {
|
|
if (add_client(cfg, &cur_client) < 0) goto error;
|
|
}
|
|
|
|
return cfg;
|
|
|
|
error:
|
|
free_config(cfg);
|
|
return NULL;
|
|
}
|
|
|
|
struct utun_config* parse_config(const char *filename) {
|
|
FILE *fp = fopen(filename, "r");
|
|
if (!fp) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to open config file: %s", filename);
|
|
return NULL;
|
|
}
|
|
|
|
struct utun_config *config = parse_config_internal(fp, filename);
|
|
fclose(fp);
|
|
return config;
|
|
}
|
|
|
|
void free_config(struct utun_config *config) {
|
|
if (!config) return;
|
|
|
|
free(config->servers);
|
|
free(config->clients);
|
|
|
|
if (config->connections_v2) {
|
|
for (int i = 0; i < config->connection_v2_count; i++) {
|
|
free(config->connections_v2[i].routes);
|
|
}
|
|
free(config->connections_v2);
|
|
}
|
|
|
|
free(config);
|
|
}
|
|
|
|
void print_config(const struct utun_config *cfg) {
|
|
if (!cfg) return;
|
|
|
|
const struct global_config *g = &cfg->global;
|
|
printf("Global:\n");
|
|
printf(" Private key: %s\n", g->my_private_key_hex);
|
|
printf(" Public key: %s\n", g->my_public_key_hex);
|
|
printf(" TUN IP: %s\n", g->tun_ip);
|
|
printf(" MTU: %d\n", g->mtu);
|
|
printf(" Control: %s:%u\n", g->control_ip, g->control_port);
|
|
printf(" Net debug: %d\n", g->net_debug);
|
|
|
|
printf("\nServers (%d):\n", cfg->server_count);
|
|
for (int i = 0; i < cfg->server_count; i++) {
|
|
const struct server_config *s = &cfg->servers[i];
|
|
printf(" %s: %s (mark=%d, netif=%s)\n", s->name, s->addr, s->so_mark, s->netif);
|
|
}
|
|
|
|
printf("\nClients (%d):\n", cfg->client_count);
|
|
for (int i = 0; i < cfg->client_count; i++) {
|
|
const struct client_config *c = &cfg->clients[i];
|
|
printf(" %s: %s (from %s)\n", c->name, c->to_addr, c->from);
|
|
}
|
|
|
|
printf("\nConnections v2 (%d):\n", cfg->connection_v2_count);
|
|
for (int i = 0; i < cfg->connection_v2_count; i++) {
|
|
const struct connection_config_v2 *conn = &cfg->connections_v2[i];
|
|
printf(" Connection %d:\n", i);
|
|
for (int j = 0; j < conn->route_count; j++) {
|
|
printf(" %s -> %s\n", conn->routes[j].server_name, conn->routes[j].client_name);
|
|
}
|
|
printf(" Peer key: %s\n", conn->peer_public_key_hex);
|
|
printf(" Keepalive: %d\n", conn->keepalive);
|
|
}
|
|
}
|
|
|
|
int update_config_keys(const char *filename, const char *priv_key, const char *pub_key) {
|
|
// Minimal implementation: just append to file
|
|
FILE *fp = fopen(filename, "a");
|
|
if (!fp) return -1;
|
|
|
|
fprintf(fp, "\n[global]\n");
|
|
fprintf(fp, "my_private_key=%s\n", priv_key);
|
|
fprintf(fp, "my_public_key=%s\n", pub_key);
|
|
|
|
fclose(fp);
|
|
return 0;
|
|
}
|