Skip to content

Commit

Permalink
HevSocks5Tunnel: Implement UDP.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed May 10, 2021
1 parent 3771af3 commit c4c02d0
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/hev-socks5-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-socks5-tunnel.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2020 Everyone.
Copyright : Copyright (c) 2019 - 2021 Everyone.
Description : Socks5 Tunnel
============================================================================
*/
Expand Down Expand Up @@ -40,6 +40,7 @@ static int event_fds[2];

static struct netif netif;
static struct tcp_pcb *tcp;
static struct udp_pcb *udp;
static struct udp_pcb *dns;

static HevTaskMutex mutex;
Expand All @@ -58,6 +59,8 @@ static void lwip_io_task_entry (void *data);
static void lwip_timer_task_entry (void *data);
static void session_manager_task_entry (void *data);
static err_t tcp_accept_handler (void *arg, struct tcp_pcb *pcb, err_t err);
static void udp_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port);
static void dns_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port);

Expand Down Expand Up @@ -144,6 +147,7 @@ hev_socks5_tunnel_init (int tunfd)
hev_task_unref (task_event);
exit_free_gateway:
udp_remove (dns);
udp_remove (udp);
tcp_close (tcp);
netif_remove (&netif);
exit_close_event:
Expand All @@ -166,6 +170,7 @@ hev_socks5_tunnel_fini (void)
hev_task_unref (task_event);

udp_remove (dns);
udp_remove (udp);
tcp_close (tcp);
netif_remove (&netif);

Expand Down Expand Up @@ -399,10 +404,26 @@ gateway_init (void)

udp_recv (dns, dns_recv_handler, NULL);

udp = udp_new_ip_type (IPADDR_TYPE_ANY);
if (!udp) {
LOG_E ("Create UDP failed!");
goto exit_free_tcp;
}

udp_bind_netif (udp, &netif);
if (udp_bind (udp, NULL, 0) != ERR_OK) {
LOG_E ("UDP bind failed!");
goto exit_free_udp;
}

udp_recv (udp, udp_recv_handler, NULL);

return 0;

exit_free_dns:
udp_remove (dns);
exit_free_udp:
udp_remove (udp);
exit_free_tcp:
tcp_close (tcp);
exit_free_netif:
Expand Down Expand Up @@ -581,6 +602,22 @@ tcp_accept_handler (void *arg, struct tcp_pcb *pcb, err_t err)
return ERR_OK;
}

static void
udp_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
{
HevSocks5Session *session;

session = hev_socks5_session_new_udp (pcb, &mutex, session_close_handler);
if (!session) {
udp_remove (pcb);
return;
}

session_manager_insert_session (session);
hev_socks5_session_run (session);
}

static void
dns_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
Expand Down

0 comments on commit c4c02d0

Please sign in to comment.