@ -216,6 +216,7 @@ static void proxy_feed_from_transport(struct proxy_conn *pc) {
if ( len > = e - > len ) { queue_dgram_free ( e ) ; queue_entry_free ( e ) ; }
if ( len > = e - > len ) { queue_dgram_free ( e ) ; queue_entry_free ( e ) ; }
else { memmove ( e - > dgram , e - > dgram + len , e - > len - len ) ; e - > len - = len ; queue_data_put_first ( pc - > transport_to_uip , e ) ; break ; }
else { memmove ( e - > dgram , e - > dgram + len , e - > len - len ) ; e - > len - = len ; queue_data_put_first ( pc - > transport_to_uip , e ) ; break ; }
} else {
} else {
DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY FEED tcp_write failed stream=%016llx len=%u ret=%d " , ( unsigned long long ) pc - > remote_stream_id , len , ret ) ;
queue_data_put_first ( pc - > transport_to_uip , e ) ;
queue_data_put_first ( pc - > transport_to_uip , e ) ;
break ;
break ;
}
}
@ -270,7 +271,7 @@ static err_t proxy_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
static err_t proxy_recv_cb ( void * arg , struct tcp_pcb * pcb , struct pbuf * p , err_t err ) {
static err_t proxy_recv_cb ( void * arg , struct tcp_pcb * pcb , struct pbuf * p , err_t err ) {
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
if ( ! pc ) { if ( p ) pbuf_free ( p ) ; return LERR_OK ; }
if ( ! pc ) { if ( p ) pbuf_free ( p ) ; DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY proxy_recv_cb: pc=NULL " ) ; return LERR_OK ; }
if ( p = = NULL | | err ! = LERR_OK ) {
if ( p = = NULL | | err ! = LERR_OK ) {
DEBUG_INFO ( DEBUG_CATEGORY_SOCKET , " PROXY FIN stream=%016llx tun=1 active=%d " , ( unsigned long long ) ( pc - > remote_stream_id ? pc - > remote_stream_id : 0 ) , pc - > active ) ;
DEBUG_INFO ( DEBUG_CATEGORY_SOCKET , " PROXY FIN stream=%016llx tun=1 active=%d " , ( unsigned long long ) ( pc - > remote_stream_id ? pc - > remote_stream_id : 0 ) , pc - > active ) ;
@ -296,6 +297,7 @@ static err_t proxy_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t
pbuf_copy_partial ( p , data , len , 0 ) ;
pbuf_copy_partial ( p , data , len , 0 ) ;
int ret = pc - > transport - > ops - > send ( pc - > transport , data , len ) ;
int ret = pc - > transport - > ops - > send ( pc - > transport , data , len ) ;
if ( ret < 0 ) {
if ( ret < 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY send failed stream=%016llx ret=%d len=%u " , ( unsigned long long ) pc - > remote_stream_id , ret , len ) ;
struct ll_entry * e = entry_from_data ( pc - > proxy - > entry_pool , data , len ) ;
struct ll_entry * e = entry_from_data ( pc - > proxy - > entry_pool , data , len ) ;
if ( e ) queue_data_put ( pc - > uip_to_transport , e ) ;
if ( e ) queue_data_put ( pc - > uip_to_transport , e ) ;
}
}
@ -317,7 +319,7 @@ static err_t proxy_sent_cb(void *arg, struct tcp_pcb *pcb, uint16_t len) {
static void proxy_err_cb ( void * arg , err_t err ) {
static void proxy_err_cb ( void * arg , err_t err ) {
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
if ( ! pc ) return ;
if ( ! pc ) { DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY proxy_err_cb: pc=NULL err=%d " , err ) ; return ; }
DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " TCP proxy: tcp error %d " , err ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " TCP proxy: tcp error %d " , err ) ;
pc - > closing_tun = 1 ;
pc - > closing_tun = 1 ;
if ( pc - > transport & & ! pc - > closing_sent ) pc - > transport - > ops - > close ( pc - > transport ) ;
if ( pc - > transport & & ! pc - > closing_sent ) pc - > transport - > ops - > close ( pc - > transport ) ;
@ -340,7 +342,7 @@ static void proxy_try_close(struct proxy_conn *pc) {
static err_t proxy_poll_cb ( void * arg , struct tcp_pcb * pcb ) {
static err_t proxy_poll_cb ( void * arg , struct tcp_pcb * pcb ) {
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
struct proxy_conn * pc = ( struct proxy_conn * ) arg ;
if ( ! pc ) return LERR_OK ;
if ( ! pc ) { DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY proxy_poll_cb: pc=NULL " ) ; return LERR_OK ; }
proxy_try_close ( pc ) ;
proxy_try_close ( pc ) ;
if ( pc - > closing_tun & & ( pc - > closing_rem | | pc - > closing_sent ) & & pc - > pcb ) {
if ( pc - > closing_tun & & ( pc - > closing_rem | | pc - > closing_sent ) & & pc - > pcb ) {
DEBUG_INFO ( DEBUG_CATEGORY_SOCKET , " PROXY CLEANUP stream=%016llx tun=1 rem=%d sent=%d cwnd=%u sndbuf=%u unsent=%p unacked=%p " ,
DEBUG_INFO ( DEBUG_CATEGORY_SOCKET , " PROXY CLEANUP stream=%016llx tun=1 rem=%d sent=%d cwnd=%u sndbuf=%u unsent=%p unacked=%p " ,
@ -611,11 +613,11 @@ static void sock_transport_error_callback(socket_t sock, void* arg) {
// ====================================================================
// ====================================================================
static int etcp_transport_send ( struct tcp_proxy_transport * t , const uint8_t * data , size_t len ) {
static int etcp_transport_send ( struct tcp_proxy_transport * t , const uint8_t * data , size_t len ) {
struct etcp_transport * et = ( struct etcp_transport * ) t ;
struct etcp_transport * et = ( struct etcp_transport * ) t ;
if ( ! et - > connected ) return - 1 ;
if ( ! et - > connected ) { DEBUG_INFO ( DEBUG_CATEGORY_SOCKET , " PROXY send not connected stream=%016llx len=%zu " , ( unsigned long long ) et - > stream_id , len ) ; return - 1 ; }
struct ll_entry * e = queue_entry_new ( 0 ) ;
struct ll_entry * e = queue_entry_new ( 0 ) ;
if ( ! e ) return - 1 ;
if ( ! e ) { DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY send alloc entry fail stream=%016llx " , ( unsigned long long ) et - > stream_id ) ; return - 1 ; }
e - > dgram = u_malloc ( TCP_PROXY_HDR_SIZE + len ) ;
e - > dgram = u_malloc ( TCP_PROXY_HDR_SIZE + len ) ;
if ( ! e - > dgram ) { queue_entry_free ( e ) ; return - 1 ; }
if ( ! e - > dgram ) { DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY send alloc dgram fail stream=%016llx len=%zu " , ( unsigned long long ) et - > stream_id , len ) ; queue_entry_free ( e ) ; return - 1 ; }
e - > dgram [ 0 ] = ETCP_ID_TCP_PROXY ; e - > dgram [ 1 ] = TCP_PROXY_SUBCMD_DATA ;
e - > dgram [ 0 ] = ETCP_ID_TCP_PROXY ; e - > dgram [ 1 ] = TCP_PROXY_SUBCMD_DATA ;
memcpy ( e - > dgram + 2 , & et - > stream_id , 8 ) ;
memcpy ( e - > dgram + 2 , & et - > stream_id , 8 ) ;
memcpy ( e - > dgram + 10 , & et - > send_seq , 2 ) ;
memcpy ( e - > dgram + 10 , & et - > send_seq , 2 ) ;
@ -691,7 +693,7 @@ static void handle_connected(struct tcp_proxy* p, uint64_t stream_id, struct ll_
return ;
return ;
}
}
struct etcp_transport * et = ( struct etcp_transport * ) pc - > transport ;
struct etcp_transport * et = ( struct etcp_transport * ) pc - > transport ;
if ( et - > base . ops ! = & etcp_transport_ops ) { queue_dgram_free ( entry ) ; queue_entry_free ( entry ) ; return ; }
if ( et - > base . ops ! = & etcp_transport_ops ) { DEBUG_ERROR ( DEBUG_CATEGORY_SOCKET , " PROXY CONNECTED wrong ops stream=%016llx " , ( unsigned long long ) stream_id ) ; queue_dgram_free ( entry ) ; queue_entry_free ( entry ) ; return ; }
if ( entry - > len > = TCP_PROXY_CONNECTED_HDR_SIZE ) {
if ( entry - > len > = TCP_PROXY_CONNECTED_HDR_SIZE ) {
uint8_t status = entry - > dgram [ TCP_PROXY_HDR_SIZE + 2 ] ;
uint8_t status = entry - > dgram [ TCP_PROXY_HDR_SIZE + 2 ] ;
if ( status = = TCP_PROXY_CONNECTED_OK ) { et - > connected = 1 ;
if ( status = = TCP_PROXY_CONNECTED_OK ) { et - > connected = 1 ;