Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Net] Add NAT-PMP port forwarding support #2338

Merged
merged 19 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: macOS
os: macos-10.15
packages: python3 autoconf automake berkeley-db4 libtool boost miniupnpc pkg-config qt5 zmq libevent qrencode gmp libsodium rust
packages: python3 autoconf automake berkeley-db4 libtool boost miniupnpc libnatpmp pkg-config qt5 zmq libevent qrencode gmp libsodium rust
cc: $(brew --prefix llvm)/bin/clang
cxx: $(brew --prefix llvm)/bin/clang++

Expand Down Expand Up @@ -165,7 +165,7 @@ jobs:
os: ubuntu-18.04
host: x86_64-unknown-linux-gnu
apt_get: python3-zmq qtbase5-dev qttools5-dev-tools libqt5svg5-dev libqt5charts5-dev libqrencode-dev libdbus-1-dev libharfbuzz-dev
dep_opts: NO_QT=1 NO_UPNP=1 DEBUG=1 ALLOW_HOST_PACKAGES=1
dep_opts: NO_QT=1 NO_UPNP=1 NO_NATPMP=1 DEBUG=1 ALLOW_HOST_PACKAGES=1

- name: macOS 10.12
os: ubuntu-18.04
Expand Down Expand Up @@ -303,7 +303,7 @@ jobs:
- name: x86_64 Linux [GOAL:install] [bionic] [no depends only system libs]
os: ubuntu-18.04
host: x86_64-unknown-linux-gnu
apt_get: python3-zmq qtbase5-dev qttools5-dev-tools libqt5svg5-dev libqt5charts5-dev libevent-dev bsdmainutils libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev libdb5.3++-dev libminiupnpc-dev libzmq3-dev libqrencode-dev libgmp-dev libsodium-dev cargo
apt_get: python3-zmq qtbase5-dev qttools5-dev-tools libqt5svg5-dev libqt5charts5-dev libevent-dev bsdmainutils libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev libdb5.3++-dev libminiupnpc-dev libnatpmp-dev libzmq3-dev libqrencode-dev libgmp-dev libsodium-dev cargo
unit_tests: true
no_depends: 1
goal: install
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ find_package(GMP REQUIRED)

find_package(ZMQ)
find_package(Miniupnp)
find_package(NAT-PMP)
find_package(Boost COMPONENTS system filesystem thread REQUIRED)
find_package(Sodium REQUIRED)

