mirror of
https://github.com/status-im/miniupnp.git
synced 2025-02-21 10:19:13 +00:00
miniupnpd: AddAnyPortMapping() tries port above and below requested port
fixes #465 if the requested port is n, it will tries successively : n, n+1, n-1, n+2, n-2, n+3, n-3, etc.
This commit is contained in:
parent
1e7fb305b6
commit
de71eef493
@ -586,7 +586,7 @@ AddAnyPortMapping(struct upnphttp * h, const char * action, const char * ns)
|
|||||||
struct NameValueParserData data;
|
struct NameValueParserData data;
|
||||||
const char * int_ip, * int_port, * ext_port, * protocol, * desc;
|
const char * int_ip, * int_port, * ext_port, * protocol, * desc;
|
||||||
const char * r_host;
|
const char * r_host;
|
||||||
unsigned short iport, eport;
|
unsigned short iport, eport, eport_below, eport_above;
|
||||||
const char * leaseduration_str;
|
const char * leaseduration_str;
|
||||||
unsigned int leaseduration;
|
unsigned int leaseduration;
|
||||||
|
|
||||||
@ -669,22 +669,37 @@ AddAnyPortMapping(struct upnphttp * h, const char * action, const char * ns)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO : accept a different external port
|
/* first try the port asked in request, then
|
||||||
* have some smart strategy to choose the port */
|
* try +1, -1, +2, -2, etc. */
|
||||||
|
eport_above = eport_below = eport;
|
||||||
for(;;) {
|
for(;;) {
|
||||||
r = upnp_redirect(r_host, eport, int_ip, iport, protocol, desc, leaseduration);
|
r = upnp_redirect(r_host, eport, int_ip, iport, protocol, desc, leaseduration);
|
||||||
if((r==-2 || r==-3) && eport < 65535) {
|
if (r == 0 || r == -1) {
|
||||||
/* next port if -2 already redirected or -3 permission check failed */
|
/* OK or failure : Stop */
|
||||||
eport++;
|
|
||||||
} else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
/* r : -2 / -4 already redirected or -3 permission check failed */
|
||||||
|
if (eport_below <= 1 && eport_above == 65535) {
|
||||||
|
/* all possible ports tried */
|
||||||
|
r = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (eport_above == 65535 || (eport > eport_below && eport_below > 1)) {
|
||||||
|
eport = --eport_below;
|
||||||
|
} else {
|
||||||
|
eport = ++eport_above;
|
||||||
|
}
|
||||||
|
/* loop invariant :
|
||||||
|
* eport is equal to either eport_below or eport_above (or both) */
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearNameValueList(&data);
|
ClearNameValueList(&data);
|
||||||
|
|
||||||
switch(r)
|
switch(r)
|
||||||
{
|
{
|
||||||
|
case 1: /* exhausted possible mappings */
|
||||||
|
SoapError(h, 728, "NoPortMapsAvailable");
|
||||||
|
break;
|
||||||
case 0: /* success */
|
case 0: /* success */
|
||||||
bodylen = snprintf(body, sizeof(body), resp,
|
bodylen = snprintf(body, sizeof(body), resp,
|
||||||
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
action, ns, /*SERVICE_TYPE_WANIPC,*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user