From f43437978cd6c81bb2d8f5e1398fe8fd9a2049c9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 21 Mar 2019 13:01:48 +0100 Subject: [PATCH] WIP refactor socks5 --- src/socks5.c | 35 ++++++++++++++++++++++++++++++++--- src/socks5.h | 3 ++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/socks5.c b/src/socks5.c index 1351cc4..24d407e 100644 --- a/src/socks5.c +++ b/src/socks5.c @@ -1,13 +1,43 @@ #include "socks5.h" -int socks5_handshake_syn(int sock) { - //@TODO: Refactor the client handshake management +struct socks5_ctx { + struct client_handshake ch; + struct server_handshake sh; + struct client_request cr; + char cr_buffer[262]; + struct server_reply sr; +}; + +void socks5_free_ctx(void* elem) { + struct socks5_ctx* ctx = elem; + free(ctx); +} + +void create_socks5_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char* proxy_port, char* addr, uint16_t port) { + int sock = create_tcp_client (proxy_host, proxy_port); + struct evt_core_fdinfo fdinfo; + struct evt_core_cat cat; + fdinfo.cat = &cat; + fdinfo.cat->name = "socks5-send-handshake"; + fdinfo.fd = sock; + fdinfo.other = malloc(sizeof(struct socks5_ctx)); + if (fdinfo.other == NULL) { + perror("malloc failed"); + exit(EXIT_FAILURE); + } + fdinfo.free_other = socks5_free_ctx; + evt_core_add_fd (ctx, &fdinfo); +} + +int socks5_handshake_syn(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { struct client_handshake ch = { .ver = 0x05, .nmethods = 0x01, .methods = {0x00} }; + fdinfo-> + int size = sizeof(uint8_t) * (2 + ch.nmethods); if (size != write(sock, &ch, size)) { perror("write failed on tcp socket in socks5"); @@ -17,7 +47,6 @@ int socks5_handshake_syn(int sock) { } int socks5_handshake_ack(int sock) { - //@TODO: Refactor the client handshake management struct client_handshake ch = { .ver = 0x05, .nmethods = 0x01, diff --git a/src/socks5.h b/src/socks5.h index 742f663..a606e3e 100644 --- a/src/socks5.h +++ b/src/socks5.h @@ -7,6 +7,7 @@ #include #include #include "net_tools.h" +#include "evt_core.h" enum socks5_state { SOCKS5_STATE_NEW, @@ -95,7 +96,7 @@ struct server_reply { uint16_t port; }; -int socks5_handshake_syn(int sock); +int socks5_handshake_syn(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo); int socks5_handshake_ack(int sock); int socks5_connect_dns(int sock, char* addr, uint16_t port); int socks5_reply(int sock);