Add UPnP support

This commit is contained in:
Ansa89 2016-03-04 18:01:20 +01:00
parent 532629d486
commit 08be91035e
5 changed files with 156 additions and 2 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 check -qq
# build apidsl
- git clone https://github.com/iphydf/apidsl
- cd apidsl

View File

@ -41,6 +41,7 @@ LIBCONFIG_FOUND="no"
LIBCHECK_FOUND="no"
WANT_NACL="no"
ADD_NACL_OBJECTS_TO_PKGCONFIG="yes"
MINIUPNP="yes"
TOXCORE_LT_LDFLAGS="-version-info $LIBTOXCORE_LT_VERSION"
TOXAV_LT_LDFLAGS="-version-info $LIBTOXAV_LT_VERSION"
@ -69,6 +70,17 @@ 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([randombytes-stir],
[AC_HELP_STRING([--enable-randombytes-stir], [use randombytes_stir() instead of sodium_init() for faster startup on android (default: disabled)]) ],
[
@ -227,6 +239,8 @@ LIBSODIUM_SEARCH_HEADERS=
LIBSODIUM_SEARCH_LIBS=
NACL_SEARCH_HEADERS=
NACL_SEARCH_LIBS=
MINIUPNP_SEARCH_HEADERS=
MINIUPNP_SEARCH_LIBS=
AC_ARG_WITH(dependency-search,
AC_HELP_STRING([--with-dependency-search=DIR],
@ -280,6 +294,24 @@ 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])
]
)
if test "x$WANT_NACL" = "xyes"; then
enable_shared=no
enable_static=yes
@ -397,6 +429,36 @@ 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
# 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 +518,34 @@ 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
# 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@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@
Cflags: -I${includedir}

View File

@ -57,12 +57,14 @@ libtoxcore_la_CFLAGS = -I$(top_srcdir) \
-I$(top_srcdir)/toxcore \
$(LIBSODIUM_CFLAGS) \
$(NACL_CFLAGS) \
$(MINIDUMP_CFLAGS) \
$(PTHREAD_CFLAGS)
libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
$(EXTRA_LT_LDFLAGS) \
$(LIBSODIUM_LDFLAGS) \
$(NACL_LDFLAGS) \
$(MINIDUMP_LDFLAGS) \
$(MATH_LDFLAGS) \
$(RT_LIBS) \
$(WINSOCK2_LIBS)
@ -70,4 +72,5 @@ libtoxcore_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
libtoxcore_la_LIBADD = $(LIBSODIUM_LIBS) \
$(NACL_OBJECTS) \
$(NAC_LIBS) \
$(MINIDUMP_LIBS) \
$(PTHREAD_LIBS)

View File

@ -43,6 +43,13 @@
#include "network.h"
#include "util.h"
#ifdef HAVE_LIBMINIUPNPC
#include <miniupnpc/miniupnpc.h>
#include <miniupnpc/miniwget.h>
#include <miniupnpc/upnpcommands.h>
#include <miniupnpc/upnperrors.h>
#endif
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
static const char *inet_ntop(sa_family_t family, void *addr, char *buf, size_t bufsize)
@ -111,6 +118,56 @@ static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf)
#endif
#ifdef HAVE_LIBMINIUPNPC
/* Setup port forwarding using UPnP */
static void upnp_map_port(uint16_t port)
{
LOGGER_DEBUG("Attempting to set up UPnP port forwarding");
int error = 0;
struct UPNPDev *devlist = NULL;
devlist = upnpDiscover(1000, NULL, NULL, 0, 0, &error);
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);
error = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, portstr, portstr, lanaddr, "Tox", "UDP", 0, "0");
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
/* Check if socket is valid.
*
* return 1 if valid
@ -674,6 +731,10 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to,
if (error)
*error = 0;
#ifdef HAVE_LIBMINIUPNPC
upnp_map_port(ntohs(temp->port));
#endif
return temp;
}