@ -143,11 +143,12 @@ static int verify_packet_data(uint8_t* buffer, int size, int expected_seq) {
return 1 ;
return 1 ;
}
}
// Универсальный callback для получения пакетов
// Callback для получения пакетов на сервере (forward direction)
// Определяет направление по соединению (client_conn vs server_conn)
static void server_recv_callback ( struct ETCP_CONN * conn , struct ll_entry * entry ) {
static void recv_callback ( struct ETCP_CONN * conn , struct ll_entry * entry ) {
( void ) conn ;
if ( ! entry | | ! entry - > dgram | | entry - > len < PACKET_HEADER_SIZE + 1 ) {
if ( ! entry | | ! entry - > dgram | | entry - > len < PACKET_HEADER_SIZE + 1 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " R eceived invalid packet" ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Server r eceived invalid packet" ) ;
if ( entry ) {
if ( entry ) {
queue_entry_free ( entry ) ;
queue_entry_free ( entry ) ;
queue_dgram_free ( entry ) ;
queue_dgram_free ( entry ) ;
@ -155,30 +156,39 @@ static void recv_callback(struct ETCP_CONN* conn, struct ll_entry* entry) {
return ;
return ;
}
}
// Определяем направление по соединению
if ( verify_packet_data ( entry - > dgram , entry - > len , packets_received_fwd ) ) {
if ( conn = = server_conn ) {
packets_received_fwd + + ;
// Пакет пришел на сервер (forward direction: client->server)
} else {
if ( verify_packet_data ( entry - > dgram , entry - > len , packets_received_fwd ) ) {
int seq = entry - > dgram [ 1 ] | ( entry - > dgram [ 2 ] < < 8 ) ;
packets_received_fwd + + ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Server packet verification failed, seq=%d, expected=%d " ,
} else {
seq , packets_received_fwd ) ;
int seq = entry - > dgram [ 1 ] | ( entry - > dgram [ 2 ] < < 8 ) ;
}
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Server packet verification failed, seq=%d, expected=%d " ,
seq , packets_received_fwd ) ;
queue_entry_free ( entry ) ;
}
queue_dgram_free ( entry ) ;
} else if ( conn = = client_conn ) {
}
// Пакет пришел на клиент (backward direction: server->client)
if ( verify_packet_data ( entry - > dgram , entry - > len , packets_received_back ) ) {
// Callback для получения пакетов на клиенте (backward direction)
packets_received_back + + ;
static void client_recv_callback ( struct ETCP_CONN * conn , struct ll_entry * entry ) {
} else {
( void ) conn ;
int seq = entry - > dgram [ 1 ] | ( entry - > dgram [ 2 ] < < 8 ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Client packet verification failed, seq=%d, expected=%d " ,
if ( ! entry | | ! entry - > dgram | | entry - > len < PACKET_HEADER_SIZE + 1 ) {
seq , packets_received_back ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Client received invalid packet " ) ;
if ( entry ) {
queue_entry_free ( entry ) ;
queue_dgram_free ( entry ) ;
}
}
return ;
}
if ( verify_packet_data ( entry - > dgram , entry - > len , packets_received_back ) ) {
packets_received_back + + ;
} else {
} else {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Unknown connection %p " , ( void * ) conn ) ;
int seq = entry - > dgram [ 1 ] | ( entry - > dgram [ 2 ] < < 8 ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " Client packet verification failed, seq=%d, expected=%d " ,
seq , packets_received_back ) ;
}
}
// Освобождаем entry и dgram
queue_entry_free ( entry ) ;
queue_entry_free ( entry ) ;
queue_dgram_free ( entry ) ;
queue_dgram_free ( entry ) ;
}
}
@ -456,26 +466,12 @@ int main() {
debug_set_categories ( DEBUG_CATEGORY_ALL ) ;
debug_set_categories ( DEBUG_CATEGORY_ALL ) ;
utun_instance_set_tun_init_enabled ( 0 ) ;
utun_instance_set_tun_init_enabled ( 0 ) ;
// Инициализация ETCP API
etcp_api_init ( ) ;
// Регистрируем callback для получения пакетов (один на все соединения)
if ( etcp_bind ( DATA_PACKET_ID , recv_callback ) ! = 0 ) {
printf ( " Failed to bind callback \n " ) ;
etcp_api_deinit ( ) ;
return 1 ;
}
printf ( " Callback registered for ID=%d \n " , DATA_PACKET_ID ) ;
printf ( " Creating server... \n " ) ;
printf ( " Creating server... \n " ) ;
ua = uasync_create ( ) ;
ua = uasync_create ( ) ;
server_instance = utun_instance_create ( ua , " test_pkt_normalizer_etcp_server.conf " ) ;
server_instance = utun_instance_create ( ua , " test_pkt_normalizer_etcp_server.conf " ) ;
if ( ! server_instance | | init_connections ( server_instance ) < 0 ) {
if ( ! server_instance | | init_connections ( server_instance ) < 0 ) {
printf ( " Failed to create server \n " ) ;
printf ( " Failed to create server \n " ) ;
etcp_unbind ( DATA_PACKET_ID ) ;
etcp_api_deinit ( ) ;
return 1 ;
return 1 ;
}
}
printf ( " Server created, waiting for connection... \n \n " ) ;
printf ( " Server created, waiting for connection... \n \n " ) ;
@ -484,11 +480,23 @@ int main() {
client_instance = utun_instance_create ( ua , " test_pkt_normalizer_etcp_client.conf " ) ;
client_instance = utun_instance_create ( ua , " test_pkt_normalizer_etcp_client.conf " ) ;
if ( ! client_instance | | init_connections ( client_instance ) < 0 ) {
if ( ! client_instance | | init_connections ( client_instance ) < 0 ) {
printf ( " Failed to create client \n " ) ;
printf ( " Failed to create client \n " ) ;
etcp_unbind ( DATA_PACKET_ID ) ;
etcp_api_deinit ( ) ;
return 1 ;
return 1 ;
}
}
printf ( " Client created \n \n " ) ;
printf ( " Client created \n \n " ) ;
// Регистрируем callbacks для получения пакетов (per-instance)
if ( etcp_bind ( server_instance , DATA_PACKET_ID , server_recv_callback ) ! = 0 ) {
printf ( " Failed to bind server callback \n " ) ;
return 1 ;
}
if ( etcp_bind ( client_instance , DATA_PACKET_ID , client_recv_callback ) ! = 0 ) {
printf ( " Failed to bind client callback \n " ) ;
return 1 ;
}
printf ( " Callbacks registered for ID=%d (server=%p, client=%p) \n " ,
DATA_PACKET_ID , ( void * ) server_recv_callback , ( void * ) client_recv_callback ) ;
printf ( " Sending %d packets in each direction via etcp_send... \n " , TOTAL_PACKETS ) ;
printf ( " Sending %d packets in each direction via etcp_send... \n " , TOTAL_PACKETS ) ;
packet_timeout_id = uasync_set_timeout ( ua , 1000 , NULL , monitor_and_send ) ;
packet_timeout_id = uasync_set_timeout ( ua , 1000 , NULL , monitor_and_send ) ;
@ -499,16 +507,18 @@ int main() {
}
}
printf ( " \n Cleaning up... \n " ) ;
printf ( " \n Cleaning up... \n " ) ;
if ( packet_timeout_id ) uasync_cancel_timeout ( ua , packet_timeout_id ) ;
if ( packet_timeout_id ) uasync_cancel_timeout ( ua , packet_timeout_id ) ;
if ( global_timeout_id ) uasync_cancel_timeout ( ua , global_timeout_id ) ;
if ( global_timeout_id ) uasync_cancel_timeout ( ua , global_timeout_id ) ;
// Отписываемся от callbacks
// Отписываемся от callbacks
etcp_unbind ( DATA_PACKET_ID ) ;
if ( server_instance ) {
etcp_unbind ( server_instance , DATA_PACKET_ID ) ;
// Деинициализация ETCP API
}
etcp_api_deinit ( ) ;
if ( client_instance ) {
etcp_unbind ( client_instance , DATA_PACKET_ID ) ;
}
if ( server_instance ) {
if ( server_instance ) {
server_instance - > running = 0 ;
server_instance - > running = 0 ;
utun_instance_destroy ( server_instance ) ;
utun_instance_destroy ( server_instance ) ;