- u_async.c: process immediate_queue BEFORE epoll_wait in uasync_poll - stcp_link.c: defensive NULL etcp_conn/etcp_link in stcp_link_close - utun_instance.c: move stcp_server_list_destroy_all to Phase J (before ETCP), add deferred drain in both Phase J and L - stcp.h/c: STCP_HS_ENC_CLIENT 6→38, STCP_HS_ENC_SERVER 7→39, exchange ed25519_pubkey in both handshake directions - etcp_connections.c: copy peer_ed25519 from STCP to ETCP_CONN in etcp_link_enter_ready_tcp; guard link->etcp in tcp_link_close_cb - chat_sync.c: fix lk->conn NULL dereference for TCP links - test_stcp.c: update stcp_server_create/stcp_client_connect callsites - tools/chat_tcp_test: change transport udp→tcp
90 lines
2.1 KiB
C
90 lines
2.1 KiB
C
// net_emulator.h - Network emulator for testing utun with delay, loss, reordering
|
|
#ifndef NET_EMULATOR_H
|
|
#define NET_EMULATOR_H
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#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 */ |