Skip to content

Commit

Permalink
HevSocks5Tunnel: Add support for setting TCP buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Oct 10, 2024
1 parent d5a43ba commit 7d38b54
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ socks5:

#misc:
# task stack size (bytes)
# task-stack-size: 81920
# task-stack-size: 86016
# tcp buffer size (bytes)
# tcp-buffer-size: 65536
# connect timeout (ms)
# connect-timeout: 5000
# read-write timeout (ms)
Expand Down
4 changes: 3 additions & 1 deletion conf/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ socks5:

#misc:
# task stack size (bytes)
# task-stack-size: 81920
# task-stack-size: 86016
# tcp buffer size (bytes)
# tcp-buffer-size: 65536
# connect timeout (ms)
# connect-timeout: 5000
# read-write timeout (ms)
Expand Down
2 changes: 1 addition & 1 deletion src/hev-config-const.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

static const int UDP_BUF_SIZE = 1500;
static const int UDP_POOL_SIZE = 512;
static const int TASK_STACK_SIZE = 86016;
static const int TASK_STACK_SIZE = 20480;

#endif /* __HEV_CONFIG_CONST_H__ */
21 changes: 18 additions & 3 deletions src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdio.h>
#include <arpa/inet.h>
#include <lwip/tcp.h>
#include <yaml.h>

#include "hev-logger.h"
Expand All @@ -29,7 +30,8 @@ static HevConfigServer srv;

static char log_file[1024];
static char pid_file[1024];
static int task_stack_size = 0;
static int task_stack_size = 86016;
static int tcp_buffer_size = 65536;
static int connect_timeout = 5000;
static int read_write_timeout = 60000;
static int limit_nofile = 65535;
Expand Down Expand Up @@ -283,6 +285,8 @@ hev_config_parse_misc (yaml_document_t *doc, yaml_node_t *base)

if (0 == strcmp (key, "task-stack-size"))
task_stack_size = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "tcp-buffer-size"))
tcp_buffer_size = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "connect-timeout"))
connect_timeout = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "read-write-timeout"))
Expand All @@ -305,6 +309,7 @@ hev_config_parse_doc (yaml_document_t *doc)
{
yaml_node_t *root;
yaml_node_pair_t *pair;
int min_task_stack_size;

root = yaml_document_get_root_node (doc);
if (!root || YAML_MAPPING_NODE != root->type)
Expand Down Expand Up @@ -337,8 +342,12 @@ hev_config_parse_doc (yaml_document_t *doc)
return -1;
}

if (task_stack_size < TASK_STACK_SIZE)
task_stack_size = TASK_STACK_SIZE;
if (tcp_buffer_size > TCP_SND_BUF)
tcp_buffer_size = TCP_SND_BUF;

min_task_stack_size = TASK_STACK_SIZE + tcp_buffer_size;
if (task_stack_size < min_task_stack_size)
task_stack_size = min_task_stack_size;

return 0;
}
Expand Down Expand Up @@ -477,6 +486,12 @@ hev_config_get_misc_task_stack_size (void)
return task_stack_size;
}

int
hev_config_get_misc_tcp_buffer_size (void)
{
return tcp_buffer_size;
}

int
hev_config_get_misc_connect_timeout (void)
{
Expand Down
1 change: 1 addition & 0 deletions src/hev-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const char *hev_config_get_tunnel_pre_down_script (void);
HevConfigServer *hev_config_get_socks5_server (void);

int hev_config_get_misc_task_stack_size (void);
int hev_config_get_misc_tcp_buffer_size (void);
int hev_config_get_misc_connect_timeout (void);
int hev_config_get_misc_read_write_timeout (void);
int hev_config_get_misc_limit_nofile (void);
Expand Down
4 changes: 3 additions & 1 deletion src/hev-socks5-session-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ static void
hev_socks5_session_tcp_splice (HevSocks5Session *base)
{
HevSocks5SessionTCP *self = HEV_SOCKS5_SESSION_TCP (base);
int tcp_buffer_size;
int res_f = 1;
int res_b = 1;

Expand All @@ -200,7 +201,8 @@ hev_socks5_session_tcp_splice (HevSocks5Session *base)
if (!self->pcb)
return;

self->buffer = hev_ring_buffer_alloca (tcp_sndbuf (self->pcb));
tcp_buffer_size = hev_config_get_misc_tcp_buffer_size ();
self->buffer = hev_ring_buffer_alloca (tcp_buffer_size);
if (!self->buffer)
return;

Expand Down

0 comments on commit 7d38b54

Please sign in to comment.