Skip to content

Commit

Permalink
HevConf: Add support for custom timeouts for port forwarding.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Apr 8, 2024
1 parent 8702604 commit d2183a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Bind options:
-b <port> port number for binding
Forward options:
-T <timeout> port forwarding timeout in seconds
-t <address> domain name or address to forward target
-p <port> port number to forward target (0: use public port)
```
Expand Down
17 changes: 16 additions & 1 deletion src/hev-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static int mode = SOCK_STREAM;
static int type = AF_UNSPEC;
static int keep;
static int dmon;
static int tmsec;

static char mport[16];
static char sport[16] = "3478";
Expand Down Expand Up @@ -56,6 +57,7 @@ hev_conf_help (void)
" -b <port> port number for binding\n"
"\n"
"Forward options:\n"
" -T <timeout> port forwarding timeout in seconds\n"
" -t <address> domain name or address to forward target\n"
" -p <port> port number to forward target (0: use public port)\n";

Expand All @@ -67,7 +69,7 @@ hev_conf_init (int argc, char *argv[])
{
int opt;

while ((opt = getopt (argc, argv, "46udk:s:h:e:b:t:p:i:")) != -1) {
while ((opt = getopt (argc, argv, "46udk:s:h:e:b:T:t:p:i:")) != -1) {
switch (opt) {
case '4':
type = AF_INET;
Expand Down Expand Up @@ -96,6 +98,9 @@ hev_conf_init (int argc, char *argv[])
case 'b':
bport = optarg;
break;
case 'T':
tmsec = strtoul (optarg, NULL, 10) * 1000;
break;
case 't':
taddr = optarg;
break;
Expand Down Expand Up @@ -133,6 +138,10 @@ hev_conf_init (int argc, char *argv[])

baddr = (type == AF_INET6) ? "::" : "0.0.0.0";

if (tmsec <= 0) {
tmsec = 120000;
}

return 0;
}

Expand Down Expand Up @@ -196,6 +205,12 @@ hev_conf_tport (void)
return tport;
}

int
hev_conf_tmsec (void)
{
return tmsec;
}

const char *
hev_conf_mport (int port)
{
Expand Down
9 changes: 9 additions & 0 deletions src/hev-conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ const char *hev_conf_taddr (void);
*/
const char *hev_conf_tport (void);

/**
* hev_conf_tmsec:
*
* Get timeout millseconds for port forwarding.
*
* Returns: returns integer number.
*/
int hev_conf_tmsec (void);

/**
* hev_conf_mport:
* @port: port number
Expand Down
3 changes: 2 additions & 1 deletion src/hev-tfwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ yielder (HevTaskYieldType type, void *data)
static void
client_task_entry (void *data)
{
int timeout = 120000;
const char *addr;
const char *port;
int timeout;
int mode;
int sfd;
int dfd;
Expand All @@ -47,6 +47,7 @@ client_task_entry (void *data)
mode = hev_conf_mode ();
addr = hev_conf_taddr ();
port = hev_conf_tport ();
timeout = hev_conf_tmsec ();

if (strtoul (port, NULL, 10) == 0)
port = hev_conf_mport (-1);
Expand Down
3 changes: 2 additions & 1 deletion src/hev-ufwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,14 @@ static void
client_task_entry (void *data)
{
const int bufsize = 2048;
int timeout = 120000;
struct sockaddr *pa;
char buf[bufsize];
Session *s = data;
int timeout;

pa = (struct sockaddr *)&s->addr;
hev_task_add_fd (hev_task_self (), s->fd, POLLIN);
timeout = hev_conf_tmsec ();

for (;;) {
int len;
Expand Down

0 comments on commit d2183a4

Please sign in to comment.