Skip to content

Commit ec8d5a5

Browse files
committed
HevConf: Add support for sequential allocation of binding ports within the range.
Fixes #73
1 parent 93f1a83 commit ec8d5a5

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Options:
5151
-f <mark> fwmark value (hex: 0x1, dec: 1, oct: 01)
5252
5353
Bind options:
54-
-b <port> port number for binding
54+
-b <port>[-port] port number range for binding
55+
- <0>: random allocation
56+
- <port>: specified
57+
- <port>-<port>: sequential allocation within the range
5558
5659
Forward options:
5760
-T <timeout> port forwarding timeout in seconds

src/hev-conf.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static int type = AF_INET;
2323
static int keep;
2424
static int dmon;
2525
static int tmsec;
26+
static int bport[3];
2627
static unsigned int mark;
2728

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

3435
static const char *path;
3536
static const char *baddr;
36-
static const char *bport;
3737
static const char *taddr;
3838
static const char *tport;
3939
static const char *iface;
@@ -58,7 +58,10 @@ hev_conf_help (void)
5858
" -f <mark> fwmark value (hex: 0x1, dec: 1, oct: 01)\n"
5959
"\n"
6060
"Bind options:\n"
61-
" -b <port> port number for binding\n"
61+
" -b <port>[-port] port number range for binding\n"
62+
" - <0>: random allocation\n"
63+
" - <port>: specified\n"
64+
" - <port>-<port>: sequential allocation within the range\n"
6265
"\n"
6366
"Forward options:\n"
6467
" -T <timeout> port forwarding timeout in seconds\n"
@@ -104,7 +107,7 @@ hev_conf_init (int argc, char *argv[])
104107
mark = strtoul (optarg, NULL, 0);
105108
break;
106109
case 'b':
107-
bport = optarg;
110+
sscanf (optarg, "%u-%u", &bport[0], &bport[1]);
108111
break;
109112
case 'T':
110113
tmsec = strtoul (optarg, NULL, 10) * 1000;
@@ -140,9 +143,13 @@ hev_conf_init (int argc, char *argv[])
140143
keep *= 1000;
141144
}
142145

143-
if (!bport) {
144-
bport = "0";
146+
if (!bport[0]) {
147+
bport[0] = 0;
145148
}
149+
if (!bport[1]) {
150+
bport[1] = bport[0];
151+
}
152+
bport[2] = bport[0];
146153

147154
if (iface && inet_pton (type, iface, &sa)) {
148155
baddr = iface;
@@ -209,7 +216,16 @@ hev_conf_baddr (void)
209216
const char *
210217
hev_conf_bport (void)
211218
{
212-
return bport;
219+
static char port[16];
220+
221+
snprintf (port, sizeof (port) - 1, "%u", bport[2]);
222+
223+
bport[2] = bport[2] + 1;
224+
if (bport[2] > bport[1]) {
225+
bport[2] = bport[0];
226+
}
227+
228+
return port;
213229
}
214230

215231
const char *

0 commit comments

Comments
 (0)