@ -38,7 +38,7 @@ static int client_derive_session(struct stcp_conn *c, const uint8_t *peer_pubkey
if ( sc_set_peer_public_key ( & sc , peer_pubkey , SC_PEER_PUBKEY_BIN ) ! = SC_OK ) { DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " client ECDH failed " ) ; return - 1 ; }
memcpy ( c - > session_key , sc . session_key , SC_SESSION_KEY_SIZE ) ;
memcpy ( c - > peer_pubkey , peer_pubkey , SC_PUBKEY_SIZE ) ; c - > peer_pubkey_set = 1 ;
log_dump ( DEBUG_LEVEL_ERROR , DEBUG_CATEGORY_DEBUG , " stcp_client session_key " , c - > session_key , 16 ) ;
log_dump ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO , " stcp_client session_key " , c - > session_key , 16 ) ;
if ( sc_stream_init ( & sc , & c - > stream_send , STCP_STREAM_CLIENT_SEND ) ! = SC_OK ) { DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " client stream_send init failed " ) ; return - 1 ; }
if ( sc_stream_init ( & sc , & c - > stream_recv , STCP_STREAM_SERVER_SEND ) ! = SC_OK ) { DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " client stream_recv init failed " ) ; return - 1 ; }
return 0 ;
@ -69,7 +69,7 @@ static void client_send_handshake(struct stcp_conn *c, const uint8_t *server_pub
}
static void process_server_response ( struct stcp_conn * c ) {
log_dump ( DEBUG_LEVEL_ERROR , DEBUG_CATEGORY_DEBUG , " stcp_client process_srv_resp recv_buf " , c - > recv_buf , c - > recv_buf_len ) ;
log_dump ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_ETCP , " stcp_client process_srv_resp recv_buf " , c - > recv_buf , c - > recv_buf_len ) ;
if ( ! c - > hs_key_processed ) {
const uint8_t * salt = c - > recv_buf ;
const uint8_t * enc_pubkey = salt + SC_PUBKEY_ENC_SALT_SIZE ;
@ -83,10 +83,10 @@ static void process_server_response(struct stcp_conn *c) {
}
uint8_t enc_hs [ STCP_HS_ENC_SERVER ] ;
memcpy ( enc_hs , c - > recv_buf + SC_PUBKEY_ENC_SIZE , STCP_HS_ENC_SERVER ) ;
log_dump ( DEBUG_LEVEL_ERROR , DEBUG_CATEGORY_DEBUG , " stcp_client enc_hs BEFORE xor " , enc_hs , STCP_HS_ENC_SERVER ) ;
log_dump ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO , " stcp_client enc_hs BEFORE xor " , enc_hs , STCP_HS_ENC_SERVER ) ;
size_t hs_data_len ;
if ( stcp_frame_decrypt ( enc_hs , STCP_HS_ENC_SERVER , & c - > stream_recv , & hs_data_len ) ) { client_do_close ( c , 3 ) ; return ; }
log_dump ( DEBUG_LEVEL_ERROR , DEBUG_CATEGORY_DEBUG , " stcp_client enc_hs AFTER xor " , enc_hs , STCP_HS_ENC_SERVER ) ;
log_dump ( DEBUG_LEVEL_DEBUG , DEBUG_CATEGORY_CRYPTO , " stcp_client enc_hs AFTER xor " , enc_hs , STCP_HS_ENC_SERVER ) ;
memcpy ( c - > peer_ed25519_pubkey , enc_hs , SC_PUBKEY_SIZE ) ; c - > peer_ed25519_set = 1 ;
uint8_t status = enc_hs [ 32 ] ;
uint16_t padding_size = ( uint16_t ) enc_hs [ 33 ] | ( ( uint16_t ) enc_hs [ 34 ] < < 8 ) ;
@ -106,7 +106,7 @@ static void finish_server_response(struct stcp_conn *c) {
}
static void stcp_conn_process_recv ( struct stcp_conn * c ) {
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: process_recv entry state=%d sock=%d recv_buf=%p len=%zu cap=%zu " ,
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " stcp_client: process_recv entry state=%d sock=%d recv_buf=%p len=%zu cap=%zu " ,
c - > state , ( int ) c - > sock , ( void * ) c - > recv_buf , c - > recv_buf_len , c - > recv_buf_cap ) ;
if ( ! c - > recv_buf & & c - > recv_buf_len > 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " stcp_client: process_recv NULL recv_buf with len=%zu — closing " , c - > recv_buf_len ) ;
@ -136,8 +136,6 @@ static void stcp_conn_process_recv(struct stcp_conn *c) {
if ( e ) { e - > dgram = u_malloc ( data_len ) ; if ( e - > dgram ) { if ( data_len ) memcpy ( e - > dgram , enc_data , data_len ) ; e - > len = ( uint16_t ) data_len ; queue_data_put ( c - > rx_queue , e ) ; } else { queue_entry_free ( e ) ; } }
if ( c - > state ! = STCP_STATE_DATA ) return ;
}
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: memmove recv_buf=%p src=%p(+%zu) len=%zu (total=%zu recv_buf_len=%zu) " ,
( void * ) c - > recv_buf , ( void * ) ( c - > recv_buf + total ) , total , c - > recv_buf_len - total , total , c - > recv_buf_len ) ;
if ( ( uintptr_t ) c - > recv_buf < 4096 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " stcp_client: suspicious recv_buf=%p (addr < 4K) — closing " , ( void * ) c - > recv_buf ) ;
client_do_close ( c , EFAULT ) ; return ;
@ -154,10 +152,10 @@ static void stcp_conn_process_recv(struct stcp_conn *c) {
static void client_conn_read_cb ( socket_t sock , void * arg ) {
struct stcp_conn * c = ( struct stcp_conn * ) arg ;
if ( c - > state = = STCP_STATE_CLOSED | | c - > state = = STCP_STATE_ERROR ) {
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: read_cb SKIP state=%d sock=%d c=%p recv_buf=%p " , c - > state , ( int ) sock , ( void * ) c , ( void * ) c - > recv_buf ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: read_cb SKIP state=%d sock=%d c=%p recv_buf=%p " , c - > state , ( int ) sock , ( void * ) c , ( void * ) c - > recv_buf ) ;
return ;
}
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: read_cb state=%d sock=%d c=%p recv_buf=%p len=%zu cap=%zu " ,
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " stcp_client: read_cb state=%d sock=%d c=%p recv_buf=%p len=%zu cap=%zu " ,
c - > state , ( int ) sock , ( void * ) c , ( void * ) c - > recv_buf , c - > recv_buf_len , c - > recv_buf_cap ) ;
if ( ! c - > recv_buf ) { c - > recv_buf_cap = STCP_RECV_BUF_INIT ; c - > recv_buf = u_malloc ( c - > recv_buf_cap ) ; if ( ! c - > recv_buf ) { client_do_close ( c , ENOMEM ) ; return ; } }
if ( c - > recv_buf_len + 4096 > c - > recv_buf_cap ) {
@ -208,14 +206,14 @@ static void client_do_close(struct stcp_conn *c, int err) {
int prev = c - > state ;
c - > state = STCP_STATE_CLOSED ;
DEBUG_INFO ( DEBUG_CATEGORY_ETCP , " stcp_client: closed is_server=%d err=%d prev_state=%d " , c - > is_server , err , prev ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: do_close sock=%d sock_id=%p recv_buf=%p len=%zu cap=%zu send_buf=%p on_close=%p " ,
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: do_close sock=%d sock_id=%p recv_buf=%p len=%zu cap=%zu send_buf=%p on_close=%p " ,
( int ) c - > sock , ( void * ) c - > socket_id , ( void * ) c - > recv_buf , c - > recv_buf_len , c - > recv_buf_cap , ( void * ) c - > send_buf , ( void * ) c - > on_close ) ;
if ( c - > socket_id ) { uasync_remove_socket_t ( c - > ua , c - > sock ) ; c - > socket_id = NULL ; }
if ( c - > sock ! = SOCKET_INVALID ) { socket_close_wrapper ( c - > sock ) ; c - > sock = SOCKET_INVALID ; }
if ( c - > recv_buf ) { void * p = c - > recv_buf ; u_free ( c - > recv_buf ) ; c - > recv_buf = NULL ; c - > recv_buf_len = 0 ; c - > recv_buf_cap = 0 ; DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: do_close u_free recv_buf=%p " , p ) ; }
if ( c - > send_buf ) { void * p = c - > send_buf ; u_free ( c - > send_buf ) ; c - > send_buf = NULL ; c - > send_len = 0 ; DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: do_close u_free send_buf=%p " , p ) ; }
if ( c - > recv_buf ) { void * p = c - > recv_buf ; u_free ( c - > recv_buf ) ; c - > recv_buf = NULL ; c - > recv_buf_len = 0 ; c - > recv_buf_cap = 0 ; DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: do_close u_free recv_buf=%p " , p ) ; }
if ( c - > send_buf ) { void * p = c - > send_buf ; u_free ( c - > send_buf ) ; c - > send_buf = NULL ; c - > send_len = 0 ; DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: do_close u_free send_buf=%p " , p ) ; }
stcp_pending_clear ( c ) ;
if ( c - > on_close ) { void ( * cb ) ( struct stcp_conn * , int , void * ) = c - > on_close ; c - > on_close = NULL ; DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: do_close calling on_close=%p " , ( void * ) cb ) ; cb ( c , err , c - > close_arg ) ; DEBUG_DEBUG ( DEBUG_CATEGORY_DEBUG , " stcp_client: do_close on_close returned " ) ; }
if ( c - > on_close ) { void ( * cb ) ( struct stcp_conn * , int , void * ) = c - > on_close ; c - > on_close = NULL ; DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: do_close calling on_close=%p " , ( void * ) cb ) ; cb ( c , err , c - > close_arg ) ; DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " stcp_client: do_close on_close returned " ) ; }
}
struct stcp_client * stcp_client_connect ( struct UASYNC * ua , const char * addr , uint16_t port ,