Skip to content

Commit

Permalink
server: fix dialreq object life
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Jan 16, 2025
1 parent 816c2c7 commit ddc1e41
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/forward.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ forward_ctx_close(struct ev_loop *loop, struct forward_ctx *restrict ctx)
FW_CTX_LOG_F(VERBOSE, ctx, "close, state=%d", ctx->state);
forward_ctx_stop(loop, ctx);

dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;
if (ctx->state < STATE_CONNECTED) {
dialreq_free(ctx->dialreq);
}
if (ctx->accepted_fd != -1) {
CLOSE_FD(ctx->accepted_fd);
ctx->accepted_fd = -1;
Expand Down Expand Up @@ -212,7 +213,6 @@ static void dialer_cb(struct ev_loop *loop, void *data)
FW_CTX_LOG_F(DEBUG, ctx, "connected, fd=%d", fd);
/* cleanup before state change */
dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;

if (G.conf->proto_timeout) {
ctx->state = STATE_CONNECTED;
Expand Down Expand Up @@ -272,7 +272,8 @@ forward_idle_cb(struct ev_loop *loop, struct ev_idle *watcher, int revents)
struct forward_ctx *restrict ctx = watcher->data;
struct ruleset *restrict ruleset = G.ruleset;
ASSERT(ruleset != NULL);
const struct dialaddr *addr = &G.basereq->addr;
const struct dialreq *restrict req = G.basereq;
const struct dialaddr *restrict addr = &req->addr;

const size_t cap =
addr->type == ATYP_DOMAIN ? addr->domain.len + 7 : 64;
Expand Down Expand Up @@ -368,7 +369,8 @@ void forward_serve(
return;
}
#endif
forward_ctx_start(loop, ctx, G.basereq);
const struct dialreq *req = G.basereq;
forward_ctx_start(loop, ctx, req);
}

#if WITH_TPROXY
Expand Down Expand Up @@ -511,11 +513,12 @@ void tproxy_serve(
}
#endif

ctx->dialreq = tproxy_makereq(ctx);
if (ctx->dialreq == NULL) {
struct dialreq *req = tproxy_makereq(ctx);
if (req == NULL) {
forward_ctx_close(loop, ctx);
return;
}
forward_ctx_start(loop, ctx, ctx->dialreq);
ctx->dialreq = req;
forward_ctx_start(loop, ctx, req);
}
#endif /* WITH_TPROXY */
9 changes: 5 additions & 4 deletions src/http_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ static void http_ctx_close(struct ev_loop *loop, struct http_ctx *restrict ctx)
VERBOSE, ctx, "close state=%d", ctx->accepted_fd, ctx->state);
http_ctx_stop(loop, ctx);

dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;
if (ctx->state < STATE_CONNECTED) {
dialreq_free(ctx->dialreq);
}
if (ctx->accepted_fd != -1) {
CLOSE_FD(ctx->accepted_fd);
ctx->accepted_fd = -1;
Expand All @@ -171,8 +172,6 @@ static void dialer_cb(struct ev_loop *loop, void *data)
{
struct http_ctx *restrict ctx = data;
ASSERT(ctx->state == STATE_CONNECT);
dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;

const int fd = dialer_get(&ctx->dialer);
if (fd < 0) {
Expand Down Expand Up @@ -362,8 +361,10 @@ static void xfer_state_cb(struct ev_loop *loop, void *data)

static void http_ctx_hijack(struct ev_loop *loop, struct http_ctx *restrict ctx)
{
/* cleanup before state change */
ev_io_stop(loop, &ctx->w_recv);
ev_io_stop(loop, &ctx->w_send);
dialreq_free(ctx->dialreq);

if (G.conf->proto_timeout) {
ctx->state = STATE_CONNECTED;
Expand Down
6 changes: 3 additions & 3 deletions src/socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ socks_ctx_close(struct ev_loop *restrict loop, struct socks_ctx *restrict ctx)
SOCKS_CTX_LOG_F(VERBOSE, ctx, "close, state=%d", ctx->state);
socks_ctx_stop(loop, ctx);

dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;
if (ctx->state < STATE_CONNECTED) {
dialreq_free(ctx->dialreq);
}
if (ctx->accepted_fd != -1) {
CLOSE_FD(ctx->accepted_fd);
ctx->accepted_fd = -1;
Expand Down Expand Up @@ -401,7 +402,6 @@ static void dialer_cb(struct ev_loop *loop, void *data)
/* cleanup before state change */
ev_io_stop(loop, &ctx->w_socket);
dialreq_free(ctx->dialreq);
ctx->dialreq = NULL;

if (G.conf->proto_timeout) {
ctx->state = STATE_CONNECTED;
Expand Down

0 comments on commit ddc1e41

Please sign in to comment.