Refactor to move common logging and stats code to topmost callback functions

Log only orig content in the topmost readcb
Improve and clean up
pull/13/head
Soner Tari 6 years ago
parent 6c4dff69eb
commit 59c11017bd

@ -220,6 +220,7 @@ static void NONNULL(1,2)
protoautossl_eventcb_connected_dst(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
protoautossl_ctx_t *autossl_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_eventcb_connected_dst: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
@ -241,6 +242,7 @@ static void NONNULL(1,2)
protoautossl_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
protoautossl_ctx_t *autossl_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_eventcb_connected_srv_dst: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
@ -248,6 +250,7 @@ protoautossl_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_c
// srv_dst may be already connected while upgrading to ssl
if (!ctx->srv_dst_connected) {
ctx->srv_dst_connected = 1;
ctx->srv_dst_fd = bufferevent_getfd(ctx->srv_dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->srv_dst_fd);
@ -257,9 +260,11 @@ protoautossl_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_c
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINE, "protoautossl_bev_eventcb_connected_srv_dst: FAILED bufferevent_socket_connect for dst, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_conn_free(ctx, 1);
return;
}
ctx->dst_fd = bufferevent_getfd(ctx->dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->dst_fd);
}
@ -298,15 +303,8 @@ protoautossl_bev_readcb_src(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->thr->intif_in_bytes += inbuf_size;
if (pxy_log_content_inbuf(ctx, inbuf, 1) == -1) {
return;
}
size_t packet_size = inbuf_size;
size_t packet_size = evbuffer_get_length(inbuf);
// +2 is for \r\n
unsigned char *packet = pxy_malloc_packet(packet_size + ctx->header_len + 2, ctx);
if (!packet) {
@ -327,6 +325,7 @@ protoautossl_bev_readcb_src(struct bufferevent *bev, void *arg)
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_src: clienthello_search Duping packet to srv_dst (size=%zu), fd=%d:\n%.*s\n",
packet_size, ctx->fd, (int)packet_size, packet);
#endif /* DEBUG_PROXY */
// Dup packet to server while searching for clienthello in autossl mode, without adding SSLproxy specific header
if (evbuffer_add(bufferevent_get_output(ctx->srv_dst.bev), packet, packet_size) == -1) {
log_err_printf("protoautossl_bev_readcb_src: clienthello_search srv_dst evbuffer_add failed, fd=%d\n", ctx->fd);
@ -343,6 +342,7 @@ protoautossl_bev_readcb_src(struct bufferevent *bev, void *arg)
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_src: NEW packet (size=%zu), fd=%d:\n%.*s\n",
packet_size, ctx->fd, (int)packet_size, packet);
#endif /* DEBUG_PROXY */
free(packet);
pxy_set_watermark(bev, ctx, ctx->dst.bev);
}
@ -352,6 +352,7 @@ protoautossl_bev_readcb_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
protoautossl_ctx_t *autossl_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_dst: ENTER, fd=%d, size=%zu\n",
ctx->fd, evbuffer_get_length(bufferevent_get_input(bev)));
@ -370,17 +371,6 @@ protoautossl_bev_readcb_dst(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->thr->intif_out_bytes += inbuf_size;
if (pxy_log_content_inbuf(ctx, inbuf, 0) == -1) {
return;
}
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_dst: dst packet size=%zu, fd=%d\n", inbuf_size, ctx->fd);
#endif /* DEBUG_PROXY */
evbuffer_add_buffer(outbuf, inbuf);
pxy_set_watermark(bev, ctx, ctx->src.bev);
}
@ -405,16 +395,16 @@ protoautossl_bev_readcb_srv_dst(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
// Discard packets to client while searching for clienthello in autossl mode, because child conn passes them along already
// Otherwise client would receive the same packet twice
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_srv_dst: clienthello_search Discarding packet, size=%zu, fd=%d\n",
inbuf_size, ctx->fd);
#endif /* DEBUG_PROXY */
// Discard packets to client while searching for clienthello in autossl mode, because child conn passes them along already
// Otherwise client would receive the same packet twice
if (evbuffer_drain(inbuf, inbuf_size) == -1) {
log_err_printf("protoautossl_bev_readcb_srv_dst: clienthello_search evbuffer_drain failed, fd=%d\n", ctx->fd);
}
return;
}
static void NONNULL(1)
@ -466,11 +456,7 @@ protoautossl_bev_readcb_src_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_out_bytes += inbuf_size;
size_t packet_size = inbuf_size;
size_t packet_size = evbuffer_get_length(inbuf);
unsigned char *packet = pxy_malloc_packet(packet_size, ctx->conn);
if (!packet) {
return;
@ -491,9 +477,7 @@ protoautossl_bev_readcb_src_child(struct bufferevent *bev, void *arg)
packet_size, ctx->fd, ctx->conn->fd, (int)packet_size, packet);
#endif /* DEBUG_PROXY */
pxy_log_content_buf((pxy_conn_ctx_t *)ctx, packet, packet_size, 1);
free(packet);
pxy_set_watermark(bev, ctx->conn, ctx->dst.bev);
}
@ -522,28 +506,18 @@ protoautossl_bev_readcb_dst_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_in_bytes += inbuf_size;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_dst_child: dst packet size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_readcb_dst_child: dst packet size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_content_inbuf((pxy_conn_ctx_t *)ctx, inbuf, 0);
evbuffer_add_buffer(outbuf, inbuf);
pxy_set_watermark(bev, ctx->conn, ctx->src.bev);
}
static void NONNULL(1)
protoautossl_bev_readcb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "protoautossl_bev_readcb: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
protoautossl_bev_readcb_src(bev, arg);
@ -560,13 +534,6 @@ static void NONNULL(1)
protoautossl_bev_readcb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "protoautossl_bev_readcb_child: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
protoautossl_bev_readcb_src_child(bev, arg);
@ -581,7 +548,6 @@ static void NONNULL(1,2)
protoautossl_bev_eventcb_connected_dst_child(UNUSED struct bufferevent *bev, pxy_conn_child_ctx_t *ctx)
{
protoautossl_ctx_t *autossl_ctx = ctx->conn->protoctx->arg;
ctx->conn->atime = time(NULL);
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_eventcb_connected_dst_child: ENTER, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
@ -592,15 +558,17 @@ protoautossl_bev_eventcb_connected_dst_child(UNUSED struct bufferevent *bev, pxy
// @attention Create and enable src.bev before, but connect here, because we check if dst.bev is NULL elsewhere
bufferevent_enable(ctx->src.bev, EV_READ|EV_WRITE);
// Check if we have come here right after autossl upgrade, which may be triggered by readcb on src
// Check if we have arrived here right after autossl upgrade, which may be triggered by readcb on src
// Autossl upgrade code leaves readcb without processing any data in input buffer of src
// So, if we don't call readcb here, the connection would stall
if (autossl_ctx->clienthello_found && evbuffer_get_length(bufferevent_get_input(ctx->src.bev))) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protoautossl_bev_eventcb_connected_dst_child: clienthello_found src inbuf len > 0, Calling pxy_bev_readcb_child for src, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
pxy_bev_readcb_child(ctx->src.bev, ctx);
}
ctx->conn->thr->max_fd = MAX(ctx->conn->thr->max_fd, MAX(bufferevent_getfd(ctx->src.bev), bufferevent_getfd(ctx->dst.bev)));
}
@ -657,7 +625,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb_src(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protoautossl_bev_eventcb_connected_src(bev, ctx);
@ -672,7 +639,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb_dst(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protoautossl_eventcb_connected_dst(bev, ctx);
@ -687,7 +653,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb_srv_dst(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protoautossl_bev_eventcb_connected_srv_dst(bev, ctx);
@ -702,7 +667,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (bev == ctx->src.bev) {
protoautossl_bev_eventcb_src(bev, events, arg);
@ -719,7 +683,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb_dst_child(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protoautossl_bev_eventcb_connected_dst_child(bev, ctx);
@ -734,7 +697,6 @@ static void NONNULL(1)
protoautossl_bev_eventcb_child(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (bev == ctx->src.bev) {
prototcp_bev_eventcb_src_child(bev, events, arg);

@ -43,8 +43,6 @@ struct protohttp_ctx {
unsigned int seen_resp_header : 1; /* 0 until response hdr complete */
unsigned int sent_http_conn_close : 1; /* 0 until Conn: close sent */
unsigned int ocsp_denied : 1; /* 1 if OCSP was denied */
unsigned int seen_req_header_on_entry : 1; /* save seen_req_header */
unsigned int seen_resp_header_on_entry : 1; /* save seen_resp_header */
/* log strings from HTTP request */
char *http_method;
@ -321,56 +319,25 @@ protohttp_filter_request_header(struct evbuffer *inbuf, struct evbuffer *outbuf,
}
}
static int NONNULL(1,2)
protohttp_bev_readcb_src_log_preexec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_log_preexec: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
// HTTP content logging at this point may record certain headers twice if have not seen all header lines yet
if (pxy_log_content_inbuf(ctx, bufferevent_get_input(bev), 1) == -1) {
return -1;
}
http_ctx->seen_req_header_on_entry = http_ctx->seen_req_header;
return 0;
}
static void NONNULL(1,2)
protohttp_bev_readcb_src_log_postexec(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
static void NONNULL(1)
protohttp_bev_readcb_src(struct bufferevent *bev, void *arg)
{
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_log_postexec: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
if (!http_ctx->seen_req_header_on_entry && http_ctx->seen_req_header && http_ctx->ocsp_denied) {
pxy_log_content_buf(ctx, (unsigned char *)ocspresp, sizeof(ocspresp) - 1, 0/*resp*/);
}
}
static int NONNULL(1,2)
protohttp_bev_readcb_src_exec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_exec: ENTER, fd=%d, size=%zu\n",
ctx->fd, evbuffer_get_length(bufferevent_get_input(bev)));
#endif /* DEBUG_PROXY */
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
ctx->thr->intif_in_bytes += evbuffer_get_length(inbuf);
if (ctx->dst.closed) {
pxy_discard_inbuf(bev);
return -1;
return;
}
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
// We insert our special header line to the first packet we get, e.g. right after the first \r\n in the case of http
// @todo Should we look for GET/POST or Host header lines to detect the first packet?
// But there is no guarantee that they will exist, due to fragmentation.
@ -384,29 +351,16 @@ protohttp_bev_readcb_src_exec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_exec: HTTP Request Header size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
protohttp_filter_request_header(inbuf, outbuf, ctx, http_ctx);
} else {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_exec: HTTP Request Body size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
evbuffer_add_buffer(outbuf, inbuf);
}
pxy_set_watermark(bev, ctx, ctx->dst.bev);
return 0;
}
static void NONNULL(1)
protohttp_bev_readcb_src(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
if (protohttp_bev_readcb_src_log_preexec(bev, ctx) == -1) {
return;
}
if (protohttp_bev_readcb_src_exec(bev, ctx) == -1) {
return;
}
protohttp_bev_readcb_src_log_postexec(bev, ctx);
}
/*
@ -643,44 +597,11 @@ out:
return;
}
static int NONNULL(1,2)
protohttp_bev_readcb_dst_log_preexec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_log_preexec: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
// HTTP content logging at this point may record certain headers twice if we have not seen all header lines yet
if (pxy_log_content_inbuf(ctx, bufferevent_get_input(bev), 1) == -1) {
return -1;
}
http_ctx->seen_resp_header_on_entry = http_ctx->seen_resp_header;
return 0;
}
static void NONNULL(1,2)
protohttp_bev_readcb_dst_log_postexec(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
static void NONNULL(1)
protohttp_bev_readcb_dst(struct bufferevent *bev, void *arg)
{
protohttp_ctx_t *http_ctx = ctx->protoctx->arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_log_postexec: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
if (!http_ctx->seen_resp_header_on_entry && http_ctx->seen_resp_header) {
/* response header complete: log connection */
if (WANT_CONNECT_LOG(ctx->conn) || ctx->opts->statslog) {
protohttp_log_connect(ctx);
}
}
}
pxy_conn_ctx_t *ctx = arg;
static int NONNULL(1,2)
protohttp_bev_readcb_dst_exec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_exec: ENTER, fd=%d, size=%zu\n",
ctx->fd, evbuffer_get_length(bufferevent_get_input(bev)));
@ -690,40 +611,33 @@ protohttp_bev_readcb_dst_exec(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
ctx->thr->intif_out_bytes += evbuffer_get_length(inbuf);
if (ctx->src.closed) {
pxy_discard_inbuf(bev);
return -1;
return;
}
int seen_resp_header_on_entry = http_ctx->seen_resp_header;
if (!http_ctx->seen_resp_header) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_exec: HTTP Response Header size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
protohttp_filter_response_header(inbuf, outbuf, ctx, http_ctx);
} else {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_exec: HTTP Response Body size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
evbuffer_add_buffer(outbuf, inbuf);
}
pxy_set_watermark(bev, ctx, ctx->src.bev);
return 0;
}
static void NONNULL(1)
protohttp_bev_readcb_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
if (protohttp_bev_readcb_dst_log_preexec(bev, ctx) == -1) {
return;
}
if (protohttp_bev_readcb_dst_exec(bev, ctx) == -1) {
return;
if (!seen_resp_header_on_entry && http_ctx->seen_resp_header) {
/* response header complete: log connection */
if (WANT_CONNECT_LOG(ctx->conn) || ctx->opts->statslog) {
protohttp_log_connect(ctx);
}
}
protohttp_bev_readcb_dst_log_postexec(bev, ctx);
}
static void NONNULL(1)
@ -736,13 +650,6 @@ static void NONNULL(1)
protohttp_bev_readcb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "protohttp_bev_readcb: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
protohttp_bev_readcb_src(bev, arg);
@ -774,21 +681,17 @@ protohttp_bev_readcb_src_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_out_bytes += inbuf_size;
if (!http_ctx->seen_req_header) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_child: HTTP Request Header size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_child: HTTP Request Header size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
// @todo Just remove SSLproxy line, do not filter response on the server side?
protohttp_filter_request_header(inbuf, outbuf, (pxy_conn_ctx_t *)ctx, http_ctx);
} else {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_child: HTTP Request Body size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_src_child: HTTP Request Body size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_content_inbuf((pxy_conn_ctx_t *)ctx, inbuf, 1);
evbuffer_add_buffer(outbuf, inbuf);
}
@ -814,21 +717,18 @@ protohttp_bev_readcb_dst_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_in_bytes += inbuf_size;
if (!http_ctx->seen_resp_header) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_child: HTTP Response Header size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_child: HTTP Response Header size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
// @todo Do not filter response on the server side?
protohttp_filter_response_header(inbuf, outbuf, (pxy_conn_ctx_t *)ctx, http_ctx);
} else {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_child: HTTP Response Body size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protohttp_bev_readcb_dst_child: HTTP Response Body size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_content_inbuf((pxy_conn_ctx_t *)ctx, inbuf, 0);
evbuffer_add_buffer(outbuf, inbuf);
}
pxy_set_watermark(bev, ctx->conn, ctx->src.bev);
@ -839,14 +739,6 @@ protohttp_bev_readcb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "protohttp_bev_readcb_child: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
protohttp_bev_readcb_src_child(bev, arg);
} else if (bev == ctx->dst.bev) {

@ -74,11 +74,11 @@ protopassthrough_bev_writecb_src(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
// @attention srv_dst.bev may be NULL
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_writecb_src: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
// @attention srv_dst.bev may be NULL
if (ctx->srv_dst.closed) {
if (pxy_try_close_conn_end(&ctx->src, ctx, &prototcp_bufferevent_free_and_close_fd)) {
#ifdef DEBUG_PROXY
@ -95,6 +95,7 @@ static void NONNULL(1)
protopassthrough_bev_writecb_srv_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_writecb_srv_dst: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
@ -154,6 +155,7 @@ protopassthrough_bev_eventcb_connected_src(UNUSED struct bufferevent *bev, pxy_c
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_eventcb_connected_src: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
protopassthrough_log_connect_src(ctx);
}
@ -196,6 +198,7 @@ protopassthrough_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, p
if (!ctx->srv_dst_connected) {
ctx->srv_dst_connected = 1;
ctx->srv_dst_fd = bufferevent_getfd(ctx->srv_dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->srv_dst_fd);
}
@ -244,9 +247,8 @@ protopassthrough_bev_eventcb_eof_src(struct bufferevent *bev, pxy_conn_ctx_t *ct
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_eventcb_eof_src: EOF, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_dbg_evbuf_info(ctx, &ctx->src, &ctx->srv_dst);
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
@ -255,12 +257,12 @@ protopassthrough_bev_eventcb_eof_src(struct bufferevent *bev, pxy_conn_ctx_t *ct
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_eventcb_eof_src: !other->closed, terminate conn, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input(bev, ctx);
pxy_try_close_conn_end(&ctx->srv_dst, ctx, &prototcp_bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->src, &prototcp_bufferevent_free_and_close_fd, &ctx->srv_dst, 1);
}
@ -269,9 +271,8 @@ protopassthrough_bev_eventcb_eof_srv_dst(struct bufferevent *bev, pxy_conn_ctx_t
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_eventcb_eof_srv_dst: EOF, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_dbg_evbuf_info(ctx, &ctx->srv_dst, &ctx->src);
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
@ -280,27 +281,23 @@ protopassthrough_bev_eventcb_eof_srv_dst(struct bufferevent *bev, pxy_conn_ctx_t
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "protopassthrough_bev_eventcb_eof_srv_dst: !other->closed, terminate conn, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input(bev, ctx);
pxy_try_close_conn_end(&ctx->src, ctx, &prototcp_bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->srv_dst, &prototcp_bufferevent_free_and_close_fd, &ctx->src, 0);
}
static void NONNULL(1,2)
protopassthrough_bev_eventcb_error_src(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
// Passthrough packets are transfered between src and srv_dst
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINER, "protopassthrough_bev_eventcb_error_src: BEV_EVENT_ERROR, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
log_err_printf("protopassthrough_bev_eventcb_error_src: Client-side BEV_EVENT_ERROR\n");
ctx->thr->errors++;
// Passthrough packets are transfered between src and srv_dst
if (!ctx->connected) {
ctx->srv_dst.closed = 1;
} else if (!ctx->srv_dst.closed) {
@ -308,22 +305,17 @@ protopassthrough_bev_eventcb_error_src(UNUSED struct bufferevent *bev, pxy_conn_
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->src, &prototcp_bufferevent_free_and_close_fd, &ctx->srv_dst, 1);
}
static void NONNULL(1,2)
protopassthrough_bev_eventcb_error_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
// Passthrough packets are transfered between src and srv_dst
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINER, "protopassthrough_bev_eventcb_error_srv_dst: BEV_EVENT_ERROR, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
log_err_printf("protopassthrough_bev_eventcb_error_srv_dst: Client-side BEV_EVENT_ERROR\n");
ctx->thr->errors++;
// Passthrough packets are transfered between src and srv_dst
if (!ctx->connected) {
ctx->src.closed = 1;
} else if (!ctx->src.closed) {
@ -331,7 +323,6 @@ protopassthrough_bev_eventcb_error_srv_dst(UNUSED struct bufferevent *bev, pxy_c
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->srv_dst, &prototcp_bufferevent_free_and_close_fd, &ctx->src, 0);
}
@ -363,13 +354,6 @@ static void NONNULL(1)
protopassthrough_bev_readcb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "protopassthrough_bev_readcb: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
protopassthrough_bev_readcb_src(bev, arg);
@ -384,7 +368,6 @@ static void NONNULL(1)
protopassthrough_bev_writecb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (bev == ctx->src.bev) {
protopassthrough_bev_writecb_src(bev, arg);
@ -399,7 +382,6 @@ static void NONNULL(1)
protopassthrough_bev_eventcb_src(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protopassthrough_bev_eventcb_connected_src(bev, ctx);
@ -414,7 +396,6 @@ static void NONNULL(1)
protopassthrough_bev_eventcb_srv_dst(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_CONNECTED) {
protopassthrough_bev_eventcb_connected_srv_dst(bev, ctx);
@ -429,7 +410,6 @@ static void NONNULL(1)
protopassthrough_bev_eventcb(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (bev == ctx->src.bev) {
protopassthrough_bev_eventcb_src(bev, events, arg);

@ -1306,6 +1306,7 @@ protossl_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_
#endif /* DEBUG_PROXY */
ctx->srv_dst_connected = 1;
ctx->srv_dst_fd = bufferevent_getfd(ctx->srv_dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->srv_dst_fd);
@ -1314,9 +1315,11 @@ protossl_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINE, "protossl_bev_eventcb_connected_srv_dst: FAILED bufferevent_socket_connect for dst, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_conn_free(ctx, 1);
return;
}
ctx->dst_fd = bufferevent_getfd(ctx->dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->dst_fd);
@ -1387,12 +1390,9 @@ void
protossl_bev_eventcb(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("protossl_bev_eventcb: Client-side BEV_EVENT_ERROR\n");
protossl_log_ssl_error(bev, ctx);
ctx->thr->errors++;
}
if (bev == ctx->src.bev) {
@ -1410,12 +1410,9 @@ void
protossl_bev_eventcb_child(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("Server-side BEV_EVENT_ERROR\n");
protossl_log_ssl_error(bev, ctx->conn);
ctx->conn->thr->errors++;
}
if (bev == ctx->src.bev) {

@ -235,6 +235,7 @@ static void NONNULL(1)
prototcp_bev_readcb_src(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_src: ENTER, fd=%d, size=%zu\n",
ctx->fd, evbuffer_get_length(bufferevent_get_input(bev)));
@ -247,15 +248,8 @@ prototcp_bev_readcb_src(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->thr->intif_in_bytes += inbuf_size;
if (pxy_log_content_inbuf(ctx, inbuf, 1) == -1) {
return;
}
size_t packet_size = inbuf_size;
size_t packet_size = evbuffer_get_length(inbuf);
// +2 is for \r\n
unsigned char *packet = pxy_malloc_packet(packet_size + ctx->header_len + 2, ctx);
if (!packet) {
@ -281,6 +275,7 @@ prototcp_bev_readcb_src(struct bufferevent *bev, void *arg)
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_src: NEW packet (size=%zu), fd=%d:\n%.*s\n",
packet_size, ctx->fd, (int)packet_size, packet);
#endif /* DEBUG_PROXY */
free(packet);
pxy_set_watermark(bev, ctx, ctx->dst.bev);
}
@ -289,6 +284,7 @@ static void NONNULL(1)
prototcp_bev_readcb_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_dst: ENTER, fd=%d, size=%zu\n",
ctx->fd, evbuffer_get_length(bufferevent_get_input(bev)));
@ -301,17 +297,11 @@ prototcp_bev_readcb_dst(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->thr->intif_out_bytes += inbuf_size;
if (pxy_log_content_inbuf(ctx, inbuf, 0) == -1) {
return;
}
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_dst: packet size=%zu, fd=%d\n", inbuf_size, ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_dst: packet size=%zu, fd=%d\n", evbuffer_get_length(inbuf), ctx->fd);
#endif /* DEBUG_PROXY */
evbuffer_add_buffer(outbuf, inbuf);
pxy_set_watermark(bev, ctx, ctx->src.bev);
}
@ -332,12 +322,6 @@ prototcp_bev_readcb_src_child(struct bufferevent *bev, void *arg)
ctx->fd, ctx->conn->fd, evbuffer_get_length(bufferevent_get_input(bev)));
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "prototcp_bev_readcb_src_child: readcb called when other end not connected - aborting.\n");
log_exceptcb();
return;
}
if (ctx->dst.closed) {
pxy_discard_inbuf(bev);
return;
@ -346,11 +330,7 @@ prototcp_bev_readcb_src_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->dst.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_out_bytes += inbuf_size;
size_t packet_size = inbuf_size;
size_t packet_size = evbuffer_get_length(inbuf);
unsigned char *packet = pxy_malloc_packet(packet_size, ctx->conn);
if (!packet) {
return;
@ -371,9 +351,7 @@ prototcp_bev_readcb_src_child(struct bufferevent *bev, void *arg)
packet_size, ctx->fd, ctx->conn->fd, (int)packet_size, packet);
#endif /* DEBUG_PROXY */
pxy_log_content_buf((pxy_conn_ctx_t *)ctx, packet, packet_size, 1);
free(packet);
pxy_set_watermark(bev, ctx->conn, ctx->dst.bev);
}
@ -387,12 +365,6 @@ prototcp_bev_readcb_dst_child(struct bufferevent *bev, void *arg)
ctx->fd, ctx->conn->fd, evbuffer_get_length(bufferevent_get_input(bev)));
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "prototcp_bev_readcb_dst_child: readcb called when other end not connected - aborting.\n");
log_exceptcb();
return;
}
if (ctx->src.closed) {
pxy_discard_inbuf(bev);
return;
@ -400,16 +372,7 @@ prototcp_bev_readcb_dst_child(struct bufferevent *bev, void *arg)
struct evbuffer *inbuf = bufferevent_get_input(bev);
struct evbuffer *outbuf = bufferevent_get_output(ctx->src.bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
ctx->conn->thr->extif_in_bytes += inbuf_size;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_readcb_dst_child: dst packet size=%zu, fd=%d\n", inbuf_size, ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_content_inbuf((pxy_conn_ctx_t *)ctx, inbuf, 0);
evbuffer_add_buffer(outbuf, inbuf);
pxy_set_watermark(bev, ctx->conn, ctx->src.bev);
}
@ -417,6 +380,7 @@ static void NONNULL(1)
prototcp_bev_writecb_src(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_writecb_src: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
@ -452,6 +416,7 @@ static void NONNULL(1)
prototcp_bev_writecb_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_writecb_dst: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
@ -474,9 +439,11 @@ static void NONNULL(1)
prototcp_bev_writecb_srv_dst(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_writecb_srv_dst: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_connect_srv_dst(bev, ctx);
}
@ -624,6 +591,7 @@ prototcp_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_
#endif /* DEBUG_PROXY */
ctx->srv_dst_connected = 1;
ctx->srv_dst_fd = bufferevent_getfd(ctx->srv_dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->srv_dst_fd);
@ -635,6 +603,7 @@ prototcp_bev_eventcb_connected_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_
pxy_conn_free(ctx, 1);
return;
}
ctx->dst_fd = bufferevent_getfd(ctx->dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->dst_fd);
@ -654,9 +623,8 @@ prototcp_bev_eventcb_eof_src(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_src: EOF, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_dbg_evbuf_info(ctx, &ctx->src, &ctx->dst);
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
@ -665,12 +633,12 @@ prototcp_bev_eventcb_eof_src(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_src: !other->closed, terminate conn, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input(bev, ctx);
pxy_try_close_conn_end(&ctx->dst, ctx, &prototcp_bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->src, ctx->protoctx->bufferevent_free_and_close_fd, &ctx->dst, 1);
}
@ -679,9 +647,8 @@ prototcp_bev_eventcb_eof_dst(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_dst: EOF, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_log_dbg_evbuf_info(ctx, &ctx->dst, &ctx->src);
#endif /* DEBUG_PROXY */
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
@ -690,12 +657,12 @@ prototcp_bev_eventcb_eof_dst(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_dst: !other->closed, terminate conn, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input(bev, ctx);
pxy_try_close_conn_end(&ctx->src, ctx, ctx->protoctx->bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->dst, &prototcp_bufferevent_free_and_close_fd, &ctx->src, 0);
}
@ -724,7 +691,6 @@ prototcp_bev_eventcb_error_src(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *c
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->src, ctx->protoctx->bufferevent_free_and_close_fd, &ctx->dst, 1);
}
@ -742,7 +708,6 @@ prototcp_bev_eventcb_error_dst(UNUSED struct bufferevent *bev, pxy_conn_ctx_t *c
}
pxy_log_dbg_disconnect(ctx);
pxy_disconnect(ctx, &ctx->dst, &prototcp_bufferevent_free_and_close_fd, &ctx->src, 0);
}
@ -757,6 +722,7 @@ prototcp_bev_eventcb_error_srv_dst(UNUSED struct bufferevent *bev, pxy_conn_ctx_
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINE, "prototcp_bev_eventcb_error_srv_dst: ERROR !ctx->connected, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
pxy_conn_free(ctx, 0);
}
}
@ -772,10 +738,10 @@ prototcp_bev_eventcb_connected_src_child(UNUSED struct bufferevent *bev, pxy_con
}
static void NONNULL(1,2)
prototcp_bev_eventcb_child_connected_dst(UNUSED struct bufferevent *bev, pxy_conn_child_ctx_t *ctx)
prototcp_bev_eventcb_connected_dst_child(UNUSED struct bufferevent *bev, pxy_conn_child_ctx_t *ctx)
{
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_child_connected_dst: ENTER, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_connected_dst_child: ENTER, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
ctx->connected = 1;
@ -797,17 +763,16 @@ prototcp_bev_eventcb_eof_src_child(struct bufferevent *bev, pxy_conn_child_ctx_t
// @todo How to handle the following case?
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINER, "prototcp_bev_eventcb_eof_src_child: EOF on outbound connection before connection establishment, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
ctx->dst.closed = 1;
} else if (!ctx->dst.closed) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_src_child: !other->closed, terminate conn, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input_child(bev, ctx);
pxy_try_close_conn_end(&ctx->dst, ctx->conn, ctx->protoctx->bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect_child(ctx);
pxy_disconnect_child(ctx, &ctx->src, &prototcp_bufferevent_free_and_close_fd, &ctx->dst);
}
@ -823,17 +788,16 @@ prototcp_bev_eventcb_eof_dst_child(struct bufferevent *bev, pxy_conn_child_ctx_t
// @todo How to handle the following case?
if (!ctx->connected) {
log_err_level_printf(LOG_WARNING, "EOF on outbound connection before connection establishment\n");
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINER, "prototcp_bev_eventcb_eof_dst_child: EOF on outbound connection before connection establishment, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
ctx->src.closed = 1;
} else if (!ctx->src.closed) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "prototcp_bev_eventcb_eof_dst_child: !other->closed, terminate conn, fd=%d, conn fd=%d\n", ctx->fd, ctx->conn->fd);
#endif /* DEBUG_PROXY */
pxy_consume_last_input_child(bev, ctx);
pxy_try_close_conn_end(&ctx->src, ctx->conn, &prototcp_bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect_child(ctx);
pxy_disconnect_child(ctx, &ctx->dst, ctx->protoctx->bufferevent_free_and_close_fd, &ctx->src);
}
@ -856,6 +820,7 @@ prototcp_bev_eventcb_error_src_child(UNUSED struct bufferevent *bev, pxy_conn_ch
* it after writing what's left in the output buffer */
pxy_try_close_conn_end(&ctx->dst, ctx->conn, ctx->protoctx->bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect_child(ctx);
pxy_disconnect_child(ctx, &ctx->src, &prototcp_bufferevent_free_and_close_fd, &ctx->dst);
}
@ -878,6 +843,7 @@ prototcp_bev_eventcb_error_dst_child(UNUSED struct bufferevent *bev, pxy_conn_ch
* it after writing what's left in the output buffer */
pxy_try_close_conn_end(&ctx->src, ctx->conn, &prototcp_bufferevent_free_and_close_fd);
}
pxy_log_dbg_disconnect_child(ctx);
pxy_disconnect_child(ctx, &ctx->dst, ctx->protoctx->bufferevent_free_and_close_fd, &ctx->src);
}
@ -902,7 +868,7 @@ prototcp_bev_eventcb_dst_child(struct bufferevent *bev, short events, void *arg)
pxy_conn_child_ctx_t *ctx = arg;
if (events & BEV_EVENT_CONNECTED) {
prototcp_bev_eventcb_child_connected_dst(bev, ctx);
prototcp_bev_eventcb_connected_dst_child(bev, ctx);
} else if (events & BEV_EVENT_EOF) {
prototcp_bev_eventcb_eof_dst_child(bev, ctx);
} else if (events & BEV_EVENT_ERROR) {
@ -956,13 +922,6 @@ static void NONNULL(1)
prototcp_bev_readcb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "prototcp_bev_readcb: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
prototcp_bev_readcb_src(bev, arg);
@ -979,7 +938,6 @@ void
prototcp_bev_writecb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (bev == ctx->src.bev) {
prototcp_bev_writecb_src(bev, arg);
@ -996,12 +954,6 @@ static void NONNULL(1)
prototcp_bev_eventcb(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("prototcp_bev_eventcb: Client-side BEV_EVENT_ERROR\n");
ctx->thr->errors++;
}
if (bev == ctx->src.bev) {
prototcp_bev_eventcb_src(bev, events, arg);
@ -1018,13 +970,6 @@ static void NONNULL(1)
prototcp_bev_readcb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "prototcp_bev_readcb_child: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
if (bev == ctx->src.bev) {
prototcp_bev_readcb_src_child(bev, arg);
@ -1039,7 +984,6 @@ void
prototcp_bev_writecb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (bev == ctx->src.bev) {
prototcp_bev_writecb_src_child(bev, arg);
@ -1054,12 +998,6 @@ static void NONNULL(1)
prototcp_bev_eventcb_child(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("Server-side BEV_EVENT_ERROR\n");
ctx->conn->thr->errors++;
}
if (bev == ctx->src.bev) {
prototcp_bev_eventcb_src_child(bev, events, arg);

@ -1174,14 +1174,32 @@ void
pxy_bev_readcb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (bev == ctx->src.bev || bev == ctx->dst.bev) {
struct evbuffer *inbuf = bufferevent_get_input(bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
if (bev == ctx->src.bev) {
ctx->thr->intif_in_bytes += inbuf_size;
} else {
ctx->thr->intif_out_bytes += inbuf_size;
}
if (ctx->proto != PROTO_PASSTHROUGH) {
// HTTP content logging at this point may record certain headers twice, if we have not seen all header lines yet
if (pxy_log_content_inbuf(ctx, inbuf, (bev == ctx->src.bev)) == -1) {
return;
}
}
}
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "pxy_bev_readcb: readcb called when other end not connected - aborting.\n");
log_err_level_printf(LOG_CRIT, "pxy_bev_readcb: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
ctx->atime = time(NULL);
ctx->protoctx->bev_readcb(bev, ctx);
}
@ -1189,14 +1207,29 @@ void
pxy_bev_readcb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
struct evbuffer *inbuf = bufferevent_get_input(bev);
size_t inbuf_size = evbuffer_get_length(inbuf);
if (bev == ctx->src.bev) {
ctx->conn->thr->extif_out_bytes += inbuf_size;
} else {
ctx->conn->thr->extif_in_bytes += inbuf_size;
}
if (ctx->proto != PROTO_PASSTHROUGH) {
if (pxy_log_content_inbuf((pxy_conn_ctx_t *)ctx, inbuf, (bev == ctx->src.bev)) == -1) {
return;
}
}
if (!ctx->connected) {
log_err_level_printf(LOG_CRIT, "pxy_bev_readcb_child: readcb called when other end not connected - aborting.\n");
log_err_level_printf(LOG_CRIT, "pxy_bev_readcb_child: readcb called when not connected - aborting.\n");
log_exceptcb();
return;
}
ctx->conn->atime = time(NULL);
ctx->protoctx->bev_readcb(bev, ctx);
}
@ -1209,6 +1242,7 @@ void
pxy_bev_writecb(struct bufferevent *bev, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
ctx->protoctx->bev_writecb(bev, ctx);
}
@ -1217,6 +1251,7 @@ void
pxy_bev_writecb_child(struct bufferevent *bev, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
ctx->protoctx->bev_writecb(bev, ctx);
}
@ -1229,7 +1264,14 @@ void
pxy_bev_eventcb(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
ctx->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("Client-side BEV_EVENT_ERROR\n");
ctx->thr->errors++;
}
ctx->protoctx->bev_eventcb(bev, events, arg);
}
@ -1237,7 +1279,14 @@ void
pxy_bev_eventcb_child(struct bufferevent *bev, short events, void *arg)
{
pxy_conn_child_ctx_t *ctx = arg;
ctx->conn->atime = time(NULL);
if (events & BEV_EVENT_ERROR) {
log_err_printf("Server-side BEV_EVENT_ERROR\n");
ctx->conn->thr->errors++;
}
ctx->protoctx->bev_eventcb(bev, events, arg);
}
@ -1285,6 +1334,7 @@ void
pxy_fd_readcb(evutil_socket_t fd, UNUSED short what, void *arg)
{
pxy_conn_ctx_t *ctx = arg;
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_fd_readcb: ENTER, fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */

Loading…
Cancel
Save