@ -244,13 +244,11 @@ static void keepalive_timer_cb(void* arg) {
if ( elapsed > timeout_units ) {
if ( link - > recv_keepalive ! = 0 ) {
link - > recv_keepalive = 0 ; // down
link - > recv_keepalive = 0 ;
link - > link_status = 0 ;
etcp_on_link_down ( link - > etcp ) ;
DEBUG_WARN ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) recv status changed to DOWN - "
" no packets received for %llu ms (timeout=%u ms) " ,
link - > etcp - > log_name , link , link - > local_link_id ,
( unsigned long long ) ( elapsed / 10 ) , link - > keepalive_timeout ) ;
DEBUG_INFO ( DEBUG_CATEGORY_GENERAL , " Link down: log_name=%s socket=%s link_id=%d status=DOWN " , link - > etcp - > log_name , link - > conn ? link - > conn - > name : " unknown " , link - > local_link_id ) ;
DEBUG_WARN ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) recv status changed to DOWN - no packets for %llu ms " , link - > etcp - > log_name , link , link - > local_link_id , ( unsigned long long ) ( elapsed / 10 ) ) ;
}
}
@ -492,10 +490,9 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
}
struct sockaddr_in * sin = ( struct sockaddr_in * ) ip ;
DEBUG_INFO ( DEBUG_CATEGORY_ETCP , " [ETCP] Successfully bound socket to local address, family=%d %s:%d " , ip - > ss_family , ip_to_str ( & sin - > sin_addr , AF_INET ) . str , ntohs ( sin - > sin_port ) ) ;
DEBUG_INFO ( DEBUG_CATEGORY_GENERAL , " Listen socket initialized: name=%s fd=%d addr=%s:%d " , e_sock - > name , e_sock - > fd , ip_to_str ( & sin - > sin_addr , AF_INET ) . str , ntohs ( sin - > sin_port ) ) ;
}
e_sock - > instance = instance ;
e_sock - > errorcode = 0 ;
e_sock - > pkt_format_errors = 0 ;
@ -503,26 +500,17 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
e_sock - > mtu = mtu ;
e_sock - > loss_rate = loss_rate ;
DEBUG_INFO ( DEBUG_CATEGORY_BGP , " Add Socket type=%d " , type ) ;
// Add to instance's socket list
e_sock - > next = instance - > etcp_sockets ;
instance - > etcp_sockets = e_sock ;
// Register socket with uasync for receiving packets
e_sock - > socket_id = uasync_add_socket_t ( instance - > ua , e_sock - > fd ,
etcp_connections_read_callback_socket ,
NULL , NULL , e_sock ) ;
e_sock - > socket_id = uasync_add_socket_t ( instance - > ua , e_sock - > fd , etcp_connections_read_callback_socket , NULL , NULL , e_sock ) ;
if ( ! e_sock - > socket_id ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " Failed to register socket with uasync " ) ;
socket_close_wrapper ( e_sock - > fd ) ;
u_free ( e_sock ) ;
return NULL ;
}
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " Registered ETCP socket with uasync " ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ETCP , " [ETCP] Socket %p registered and active " , e_sock ) ;
return e_sock ;
}
@ -938,13 +926,12 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
if ( ! conn | | conn - > peer_node_id ! = peer_id ) { // создаём новое подключение [new etcp]
new_conn = 1 ;
conn = etcp_connection_create ( e_sock - > instance , " " ) ;
if ( ! conn ) { errorcode = 55 ; DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " etcp_connections_read_callback: failed to create connection " ) ; goto ec_fr ; } // облом
memcpy ( & conn - > crypto_ctx , & sc , sizeof ( sc ) ) ; // добавляем ключ
if ( ! conn ) { errorcode = 55 ; DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " etcp_connections_read_callback: failed to create connection " ) ; goto ec_fr ; }
memcpy ( & conn - > crypto_ctx , & sc , sizeof ( sc ) ) ;
conn - > peer_node_id = peer_id ;
etcp_update_log_name ( conn ) ; // Update log_name with peer_node_id
DEBUG_DEBUG ( DEBUG_CATEGORY_CONNECTION , " New connection from %s peer_id=%ld etcp=%p " ,
ip_to_str ( & ( ( struct sockaddr_in * ) & addr ) - > sin_addr . s_addr , addr . ss_family ) . str , peer_id , conn ) ;
// Add connection to instance list
etcp_update_log_name ( conn ) ;
DEBUG_INFO ( DEBUG_CATEGORY_GENERAL , " New connection received on socket %s: log_name=%s peer_id=%lu " , e_sock - > name , conn - > log_name , ( unsigned long ) peer_id ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_CONNECTION , " New connection from %s peer_id=%ld etcp=%p " , ip_to_str ( & ( ( struct sockaddr_in * ) & addr ) - > sin_addr . s_addr , addr . ss_family ) . str , peer_id , conn ) ;
conn - > next = e_sock - > instance - > connections ;
e_sock - > instance - > connections = conn ;
e_sock - > instance - > connections_count + + ;
@ -1059,21 +1046,18 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
etcp_encrypt_send ( pkt ) ;
memory_pool_free ( e_sock - > instance - > pkt_pool , pkt ) ;
link - > initialized = 1 ; // получен init request (server), считаем линк уже готовым к работе
link - > link_state = 3 ; // connected
link - > initialized = 1 ;
link - > link_state = 3 ;
if ( link - > etcp - > initialized = = 0 ) {
etcp_conn_ready ( link - > etcp ) ;
}
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) initialized and marked as UP (server) " ,
link - > etcp - > log_name , link , link - > local_link_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_GENERAL , " Connection established: log_name=%s socket=%s link_id=%d status=UP " , link - > etcp - > log_name , e_sock - > name , link - > local_link_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) initialized and marked as UP (server) " , link - > etcp - > log_name , link , link - > local_link_id ) ;
start_keepalive_timer ( link ) ;
loadbalancer_link_ready ( link ) ;
return ;
process_decrypted :
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " Decrypt ok - normal pkt " ) ;
if ( pkt_len < 3 ) { errorcode = 46 ; DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " etcp_connections_read_callback: decrypted packet too small, size=%zu " , pkt_len ) ; goto ec_fr ; }