You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
7.0 KiB
208 lines
7.0 KiB
/** |
|
* @file control_socket.h |
|
* @brief Control socket for statistics and diagnostics over UDP |
|
* @details Provides remote monitoring of utun VPN tunnel statistics |
|
*/ |
|
|
|
#ifndef CONTROL_SOCKET_H |
|
#define CONTROL_SOCKET_H |
|
|
|
#include <stdint.h> |
|
#include <stddef.h> |
|
#include <stdbool.h> |
|
|
|
// Forward declarations for application state |
|
struct utun_state; |
|
|
|
#ifdef __cplusplus |
|
extern "C" { |
|
#endif |
|
|
|
// Maximum connections for statistics |
|
#define MAX_CONN_STATS 16 |
|
#define MAX_ROUTE_STATS 100 |
|
|
|
// Control socket command types |
|
typedef enum { |
|
CONTROL_CMD_GET_STATS = 1, // Request statistics |
|
CONTROL_CMD_RESET_STATS = 2, // Reset statistics counters |
|
CONTROL_CMD_GET_STATUS = 3, // Request status information |
|
} control_cmd_t; |
|
|
|
// Response types |
|
typedef enum { |
|
CONTROL_RESP_STATS = 1, // Statistics response |
|
CONTROL_RESP_STATUS = 2, // Status response |
|
CONTROL_RESP_ERROR = 0xFF, // Error response |
|
} control_resp_t; |
|
|
|
// Error codes |
|
typedef enum { |
|
CONTROL_ERR_NONE = 0, |
|
CONTROL_ERR_INVALID_CMD = 1, |
|
CONTROL_ERR_INTERNAL = 2, |
|
} control_err_t; |
|
|
|
// Packet normalizer statistics |
|
typedef struct __attribute__((packed)) { |
|
uint32_t packer_errors; // Packer error count |
|
uint32_t unpacker_errors; // Unpacker error count |
|
uint32_t fragments_assembled; // Fragments assembled successfully |
|
uint32_t service_packets; // Service packets processed |
|
} pkt_normalizer_stats_t; |
|
|
|
// ETCP statistics |
|
typedef struct __attribute__((packed)) { |
|
uint32_t retransmissions; // Retransmission count |
|
uint32_t total_packets_sent; // Total packets sent (including retransmissions) |
|
uint32_t unique_packets_sent; // Unique packets sent (excluding retransmissions) |
|
uint32_t bytes_sent_total; // Total bytes sent |
|
uint32_t bytes_received_total;// Total bytes received |
|
uint32_t ack_packets_count; // ACK packets sent |
|
uint32_t control_packets_count; // Control packets sent |
|
uint16_t current_rtt; // Current RTT (0.1ms units) |
|
uint16_t jitter; // Jitter (0.1ms units) |
|
uint32_t window_size; // Current window size (bytes) |
|
uint32_t unacked_bytes; // Unacknowledged bytes in flight |
|
} etcp_stats_t; |
|
|
|
// Connection statistics |
|
typedef struct __attribute__((packed)) { |
|
uint64_t bytes_sent; |
|
uint64_t bytes_received; |
|
uint32_t packets_sent; |
|
uint32_t packets_received; |
|
uint32_t retransmissions; |
|
uint32_t fragments_assembled; |
|
uint16_t current_rtt_ms; |
|
uint16_t jitter_ms; |
|
uint8_t is_active; // 1 if connection is active |
|
char name[32]; // Connection name from config |
|
} conn_stats_entry_t; |
|
|
|
// Routing statistics |
|
typedef struct __attribute__((packed)) { |
|
uint32_t total_routes; // Total routes in table |
|
uint32_t static_routes; // Static routes count |
|
uint32_t dynamic_routes; // Dynamic routes count |
|
uint32_t local_routes; // Local routes count |
|
uint32_t learned_routes; // Learned routes count |
|
uint32_t lookup_count; // Total lookups performed |
|
uint32_t hit_count; // Successful lookups |
|
uint32_t miss_count; // Failed lookups (no route) |
|
} routing_stats_t; |
|
|
|
// TUN interface statistics |
|
typedef struct __attribute__((packed)) { |
|
uint64_t bytes_read; // Bytes read from TUN |
|
uint64_t bytes_written; // Bytes written to TUN |
|
uint32_t packets_read; // Packets read from TUN |
|
uint32_t packets_written; // Packets written to TUN |
|
uint32_t read_errors; // Read errors |
|
uint32_t write_errors; // Write errors |
|
char ifname[16]; // Interface name |
|
char ip_addr[32]; // IP address with prefix |
|
} tun_stats_t; |
|
|
|
// System status |
|
typedef struct __attribute__((packed)) { |
|
uint32_t uptime_seconds; // Seconds since startup |
|
uint32_t memory_usage_kb; // Memory usage in KB (approximate) |
|
uint16_t cpu_usage_percent; // CPU usage percentage (0-100) |
|
uint8_t running; // 1 if main loop is running |
|
uint8_t connection_count; // Number of active connections |
|
uint16_t control_port; // Control socket port |
|
char version[32]; // Version string |
|
} system_status_t; |
|
|
|
// Complete statistics packet |
|
typedef struct __attribute__((packed)) { |
|
uint8_t response_type; // CONTROL_RESP_STATS |
|
uint8_t error_code; // CONTROL_ERR_NONE if success |
|
uint16_t sequence; // Sequence number (echoed from request) |
|
|
|
// Timestamps |
|
uint64_t timestamp; // Unix timestamp in microseconds |
|
|
|
// Module statistics |
|
pkt_normalizer_stats_t pkt_normalizer; |
|
etcp_stats_t etcp; |
|
routing_stats_t routing; |
|
tun_stats_t tun; |
|
|
|
// Connection statistics (variable length, up to MAX_CONN_STATS) |
|
uint8_t conn_count; // Number of connection entries |
|
conn_stats_entry_t connections[MAX_CONN_STATS]; |
|
|
|
// System status |
|
system_status_t system; |
|
|
|
// Reserved for future expansion |
|
uint8_t reserved[64]; |
|
} control_stats_packet_t; |
|
|
|
// Status response packet |
|
typedef struct __attribute__((packed)) { |
|
uint8_t response_type; // CONTROL_RESP_STATUS |
|
uint8_t error_code; // CONTROL_ERR_NONE if success |
|
uint16_t sequence; // Sequence number |
|
|
|
system_status_t system; |
|
uint8_t running; // Main loop running flag |
|
char status_msg[128]; // Human-readable status message |
|
} control_status_packet_t; |
|
|
|
// Request packet (from client) |
|
typedef struct __attribute__((packed)) { |
|
uint8_t command; // CONTROL_CMD_* |
|
uint16_t sequence; // Sequence number for response matching |
|
uint8_t reserved; // Reserved, must be 0 |
|
} control_request_packet_t; |
|
|
|
// Control socket handle (opaque) |
|
typedef struct control_socket control_socket_t; |
|
|
|
/** |
|
* @brief Create and initialize control socket |
|
* @param ip IP address to bind to (NULL for any) |
|
* @param port UDP port to listen on |
|
* @return Control socket handle or NULL on error |
|
*/ |
|
control_socket_t* control_socket_create(const char *ip, uint16_t port); |
|
|
|
/** |
|
* @brief Destroy control socket and free resources |
|
* @param cs Control socket handle |
|
*/ |
|
void control_socket_destroy(control_socket_t *cs); |
|
|
|
/** |
|
* @brief Get file descriptor for polling |
|
* @param cs Control socket handle |
|
* @return Socket file descriptor or -1 if invalid |
|
*/ |
|
int control_socket_get_fd(const control_socket_t *cs); |
|
|
|
/** |
|
* @brief Process incoming control socket requests |
|
* @param cs Control socket handle |
|
* @param state Application state (for collecting statistics) |
|
* @return 0 on success, -1 on error |
|
* |
|
* This function should be called when the socket is readable. |
|
* It will handle incoming requests and send responses. |
|
*/ |
|
int control_socket_process(control_socket_t *cs, void *state); |
|
|
|
/** |
|
* @brief Set application state pointer for statistics collection |
|
* @param cs Control socket handle |
|
* @param state Application state (utun_state_t*) |
|
*/ |
|
void control_socket_set_state(control_socket_t *cs, void *state); |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|
|
#endif /* CONTROL_SOCKET_H */ |