Skip to content

Commit

Permalink
HevSocks5SessionUDP: Fix timeout for bidirectional link.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Dec 4, 2024
1 parent 79490aa commit d46a36f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/hev-socks5-session-udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ hev_socks5_session_udp_fwd_f (HevSocks5SessionUDP *self)
break;

res = task_io_yielder (HEV_TASK_WAITIO, self);
if (res < 0)
if (res < 0) {
self->alive &= ~HEV_SOCKS5_SESSION_UDP_ALIVE_F;
if (self->alive && hev_socks5_get_timeout (HEV_SOCKS5 (self)))
return 0;
return -1;
}
}

frame = container_of (node, HevSocks5UDPFrame, node);
Expand All @@ -64,17 +68,23 @@ hev_socks5_session_udp_fwd_f (HevSocks5SessionUDP *self)

udp = HEV_SOCKS5_UDP (self);
res = hev_socks5_udp_sendto (udp, buf->payload, buf->len, addr);
hev_list_del (&self->frame_list, node);
hev_free (frame);
pbuf_free (buf);
self->frames--;
if (res <= 0) {
if (res < -1) {
self->alive &= ~HEV_SOCKS5_SESSION_UDP_ALIVE_F;
if (self->alive && hev_socks5_get_timeout (HEV_SOCKS5 (self)))
return 0;
}
LOG_D ("%p socks5 session udp fwd f send", self);
res = -1;
}

hev_list_del (&self->frame_list, node);
hev_free (frame);
pbuf_free (buf);
self->frames--;
self->alive |= HEV_SOCKS5_SESSION_UDP_ALIVE_F;

return res;
return 0;
}

static int
Expand Down Expand Up @@ -109,6 +119,11 @@ hev_socks5_session_udp_fwd_b (HevSocks5SessionUDP *self)

res = hev_socks5_udp_recvfrom (udp, buf->payload, buf->len, saddr);
if (res <= 0) {
if (res < -1) {
self->alive &= ~HEV_SOCKS5_SESSION_UDP_ALIVE_B;
if (self->alive && hev_socks5_get_timeout (HEV_SOCKS5 (self)))
return 0;
}
LOG_D ("%p socks5 session udp fwd b recv", self);
pbuf_free (buf);
return -1;
Expand All @@ -134,6 +149,8 @@ hev_socks5_session_udp_fwd_b (HevSocks5SessionUDP *self)
return -1;
}

self->alive |= HEV_SOCKS5_SESSION_UDP_ALIVE_B;

return 0;
}

Expand Down Expand Up @@ -220,10 +237,10 @@ static void
splice_task_entry (void *data)
{
HevTask *task = hev_task_self ();
HevSocks5UDP *self = data;
HevSocks5SessionUDP *self = data;
int fd;

fd = hev_task_io_dup (hev_socks5_udp_get_fd (self));
fd = hev_task_io_dup (hev_socks5_udp_get_fd (HEV_SOCKS5_UDP (self)));
if (fd < 0)
return;

Expand All @@ -235,6 +252,7 @@ splice_task_entry (void *data)
break;
}

self->alive &= ~HEV_SOCKS5_SESSION_UDP_ALIVE_B;
hev_task_del_fd (task, fd);
close (fd);
}
Expand All @@ -249,6 +267,8 @@ hev_socks5_session_udp_splice (HevSocks5Session *base)

LOG_D ("%p socks5 session udp splice", self);

self->alive = HEV_SOCKS5_SESSION_UDP_ALIVE_F |
HEV_SOCKS5_SESSION_UDP_ALIVE_B;
fd = hev_socks5_udp_get_fd (HEV_SOCKS5_UDP (self));
if (hev_task_mod_fd (task, fd, POLLOUT) < 0)
hev_task_add_fd (task, fd, POLLOUT);
Expand All @@ -263,6 +283,7 @@ hev_socks5_session_udp_splice (HevSocks5Session *base)
break;
}

self->alive &= ~HEV_SOCKS5_SESSION_UDP_ALIVE_F;
hev_task_join (task);
hev_task_unref (task);
}
Expand Down
8 changes: 8 additions & 0 deletions src/hev-socks5-session-udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@
#define HEV_SOCKS5_SESSION_UDP_CLASS(p) ((HevSocks5SessionUDPClass *)p)
#define HEV_SOCKS5_SESSION_UDP_TYPE (hev_socks5_session_udp_class ())

typedef enum _HevSocks5SessionUDPAlive HevSocks5SessionUDPAlive;
typedef struct _HevSocks5SessionUDP HevSocks5SessionUDP;
typedef struct _HevSocks5SessionUDPClass HevSocks5SessionUDPClass;

enum _HevSocks5SessionUDPAlive
{
HEV_SOCKS5_SESSION_UDP_ALIVE_F = (1 << 0),
HEV_SOCKS5_SESSION_UDP_ALIVE_B = (1 << 1),
};

struct _HevSocks5SessionUDP
{
HevSocks5ClientUDP base;

HevSocks5SessionData data;

HevSocks5SessionUDPAlive alive;
HevList frame_list;
struct udp_pcb *pcb;
HevTaskMutex *mutex;
Expand Down

0 comments on commit d46a36f

Please sign in to comment.