From b105e25c38eb0a7446c086f6e610c907bc5a053c Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 22 Mar 2019 18:05:40 +0100 Subject: [PATCH] Callbacks are called to flush past events --- src/evt_core.c | 6 ++++++ src/socks5.c | 14 -------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/evt_core.c b/src/evt_core.c index ff6f942..297d479 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -80,6 +80,9 @@ void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, st // 4. Update epoll flags update_fd_epoll (ctx->epollfd, fdinfo->fd, fdinfo->cat->flags); + + // 5. Handle cases where data arrived before registering the file descriptor + fdinfo->cat->cb(ctx, fdinfo); } void evt_core_mv_fd2(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, char* to_cat) { @@ -127,6 +130,9 @@ struct evt_core_fdinfo* evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_cor add_fd_to_epoll(ctx->epollfd, user_data->fd, cat->flags); printf("Added fd=%d with url=%s in cat=%s\n", fdinfo->fd, fdinfo->url, fdinfo->cat->name); + // 6. Ensure that events arrived before epoll registering are handled + fdinfo->cat->cb(ctx, fdinfo); + return fdinfo; } diff --git a/src/socks5.c b/src/socks5.c index 277bfdf..3e20d43 100644 --- a/src/socks5.c +++ b/src/socks5.c @@ -80,7 +80,6 @@ void create_socks5_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char* fill_buffer(&s5ctx->cr_size, s5ctx->cr_buffer, &s5ctx->cr.port, sizeof(uint16_t)); reg_fdinfo = evt_core_add_fd (ctx, &fdinfo); - reg_fdinfo->cat->cb(ctx, reg_fdinfo); } int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { @@ -91,14 +90,12 @@ int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f if (written < 0) { perror("write failed on tcp socket in socks5"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } s5ctx->ch_cursor += written; if (s5ctx->ch_cursor < s5ctx->ch_size) return 0; evt_core_mv_fd2(ctx, fdinfo, "socks5-recv-handshake"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } @@ -111,7 +108,6 @@ int on_socks5_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f if (readn < 0) { perror("sock5 handshake failed read"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } @@ -122,12 +118,10 @@ int on_socks5_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f fprintf(stderr, "Protocol error: client asks for ver=%d, method=%d and server answers with ver=%d, method=%d\n", s5ctx->ch.ver, s5ctx->ch.methods[0], s5ctx->sh.ver, s5ctx->sh.method); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } printf("[socks5_server_handshake] fd=%d, ver=%d, method=%d\n", fdinfo->fd, s5ctx->sh.ver, s5ctx->sh.method); evt_core_mv_fd2(ctx, fdinfo, "socks5-send-client-req"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } @@ -140,14 +134,12 @@ int on_socks5_send_client_req(struct evt_core_ctx* ctx, struct evt_core_fdinfo* if (written < 0) { fprintf(stderr, "socks5 send client request failed\n"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } s5ctx->cr_cursor += written; if (s5ctx->cr_cursor < s5ctx->cr_size) return 0; evt_core_mv_fd2 (ctx, fdinfo, "socks5-recv-server-reply"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } @@ -166,7 +158,6 @@ int socks5_server_reply_atyp_ipv4(struct evt_core_ctx* ctx, struct evt_core_fdin if (nread < 0) { perror("write failed on tcp socket in socks5"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } @@ -192,7 +183,6 @@ int socks5_server_reply_atyp_ipv6(struct evt_core_ctx* ctx, struct evt_core_fdin if (nread < 0) { perror("write failed on tcp socket in socks5"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } s5ctx->sr_cursor += nread; @@ -216,7 +206,6 @@ int socks5_server_reply_atyp_dn(struct evt_core_ctx* ctx, struct evt_core_fdinfo if (nread < 0) { perror("write failed on tcp socket in socks5"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } s5ctx->sr_cursor += nread; @@ -229,7 +218,6 @@ int socks5_server_reply_atyp_dn(struct evt_core_ctx* ctx, struct evt_core_fdinfo if (nread < 0) { perror("write failed on tcp socket in socks5"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; } s5ctx->sr_cursor += nread; @@ -298,11 +286,9 @@ int on_socks5_recv_server_reply(struct evt_core_ctx* ctx, struct evt_core_fdinfo if (s5ctx->sr.rep != SOCKS5_REP_SUCCESS) goto move_to_failed; evt_core_mv_fd2 (ctx, fdinfo, "socks5-success"); - fdinfo->cat->cb(ctx, fdinfo); return 1; move_to_failed: evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed"); - fdinfo->cat->cb(ctx, fdinfo); return 1; }