From 6e8762b30a17b515deb74c62a3a98db4206d363e Mon Sep 17 00:00:00 2001 From: saneki Date: Fri, 27 Feb 2015 11:58:00 -0600 Subject: Allow for specifying the port range to use in Tox_Options --- toxcore/network.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'toxcore/network.c') diff --git a/toxcore/network.c b/toxcore/network.c index deccbb63..9433e368 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -466,6 +466,14 @@ static void at_shutdown(void) } */ +/* Initialize networking. + * Added for reverse compatibility with old new_networking calls. + */ +Networking_Core *new_networking(IP ip, uint16_t port) +{ + return new_networking_ex(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM)); +} + /* Initialize networking. * Bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). @@ -474,8 +482,28 @@ static void at_shutdown(void) * return Networking_Core object if no problems * return NULL if there are problems. */ -Networking_Core *new_networking(IP ip, uint16_t port) +Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to) { + /* If both from and to are 0, use default port range + * If one is 0 and the other is non-0, use the non-0 value as only port + * If from > to, swap + */ + if(port_from == 0 && port_to == 0) { + port_from = TOX_PORTRANGE_FROM; + port_to = TOX_PORTRANGE_TO; + } + else if(port_from == 0 && port_to != 0) { + port_from = port_to; + } + else if(port_from != 0 && port_to == 0) { + port_to = port_from; + } + else if(port_from > port_to) { + uint16_t temp = port_from; + port_from = port_to; + port_to = temp; + } + /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ if (ip.family != AF_INET && ip.family != AF_INET6) { #ifdef DEBUG @@ -600,11 +628,11 @@ Networking_Core *new_networking(IP ip, uint16_t port) * some clients might not test return of tox_new(), blindly assuming that * it worked ok (which it did previously without a successful bind) */ - uint16_t port_to_try = port; + uint16_t port_to_try = port_from; *portptr = htons(port_to_try); int tries; - for (tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) { + for (tries = port_from; tries <= port_to; tries++) { int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); if (!res) { @@ -623,8 +651,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) port_to_try++; - if (port_to_try > TOX_PORTRANGE_TO) - port_to_try = TOX_PORTRANGE_FROM; + if (port_to_try > port_to) + port_to_try = port_from; *portptr = htons(port_to_try); } -- cgit v1.2.3