/* * control_server.h - Control Socket Server for ETCP Monitoring * * Provides TCP control interface for ETCP connection monitoring */ #ifndef CONTROL_SERVER_H #define CONTROL_SERVER_H #include "../tools/etcpmon/etcpmon_protocol.h" #include "../lib/socket_compat.h" #include #include #ifdef __cplusplus extern "C" { #endif /* Forward declarations */ struct UTUN_INSTANCE; struct UASYNC; /* Forward declaration */ struct control_server; /* Client connection state */ struct control_client { struct control_client* next; /* Linked list */ struct control_server* server; /* Back pointer to server */ socket_t fd; /* Client socket */ void* socket_id; /* uasync socket ID */ /* Receive buffer */ uint8_t recv_buffer[ETCPMON_MAX_MSG_SIZE]; uint16_t recv_len; /* Bytes received so far */ /* Selected connection */ uint64_t selected_peer_id; /* 0 = none selected */ /* Client state */ // uint8_t connected; }; /* Control server state */ struct control_server { struct UTUN_INSTANCE* instance; /* Parent instance */ struct UASYNC* ua; /* Async context */ /* Listening socket */ socket_t listen_fd; void* listen_socket_id; /* uasync socket ID */ struct sockaddr_storage bind_addr; /* Bound address */ /* Client connections */ struct control_client* clients; /* Linked list of clients */ uint32_t client_count; /* Number of connected clients */ uint32_t max_clients; /* Maximum allowed clients */ /* Log file */ FILE* log_file; }; /* ============================================================================ * Public API * ============================================================================ */ /* Initialize control server * * Creates listening socket and registers with uasync * * Parameters: * server - Server state structure to initialize * ua - uasync context for event handling * instance - Parent utun instance * bind_addr - Address to bind (from config control_sock) * max_clients - Maximum concurrent client connections * * Returns: * 0 on success, -1 on error */ int control_server_init(struct control_server* server, struct UASYNC* ua, struct UTUN_INSTANCE* instance, struct sockaddr_storage* bind_addr, uint32_t max_clients); /* Shutdown control server * * Closes all client connections and listening socket * * Parameters: * server - Server state to shutdown */ void control_server_shutdown(struct control_server* server); /* Process periodic updates * * Should be called periodically (e.g., from main loop) to send * metrics updates to clients that have selected connections * * Parameters: * server - Server state */ void control_server_process_updates(struct control_server* server); /* Get number of connected clients * * Parameters: * server - Server state * * Returns: * Number of connected clients */ uint32_t control_server_get_client_count(const struct control_server* server); #ifdef __cplusplus } #endif #endif /* CONTROL_SERVER_H */