Merge branch 'nat-traversal' of https://github.com/Ansa89/toxcore into new

This commit is contained in:
irungentoo 2016-06-04 15:21:35 -04:00
commit c7399d06a9
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
15 changed files with 531 additions and 11 deletions

View File

@ -9,7 +9,7 @@ before_script:
- sudo add-apt-repository ppa:avsm/ocaml42+opam12 -y
- sudo apt-get update -qq
- sudo apt-get install ocaml opam astyle -qq
- sudo apt-get install libconfig-dev libvpx-dev libopus-dev check -qq
- sudo apt-get install libconfig-dev libvpx-dev libopus-dev libminiupnpc-dev libnatpmp-dev check -qq
# build apidsl
- git clone https://github.com/iphydf/apidsl
- cd apidsl
@ -20,9 +20,9 @@ before_script:
- make -j3
- cd ..
# install sodium, as it's not in Ubuntu Trusty
- git clone git://github.com/jedisct1/libsodium.git
- git clone https://github.com/jedisct1/libsodium.git
- cd libsodium
- git checkout tags/1.0.8
- git checkout tags/1.0.10
- ./autogen.sh
- ./configure
- make -j3

View File

@ -41,6 +41,8 @@ LIBCONFIG_FOUND="no"
LIBCHECK_FOUND="no"
WANT_NACL="no"
ADD_NACL_OBJECTS_TO_PKGCONFIG="yes"
MINIUPNP="yes"
NATPMP="yes"
TOXCORE_LT_LDFLAGS="-version-info $LIBTOXCORE_LT_VERSION"
TOXAV_LT_LDFLAGS="-version-info $LIBTOXAV_LT_VERSION"
@ -69,6 +71,28 @@ AC_ARG_ENABLE([nacl],
]
)
AC_ARG_ENABLE([miniupnp],
[AC_HELP_STRING([--disable-miniupnp], [upnp port forwarding (default: enabled)]) ],
[
if test "x$enableval" = "xyes"; then
MINIUPNP="yes"
elif test "x$enableval" = "xno"; then
MINIUPNP="no"
fi
]
)
AC_ARG_ENABLE([natpmp],
[AC_HELP_STRING([--disable-natpmp], [nat-pmp port forwarding (default: enabled)]) ],
[
if test "x$enableval" = "xyes"; then
NATPMP="yes"
elif test "x$enableval" = "xno"; then
NATPMP="no"
fi
]
)
AC_ARG_ENABLE([randombytes-stir],
[AC_HELP_STRING([--enable-randombytes-stir], [use randombytes_stir() instead of sodium_init() for faster startup on android (default: disabled)]) ],
[
@ -227,6 +251,10 @@ LIBSODIUM_SEARCH_HEADERS=
LIBSODIUM_SEARCH_LIBS=
NACL_SEARCH_HEADERS=
NACL_SEARCH_LIBS=
MINIUPNP_SEARCH_HEADERS=
MINIUPNP_SEARCH_LIBS=
NATPMP_SEARCH_HEADERS=
NATPMP_SEARCH_LIBS=
AC_ARG_WITH(dependency-search,
AC_HELP_STRING([--with-dependency-search=DIR],
@ -280,6 +308,42 @@ AC_ARG_WITH(libsodium-libs,
]
)
AC_ARG_WITH(miniupnp-headers,
AC_HELP_STRING([--with-miniupnp-headers=DIR],
[search for miniupnp header files in DIR]),
[
MINIUPNP_SEARCH_HEADERS="$withval"
AC_MSG_NOTICE([will search for miniupnp header files in $withval])
]
)
AC_ARG_WITH(miniupnp-libs,
AC_HELP_STRING([--with-miniupnp-libs=DIR],
[search for miniupnp libraries in DIR]),
[
MINIUPNP_SEARCH_LIBS="$withval"
AC_MSG_NOTICE([will search for miniupnp libraries in $withval])
]
)
AC_ARG_WITH(natpmp-headers,
AC_HELP_STRING([--with-natpmp-headers=DIR],
[search for natpmp header files in DIR]),
[
NATPMP_SEARCH_HEADERS="$withval"
AC_MSG_NOTICE([will search for natpmp header files in $withval])
]
)
AC_ARG_WITH(natpmp-libs,
AC_HELP_STRING([--with-natpmp-libs=DIR],
[search for natpmp libraries in DIR]),
[
NATPMP_SEARCH_LIBS="$withval"
AC_MSG_NOTICE([will search for natpmp libraries in $withval])
]
)
if test "x$WANT_NACL" = "xyes"; then
enable_shared=no
enable_static=yes
@ -397,6 +461,66 @@ else
AC_SUBST(LIBSODIUM_LDFLAGS)
fi
if test "x$MINIUPNP" = "xyes"; then
MINIUPNP_LIBS=
MINIUPNP_LDFLAGS=
LDFLAGS_SAVE="$LDFLAGS"
if test -n "$MINIUPNP_SEARCH_LIBS"; then
LDFLAGS="-L$MINIUPNP_SEARCH_LIBS $LDFLAGS"
AC_CHECK_LIB(miniupnpc, upnpDiscover,
[
MINIUPNP_LDFLAGS="-L$MINIUPNP_SEARCH_LIBS"
MINIUPNP_LIBS="-lminiupnpc"
],
[
AC_MSG_ERROR([required library libminiupnpc was not found in requested location $MINIUPNP_SEARCH_LIBS])
]
)
else
AC_CHECK_LIB(miniupnpc, upnpDiscover,
[],
[
AC_MSG_ERROR([required library libminiupnpc was not found on your system, please check http://miniupnp.free.fr/])
]
)
fi
LDFLAGS="$LDFLAGS_SAVE"
AC_SUBST(MINIUPNP_LIBS)
AC_SUBST(MINIUPNP_LDFLAGS)
fi
if test "x$NATPMP" = "xyes"; then
NATPMP_LIBS=
NATPMP_LDFLAGS=
LDFLAGS_SAVE="$LDFLAGS"
if test -n "$NATPMP_SEARCH_LIBS"; then
LDFLAGS="-L$NATPMP_SEARCH_LIBS $LDFLAGS"
AC_CHECK_LIB(natpmp, initnatpmp
[
NATPMP_LDFLAGS="-L$NATPMP_SEARCH_LIBS"
NATPMP_LIBS="-lnatpmp"
],
[
AC_MSG_ERROR([required library libnatpmp was not found in requested location $NATPMP_SEARCH_LIBS])
]
)
else
AC_CHECK_LIB(natpmp, initnatpmp,
[],
[
AC_MSG_ERROR([required library libnatpmp was not found on your system, please check http://miniupnp.free.fr/])
]
)
fi
LDFLAGS="$LDFLAGS_SAVE"
AC_SUBST(NATPMP_LIBS)
AC_SUBST(NATPMP_LDFLAGS)
fi
# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h])
@ -456,6 +580,62 @@ else
AC_SUBST(LIBSODIUM_CFLAGS)
fi
if test "x$MINIUPNP" = "xyes"; then
MINIUPNP_CFLAGS=
CFLAGS_SAVE="$CFLAGS"
CPPFLAGS_SAVE="$CPPFLAGS"
if test -n "$MINIUPNP_SEARCH_HEADERS"; then
CFLAGS="-I$MINIUPNP_SEARCH_HEADERS $CFLAGS"
CPPFLAGS="-I$MINIUPNP_SEARCH_HEADERS $CPPFLAGS"
AC_CHECK_HEADERS([miniupnpc/miniupnpc.h miniupnpc/miniwget.h miniupnpc/upnpcommands.h miniupnpc/upnperrors.h],
[
MINIUPNP_CFLAGS="-I$MINIUPNP_SEARCH_HEADERS"
],
[
AC_MSG_ERROR([header files for required library libminiupnpc were not found in requested location $MINIUPNP_SEARCH_HEADERS])
]
)
else
AC_CHECK_HEADERS([miniupnpc/miniupnpc.h miniupnpc/miniwget.h miniupnpc/upnpcommands.h miniupnpc/upnperrors.h],
[],
[
AC_MSG_ERROR([header files for required library libminiupnpc was not found on your system, please check http://miniupnp.free.fr/])
]
)
fi
CFLAGS="$CFLAGS_SAVE"
CPPFLAGS="$CPPFLAGS_SAVE"
AC_SUBST(MINIUPNP_CFLAGS)
fi
if test "x$NATPMP" = "xyes"; then
NATPMP_CFLAGS=
CFLAGS_SAVE="$CFLAGS"
CPPFLAGS_SAVE="$CPPFLAGS"
if test -n "$NATPMP_SEARCH_HEADERS"; then
CFLAGS="-I$NATPMP_SEARCH_HEADERS $CFLAGS"
CPPFLAGS="-I$NATPMP_SEARCH_HEADERS $CPPFLAGS"
AC_CHECK_HEADERS(natpmp.h,
[
NATPMP_CFLAGS="-I$NATPMP_SEARCH_HEADERS"
],
[
AC_MSG_ERROR([header files for required library libnatpmp were not found in requested location $NATPMP_SEARCH_HEADERS])
]
)
else
AC_CHECK_HEADERS(natpmp.h,
[],
[
AC_MSG_ERROR([header files for required library libnatpmp was not found on your system, please check http://miniupnp.free.fr/])
]
)
fi
CFLAGS="$CFLAGS_SAVE"
CPPFLAGS="$CPPFLAGS_SAVE"
AC_SUBST(NATPMP_CFLAGS)
fi
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_TYPE_INT16_T

View File

@ -7,5 +7,5 @@ Name: libtoxcore
Description: Tox protocol library
Requires:
Version: @PACKAGE_VERSION@
Libs: @NACL_OBJECTS_PKGCONFIG@ -L${libdir} @NACL_LDFLAGS@ -ltoxdns -ltoxencryptsave -ltoxcore @NACL_LIBS@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@
Libs: @NACL_OBJECTS_PKGCONFIG@ -L${libdir} @NACL_LDFLAGS@ -ltoxdns -ltoxencryptsave -ltoxcore @NACL_LIBS@ @MINIUPNP_LIBS@ @NATPMP_LIBS@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@
Cflags: -I${includedir}

View File

@ -348,6 +348,28 @@ enum class PROXY_TYPE {
SOCKS5,
}
/**
* Type of technology used to try to traverse a NAT.
*/
enum class TRAVERSAL_TYPE {
/**
* Don't use any particular technology.
*/
NONE,
/**
* Use UPnP technology.
*/
UPNP,
/**
* Use NAT-PMP technology.
*/
NATPMP,
/**
* Use both UPnP and NAT-PMP technologies.
*/
ALL,
}
/**
* Type of savedata to create the Tox instance from.
*/
@ -393,6 +415,11 @@ static class options {
*/
bool udp_enabled;
/**
* Try to traverse a NAT.
*/
TRAVERSAL_TYPE traversal_type;
namespace proxy {
/**
* Pass communications through a proxy.

View File

@ -8,6 +8,8 @@ libtoxcore_la_includedir = $(includedir)/tox
libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/DHT.c \
../toxcore/nat_traversal.h \
../toxcore/nat_traversal.c \
../toxcore/network.h \
../toxcore/network.c \
../toxcore/crypto_core.h \
@ -55,12 +57,16 @@ libtoxcore_la_CFLAGS = -I$(top_srcdir) \
-I$(top_srcdir)/toxcore \
$(LIBSODIUM_CFLAGS) \
$(NACL_CFLAGS) \
$(MINIUPNP_CFLAGS) \
$(NATPMP_CFLAGS) \
$(PTHREAD_CFLAGS)
libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
$(EXTRA_LT_LDFLAGS) \
$(LIBSODIUM_LDFLAGS) \
$(NACL_LDFLAGS) \
$(MINIUPNP_LDFLAGS) \
$(NATPMP_LDFLAGS) \
$(MATH_LDFLAGS) \
$(RT_LIBS) \
$(WINSOCK2_LIBS)
@ -68,4 +74,6 @@ libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
libtoxcore_la_LIBADD = $(LIBSODIUM_LIBS) \
$(NACL_OBJECTS) \
$(NAC_LIBS) \
$(MINIUPNP_LIBS) \
$(NATPMP_LIBS) \
$(PTHREAD_LIBS)

View File

@ -1765,7 +1765,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
} else {
IP ip;
ip_init(&ip, options->ipv6enabled);
m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1], &net_err);
m->net = new_networking_nat(ip, options->port_range[0], options->port_range[1], options->traversal_type, &net_err);
}
if (m->net == NULL) {
@ -1813,7 +1813,8 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
}
if (options->tcp_server_port) {
m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_secret_key, m->onion);
m->tcp_server = new_TCP_server_nat(options->ipv6enabled, 1, &options->tcp_server_port, options->traversal_type,
m->dht->self_secret_key, m->onion);
if (m->tcp_server == NULL) {
kill_friend_connections(m->fr_c);

View File

@ -26,6 +26,7 @@
#ifndef MESSENGER_H
#define MESSENGER_H
#include "tox.h"
#include "friend_requests.h"
#include "friend_connection.h"
@ -68,6 +69,7 @@ enum {
typedef struct {
uint8_t ipv6enabled;
uint8_t udp_disabled;
TOX_TRAVERSAL_TYPE traversal_type;
TCP_Proxy_Info proxy_info;
uint16_t port_range[2];
uint16_t tcp_server_port;

View File

@ -30,7 +30,9 @@
#include <sys/ioctl.h>
#endif
#include <arpa/inet.h>
#include "util.h"
#include "nat_traversal.h"
/* return 1 on success
* return 0 on failure
@ -939,8 +941,18 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port)
return sock;
}
/**
* Added for reverse compatibility with old new_TCP_server calls.
*/
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
Onion *onion)
{
return new_TCP_server_nat(ipv6_enabled, num_sockets, ports, TOX_TRAVERSAL_TYPE_NONE, secret_key, onion);
}
TCP_Server *new_TCP_server_nat(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
TOX_TRAVERSAL_TYPE traversal_type,
const uint8_t *secret_key, Onion *onion)
{
if (num_sockets == 0 || ports == NULL)
return NULL;
@ -997,6 +1009,20 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
continue;
}
#endif
#ifdef HAVE_LIBMINIUPNPC
if ((traversal_type == TOX_TRAVERSAL_TYPE_UPNP) || (traversal_type == TOX_TRAVERSAL_TYPE_ALL))
upnp_map_port(NAT_TRAVERSAL_TCP, ntohs(ports[i]));
#endif
#ifdef HAVE_LIBNATPMP
if ((traversal_type == TOX_TRAVERSAL_TYPE_NATPMP) || (traversal_type == TOX_TRAVERSAL_TYPE_ALL))
natpmp_map_port(NAT_TRAVERSAL_TCP, ntohs(ports[i]));
#endif
temp->socks_listening[temp->num_listening_socks] = sock;

View File

@ -23,6 +23,7 @@
#ifndef TCP_SERVER_H
#define TCP_SERVER_H
#include "tox.h"
#include "crypto_core.h"
#include "onion.h"
#include "list.h"
@ -142,10 +143,17 @@ typedef struct {
} TCP_Server;
/* Create new TCP server instance.
* Added for reverse compatibility with old new_TCP_server calls.
*/
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
Onion *onion);
/* Create new TCP server instance.
*/
TCP_Server *new_TCP_server_nat(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
TOX_TRAVERSAL_TYPE traversal_type,
const uint8_t *secret_key, Onion *onion);
/* Run the TCP_server
*/
void do_TCP_server(TCP_Server *TCP_server);

148
toxcore/nat_traversal.c Normal file
View File

@ -0,0 +1,148 @@
/* nat_traversal.c -- Functions to traverse a NAT (UPnP, NAT-PMP).
*
* Copyright (C) 2014 Tox project All Rights Reserved.
*
* This file is part of Tox.
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIBMINIUPNPC
#include <stdio.h>
#include <miniupnpc/miniupnpc.h>
#include <miniupnpc/miniwget.h>
#include <miniupnpc/upnpcommands.h>
#include <miniupnpc/upnperrors.h>
#endif
#ifdef HAVE_LIBNATPMP
#include <unistd.h>
#include <natpmp.h>
#endif
#include "nat_traversal.h"
#include "logger.h"
#ifdef HAVE_LIBMINIUPNPC
/* Setup port forwarding using UPnP */
void upnp_map_port(NAT_TRAVERSAL_PROTO proto, uint16_t port)
{
LOGGER_DEBUG("Attempting to set up UPnP port forwarding");
int error = 0;
struct UPNPDev *devlist = NULL;
#if MINIUPNPC_API_VERSION < 14
devlist = upnpDiscover(1000, NULL, NULL, 0, 0, &error);
#else
devlist = upnpDiscover(1000, NULL, NULL, 0, 0, 2, &error);
#endif
if (error) {
LOGGER_WARNING("UPnP discovery failed (error = %d)", error);
return;
}
struct UPNPUrls urls;
struct IGDdatas data;
char lanaddr[64];
error = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
freeUPNPDevlist(devlist);
if (error) {
if (error == 1) {
LOGGER_INFO("A valid IGD has been found.");
char portstr[10];
snprintf(portstr, sizeof(portstr), "%d", port);
if (proto == NAT_TRAVERSAL_UDP)
error = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, portstr, portstr, lanaddr, "Tox", "UDP", 0, "0");
else if (proto == NAT_TRAVERSAL_TCP)
error = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, portstr, portstr, lanaddr, "Tox", "TCP", 0, "0");
else
LOGGER_WARNING("UPnP port mapping failed (unknown NAT_TRAVERSAL_PROTO)");
if (error)
LOGGER_WARNING("UPnP port mapping failed (error = %d)", error);
else
LOGGER_INFO("UPnP mapped port %d", port);
} else if (error == 2)
LOGGER_WARNING("IGD was found but reported as not connected.");
else if (error == 3)
LOGGER_WARNING("UPnP device was found but not recoginzed as IGD.");
else
LOGGER_WARNING("Unknown error finding IGD: %d", error);
FreeUPNPUrls(&urls);
} else
LOGGER_WARNING("No IGD was found.");
}
#endif
#ifdef HAVE_LIBNATPMP
/* Setup port forwarding using NAT-PMP */
void natpmp_map_port(NAT_TRAVERSAL_PROTO proto, uint16_t port)
{
LOGGER_DEBUG("Attempting to set up NAT-PMP port forwarding");
int error;
natpmp_t natpmp;
natpmpresp_t resp;
error = initnatpmp(&natpmp, 0, 0);
if (error) {
LOGGER_WARNING("NAT-PMP initialization failed (error = %d)", error);
return;
}
if (proto == NAT_TRAVERSAL_UDP)
error = sendnewportmappingrequest(&natpmp, NATPMP_PROTOCOL_UDP, port, port, 3600);
else if (proto == NAT_TRAVERSAL_TCP)
error = sendnewportmappingrequest(&natpmp, NATPMP_PROTOCOL_TCP, port, port, 3600);
else {
LOGGER_WARNING("NAT-PMP port mapping failed (unknown NAT_TRAVERSAL_PROTO)");
closenatpmp(&natpmp);
return;
}
if (error != 12) {
LOGGER_WARNING("NAT-PMP send request failed (error = %d)", error);
closenatpmp(&natpmp);
return;
}
error = readnatpmpresponseorretry(&natpmp, &resp);
for ( ; error == NATPMP_TRYAGAIN ; error = readnatpmpresponseorretry(&natpmp, &resp) )
sleep(1);
if (error)
LOGGER_WARNING("NAT-PMP port mapping failed (error = %d)", error);
else
LOGGER_INFO("NAT-PMP mapped port %d", port);
closenatpmp(&natpmp);
}
#endif

57
toxcore/nat_traversal.h Normal file
View File

@ -0,0 +1,57 @@
/* nat_traversal.h -- Functions to traverse a NAT (UPnP, NAT-PMP).
*
* Copyright (C) 2014 Tox project All Rights Reserved.
*
* This file is part of Tox.
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef NAT_TRAVERSAL_H
#define NAT_TRAVERSAL_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
/**
* The protocol that will be used by the nat traversal.
*/
#if defined(HAVE_LIBMINIUPNPC) || defined(HAVE_LIBNATPMP)
typedef enum NAT_TRAVERSAL_PROTO {
/* UDP */
NAT_TRAVERSAL_UDP,
/* TCP */
NAT_TRAVERSAL_TCP,
} NAT_TRAVERSAL_PROTO;
#endif
#ifdef HAVE_LIBMINIUPNPC
/* Setup port forwarding using UPnP */
void upnp_map_port(NAT_TRAVERSAL_PROTO proto, uint16_t port);
#endif
#ifdef HAVE_LIBNATPMP
/* Setup port forwarding using NAT-PMP */
void natpmp_map_port(NAT_TRAVERSAL_PROTO proto, uint16_t port);
#endif
#endif

View File

@ -42,6 +42,7 @@
#include "network.h"
#include "util.h"
#include "nat_traversal.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@ -482,7 +483,15 @@ static void at_shutdown(void)
*/
Networking_Core *new_networking(IP ip, uint16_t port)
{
return new_networking_ex(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), 0);
return new_networking_nat(ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), TOX_TRAVERSAL_TYPE_NONE, 0);
}
/* Initialize networking.
* Added for reverse compatibility with old new_networking_ex calls.
*/
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error)
{
return new_networking_nat(ip, port_from, port_to, TOX_TRAVERSAL_TYPE_NONE, 0);
}
/* Initialize networking.
@ -495,7 +504,8 @@ Networking_Core *new_networking(IP ip, uint16_t port)
*
* If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other.
*/
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error)
Networking_Core *new_networking_nat(IP ip, uint16_t port_from, uint16_t port_to, TOX_TRAVERSAL_TYPE traversal_type,
unsigned int *error)
{
/* 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
@ -674,6 +684,20 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to,
if (error)
*error = 0;
#ifdef HAVE_LIBMINIUPNPC
if ((traversal_type == TOX_TRAVERSAL_TYPE_UPNP) || (traversal_type == TOX_TRAVERSAL_TYPE_ALL))
upnp_map_port(NAT_TRAVERSAL_UDP, ntohs(temp->port));
#endif
#ifdef HAVE_LIBNATPMP
if ((traversal_type == TOX_TRAVERSAL_TYPE_NATPMP) || (traversal_type == TOX_TRAVERSAL_TYPE_ALL))
natpmp_map_port(NAT_TRAVERSAL_UDP, ntohs(temp->port));
#endif
return temp;
}

View File

@ -34,6 +34,7 @@
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "tox.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) /* Put win32 includes here */
#ifndef WINVER
@ -378,6 +379,8 @@ void networking_poll(Networking_Core *net);
*/
Networking_Core *new_networking(IP ip, uint16_t port);
Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error);
Networking_Core *new_networking_nat(IP ip, uint16_t port_from, uint16_t port_to, TOX_TRAVERSAL_TYPE traversal_type,
unsigned int *error);
/* Function to cleanup networking stuff (doesn't do much right now). */
void kill_networking(Networking_Core *net);

View File

@ -25,15 +25,15 @@
#include "config.h"
#endif
#define TOX_DEFINED
typedef struct Messenger Tox;
#include "Messenger.h"
#include "group.h"
#include "logger.h"
#include "../toxencryptsave/defines.h"
#define TOX_DEFINED
typedef struct Messenger Tox;
#include "tox.h"
#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}}
@ -101,6 +101,7 @@ void tox_options_default(struct Tox_Options *options)
memset(options, 0, sizeof(struct Tox_Options));
options->ipv6_enabled = 1;
options->udp_enabled = 1;
options->traversal_type = TOX_TRAVERSAL_TYPE_NONE;
options->proxy_type = TOX_PROXY_TYPE_NONE;
}
}
@ -166,6 +167,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error)
m_options.ipv6enabled = options->ipv6_enabled;
m_options.udp_disabled = !options->udp_enabled;
m_options.traversal_type = options->traversal_type;
m_options.port_range[0] = options->start_port;
m_options.port_range[1] = options->end_port;
m_options.tcp_server_port = options->tcp_port;

View File

@ -360,6 +360,34 @@ typedef enum TOX_PROXY_TYPE {
} TOX_PROXY_TYPE;
/**
* Type of technology used to try to traverse a NAT.
*/
typedef enum TOX_TRAVERSAL_TYPE {
/**
* Don't use any particular technology.
*/
TOX_TRAVERSAL_TYPE_NONE,
/**
* Use UPnP technology.
*/
TOX_TRAVERSAL_TYPE_UPNP,
/**
* Use NAT-PMP technology.
*/
TOX_TRAVERSAL_TYPE_NATPMP,
/**
* Use both UPnP and NAT-PMP technologies.
*/
TOX_TRAVERSAL_TYPE_ALL,
} TOX_TRAVERSAL_TYPE;
/**
* Type of savedata to create the Tox instance from.
*/
@ -411,6 +439,12 @@ struct Tox_Options {
bool udp_enabled;
/**
* Try to traverse a NAT.
*/
TOX_TRAVERSAL_TYPE traversal_type;
/**
* Pass communications through a proxy.
*/