Expand Down Expand Up @@ -195,6 +196,7 @@ set(SERVER_SOURCES
./src/interfaces/wallet.cpp
./src/dbwrapper.cpp
./src/legacy/validation_zerocoin_legacy.cpp
./src/mapport.cpp
./src/merkleblock.cpp
./src/miner.cpp
./src/blockassembler.cpp
Expand Down Expand Up @@ -559,6 +561,10 @@ if(MINIUPNP_FOUND)
target_link_libraries(pivxd ${MINIUPNP_LIBRARY})
target_include_directories(pivxd PUBLIC ${MINIUPNP_INCLUDE_DIR})
endif()
if(NAT-PMP_FOUND)
target_link_libraries(pivxd ${NAT-PMP_LIBRARY})
target_include_directories(pivxd PUBLIC ${NAT-PMP_INCLUDE_DIR})
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(pivxd "-framework Cocoa")
Expand Down
2 changes: 2 additions & 0 deletions build-aux/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ parts:
- libsodium-dev
- cargo
- libminiupnpc-dev
- libnatpmp-dev
- libzmq3-dev
- libqt5gui5
- libqt5core5a
Expand Down Expand Up @@ -352,6 +353,7 @@ parts:
- libevent-2.1-6
- libevent-pthreads-2.1-6
- libminiupnpc10
- libnatpmp1
- libnorm1
- libpgm-5.2-0
- libprotobuf10
Expand Down
51 changes: 51 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ AC_ARG_ENABLE([upnp-default],
[use_upnp_default=$enableval],
[use_upnp_default=no])

AC_ARG_WITH([natpmp],
[AS_HELP_STRING([--with-natpmp],
[enable NAT-PMP (default is yes if libnatpmp is found)])],
[use_natpmp=$withval],
[use_natpmp=auto])

AC_ARG_ENABLE([natpmp-default],
[AS_HELP_STRING([--enable-natpmp-default],
[if NAT-PMP is enabled, turn it on at startup (default is no)])],
[use_natpmp_default=$enableval],
[use_natpmp_default=no])

AC_ARG_ENABLE(tests,
AS_HELP_STRING([--disable-tests],[do not compile tests (default is to compile)]),
[use_tests=$enableval],
Expand Down Expand Up @@ -1011,6 +1023,7 @@ if test "x$enable_fuzz" = "xyes"; then
enable_wallet=yes # needs to be built for now.
use_bench=no
use_upnp=no
use_natpmp=no
use_zmq=no
else
BITCOIN_QT_INIT
Expand Down Expand Up @@ -1053,6 +1066,13 @@ if test x$have_miniupnpc != xno; then
fi
fi

dnl Check for libnatpmp (optional).
if test "x$use_natpmp" != xno; then
AC_CHECK_HEADERS([natpmp.h],
[AC_CHECK_LIB([natpmp], [initnatpmp], [NATPMP_LIBS=-lnatpmp], [have_natpmp=no])],
[have_natpmp=no])
fi

if test x$build_bitcoin_utils$build_bitcoind$bitcoin_enable_qt$use_tests$use_bench = xnonononono; then
use_boost=no
else
Expand Down Expand Up @@ -1358,6 +1378,34 @@ else
fi
fi

dnl Enable NAT-PMP support.
AC_MSG_CHECKING([whether to build with support for NAT-PMP])
if test "x$have_natpmp" = xno; then
if test "x$use_natpmp" = xyes; then
AC_MSG_ERROR([NAT-PMP requested but cannot be built. Use --without-natpmp])
fi
AC_MSG_RESULT([no])
use_natpmp=no
else
if test "x$use_natpmp" != xno; then
AC_MSG_RESULT([yes])
AC_MSG_CHECKING([whether to build with NAT-PMP enabled by default])
use_natpmp=yes
natpmp_setting=0
if test "x$use_natpmp_default" != xno; then
use_natpmp_default=yes
natpmp_setting=1
fi
AC_MSG_RESULT($use_natpmp_default)
AC_DEFINE_UNQUOTED([USE_NATPMP], [$natpmp_setting], [NAT-PMP support not compiled if undefined, otherwise value (0 or 1) determines default state])
if test x$TARGET_OS = xwindows; then
NATPMP_CPPFLAGS="-DSTATICLIB -DNATPMP_STATICLIB"
fi
else
AC_MSG_RESULT([no])
fi
fi

dnl these are only used when qt is enabled
BUILD_TEST_QT=""
if test x$bitcoin_enable_qt != xno; then
Expand Down Expand Up @@ -1492,6 +1540,8 @@ AC_SUBST(BOOST_LIBS)
AC_SUBST(TESTDEFS)
AC_SUBST(MINIUPNPC_CPPFLAGS)
AC_SUBST(MINIUPNPC_LIBS)
AC_SUBST(NATPMP_CPPFLAGS)
AC_SUBST(NATPMP_LIBS)
AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(SODIUM_LIBS)
Expand Down Expand Up @@ -1580,6 +1630,7 @@ if test x$use_tests != xno; then
fi
echo " with bench = $use_bench"
echo " with upnp = $use_upnp"
echo " with natpmp = $use_natpmp"
echo " with params = $params_path"
echo " use asm = $use_asm"
echo " sanitizers = $use_sanitizers"
Expand Down
40 changes: 40 additions & 0 deletions contrib/cmake/FindNAT-PMP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# - Find NAT-PMP
# This module defines
# NAT-PMP_INCLUDE_DIR, where to find NAT-PMP headers
# NAT-PMP_LIBRARY, NAT-PMP libraries
# NAT-PMP_FOUND, If false, do not try to use NAT-PMP

set(NAT-PMP_PREFIX "" CACHE PATH "path ")

find_path(NAT-PMP_INCLUDE_DIR natpmp.h
PATHS ${NAT-PMP_PREFIX}/include /usr/include /usr/local/include)

find_library(NAT-PMP_LIBRARY NAMES natpmp libnatpmp
PATHS ${NAT-PMP_PREFIX}/lib /usr/lib /usr/local/lib)

if(NAT-PMP_INCLUDE_DIR AND NAT-PMP_LIBRARY)
get_filename_component(NAT-PMP_LIBRARY_DIR ${NAT-PMP_LIBRARY} PATH)
set(NAT-PMP_FOUND TRUE)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
NAT-PMP DEFAULT_MSG
NAT-PMP_INCLUDE_DIR
NAT-PMP_LIBRARY
)

if(NAT-PMP_FOUND)
if(NOT NAT-PMP_FIND_QUIETLY)
MESSAGE(STATUS "Found NAT-PMP: ${NAT-PMP_LIBRARY}")
endif()
else()
if(NAT-PMP_FIND_REQUIRED)
message(FATAL_ERROR "Could not find NAT-PMP")
endif()
endif()

mark_as_advanced(
NAT-PMP_LIBRARY
NAT-PMP_INCLUDE_DIR
)
6 changes: 5 additions & 1 deletion depends/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NO_QT ?=
NO_WALLET ?=
NO_ZMQ ?=
NO_UPNP ?=
NO_NATPMP ?=
NO_RUST ?=
FALLBACK_DOWNLOAD_PATH ?= https://depends.pivx.org

Expand Down Expand Up @@ -130,9 +131,11 @@ endif
qt_packages_$(NO_QT) = $(qt_packages) $(qt_$(host_os)_packages) $(qt_$(host_arch)_$(host_os)_packages)
wallet_packages_$(NO_WALLET) = $(wallet_packages)
upnp_packages_$(NO_UPNP) = $(upnp_packages)
natpmp_packages_$(NO_NATPMP) = $(natpmp_packages)

zmq_packages_$(NO_ZMQ) = $(zmq_packages)

packages += $($(host_arch)_$(host_os)_packages) $($(host_os)_packages) $(qt_packages_) $(rust_packages) $(wallet_packages_) $(upnp_packages_)
packages += $($(host_arch)_$(host_os)_packages) $($(host_os)_packages) $(qt_packages_) $(rust_packages) $(wallet_packages_) $(upnp_packages_) $(natpmp_packages_)
native_packages += $($(host_arch)_$(host_os)_native_packages) $($(host_os)_native_packages)

ifneq ($(qt_packages_),)
Expand Down Expand Up @@ -189,6 +192,7 @@ $(host_prefix)/share/config.site : config.site.in $(host_prefix)/.stamp_$(final_
-e 's|@no_zmq@|$(NO_ZMQ)|' \
-e 's|@no_wallet@|$(NO_WALLET)|' \
-e 's|@no_upnp@|$(NO_UPNP)|' \
-e 's|@no_natpmp@|$(NO_NATPMP)|' \
-e 's|@debug@|$(DEBUG)|' \
$< > $@
$(AT)touch $@
Expand Down
2 changes: 2 additions & 0 deletions depends/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ The following can be set when running make: `make FOO=bar`
<dd>Don't download/build/cache libs needed to enable the wallet</dd>
<dt>NO_UPNP</dt>
<dd>Don't download/build/cache packages needed for enabling upnp</dd>
<dt>NO_NATPMP</dt>
<dd>Don't download/build/cache packages needed for enabling NAT-PMP</dd>
<dt>NO_RUST</dt>
<dd>Don't download/build/cache rust packages (including librustzcash)</dd>
<dt>ALLOW_HOST_PACKAGES</dt>
Expand Down
4 changes: 4 additions & 0 deletions depends/config.site.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ if test -z $with_miniupnpc && test -n "@no_upnp@"; then
with_miniupnpc=no
fi

if test -z $with_natpmp && test -n "@no_natpmp@"; then
with_natpmp=no
fi

if test -z $with_gui && test -n "@no_qt@"; then
with_gui=no
fi
Expand Down
22 changes: 22 additions & 0 deletions depends/packages/libnatpmp.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package=libnatpmp
$(package)_version=4536032ae32268a45c073a4d5e91bbab4534773a
$(package)_download_path=https://github.com/miniupnp/libnatpmp/archive
$(package)_file_name=$($(package)_version).tar.gz
$(package)_sha256_hash=543b460aab26acf91e11d15e17d8798f845304199eea2d76c2f444ec749c5383

define $(package)_set_vars
$(package)_build_opts=CC="$($(package)_cc)"
$(package)_build_opts_mingw32=CPPFLAGS=-DNATPMP_STATICLIB
$(package)_build_opts_darwin=LIBTOOL="$($(package)_libtool)"
$(package)_build_env+=CFLAGS="$($(package)_cflags) $($(package)_cppflags)" AR="$($(package)_ar)"
endef

define $(package)_build_cmds
$(MAKE) libnatpmp.a $($(package)_build_opts)
endef

define $(package)_stage_cmds
mkdir -p $($(package)_staging_prefix_dir)/include $($(package)_staging_prefix_dir)/lib &&\
install *.h $($(package)_staging_prefix_dir)/include &&\
install libnatpmp.a $($(package)_staging_prefix_dir)/lib
endef
1 change: 1 addition & 0 deletions depends/packages/packages.mk
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ wallet_packages=bdb
zmq_packages=zeromq

upnp_packages=miniupnpc
natpmp_packages=libnatpmp

darwin_native_packages = native_ds_store native_mac_alias

Expand Down
2 changes: 1 addition & 1 deletion doc/build-osx.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Then install [Homebrew](https://brew.sh).
Dependencies
----------------------

brew install autoconf automake berkeley-db4 libtool boost miniupnpc pkg-config python3 qt5 zmq libevent qrencode gmp libsodium rust
brew install autoconf automake berkeley-db4 libtool boost miniupnpc libnatpmp pkg-config python3 qt5 zmq libevent qrencode gmp libsodium rust
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future nit: miniupnpc and libnatpmp are optional.


See [dependencies.md](dependencies.md) for a complete overview.

Expand Down
22 changes: 17 additions & 5 deletions doc/build-unix.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Optional dependencies:
Library | Purpose | Description
------------|------------------|----------------------
miniupnpc | UPnP Support | Firewall-jumping support
libnatpmp | NAT-PMP Support | Firewall-jumping support
libdb4.8 | Berkeley DB | Wallet storage (only needed when wallet enabled)
qt | GUI | GUI toolkit (only needed when GUI enabled)
univalue | Utility | JSON parsing and encoding (bundled version will be used unless --with-system-univalue passed to configure)
Expand Down Expand Up @@ -93,9 +94,9 @@ Otherwise, you can build from self-compiled `depends` (see above).
To build PIVX Core without wallet, see [*Disable-wallet mode*](/doc/build-unix.md#disable-wallet-mode)


Optional (see --with-miniupnpc and --enable-upnp-default):
Optional port mapping libraries (see: `--with-miniupnpc`, and `--enable-upnp-default`, `--with-natpmp`, `--enable-natpmp-default`):

sudo apt-get install libminiupnpc-dev
sudo apt install libminiupnpc-dev libnatpmp-dev

ZMQ dependencies (provides ZMQ API):

Expand Down Expand Up @@ -127,7 +128,7 @@ Build requirements:

Optional:

sudo dnf install miniupnpc-devel zeromq-devel
sudo dnf install miniupnpc-devel libnatpmp-devel zeromq-devel

To build with Qt 5 you need the following:

Expand All @@ -144,12 +145,23 @@ miniupnpc

[miniupnpc](http://miniupnp.free.fr/) may be used for UPnP port mapping. It can be downloaded from [here](
http://miniupnp.tuxfamily.org/files/). UPnP support is compiled in and
turned off by default. See the configure options for upnp behavior desired:
turned off by default. See the configure options for UPnp behavior desired:

--without-miniupnpc No UPnP support miniupnp not required
--without-miniupnpc No UPnP support, miniupnp not required
--disable-upnp-default (the default) UPnP support turned off by default at runtime
--enable-upnp-default UPnP support turned on by default at runtime

libnatpmp
---------

[libnatpmp](https://miniupnp.tuxfamily.org/libnatpmp.html) may be used for NAT-PMP port mapping. It can be downloaded
from [here](https://miniupnp.tuxfamily.org/files/). NAT-PMP support is compiled in and
turned off by default. See the configure options for NAT-PMP behavior desired:

--without-natpmp No NAT-PMP support, libnatpmp not required
--disable-natpmp-default (the default) NAT-PMP support turned off by default at runtime
--enable-natpmp-default NAT-PMP support turned on by default at runtime

To build:

tar -xzvf miniupnpc-1.6.tar.gz
Expand Down
4 changes: 3 additions & 1 deletion doc/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ These are the dependencies currently used by PIVX Core. You can find instruction
| GCC | | [4.8+](https://gcc.gnu.org/) (C++11 support) | | | |
| HarfBuzz-NG | | | | | |
| libevent | [2.1.8-stable](https://github.com/libevent/libevent/releases) | 2.0.22 | No | | |
| libnatpmp | [20150609](https://miniupnp.tuxfamily.org/files) | | No | | |
| libjpeg | | | | | [Yes](https://github.com/pivx-project/pivx/blob/master/depends/packages/qt.mk#L65) |
| libpng | | | | | [Yes](https://github.com/pivx-project/pivx/blob/master/depends/packages/qt.mk#L64) |
| librsvg | | | | | |
Expand All @@ -36,7 +37,8 @@ Controlling dependencies
Some dependencies are not needed in all configurations. The following are some factors that affect the dependency list.

#### Options passed to `./configure`
* MiniUPnPc is not needed with `--with-miniupnpc=no`.
* MiniUPnPc is not needed with `--without-miniupnpc`.
* libnatpmp is not needed with `--without-natpmp`.
* Berkeley DB is not needed with `--disable-wallet`.
* Qt is not needed with `--without-gui`.
* If the qrencode dependency is absent, QR support won't be added. To force an error when that happens, pass `--with-qrencode`.
Expand Down
Loading