summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-03-02 21:03:34 -0500
committerirungentoo <irungentoo@gmail.com>2015-03-02 21:03:34 -0500
commit7a82565c8c6f1ddb3de083d9676a593a165d4c4f (patch)
tree906bd287ee514a794a6464f284d3b2fed2d02492 /toxcore/network.c
parent36c3a270fd5fff5e33371a91861db16e5816964a (diff)
parent6e8762b30a17b515deb74c62a3a98db4206d363e (diff)
Merge branch 'port_range_option' of https://github.com/saneki/toxcore into new_api
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c35
1 files changed, 30 insertions, 5 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index deccbb63..1488d980 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -467,6 +467,14 @@ static void at_shutdown(void)
467*/ 467*/
468 468
469/* Initialize networking. 469/* Initialize networking.
470 * Added for reverse compatibility with old new_networking calls.
471 */
472Networking_Core *new_networking(IP ip, uint16_t port)
473{
474 return new_networking_ex(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM));
475}
476
477/* Initialize networking.
470 * Bind to ip and port. 478 * Bind to ip and port.
471 * ip must be in network order EX: 127.0.0.1 = (7F000001). 479 * ip must be in network order EX: 127.0.0.1 = (7F000001).
472 * port is in host byte order (this means don't worry about it). 480 * port is in host byte order (this means don't worry about it).
@@ -474,8 +482,25 @@ static void at_shutdown(void)
474 * return Networking_Core object if no problems 482 * return Networking_Core object if no problems
475 * return NULL if there are problems. 483 * return NULL if there are problems.
476 */ 484 */
477Networking_Core *new_networking(IP ip, uint16_t port) 485Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to)
478{ 486{
487 /* If both from and to are 0, use default port range
488 * If one is 0 and the other is non-0, use the non-0 value as only port
489 * If from > to, swap
490 */
491 if (port_from == 0 && port_to == 0) {
492 port_from = TOX_PORTRANGE_FROM;
493 port_to = TOX_PORTRANGE_TO;
494 } else if (port_from == 0 && port_to != 0) {
495 port_from = port_to;
496 } else if (port_from != 0 && port_to == 0) {
497 port_to = port_from;
498 } else if (port_from > port_to) {
499 uint16_t temp = port_from;
500 port_from = port_to;
501 port_to = temp;
502 }
503
479 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 504 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
480 if (ip.family != AF_INET && ip.family != AF_INET6) { 505 if (ip.family != AF_INET && ip.family != AF_INET6) {
481#ifdef DEBUG 506#ifdef DEBUG
@@ -600,11 +625,11 @@ Networking_Core *new_networking(IP ip, uint16_t port)
600 * some clients might not test return of tox_new(), blindly assuming that 625 * some clients might not test return of tox_new(), blindly assuming that
601 * it worked ok (which it did previously without a successful bind) 626 * it worked ok (which it did previously without a successful bind)
602 */ 627 */
603 uint16_t port_to_try = port; 628 uint16_t port_to_try = port_from;
604 *portptr = htons(port_to_try); 629 *portptr = htons(port_to_try);
605 int tries; 630 int tries;
606 631
607 for (tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) { 632 for (tries = port_from; tries <= port_to; tries++) {
608 int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); 633 int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize);
609 634
610 if (!res) { 635 if (!res) {
@@ -623,8 +648,8 @@ Networking_Core *new_networking(IP ip, uint16_t port)
623 648
624 port_to_try++; 649 port_to_try++;
625 650
626 if (port_to_try > TOX_PORTRANGE_TO) 651 if (port_to_try > port_to)
627 port_to_try = TOX_PORTRANGE_FROM; 652 port_to_try = port_from;
628 653
629 *portptr = htons(port_to_try); 654 *portptr = htons(port_to_try);
630 } 655 }