diff --git a/.gitignore b/.gitignore index a58f699..ee8288b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +cmake-build *.o Makefile.depend udptunnel diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3b13a73 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required (VERSION 3.10) +project ("udptunnel" "C") + +if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message (FATAL_ERROR "In-source builds are not allowed") +endif () + +set (CMAKE_PROJECT_DESCRIPTION "UDP tunnel over TCP") +set (CMAKE_PROJECT_HOMEPAGE_URL "https://github.com/andrew-aladjev/udptunnel") +set (PROJECT_VERSION "1.2.1") + +if (NOT DEFINED CMAKE_INSTALL_BINDIR) + set (CMAKE_INSTALL_BINDIR "sbin" CACHE PATH "output directory for binaries") +endif () + +set (CMAKE_C_FLAGS "-O2 -std=c11 -Wall") +set (CMAKE_EXE_LINKER_FLAGS "-static") + +if (NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "") + set (CMAKE_BUILD_TYPE "Release") +endif () + +set ( + SOURCES + "log.c" + "network.c" + "udptunnel.c" + "utils.c" +) + +add_executable (${PROJECT_NAME} ${SOURCES}) +install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + +include (CPackConfig.cmake) +include (CPack) diff --git a/CPackConfig.cmake b/CPackConfig.cmake new file mode 100644 index 0000000..270cf19 --- /dev/null +++ b/CPackConfig.cmake @@ -0,0 +1,29 @@ +set (CPACK_PACKAGE_NAME ${PROJECT_NAME}) +set (CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) +set (CPACK_RESOURCE_FILE_README "${PROJECT_SOURCE_DIR}/README.md") +set (CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/COPYING") + +set (CPACK_PACKAGE_LICENSE "LGPLv2") +set (CPACK_RPM_PACKAGE_LICENSE ${CPACK_PACKAGE_LICENSE}) +set (CPACK_FREEBSD_PACKAGE_LICENSE ${CPACK_PACKAGE_LICENSE}) + +set (CPACK_PACKAGE_MAINTAINER "Andrew Aladjev ") +set (CPACK_DEBIAN_PACKAGE_MAINTAINER ${CPACK_PACKAGE_MAINTAINER}) +set (CPACK_FREEBSD_PACKAGE_MAINTAINER ${CPACK_PACKAGE_MAINTAINER}) + +set (CPACK_DEBIAN_PACKAGE_SECTION "net") +set (CPACK_FREEBSD_PACKAGE_ORIGIN "net/${PROJECT_NAME}") + +set (CPACK_PACKAGE_ARCHITECTURE "amd64") +set (CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${CPACK_PACKAGE_ARCHITECTURE}) +set (CPACK_RPM_PACKAGE_ARCHITECTURE ${CPACK_PACKAGE_ARCHITECTURE}) + +if (CMAKE_SYSTEM_NAME MATCHES "Linux") + set (CPACK_GENERATOR "DEB" "RPM") +elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + set (CPACK_GENERATOR "FREEBSD") +endif () + +set (PACKAGE_NAME "${CPACK_PACKAGE_NAME}") +set (CPACK_PACKAGE_FILE_NAME "${PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}.${CPACK_PACKAGE_ARCHITECTURE}") +set (CPACK_STRIP_FILES true) diff --git a/README.md b/README.md new file mode 100644 index 0000000..aaf7c1d --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +UDP tunnel over TCP. License: LGPLv2. + +Please build it using the following commands: + +```bash +rm -rf cmake-build +cmake -S . -B cmake-build +cmake --build cmake-build +cmake --build cmake-build --target package +``` + +After that you will receive packages for DEB-based and RPM-based distros: + +``` +cmake-build/udptunnel-*.amd64.deb +cmake-build/udptunnel-*.amd64.rpm +``` diff --git a/network.c b/network.c index 2c0f2c1..43453a8 100644 --- a/network.c +++ b/network.c @@ -198,6 +198,11 @@ int *tcp_listener(const char *s) if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) continue; /* ignore */ opt = 1; + if (ai->ai_family == AF_INET6) { + /* we are going to bind to IPv6 address only */ + if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) < 0) + err_sys("setsockopt(IPPROTO_IPV6, IPV6_V6ONLY)"); + } if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) err_sys("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); if (bind(fd, (struct sockaddr *) ai->ai_addr, ai->ai_addrlen) < 0) diff --git a/udptunnel.c b/udptunnel.c index 8dd9c17..f5b6b5b 100644 --- a/udptunnel.c +++ b/udptunnel.c @@ -379,9 +379,7 @@ static void tcp_to_udp(struct relay *relay) static void send_handshake(struct relay *relay) { - if (sendto(relay->tcp_sock, relay->handshake, sizeof(relay->handshake), 0, - (struct sockaddr *) &relay->remote_udpaddr, - sizeof(relay->remote_udpaddr)) < 0) + if (send(relay->tcp_sock, relay->handshake, sizeof(relay->handshake), 0) < 0) err_sys("sendto(tcp, handshake)"); }