// config_parser.h - Configuration parser for utun application #ifndef CONFIG_PARSER_H #define CONFIG_PARSER_H #include #include #include "connection.h" #ifdef __cplusplus extern "C" { #endif // Maximum lengths for strings in config #define MAX_CONN_NAME_LEN 64 #define MAX_KEY_LEN 256 // HEX key length (64 bytes * 2 chars + null) #define MAX_ADDR_LEN 64 #define MAX_NETIF_LEN 16 #define MAX_OPTION_VALUE_LEN 256 #define MAX_ALLOWED_SUBNETS 32 // Connection mode (config parser internal) typedef enum { CONFIG_MODE_UNKNOWN = 0, CONFIG_MODE_CLIENT, CONFIG_MODE_SERVER } config_conn_mode_t; // Subnet entry for allowed routing typedef struct { char subnet[MAX_ADDR_LEN]; // Format: "192.168.0.0/24" } subnet_entry_t; // Connection configuration typedef struct { char name[MAX_CONN_NAME_LEN]; // Connection name from [connection: name] config_conn_mode_t mode; // server or client char local_addr[MAX_ADDR_LEN]; // For client: from_addr, for server: addr char remote_addr[MAX_ADDR_LEN]; // For client: to_addr, for server: N/A char peer_public_key_hex[MAX_KEY_LEN]; // Peer's public key in HEX int so_mark; // Socket mark (0 = not set) char netif[MAX_NETIF_LEN]; // Network interface to bind to char tun_ifname[MAX_NETIF_LEN]; // TUN interface name (e.g., tun12) char tun_ip[MAX_ADDR_LEN]; // TUN interface IP address (e.g., 10.0.0.1/24) } connection_config_t; // Global configuration typedef struct { char my_private_key_hex[MAX_KEY_LEN]; // Our private key in HEX char my_public_key_hex[MAX_KEY_LEN]; // Our public key in HEX char option_value[MAX_OPTION_VALUE_LEN]; // Global option value char control_ip[MAX_ADDR_LEN]; // Control socket IP (empty for disabled) uint16_t control_port; // Control socket port int net_debug; // Network debug mode: 1 = enable network emulator } global_config_t; // Complete configuration typedef struct { global_config_t global; connection_config_t *connections; int connection_count; int connection_capacity; subnet_entry_t allowed_subnets[MAX_ALLOWED_SUBNETS]; int allowed_subnet_count; } utun_config_t; /** * @brief Parse configuration file * @param filename Path to configuration file * @return Pointer to parsed configuration, NULL on error * * Parses INI-style configuration file with format: * [global] * option=value * my_private_key=HEX * my_public_key=HEX * * [connection: name] * mode=server|client * addr=ip:port (for server) * from_addr=ip:port (for client) * to_addr=ip:port (for client) * peer_public_key=HEX * so_mark=number * netif=interface_name */ utun_config_t* parse_config(const char *filename); /** * @brief Free configuration structure * @param config Configuration to free */ void free_config(utun_config_t *config); /** * @brief Print configuration for debugging * @param config Configuration to print */ void print_config(const utun_config_t *config); /** * @brief Update keys in configuration file * @param filename Path to configuration file * @param private_key_hex Private key in HEX format * @param public_key_hex Public key in HEX format * @return 0 on success, -1 on error * * Updates or adds my_private_key and my_public_key lines in [global] section */ int update_config_keys(const char *filename, const char *private_key_hex, const char *public_key_hex); #ifdef __cplusplus } #endif #endif /* CONFIG_PARSER_H */