summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/Messenger.h1
-rw-r--r--toxcore/network.c38
-rw-r--r--toxcore/network.h1
-rw-r--r--toxcore/tox.c2
-rw-r--r--toxcore/tox.h19
6 files changed, 57 insertions, 6 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index aa7b05bc..544b8b0d 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 f4046f2e..4659ddf9 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..9433e368 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,28 @@ 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 }
495 else if(port_from == 0 && port_to != 0) {
496 port_from = port_to;
497 }
498 else if(port_from != 0 && port_to == 0) {
499 port_to = port_from;
500 }
501 else if(port_from > port_to) {
502 uint16_t temp = port_from;
503 port_from = port_to;
504 port_to = temp;
505 }
506
479 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 507 /* 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) { 508 if (ip.family != AF_INET && ip.family != AF_INET6) {
481#ifdef DEBUG 509#ifdef DEBUG
@@ -600,11 +628,11 @@ Networking_Core *new_networking(IP ip, uint16_t port)
600 * some clients might not test return of tox_new(), blindly assuming that 628 * some clients might not test return of tox_new(), blindly assuming that
601 * it worked ok (which it did previously without a successful bind) 629 * it worked ok (which it did previously without a successful bind)
602 */ 630 */
603 uint16_t port_to_try = port; 631 uint16_t port_to_try = port_from;
604 *portptr = htons(port_to_try); 632 *portptr = htons(port_to_try);
605 int tries; 633 int tries;
606 634
607 for (tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) { 635 for (tries = port_from; tries <= port_to; tries++) {
608 int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); 636 int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize);
609 637
610 if (!res) { 638 if (!res) {
@@ -623,8 +651,8 @@ Networking_Core *new_networking(IP ip, uint16_t port)
623 651
624 port_to_try++; 652 port_to_try++;
625 653
626 if (port_to_try > TOX_PORTRANGE_TO) 654 if (port_to_try > port_to)
627 port_to_try = TOX_PORTRANGE_FROM; 655 port_to_try = port_from;
628 656
629 *portptr = htons(port_to_try); 657 *portptr = htons(port_to_try);
630 } 658 }
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 2acc70ea..a66bcb17 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