@ -807,9 +807,7 @@ static void link_stats_timer_cb(void* arg) {
}
int etcp_encrypt_send ( struct ETCP_DGRAM * dgram ) {
DEBUG_TRACE ( DEBUG_CATEGORY_CONNECTION , " " ) ;
if ( ! dgram | | ! dgram - > link ) return - 1 ;
if ( ! dgram | | ! dgram - > link ) { DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " Null pointer " ) ; return - 1 ; }
DEBUG_DEBUG ( DEBUG_CATEGORY_CONNECTION , " [%s] Send rk=%d lk=%d up=%d " ,
dgram - > link - > etcp - > log_name , dgram - > link - > recv_keepalive , dgram - > link - > remote_keepalive , dgram - > link - > link_status ) ;
@ -827,24 +825,17 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) {
dgram - > timestamp = get_current_timestamp ( ) ;
dgram - > link - > total_encrypted + = dgram - > data_len ;
// DUMP: Show packet before encryption
if ( debug_should_output ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO ) ) log_dump ( " ECTP_ENCRYPT_SEND " , dgram - > data , dgram - > data_len ) ;
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Encrypt start");
sc_encrypt ( sc , ( uint8_t * ) & dgram - > timestamp /*не править это, тут верно!*/ , 3 + len , enc_buf , & enc_buf_len ) ;
// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Encrypt end");
if ( debug_should_output ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO ) ) log_dump ( " Before encryption " , dgram - > data , dgram - > data_len ) ;
sc_encrypt ( sc , ( uint8_t * ) & dgram - > timestamp , 3 + len , enc_buf , & enc_buf_len ) ;
if ( enc_buf_len = = 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " etcp_encrypt_send: encryption failed for node %016llx " , ( unsigned long long ) dgram - > link - > etcp - > instance - > node_id ) ;
dgram - > link - > send_errors + + ;
errcode = 2 ;
goto es_err ;
}
if ( enc_buf_len + dgram - > noencrypt_len > 1472 ) { dgram - > link - > send_errors + + ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " packet too long len=%d ne_len=%d " , enc_buf_len , dgram - > noencrypt_len ) ;
errcode = 3 ; goto es_err ; }
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " eencryption failed for node %016llx " , ( unsigned long long ) dgram - > link - > etcp - > instance - > node_id ) ;
dgram - > link - > send_errors + + ; errcode = 2 ; goto es_err ; }
if ( enc_buf_len + dgram - > noencrypt_len > 1472 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " packet too long len=%d ne_len=%d " , enc_buf_len , dgram - > noencrypt_len ) ;
dgram - > link - > send_errors + + ; errcode = 3 ; goto es_err ; }
memcpy ( enc_buf + enc_buf_len , dgram - > data + len , dgram - > noencrypt_len ) ;
// DUMP: Show complete packet before sending
if ( debug_should_output ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO ) ) log_dump ( " ENCRYPTED, READY TO SEND " , enc_buf , enc_buf_len + dgram - > noencrypt_len ) ;
if ( debug_should_output ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO ) ) log_dump ( " Encrypted " , enc_buf , enc_buf_len + dgram - > noencrypt_len ) ;
struct sockaddr_storage * addr = & dgram - > link - > remote_addr ;
socklen_t addr_len = ( addr - > ss_family = = AF_INET ) ? sizeof ( struct sockaddr_in ) : sizeof ( struct sockaddr_in6 ) ;
@ -862,18 +853,18 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) {
sent = socket_sendto ( dgram - > link - > conn - > fd , enc_buf , enc_buf_len + dgram - > noencrypt_len ,
( struct sockaddr * ) addr , addr_len ) ;
} else {
DEBUG_WARN ( DEBUG_CATEGORY_ETCP , " [%s] Packet dropped by loss_rate (rnd=%d, loss_rate=%d%%) " ,
DEBUG_WARN ( DEBUG_CATEGORY_CONNECTION , " [%s] Packet dropped by loss_rate (rnd=%d, loss_rate=%d%%) " ,
dgram - > link - > etcp - > log_name , rnd , loss_rate ) ;
}
if ( sent < 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " sendto failed, sock_err=%d " , socket_get_error ( ) ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " sendto failed, sock_err=%d " , socket_get_error ( ) ) ;
dgram - > link - > send_errors + + ; errcode = 4 ; goto es_err ;
} else {
// DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "sendto succeeded, sent=%zd bytes to port %d", sent, ntohs(((struct sockaddr_in*)addr)->sin_port));
DEBUG_TRACE ( DEBUG_CATEGORY_CONNECTION , " sendto succeeded, sent=%zd bytes to port %d " , sent , ntohs ( ( ( struct sockaddr_in * ) addr ) - > sin_port ) ) ;
}
return ( int ) sent ;
es_err :
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [ETCP] encrypt_send error %d" , errcode ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " error %d " , errcode ) ;
return - 1 ;
}
@ -932,13 +923,13 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
goto process_decrypted ;
}
// Normal decryption failed - might be INIT packet, fall through to INIT handling
DEBUG_INFO ( DEBUG_CATEGORY_ETCP , " Normal decryption failed, trying INIT decryption " ) ;
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " Normal decryption failed, trying INIT decryption " ) ;
}
// Try INIT decryption (for incoming connection requests)
// This handles: no link found, or link without session, or normal decrypt failed
if ( recv_len < = SC_PUBKEY_ENC_SIZE + UDP_SC_HDR_SIZE ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " etcp_connections_read_callback: packet too small for init, size=%zd" , recv_len ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " packet too small for init, size=%zd " , recv_len ) ;
errorcode = 1 ;
goto ec_fr ;
}
@ -953,19 +944,19 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
sc_obfuscate_pubkey ( salt , e_sock - > instance - > my_keys . public_key , encrypted_pubkey , decrypted_pubkey ) ;
if ( sc_set_peer_public_key ( & sc , decrypted_pubkey , SC_PEER_PUBKEY_BIN ) ! = SC_OK ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " etcp_connections_read_callback: failed to set peer public key during init" ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " failed to set peer public key during init " ) ;
errorcode = 2 ;
goto ec_fr ;
}
if ( sc_decrypt ( & sc , data , recv_len - SC_PUBKEY_ENC_SIZE , ( uint8_t * ) & pkt - > timestamp , & pkt_len ) ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " etcp_connections_read_callback: failed to decrypt init packet" ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " failed to decrypt init packet " ) ;
errorcode = 3 ;
goto ec_fr ;
}
// INIT decryption succeeded - process as new incoming connection
if ( pkt_len < 3 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " etcp_connections_read_callback: too short packet" ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " too short packet " ) ;
errorcode = 7 ;
goto ec_fr ;
}
@ -983,7 +974,7 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
} * ack_hdr = ( void * ) & pkt - > data [ 0 ] ;
uint64_t peer_id = be64toh ( * ( uint64_t * ) ack_hdr - > id ) ;
if ( ack_hdr - > code ! = ETCP_INIT_REQUEST & & ack_hdr - > code ! = ETCP_INIT_REQUEST_NOINIT ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " etcp_connections_read_callback: not an init packet, code=%02x" , ack_hdr - > code ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " not an init packet, code=%02x " , ack_hdr - > code ) ;
errorcode = 4 ;
goto ec_fr ;
} // не init
@ -998,7 +989,7 @@ 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 ; }
if ( ! conn ) { errorcode = 55 ; DEBUG_ERROR ( DEBUG_CATEGORY_CONNECTION , " 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 ) ;
@ -1010,7 +1001,7 @@ static void etcp_connections_read_callback_socket(socket_t sock, void* arg) {
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " Added incoming connection %p to instance, total count: %d " , conn , e_sock - > instance - > connections_count ) ;
}
else { // check keys если существующее подключение
if ( memcmp ( conn - > crypto_ctx . peer_public_key , sc . peer_public_key , SC_PUBKEY_SIZE ) ) { errorcode = 5 ; DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " etcp_connections_read_callback: peer key mismatch for node %016llx" , ( unsigned long long ) peer_id ) ; goto ec_fr ; } // коллизия - peer id совпал а ключи разные.
if ( memcmp ( conn - > crypto_ctx . peer_public_key , sc . peer_public_key , SC_PUBKEY_SIZE ) ) { errorcode = 5 ; DEBUG_ERROR ( DEBUG_CATEGORY_CRYPTO , " peer key mismatch for node %016llx " , ( unsigned long long ) peer_id ) ; goto ec_fr ; } // коллизия - peer id совпал а ключи разные.
}
// Check if link already exists (for CHANNEL_INIT recovery)