Merge branch 'port_range_option' of https://github.com/saneki/toxcore into new_api

This commit is contained in:
irungentoo 2015-03-02 21:03:34 -05:00
commit 7a82565c8c
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
6 changed files with 54 additions and 6 deletions

View File

@ -1484,7 +1484,7 @@ Messenger *new_messenger(Messenger_Options *options)
} else { } else {
IP ip; IP ip;
ip_init(&ip, options->ipv6enabled); ip_init(&ip, options->ipv6enabled);
m->net = new_networking(ip, TOX_PORT_DEFAULT); m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1]);
} }
if (m->net == NULL) { if (m->net == NULL) {

View File

@ -69,6 +69,7 @@ typedef struct {
uint8_t ipv6enabled; uint8_t ipv6enabled;
uint8_t udp_disabled; uint8_t udp_disabled;
TCP_Proxy_Info proxy_info; TCP_Proxy_Info proxy_info;
uint16_t port_range[2];
} Messenger_Options; } Messenger_Options;

View File

@ -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. /* Initialize networking.
* Bind to ip and port. * Bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001). * ip must be in network order EX: 127.0.0.1 = (7F000001).
@ -474,8 +482,25 @@ static void at_shutdown(void)
* return Networking_Core object if no problems * return Networking_Core object if no problems
* return NULL if there are 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 */ /* 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) { if (ip.family != AF_INET && ip.family != AF_INET6) {
#ifdef DEBUG #ifdef DEBUG
@ -600,11 +625,11 @@ Networking_Core *new_networking(IP ip, uint16_t port)
* some clients might not test return of tox_new(), blindly assuming that * some clients might not test return of tox_new(), blindly assuming that
* it worked ok (which it did previously without a successful bind) * 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); *portptr = htons(port_to_try);
int tries; 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); int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize);
if (!res) { if (!res) {
@ -623,8 +648,8 @@ Networking_Core *new_networking(IP ip, uint16_t port)
port_to_try++; port_to_try++;
if (port_to_try > TOX_PORTRANGE_TO) if (port_to_try > port_to)
port_to_try = TOX_PORTRANGE_FROM; port_to_try = port_from;
*portptr = htons(port_to_try); *portptr = htons(port_to_try);
} }

View File

@ -364,6 +364,7 @@ void networking_poll(Networking_Core *net);
* return NULL if there are problems. * return NULL if there are problems.
*/ */
Networking_Core *new_networking(IP ip, uint16_t port); Networking_Core *new_networking(IP ip, uint16_t port);
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to);
/* Function to cleanup networking stuff (doesn't do much right now). */ /* Function to cleanup networking stuff (doesn't do much right now). */
void kill_networking(Networking_Core *net); void kill_networking(Networking_Core *net);

View File

@ -107,6 +107,8 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng
} else { } else {
m_options.ipv6enabled = options->ipv6_enabled; m_options.ipv6enabled = options->ipv6_enabled;
m_options.udp_disabled = !options->udp_enabled; m_options.udp_disabled = !options->udp_enabled;
m_options.port_range[0] = options->start_port;
m_options.port_range[1] = options->end_port;
switch (options->proxy_type) { switch (options->proxy_type) {
case TOX_PROXY_TYPE_HTTP: case TOX_PROXY_TYPE_HTTP:

View File

@ -353,6 +353,25 @@ struct Tox_Options {
* proxy_enabled is false. * proxy_enabled is false.
*/ */
uint16_t proxy_port; uint16_t proxy_port;
/**
* The start port of the inclusive port range to attempt to use.
*
* If both start_port and end_port are 0, the default port range will be
* used: [33445, 33545].
*
* If either start_port or end_port is 0 while the other is non-zero, the
* non-zero port will be the only port in the range.
*
* Having start_port > end_port will yield the same behavior as if start_port
* and end_port were swapped.
*/
uint16_t start_port;
/**
* The end port of the inclusive port range to attempt to use.
*/
uint16_t end_port;
}; };