Skip to content

Commit

Permalink
HevConf: Add support for sequential allocation of binding ports withi…
Browse files Browse the repository at this point in the history
…n the range.

Fixes #73
  • Loading branch information
heiher committed Aug 11, 2024
1 parent 93f1a83 commit ec8d5a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ Options:
-f <mark> fwmark value (hex: 0x1, dec: 1, oct: 01)
Bind options:
-b <port> port number for binding
-b <port>[-port] port number range for binding
- <0>: random allocation
- <port>: specified
- <port>-<port>: sequential allocation within the range
Forward options:
-T <timeout> port forwarding timeout in seconds
Expand Down
28 changes: 22 additions & 6 deletions src/hev-conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static int type = AF_INET;
static int keep;
static int dmon;
static int tmsec;
static int bport[3];
static unsigned int mark;

static char mport[16];
Expand All @@ -33,7 +34,6 @@ static char http[256];

static const char *path;
static const char *baddr;
static const char *bport;
static const char *taddr;
static const char *tport;
static const char *iface;
Expand All @@ -58,7 +58,10 @@ hev_conf_help (void)
" -f <mark> fwmark value (hex: 0x1, dec: 1, oct: 01)\n"
"\n"
"Bind options:\n"
" -b <port> port number for binding\n"
" -b <port>[-port] port number range for binding\n"
" - <0>: random allocation\n"
" - <port>: specified\n"
" - <port>-<port>: sequential allocation within the range\n"
"\n"
"Forward options:\n"
" -T <timeout> port forwarding timeout in seconds\n"
Expand Down Expand Up @@ -104,7 +107,7 @@ hev_conf_init (int argc, char *argv[])
mark = strtoul (optarg, NULL, 0);
break;
case 'b':
bport = optarg;
sscanf (optarg, "%u-%u", &bport[0], &bport[1]);
break;
case 'T':
tmsec = strtoul (optarg, NULL, 10) * 1000;
Expand Down Expand Up @@ -140,9 +143,13 @@ hev_conf_init (int argc, char *argv[])
keep *= 1000;
}

if (!bport) {
bport = "0";
if (!bport[0]) {
bport[0] = 0;
}
if (!bport[1]) {
bport[1] = bport[0];
}
bport[2] = bport[0];

if (iface && inet_pton (type, iface, &sa)) {
baddr = iface;
Expand Down Expand Up @@ -209,7 +216,16 @@ hev_conf_baddr (void)
const char *
hev_conf_bport (void)
{
return bport;
static char port[16];

snprintf (port, sizeof (port) - 1, "%u", bport[2]);

bport[2] = bport[2] + 1;
if (bport[2] > bport[1]) {
bport[2] = bport[0];
}

return port;
}

const char *
Expand Down

0 comments on commit ec8d5a5

Please sign in to comment.