// net_emulator.h - Network emulator for testing utun with delay, loss, reordering #ifndef NET_EMULATOR_H #define NET_EMULATOR_H #include #include #include #include #include "u_async.h" #ifdef __cplusplus extern "C" { #endif // Maximum number of rules per endpoint #define MAX_RULES_PER_ENDPOINT 10 #define MAX_ENDPOINTS 100 #define MAX_PACKET_SIZE 65536 // Forward declaration typedef struct net_emulator_s net_emulator_t; // Delay range (milliseconds) typedef struct { int min_ms; int max_ms; } delay_range_t; // Rule: probability and delay typedef struct { int probability_percent; // 0-100 delay_range_t delay; } rule_t; // Network endpoint configuration typedef struct { struct sockaddr_in listen_addr; // Address to listen on struct sockaddr_in target_addr; // Address to forward to (port - 10000) rule_t rules[MAX_RULES_PER_ENDPOINT]; int rule_count; int total_probability; // Sum of probabilities (should be <= 100) // For reordering simulation int reorder_probability; // Probability to reorder with next packet (0-100) // Socket descriptor int sockfd; net_emulator_t* emulator; // Parent emulator instance } endpoint_t; // Main emulator state struct net_emulator_s { endpoint_t endpoints[MAX_ENDPOINTS]; int endpoint_count; int running; uasync_t* ua; // uasync instance }; /** * @brief Parse configuration file * @param filename Configuration file path * @param emulator Emulator state to fill * @return 0 on success, -1 on error */ int parse_emulator_config(const char *filename, net_emulator_t *emulator); /** * @brief Initialize network emulator * @param emulator Emulator state * @return 0 on success, -1 on error */ int net_emulator_init(net_emulator_t *emulator); /** * @brief Run network emulator main loop * @param emulator Emulator state */ void net_emulator_run(net_emulator_t *emulator); /** * @brief Clean up emulator resources * @param emulator Emulator state */ void net_emulator_cleanup(net_emulator_t *emulator); #ifdef __cplusplus } #endif #endif /* NET_EMULATOR_H */