HevConf: Add support for custom timeouts for port forwarding.

This commit is contained in:
hev
2024-04-08 21:07:10 +08:00
parent 8702604722
commit d2183a4d58
5 changed files with 30 additions and 3 deletions

View File

@@ -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)
```

View File

@@ -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";
@@ -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";
@@ -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;
@@ -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;
@@ -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;
}
@@ -196,6 +205,12 @@ hev_conf_tport (void)
return tport;
}
int
hev_conf_tmsec (void)
{
return tmsec;
}
const char *
hev_conf_mport (int port)
{

View File

@@ -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

View File

@@ -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;
@@ -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);

View File

@@ -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;