Callbacks are called to flush past events

This commit is contained in:
Quentin Dufour 2019-03-22 18:05:40 +01:00
parent a579f0de61
commit b105e25c38
2 changed files with 6 additions and 14 deletions

View file

@ -80,6 +80,9 @@ void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, st
// 4. Update epoll flags // 4. Update epoll flags
update_fd_epoll (ctx->epollfd, fdinfo->fd, fdinfo->cat->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) { 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); 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); 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; return fdinfo;
} }

View file

@ -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)); fill_buffer(&s5ctx->cr_size, s5ctx->cr_buffer, &s5ctx->cr.port, sizeof(uint16_t));
reg_fdinfo = evt_core_add_fd (ctx, &fdinfo); 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) { 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) { if (written < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
s5ctx->ch_cursor += written; s5ctx->ch_cursor += written;
if (s5ctx->ch_cursor < s5ctx->ch_size) return 0; if (s5ctx->ch_cursor < s5ctx->ch_size) return 0;
evt_core_mv_fd2(ctx, fdinfo, "socks5-recv-handshake"); evt_core_mv_fd2(ctx, fdinfo, "socks5-recv-handshake");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
@ -111,7 +108,6 @@ int on_socks5_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f
if (readn < 0) { if (readn < 0) {
perror("sock5 handshake failed read"); perror("sock5 handshake failed read");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; 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", 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); s5ctx->ch.ver, s5ctx->ch.methods[0], s5ctx->sh.ver, s5ctx->sh.method);
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
printf("[socks5_server_handshake] fd=%d, ver=%d, method=%d\n", fdinfo->fd, s5ctx->sh.ver, s5ctx->sh.method); 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"); evt_core_mv_fd2(ctx, fdinfo, "socks5-send-client-req");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
@ -140,14 +134,12 @@ int on_socks5_send_client_req(struct evt_core_ctx* ctx, struct evt_core_fdinfo*
if (written < 0) { if (written < 0) {
fprintf(stderr, "socks5 send client request failed\n"); fprintf(stderr, "socks5 send client request failed\n");
evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
s5ctx->cr_cursor += written; s5ctx->cr_cursor += written;
if (s5ctx->cr_cursor < s5ctx->cr_size) return 0; if (s5ctx->cr_cursor < s5ctx->cr_size) return 0;
evt_core_mv_fd2 (ctx, fdinfo, "socks5-recv-server-reply"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-recv-server-reply");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
@ -166,7 +158,6 @@ int socks5_server_reply_atyp_ipv4(struct evt_core_ctx* ctx, struct evt_core_fdin
if (nread < 0) { if (nread < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
@ -192,7 +183,6 @@ int socks5_server_reply_atyp_ipv6(struct evt_core_ctx* ctx, struct evt_core_fdin
if (nread < 0) { if (nread < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
s5ctx->sr_cursor += nread; 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) { if (nread < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
s5ctx->sr_cursor += nread; 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) { if (nread < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
s5ctx->sr_cursor += nread; 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; if (s5ctx->sr.rep != SOCKS5_REP_SUCCESS) goto move_to_failed;
evt_core_mv_fd2 (ctx, fdinfo, "socks5-success"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-success");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
move_to_failed: move_to_failed:
evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }