@ -2162,9 +2162,14 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
return ;
return ;
}
}
// @attention srv_dst r cb is not set/enabled, so we only have src and dst at this point
pxy_conn_desc_t * other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
struct evbuffer * inbuf = bufferevent_get_input ( bev ) ;
struct evbuffer * inbuf = bufferevent_get_input ( bev ) ;
pxy_conn_desc_t * other ;
if ( ! ctx - > passthrough ) {
other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
} else {
// Passthrough packets are transfered between src and srv_dst
other = ( bev = = ctx - > src . bev ) ? & ctx - > srv_dst : & ctx - > src ;
}
struct evbuffer * outbuf = bufferevent_get_output ( other - > bev ) ;
struct evbuffer * outbuf = bufferevent_get_output ( other - > bev ) ;
if ( other - > closed ) {
if ( other - > closed ) {
@ -2174,7 +2179,10 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
return ;
return ;
}
}
if ( bev = = ctx - > src . bev ) {
if ( ctx - > passthrough ) {
// Just pass packets along
goto leave ;
} else if ( bev = = ctx - > src . bev ) {
ctx - > thr - > intif_in_bytes + = bytes ;
ctx - > thr - > intif_in_bytes + = bytes ;
if ( ctx - > clienthello_search ) {
if ( ctx - > clienthello_search ) {
@ -2506,7 +2514,7 @@ pxy_conn_connect_child(pxy_conn_child_ctx_t *ctx)
/* create server-side socket and eventbuffer */
/* create server-side socket and eventbuffer */
// Children rely on the findings of parent
// Children rely on the findings of parent
if ( ( parent - > spec - > ssl | | parent - > clienthello_found ) & & ! parent - > passthrough ) {
if ( parent - > spec - > ssl | | parent - > clienthello_found ) {
ctx - > dst . ssl = pxy_dstssl_create ( parent ) ;
ctx - > dst . ssl = pxy_dstssl_create ( parent ) ;
if ( ! ctx - > dst . ssl ) {
if ( ! ctx - > dst . ssl ) {
log_err_level_printf ( LOG_CRIT , " Error creating SSL \n " ) ;
log_err_level_printf ( LOG_CRIT , " Error creating SSL \n " ) ;
@ -2664,6 +2672,7 @@ pxy_connected_enable(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
ctx - > srv_dst_fd = bufferevent_getfd ( ctx - > srv_dst . bev ) ;
ctx - > srv_dst_fd = bufferevent_getfd ( ctx - > srv_dst . bev ) ;
ctx - > thr - > max_fd = MAX ( ctx - > thr - > max_fd , ctx - > srv_dst_fd ) ;
ctx - > thr - > max_fd = MAX ( ctx - > thr - > max_fd , ctx - > srv_dst_fd ) ;
if ( ! ctx - > passthrough ) {
// @attention Create and enable dst.bev before, but connect here, because we check if dst.bev is NULL elsewhere
// @attention Create and enable dst.bev before, but connect here, because we check if dst.bev is NULL elsewhere
if ( bufferevent_socket_connect ( ctx - > dst . bev ,
if ( bufferevent_socket_connect ( ctx - > dst . bev ,
( struct sockaddr * ) & ctx - > spec - > parent_dst_addr ,
( struct sockaddr * ) & ctx - > spec - > parent_dst_addr ,
@ -2677,12 +2686,13 @@ pxy_connected_enable(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
ctx - > dst_fd = bufferevent_getfd ( ctx - > dst . bev ) ;
ctx - > dst_fd = bufferevent_getfd ( ctx - > dst . bev ) ;
ctx - > thr - > max_fd = MAX ( ctx - > thr - > max_fd , ctx - > dst_fd ) ;
ctx - > thr - > max_fd = MAX ( ctx - > thr - > max_fd , ctx - > dst_fd ) ;
}
}
}
if ( bev = = ctx - > dst . bev & & ! ctx - > dst_connected) {
if ( bev = = ctx - > dst . bev & & ! ctx - > passthrough & & ! ctx - > dst_connected) {
ctx - > dst_connected = 1 ;
ctx - > dst_connected = 1 ;
}
}
if ( ctx - > srv_dst_connected & & ctx - > dst_connected & & ! ctx - > connected ) {
if ( ctx - > srv_dst_connected & & ( ctx - > dst_connected | | ctx - > passthrough ) & & ! ctx - > connected ) {
ctx - > connected = 1 ;
ctx - > connected = 1 ;
pxy_conn_desc_t * srv_dst_ctx = & ctx - > srv_dst ;
pxy_conn_desc_t * srv_dst_ctx = & ctx - > srv_dst ;
@ -2695,9 +2705,7 @@ pxy_connected_enable(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
if ( ctx - > opts - > passthrough & & ! ctx - > enomem ) {
if ( ctx - > opts - > passthrough & & ! ctx - > enomem ) {
ctx - > passthrough = 1 ;
ctx - > passthrough = 1 ;
ctx - > connected = 0 ;
ctx - > connected = 0 ;
log_dbg_printf ( " No cert found; "
log_err_level_printf ( LOG_WARNING , " No cert found; falling back to passthrough \n " ) ;
" falling back "
" to passthrough \n " ) ;
pxy_fd_readcb ( fd , 0 , ctx ) ;
pxy_fd_readcb ( fd , 0 , ctx ) ;
return 0 ;
return 0 ;
}
}
@ -2783,6 +2791,7 @@ pxy_connected_enable(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
}
}
}
}
if ( ! ctx - > passthrough ) {
// @attention Free the srv_dst of the parent ctx asap, we don't need it, but we need its fd
// @attention Free the srv_dst of the parent ctx asap, we don't need it, but we need its fd
// So save its ssl info for logging
// So save its ssl info for logging
if ( ctx - > srv_dst . ssl ) {
if ( ctx - > srv_dst . ssl ) {
@ -2871,6 +2880,8 @@ pxy_connected_enable(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
# ifdef DEBUG_PROXY
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINEST , " pxy_connected_enable: Enable src, SSLproxy header= %s, fd=%d, child_fd=%d \n " , ctx - > header_str , fd , ctx - > child_fd ) ;
log_dbg_level_printf ( LOG_DBG_MODE_FINEST , " pxy_connected_enable: Enable src, SSLproxy header= %s, fd=%d, child_fd=%d \n " , ctx - > header_str , fd , ctx - > child_fd ) ;
# endif /* DEBUG_PROXY */
# endif /* DEBUG_PROXY */
}
// Now open the gates
// Now open the gates
bufferevent_enable ( ctx - > src . bev , EV_READ | EV_WRITE ) ;
bufferevent_enable ( ctx - > src . bev , EV_READ | EV_WRITE ) ;
}
}
@ -2963,8 +2974,19 @@ pxy_bev_writecb(struct bufferevent *bev, void *arg)
ctx - > atime = time ( NULL ) ;
ctx - > atime = time ( NULL ) ;
if ( ( bev = = ctx - > src . bev ) | | ( bev = = ctx - > dst . bev ) ) {
if ( bev = = ctx - > srv_dst . bev & & ! ctx - > srv_dst_connected ) {
if ( bev = = ctx - > dst . bev & & ! ctx - > dst_connected ) {
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINE , " pxy_bev_writecb: writecb before connected %s fd=%d, child_fd=%d \n " , event_name , ctx - > fd , ctx - > child_fd ) ;
# endif /* DEBUG_PROXY */
// @attention Sometimes dst write cb fires but not event cb, especially if the listener cb is not finished yet, so the conn stalls. This is a workaround for this error condition, nothing else seems to work.
// @attention Do not try to free the conn here, since the listener cb may not be finished yet, causes multithreading issues
// XXX: Workaround, should find the real cause: BEV_OPT_DEFER_CALLBACKS?
pxy_bev_eventcb ( bev , BEV_EVENT_CONNECTED , ctx ) ;
return ;
}
if ( ( bev = = ctx - > src . bev ) | | ( bev = = ctx - > dst . bev ) | | ctx - > passthrough ) {
if ( bev = = ctx - > dst . bev & & ! ctx - > passthrough & & ! ctx - > dst_connected ) {
# ifdef DEBUG_PROXY
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINE , " pxy_bev_writecb: writecb before connected %s, fd=%d, child_fd=%d \n " , event_name , ctx - > fd , ctx - > child_fd ) ;
log_dbg_level_printf ( LOG_DBG_MODE_FINE , " pxy_bev_writecb: writecb before connected %s, fd=%d, child_fd=%d \n " , event_name , ctx - > fd , ctx - > child_fd ) ;
# endif /* DEBUG_PROXY */
# endif /* DEBUG_PROXY */
@ -2972,9 +2994,23 @@ pxy_bev_writecb(struct bufferevent *bev, void *arg)
// @todo Should return instead?
// @todo Should return instead?
}
}
pxy_conn_desc_t * this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > dst ;
pxy_conn_desc_t * this ;
pxy_conn_desc_t * other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
pxy_conn_desc_t * other ;
void ( * this_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) = ( bev = = ctx - > dst . bev ) ? & bufferevent_free_and_close_fd_nonssl : & bufferevent_free_and_close_fd ;
void ( * this_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) ;
int by_requestor ;
if ( ! ctx - > passthrough ) {
this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > dst ;
other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
this_free_and_close_fd_func = ( bev = = ctx - > dst . bev ) ? & bufferevent_free_and_close_fd_nonssl : & bufferevent_free_and_close_fd ;
by_requestor = ( bev = = ctx - > dst . bev ) ;
} else {
// Passthrough packets are transfered between src and srv_dst
this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > srv_dst ;
other = ( bev = = ctx - > src . bev ) ? & ctx - > srv_dst : & ctx - > src ;
this_free_and_close_fd_func = & bufferevent_free_and_close_fd_nonssl ;
by_requestor = ( bev = = ctx - > srv_dst . bev ) ;
}
if ( other - > closed ) {
if ( other - > closed ) {
struct evbuffer * outbuf = bufferevent_get_output ( bev ) ;
struct evbuffer * outbuf = bufferevent_get_output ( bev ) ;
@ -2987,7 +3023,7 @@ pxy_bev_writecb(struct bufferevent *bev, void *arg)
this - > closed = 1 ;
this - > closed = 1 ;
this_free_and_close_fd_func ( bev , ctx ) ;
this_free_and_close_fd_func ( bev , ctx ) ;
this - > bev = NULL ;
this - > bev = NULL ;
pxy_conn_free ( ctx , ( bev = = ctx - > dst . bev ) ) ;
pxy_conn_free ( ctx , by_requestor ) ;
}
}
return ;
return ;
}
}
@ -3002,15 +3038,6 @@ pxy_bev_writecb(struct bufferevent *bev, void *arg)
bufferevent_enable ( other - > bev , EV_READ ) ;
bufferevent_enable ( other - > bev , EV_READ ) ;
ctx - > thr - > unset_watermarks + + ;
ctx - > thr - > unset_watermarks + + ;
}
}
} else if ( bev = = ctx - > srv_dst . bev & & ! ctx - > srv_dst_connected ) {
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINE , " pxy_bev_writecb: writecb before connected %s fd=%d, child_fd=%d \n " , event_name , ctx - > fd , ctx - > child_fd ) ;
# endif /* DEBUG_PROXY */
// @todo Should enable the lines below to workaround eventcb issue? Would it help?
// @attention Sometimes dst write cb fires but not event cb, especially if the listener cb is not finished yet, so the conn stalls. This is a workaround for this error condition, nothing else seems to work.
// @attention Do not try to free the conn here, since the listener cb may not be finished yet, causes multithreading issues
// XXX: Workaround, should find the real cause: BEV_OPT_DEFER_CALLBACKS?
pxy_bev_eventcb ( bev , BEV_EVENT_CONNECTED , ctx ) ;
}
}
}
}
@ -3170,15 +3197,26 @@ pxy_bev_eventcb(struct bufferevent *bev, short events, void *arg)
return ;
return ;
}
}
// @attention If the bev is srv_dst.bev, these variables are initialized wrong, but they are not used in that case.
pxy_conn_desc_t * this ;
pxy_conn_desc_t * this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > dst ;
pxy_conn_desc_t * other ;
pxy_conn_desc_t * other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
void ( * this_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) ;
void ( * other_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) ;
void ( * this_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) = ( this - > bev = = ctx - > src . bev ) ? & bufferevent_free_and_close_fd : & bufferevent_free_and_close_fd_nonssl ;
void ( * other_free_and_close_fd_func ) ( struct bufferevent * , pxy_conn_ctx_t * ) = ( other - > bev = = ctx - > dst . bev ) ? & bufferevent_free_and_close_fd_nonssl : & bufferevent_free_and_close_fd ;
if ( ! ctx - > passthrough ) {
this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > dst ;
other = ( bev = = ctx - > src . bev ) ? & ctx - > dst : & ctx - > src ;
this_free_and_close_fd_func = ( this - > bev = = ctx - > src . bev ) ? & bufferevent_free_and_close_fd : & bufferevent_free_and_close_fd_nonssl ;
other_free_and_close_fd_func = ( other - > bev = = ctx - > dst . bev ) ? & bufferevent_free_and_close_fd_nonssl : & bufferevent_free_and_close_fd ;
} else {
// Passthrough packets are transfered between src and srv_dst
this = ( bev = = ctx - > src . bev ) ? & ctx - > src : & ctx - > srv_dst ;
other = ( bev = = ctx - > src . bev ) ? & ctx - > srv_dst : & ctx - > src ;
this_free_and_close_fd_func = & bufferevent_free_and_close_fd_nonssl ;
other_free_and_close_fd_func = & bufferevent_free_and_close_fd_nonssl ;
}
if ( events & BEV_EVENT_ERROR ) {
if ( events & BEV_EVENT_ERROR ) {
log_err_printf ( " BEV_EVENT_ERROR \n " ) ;
log_err_printf ( " Client-side BEV_EVENT_ERROR\n " ) ;
# ifdef DEBUG_PROXY
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINER , " ERROR: pxy_bev_eventcb error, fd=%d \n " , ctx - > fd ) ;
log_dbg_level_printf ( LOG_DBG_MODE_FINER , " ERROR: pxy_bev_eventcb error, fd=%d \n " , ctx - > fd ) ;
# endif /* DEBUG_PROXY */
# endif /* DEBUG_PROXY */
@ -3193,6 +3231,18 @@ pxy_bev_eventcb(struct bufferevent *bev, short events, void *arg)
/* the callout to the original destination failed,
/* the callout to the original destination failed,
* e . g . because it asked for client cert auth , so
* e . g . because it asked for client cert auth , so
* close the accepted socket and clean up */
* close the accepted socket and clean up */
if ( ctx - > srv_dst . ssl & & ctx - > opts - > passthrough & &
bufferevent_get_openssl_error ( bev ) ) {
/* ssl callout failed, fall back to plain
* TCP passthrough of SSL connection */
bufferevent_free_and_close_fd ( bev , ctx ) ;
ctx - > srv_dst . bev = NULL ;
ctx - > srv_dst . ssl = NULL ;
ctx - > passthrough = 1 ;
log_err_level_printf ( LOG_WARNING , " SSL srv_dst connection failed; falling back to passthrough \n " ) ;
pxy_fd_readcb ( ctx - > fd , 0 , ctx ) ;
return ;
}
pxy_conn_free ( ctx , 1 ) ;
pxy_conn_free ( ctx , 1 ) ;
}
}
return ;
return ;
@ -3340,7 +3390,7 @@ pxy_bev_eventcb_child(struct bufferevent *bev, short events, void *arg)
}
}
if ( events & BEV_EVENT_ERROR ) {
if ( events & BEV_EVENT_ERROR ) {
log_err_printf ( " BEV_EVENT_ERROR\n " ) ;
log_err_printf ( " Server-side BEV_EVENT_ERROR\n " ) ;
# ifdef DEBUG_PROXY
# ifdef DEBUG_PROXY
log_dbg_level_printf ( LOG_DBG_MODE_FINER , " ERROR: pxy_bev_eventcb_child error, fd=%d \n " , ctx - > fd ) ;
log_dbg_level_printf ( LOG_DBG_MODE_FINER , " ERROR: pxy_bev_eventcb_child error, fd=%d \n " , ctx - > fd ) ;
# endif /* DEBUG_PROXY */
# endif /* DEBUG_PROXY */
@ -3462,6 +3512,7 @@ pxy_conn_connect(pxy_conn_ctx_t *ctx)
return ;
return ;
}
}
if ( ! ctx - > passthrough ) {
ctx - > dst . ssl = NULL ;
ctx - > dst . ssl = NULL ;
ctx - > dst . bev = pxy_bufferevent_setup ( ctx , - 1 , ctx - > dst . ssl ) ;
ctx - > dst . bev = pxy_bufferevent_setup ( ctx , - 1 , ctx - > dst . ssl ) ;
if ( ! ctx - > dst . bev ) {
if ( ! ctx - > dst . bev ) {
@ -3475,7 +3526,7 @@ pxy_conn_connect(pxy_conn_ctx_t *ctx)
bufferevent_enable ( ctx - > dst . bev , EV_READ | EV_WRITE ) ;
bufferevent_enable ( ctx - > dst . bev , EV_READ | EV_WRITE ) ;
/* create server-side socket and eventbuffer */
/* create server-side socket and eventbuffer */
if ( ctx - > spec - > ssl & & ! ctx - > passthrough ) {
if ( ctx - > spec - > ssl ) {
ctx - > srv_dst . ssl = pxy_dstssl_create ( ctx ) ;
ctx - > srv_dst . ssl = pxy_dstssl_create ( ctx ) ;
if ( ! ctx - > srv_dst . ssl ) {
if ( ! ctx - > srv_dst . ssl ) {
log_err_level_printf ( LOG_CRIT , " Error creating SSL for srv_dst \n " ) ;
log_err_level_printf ( LOG_CRIT , " Error creating SSL for srv_dst \n " ) ;
@ -3483,6 +3534,7 @@ pxy_conn_connect(pxy_conn_ctx_t *ctx)
return ;
return ;
}
}
}
}
}
ctx - > srv_dst . bev = pxy_bufferevent_setup ( ctx , - 1 , ctx - > srv_dst . ssl ) ;
ctx - > srv_dst . bev = pxy_bufferevent_setup ( ctx , - 1 , ctx - > srv_dst . ssl ) ;
if ( ! ctx - > srv_dst . bev ) {
if ( ! ctx - > srv_dst . bev ) {
@ -3510,9 +3562,15 @@ pxy_conn_connect(pxy_conn_ctx_t *ctx)
// @todo Why does event cb not fire sometimes?
// @todo Why does event cb not fire sometimes?
// @attention BEV_OPT_DEFER_CALLBACKS seems responsible for the issue with srv_dst, libevent acts as if we call event connect() ourselves.
// @attention BEV_OPT_DEFER_CALLBACKS seems responsible for the issue with srv_dst, libevent acts as if we call event connect() ourselves.
// @see Launching connections on socket-based bufferevents at http://www.wangafu.net/~nickm/libevent-book/Ref6_bufferevent.html
// @see Launching connections on socket-based bufferevents at http://www.wangafu.net/~nickm/libevent-book/Ref6_bufferevent.html
// Disable and NULL r cb, we do nothing for srv_dst in r cb.
if ( ! ctx - > passthrough ) {
// Disable and NULL r cb, we do nothing for srv_dst in r cb
bufferevent_setcb ( ctx - > srv_dst . bev , NULL , pxy_bev_writecb , pxy_bev_eventcb , ctx ) ;
bufferevent_setcb ( ctx - > srv_dst . bev , NULL , pxy_bev_writecb , pxy_bev_eventcb , ctx ) ;
bufferevent_enable ( ctx - > srv_dst . bev , EV_WRITE ) ;
bufferevent_enable ( ctx - > srv_dst . bev , EV_WRITE ) ;
} else {
// Enable srv_dst r cb for passthrough
bufferevent_setcb ( ctx - > srv_dst . bev , pxy_bev_readcb , pxy_bev_writecb , pxy_bev_eventcb , ctx ) ;
bufferevent_enable ( ctx - > srv_dst . bev , EV_READ | EV_WRITE ) ;
}
/* initiate connection */
/* initiate connection */
if ( bufferevent_socket_connect ( ctx - > srv_dst . bev , ( struct sockaddr * ) & ctx - > addr , ctx - > addrlen ) = = - 1 ) {
if ( bufferevent_socket_connect ( ctx - > srv_dst . bev , ( struct sockaddr * ) & ctx - > addr , ctx - > addrlen ) = = - 1 ) {