summaryrefslogtreecommitdiff
path: root/toxcore
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
parent36c3a270fd5fff5e33371a91861db16e5816964a (diff)
parent6e8762b30a17b515deb74c62a3a98db4206d363e (diff)
Merge branch 'port_range_option' of https://github.com/saneki/toxcore into new_api
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/Messenger.h1
-rw-r--r--toxcore/network.c35
-rw-r--r--toxcore/network.h1
-rw-r--r--toxcore/tox.c2
-rw-r--r--toxcore/tox.h19
6 files changed, 54 insertions, 6 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 6427d435..f6c37ac4 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1484,7 +1484,7 @@ Messenger *new_messenger(Messenger_Options *options)
1484 } else { 1484 } else {
1485 IP ip; 1485 IP ip;
1486 ip_init(&ip, options->ipv6enabled); 1486 ip_init(&ip, options->ipv6enabled);
1487 m->net = new_networking(ip, TOX_PORT_DEFAULT); 1487 m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1]);
1488 } 1488 }
1489 1489
1490 if (m->net == NULL) { 1490 if (m->net == NULL) {
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 4aeb780e..541894ab 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -69,6 +69,7 @@ typedef struct {
69 uint8_t ipv6enabled; 69 uint8_t ipv6enabled;
70 uint8_t udp_disabled; 70 uint8_t udp_disabled;
71 TCP_Proxy_Info proxy_info; 71 TCP_Proxy_Info proxy_info;
72 uint16_t port_range[2];
72} Messenger_Options; 73} Messenger_Options;
73 74
74 75
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 }
diff --git a/toxcore/network.h b/toxcore/network.h
index 15e1c0a4..0e7e5948 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -364,6 +364,7 @@ void networking_poll(Networking_Core *net);
364 * return NULL if there are problems. 364 * return NULL if there are problems.
365 */ 365 */
366Networking_Core *new_networking(IP ip, uint16_t port); 366Networking_Core *new_networking(IP ip, uint16_t port);
367Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to);
367 368
368/* Function to cleanup networking stuff (doesn't do much right now). */ 369/* Function to cleanup networking stuff (doesn't do much right now). */
369void kill_networking(Networking_Core *net); 370void kill_networking(Networking_Core *net);
diff --git a/toxcore/tox.c b/toxcore/tox.c
index e95bbefb..fc6ffc2b 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -107,6 +107,8 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng
107 } else { 107 } else {
108 m_options.ipv6enabled = options->ipv6_enabled; 108 m_options.ipv6enabled = options->ipv6_enabled;
109 m_options.udp_disabled = !options->udp_enabled; 109 m_options.udp_disabled = !options->udp_enabled;
110 m_options.port_range[0] = options->start_port;
111 m_options.port_range[1] = options->end_port;
110 112
111 switch (options->proxy_type) { 113 switch (options->proxy_type) {
112 case TOX_PROXY_TYPE_HTTP: 114 case TOX_PROXY_TYPE_HTTP:
diff --git a/toxcore/tox.h b/toxcore/tox.h
index abae26e6..8a489411 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -353,6 +353,25 @@ struct Tox_Options {
353 * proxy_enabled is false. 353 * proxy_enabled is false.
354 */ 354 */
355 uint16_t proxy_port; 355 uint16_t proxy_port;
356
357 /**
358 * The start port of the inclusive port range to attempt to use.
359 *
360 * If both start_port and end_port are 0, the default port range will be
361 * used: [33445, 33545].
362 *
363 * If either start_port or end_port is 0 while the other is non-zero, the
364 * non-zero port will be the only port in the range.
365 *
366 * Having start_port > end_port will yield the same behavior as if start_port
367 * and end_port were swapped.
368 */
369 uint16_t start_port;
370
371 /**
372 * The end port of the inclusive port range to attempt to use.
373 */
374 uint16_t end_port;
356}; 375};
357 376
358 377