From d5532e4de42c24508faee9cd91a04a3c41a98aa4 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 8 Aug 2018 15:37:33 -0400 Subject: [PATCH] more boilerplate and enable tun/tap build --- CMakeLists.txt | 11 +- Makefile | 8 +- include/llarp/endpoint.hpp | 18 -- include/llarp/handlers/tun.hpp | 15 ++ include/llarp/path.hpp | 1 - include/llarp/service/endpoint.hpp | 7 + include/llarp/service/handler.hpp | 17 ++ include/llarp/service/tun.hpp | 20 ++ include/tuntap.h | 252 +++++++++++++++++++++++++ llarp/handlers/tun.cpp | 0 llarp/router.hpp | 3 + llarp/service/endpoint.cpp | 4 - llarp/service/tun.cpp | 14 ++ vendor/libtuntap-master/tuntap.h | 213 --------------------- vendor/libtuntap-master/tuntap_log.c | 163 ---------------- vendor/libtuntap-master/tuntap_log.cpp | 156 +++++++++++++++ 16 files changed, 496 insertions(+), 406 deletions(-) delete mode 100644 include/llarp/endpoint.hpp create mode 100644 include/llarp/handlers/tun.hpp create mode 100644 include/llarp/service/handler.hpp create mode 100644 include/llarp/service/tun.hpp create mode 100644 include/tuntap.h create mode 100644 llarp/handlers/tun.cpp create mode 100644 llarp/service/tun.cpp delete mode 100644 vendor/libtuntap-master/tuntap.h delete mode 100644 vendor/libtuntap-master/tuntap_log.c create mode 100644 vendor/libtuntap-master/tuntap_log.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e8b782b7d..da17edf39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,11 +177,15 @@ endif(UNIX) if(TUNTAP) set(LIBTUNTAP_SRC ${TT_ROOT}/tuntap.c - ${TT_ROOT}/tuntap_log.c + ${TT_ROOT}/tuntap_log.cpp + ${LIBTUNTAP_IMPL}) if (UNIX) - ${TT_ROOT}/tuntap-unix.c + set(LIBTUNTAP_SRC + ${TT_ROOT}/tuntap-unix.c + ${LIBTUNTAP_SRC} + ) endif() - ${LIBTUNTAP_IMPL}) + else() set(LIBTUNTAP_SRC "") endif() @@ -282,6 +286,7 @@ set(LIB_SRC llarp/dht/got_router.cpp llarp/dht/search_job.cpp llarp/dht/publish_intro.cpp + llarp/handlers/tun.cpp llarp/iwp/frame_header.cpp llarp/iwp/frame_state.cpp llarp/iwp/session.cpp diff --git a/Makefile b/Makefile index 6f4326f56..03973b3e8 100644 --- a/Makefile +++ b/Makefile @@ -40,10 +40,10 @@ clean: rm -f *.a *.so debug-configure: - cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) + cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DTUNTAP=ON release-configure: clean - cmake -GNinja -DSTATIC_LINK=ON -DCMAKE_BUILD_TYPE=Release -DRELEASE_MOTTO="$(shell cat motto.txt)" -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) + cmake -GNinja -DSTATIC_LINK=ON -DCMAKE_BUILD_TYPE=Release -DRELEASE_MOTTO="$(shell cat motto.txt)" -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DTUNTAP=ON debug: debug-configure ninja @@ -82,7 +82,7 @@ testnet-clean: clean rm -rf $(TESTNET_ROOT) testnet-configure: testnet-clean - cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) + cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DTUNTAP=ON testnet-build: testnet-configure ninja @@ -91,7 +91,7 @@ $(TESTNET_EXE): testnet-build cp -f $(REPO)/llarpd $(TESTNET_EXE) shared-configure: clean - cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DWITH_SHARED=ON + cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DWITH_SHARED=ON -DTUNTAP=ON shared: shared-configure ninja diff --git a/include/llarp/endpoint.hpp b/include/llarp/endpoint.hpp deleted file mode 100644 index 893ac16be..000000000 --- a/include/llarp/endpoint.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef LLARP_ENDPOINT_HANDLER_HPP -#define LLARP_ENDPOINT_HANDLER_HPP - -#include - -namespace llarp -{ - // hidden service endpoint handler - struct IEndpointHandler - { - ~IEndpointHandler(){}; - - virtual void - HandleMessage(llarp_buffer_t buf) = 0; - }; -} // namespace llarp - -#endif \ No newline at end of file diff --git a/include/llarp/handlers/tun.hpp b/include/llarp/handlers/tun.hpp new file mode 100644 index 000000000..2fbf55a0a --- /dev/null +++ b/include/llarp/handlers/tun.hpp @@ -0,0 +1,15 @@ +#ifndef LLARP_HANDLERS_TUN_HPP +#define LLARP_HANDLERS_TUN_HPP +#include + +namespace llarp +{ + namespace handlers + { + struct TunHandler : public service::IDataHandler + { + }; + } // namespace handlers +} // namespace llarp + +#endif \ No newline at end of file diff --git a/include/llarp/path.hpp b/include/llarp/path.hpp index 369c9bce0..564825627 100644 --- a/include/llarp/path.hpp +++ b/include/llarp/path.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/include/llarp/service/endpoint.hpp b/include/llarp/service/endpoint.hpp index c5a870249..ea18161ea 100644 --- a/include/llarp/service/endpoint.hpp +++ b/include/llarp/service/endpoint.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace llarp @@ -20,6 +21,9 @@ namespace llarp Endpoint(const std::string& nickname, llarp_router* r); ~Endpoint(); + void + SetHandler(IDataHandler* h); + bool SetOption(const std::string& k, const std::string& v); @@ -179,6 +183,9 @@ namespace llarp uint64_t GenTXID(); + protected: + IDataHandler* m_DataHandler = nullptr; + private: llarp_router* m_Router; std::string m_Keyfile; diff --git a/include/llarp/service/handler.hpp b/include/llarp/service/handler.hpp new file mode 100644 index 000000000..416464158 --- /dev/null +++ b/include/llarp/service/handler.hpp @@ -0,0 +1,17 @@ +#ifndef LLARP_SERVICE_HANDLER_HPP +#define LLARP_SERVICE_HANDLER_HPP +#include + +namespace llarp +{ + namespace service + { + struct IDataHandler + { + virtual void + HandleDataMessage(ProtocolMessage* msg) = 0; + }; + } // namespace service +} // namespace llarp + +#endif \ No newline at end of file diff --git a/include/llarp/service/tun.hpp b/include/llarp/service/tun.hpp new file mode 100644 index 000000000..b158fcdc3 --- /dev/null +++ b/include/llarp/service/tun.hpp @@ -0,0 +1,20 @@ +#ifndef LLARP_SERVICE_TUN_HPP +#define LLARP_SERVICE_TUN_HPP +#include +#include + +namespace llarp +{ + namespace service + { + struct TunEndpoint : public Endpoint + { + TunEndpoint(const std::string& ifname, llarp_router* r); + ~TunEndpoint(); + + device* m_tunif; + }; + } // namespace service +} // namespace llarp + +#endif \ No newline at end of file diff --git a/include/tuntap.h b/include/tuntap.h new file mode 100644 index 000000000..f088682d9 --- /dev/null +++ b/include/tuntap.h @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2012 Tristan Le Guern + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * Copyright (c) 2016 Mahdi Mokhtari + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#if defined Windows +#include +#else /* Unix */ +#include +#endif + +#if !defined Windows /* Unix :) */ +#if defined Linux +#include +#else +#include +#endif +#include +#include +#endif + +#include + +#ifndef LIBTUNTAP_H_ +#define LIBTUNTAP_H_ + +/* + * Uniformize macros + * - ETHER_ADDR_LEN: Magic number from IEEE 802.3 + * - IF_NAMESIZE: Length of interface external name + * - IF_DESCRSIZE: Length of interface description + * - TUNSDEBUG: ioctl flag to enable the debug mode of a tun device + * - TUNFD_INVALID_VALUE: Invalid value for tun_fd + */ +#if defined ETH_ALEN /* Linux */ +#define ETHER_ADDR_LEN ETH_ALEN +#elif defined Windows +#define ETHER_ADDR_LEN 6 +#endif + +#if defined IFNAMSIZ && !defined IF_NAMESIZE +#define IF_NAMESIZE IFNAMSIZ /* Historical BSD name */ +#elif !defined IF_NAMESIZE +#define IF_NAMESIZE 16 +#endif + +#define IF_DESCRSIZE 50 /* XXX: Tests needed on NetBSD and OpenBSD */ + +#if defined TUNSETDEBUG +#define TUNSDEBUG TUNSETDEBUG +#endif + +#if defined Windows +#define TUNFD_INVALID_VALUE INVALID_HANDLE_VALUE +#else /* Unix */ +#define TUNFD_INVALID_VALUE -1 +#endif + +/* + * Uniformize types + * - t_tun: tun device file descriptor + * - t_tun_in_addr: struct in_addr/IN_ADDR + * - t_tun_in6_addr: struct in6_addr/IN6_ADDR + */ +#if defined Windows +typedef HANDLE t_tun; +typedef IN_ADDR t_tun_in_addr; +typedef IN6_ADDR t_tun_in6_addr; +#else /* Unix */ +typedef int t_tun; +typedef struct in_addr t_tun_in_addr; +typedef struct in6_addr t_tun_in6_addr; +#endif + +/* + * Windows helpers + */ +#if defined Windows +#define snprintf(x, y, z, ...) _snprintf_s((x), (y), (y), (z), __VA_ARGS__); +#define strncat(x, y, z) strncat_s((x), _countof(x), (y), (z)); +#define strdup(x) _strdup(x) +#endif + +#define TUNTAP_ID_MAX 256 +#define TUNTAP_ID_ANY 257 + +#define TUNTAP_MODE_ETHERNET 0x0001 +#define TUNTAP_MODE_TUNNEL 0x0002 +#define TUNTAP_MODE_PERSIST 0x0004 + +#define TUNTAP_LOG_NONE 0x0000 +#define TUNTAP_LOG_DEBUG 0x0001 +#define TUNTAP_LOG_INFO 0x0002 +#define TUNTAP_LOG_NOTICE 0x0004 +#define TUNTAP_LOG_WARN 0x0008 +#define TUNTAP_LOG_ERR 0x0016 + +/* Versioning: 0xMMmm, with 'M' for major and 'm' for minor */ +#define TUNTAP_VERSION_MAJOR 0 +#define TUNTAP_VERSION_MINOR 3 +#define TUNTAP_VERSION ((TUNTAP_VERSION_MAJOR << 8) | TUNTAP_VERSION_MINOR) + +#define TUNTAP_GET_FD(x) (x)->tun_fd + +/* Handle Windows symbols export */ +#if defined Windows +#if defined(tuntap_EXPORTS) /* CMake generated goo */ +#define TUNTAP_EXPORT __declspec(dllexport) +#else +#define TUNTAP_EXPORT __declspec(dllimport) +#endif +#else /* Unix */ +#define TUNTAP_EXPORT extern +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + + struct device + { + t_tun tun_fd; + int ctrl_sock; + int flags; /* ifr.ifr_flags on Unix */ + unsigned char hwaddr[ETHER_ADDR_LEN]; + char if_name[IF_NAMESIZE]; +#if defined(FreeBSD) + int mode; +#endif + }; + + /* User definable log callback */ + typedef void (*t_tuntap_log)(int, const char *); + TUNTAP_EXPORT t_tuntap_log tuntap_log; + + /* Portable "public" functions */ + TUNTAP_EXPORT struct device * + tuntap_init(void); + TUNTAP_EXPORT int + tuntap_version(void); + TUNTAP_EXPORT void + tuntap_destroy(struct device *); + TUNTAP_EXPORT void + tuntap_release(struct device *); + TUNTAP_EXPORT int + tuntap_start(struct device *, int, int); + TUNTAP_EXPORT char * + tuntap_get_ifname(struct device *); + TUNTAP_EXPORT int + tuntap_set_ifname(struct device *, const char *); + TUNTAP_EXPORT char * + tuntap_get_hwaddr(struct device *); + TUNTAP_EXPORT int + tuntap_set_hwaddr(struct device *, const char *); + TUNTAP_EXPORT int + tuntap_set_descr(struct device *, const char *); + TUNTAP_EXPORT int + tuntap_up(struct device *); + TUNTAP_EXPORT int + tuntap_down(struct device *); + TUNTAP_EXPORT int + tuntap_get_mtu(struct device *); + TUNTAP_EXPORT int + tuntap_set_mtu(struct device *, int); + + /* + * It's impossible to set single IP for `tun` devices on FreeBSD . + * FreeBSD's `tun` interface needs 2 IP addresses. + * So a new (and backward compatible) version of tuntap_set_ip() is + * implemented. + */ + TUNTAP_EXPORT int + tuntap_set_ip(struct device *, ...); + // TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char + // *, int); + /*TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char + * *, int);*/ + TUNTAP_EXPORT int + tuntap_read(struct device *, void *, size_t); + TUNTAP_EXPORT int + tuntap_write(struct device *, void *, size_t); + TUNTAP_EXPORT int + tuntap_get_readable(struct device *); + TUNTAP_EXPORT int + tuntap_set_nonblocking(struct device *dev, int); + TUNTAP_EXPORT int + tuntap_set_debug(struct device *dev, int); + + /* Logging functions */ + TUNTAP_EXPORT void + tuntap_log_set_cb(t_tuntap_log cb); + void + tuntap_log_default(int, const char *); + void + tuntap_log_hexdump(void *, size_t); + void + tuntap_log_chksum(void *, int); + + /* OS specific functions */ + int + tuntap_sys_start(struct device *, int, int); + void + tuntap_sys_destroy(struct device *); + int + tuntap_sys_set_hwaddr(struct device *, struct ether_addr *); + int + tuntap_sys_set_ipv4(struct device *, t_tun_in_addr *, uint32_t); + +#if defined(FreeBSD) + int + tuntap_sys_set_ipv4_tap(struct device *, t_tun_in_addr *, uint32_t); + int + tuntap_sys_set_ipv4_tun(struct device *dev, t_tun_in_addr *s4, + t_tun_in_addr *s4dest, uint32_t bits); +#endif + + int + tuntap_sys_set_ipv6(struct device *, t_tun_in6_addr *, uint32_t); + int + tuntap_sys_set_ifname(struct device *, const char *, size_t); + int + tuntap_sys_set_descr(struct device *, const char *, size_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/llarp/router.hpp b/llarp/router.hpp index 79a6f8644..11a50f76a 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "llarp/iwp/establish_job.hpp" #include "crypto.hpp" @@ -96,6 +97,8 @@ struct llarp_router llarp::service::Context hiddenServiceContext; + llarp::service::TunEndpoint *tunEndpoint = nullptr; + llarp_link *outboundLink = nullptr; std::list< llarp_link * > inboundLinks; diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index c10acf3fb..17bac5f1e 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -38,10 +38,6 @@ namespace llarp if(addr.FromString(v)) m_PrefetchAddrs.insert(addr); } - if(k == "isolate-net") - { - // TODO: implement me - } return true; } diff --git a/llarp/service/tun.cpp b/llarp/service/tun.cpp new file mode 100644 index 000000000..8201e714a --- /dev/null +++ b/llarp/service/tun.cpp @@ -0,0 +1,14 @@ +#include + +namespace llarp +{ + namespace service + { + TunEndpoint::TunEndpoint(const std::string& ifname, llarp_router* r) + : Endpoint("tun-" + ifname, r) + { + m_tunif = tuntap_init(); + tuntap_set_ifname(m_tunif, ifname.c_str()); + } + } // namespace service +} // namespace llarp \ No newline at end of file diff --git a/vendor/libtuntap-master/tuntap.h b/vendor/libtuntap-master/tuntap.h deleted file mode 100644 index 663974ae0..000000000 --- a/vendor/libtuntap-master/tuntap.h +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2012 Tristan Le Guern - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * Copyright (c) 2016 Mahdi Mokhtari - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#if defined Windows -# include -#else /* Unix */ -# include -#endif - -#if !defined Windows /* Unix :) */ -# if defined Linux -# include -# else -# include -# endif -# include -# include -#endif - -#include - -#ifndef LIBTUNTAP_H_ -# define LIBTUNTAP_H_ - -/* - * Uniformize macros - * - ETHER_ADDR_LEN: Magic number from IEEE 802.3 - * - IF_NAMESIZE: Length of interface external name - * - IF_DESCRSIZE: Length of interface description - * - TUNSDEBUG: ioctl flag to enable the debug mode of a tun device - * - TUNFD_INVALID_VALUE: Invalid value for tun_fd - */ -# if defined ETH_ALEN /* Linux */ -# define ETHER_ADDR_LEN ETH_ALEN -# elif defined Windows -# define ETHER_ADDR_LEN 6 -# endif - -# if defined IFNAMSIZ && !defined IF_NAMESIZE -# define IF_NAMESIZE IFNAMSIZ /* Historical BSD name */ -# elif !defined IF_NAMESIZE -# define IF_NAMESIZE 16 -# endif - -# define IF_DESCRSIZE 50 /* XXX: Tests needed on NetBSD and OpenBSD */ - -# if defined TUNSETDEBUG -# define TUNSDEBUG TUNSETDEBUG -# endif - -# if defined Windows -# define TUNFD_INVALID_VALUE INVALID_HANDLE_VALUE -# else /* Unix */ -# define TUNFD_INVALID_VALUE -1 -# endif - -/* - * Uniformize types - * - t_tun: tun device file descriptor - * - t_tun_in_addr: struct in_addr/IN_ADDR - * - t_tun_in6_addr: struct in6_addr/IN6_ADDR - */ -# if defined Windows -typedef HANDLE t_tun; -typedef IN_ADDR t_tun_in_addr; -typedef IN6_ADDR t_tun_in6_addr; -# else /* Unix */ -typedef int t_tun; -typedef struct in_addr t_tun_in_addr; -typedef struct in6_addr t_tun_in6_addr; -# endif - -/* - * Windows helpers - */ -# if defined Windows -# define snprintf(x, y, z, ...) _snprintf_s((x), (y), (y), (z), __VA_ARGS__); -# define strncat(x, y, z) strncat_s((x), _countof(x), (y), (z)); -# define strdup(x) _strdup(x) -# endif - -# define TUNTAP_ID_MAX 256 -# define TUNTAP_ID_ANY 257 - -# define TUNTAP_MODE_ETHERNET 0x0001 -# define TUNTAP_MODE_TUNNEL 0x0002 -# define TUNTAP_MODE_PERSIST 0x0004 - -# define TUNTAP_LOG_NONE 0x0000 -# define TUNTAP_LOG_DEBUG 0x0001 -# define TUNTAP_LOG_INFO 0x0002 -# define TUNTAP_LOG_NOTICE 0x0004 -# define TUNTAP_LOG_WARN 0x0008 -# define TUNTAP_LOG_ERR 0x0016 - -/* Versioning: 0xMMmm, with 'M' for major and 'm' for minor */ -# define TUNTAP_VERSION_MAJOR 0 -# define TUNTAP_VERSION_MINOR 3 -# define TUNTAP_VERSION ((TUNTAP_VERSION_MAJOR<<8)|TUNTAP_VERSION_MINOR) - -# define TUNTAP_GET_FD(x) (x)->tun_fd - -/* Handle Windows symbols export */ -# if defined Windows -# if defined(tuntap_EXPORTS) /* CMake generated goo */ -# define TUNTAP_EXPORT __declspec(dllexport) -# else -# define TUNTAP_EXPORT __declspec(dllimport) -# endif -# else /* Unix */ -# define TUNTAP_EXPORT -# endif - -# ifdef __cplusplus -extern "C" { -# endif - -struct device { - t_tun tun_fd; - int ctrl_sock; - int flags; /* ifr.ifr_flags on Unix */ - unsigned char hwaddr[ETHER_ADDR_LEN]; - char if_name[IF_NAMESIZE]; -#if defined(FreeBSD) - int mode; -#endif -}; - -/* User definable log callback */ -typedef void (*t_tuntap_log)(int, const char *); -TUNTAP_EXPORT t_tuntap_log tuntap_log; - -/* Portable "public" functions */ -TUNTAP_EXPORT struct device *tuntap_init(void); -TUNTAP_EXPORT int tuntap_version(void); -TUNTAP_EXPORT void tuntap_destroy(struct device *); -TUNTAP_EXPORT void tuntap_release(struct device *); -TUNTAP_EXPORT int tuntap_start(struct device *, int, int); -TUNTAP_EXPORT char *tuntap_get_ifname(struct device *); -TUNTAP_EXPORT int tuntap_set_ifname(struct device *, const char *); -TUNTAP_EXPORT char *tuntap_get_hwaddr(struct device *); -TUNTAP_EXPORT int tuntap_set_hwaddr(struct device *, const char *); -TUNTAP_EXPORT int tuntap_set_descr(struct device *, const char *); -TUNTAP_EXPORT int tuntap_up(struct device *); -TUNTAP_EXPORT int tuntap_down(struct device *); -TUNTAP_EXPORT int tuntap_get_mtu(struct device *); -TUNTAP_EXPORT int tuntap_set_mtu(struct device *, int); - -/* - * It's impossible to set single IP for `tun` devices on FreeBSD . - * FreeBSD's `tun` interface needs 2 IP addresses. - * So a new (and backward compatible) version of tuntap_set_ip() is implemented. - */ -TUNTAP_EXPORT int tuntap_set_ip(struct device *, ...); -//TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char *, int); -/*TUNTAP_EXPORT int tuntap_set_ip_old(struct device *, const char *, int);*/ -TUNTAP_EXPORT int tuntap_read(struct device *, void *, size_t); -TUNTAP_EXPORT int tuntap_write(struct device *, void *, size_t); -TUNTAP_EXPORT int tuntap_get_readable(struct device *); -TUNTAP_EXPORT int tuntap_set_nonblocking(struct device *dev, int); -TUNTAP_EXPORT int tuntap_set_debug(struct device *dev, int); - -/* Logging functions */ -TUNTAP_EXPORT void tuntap_log_set_cb(t_tuntap_log cb); -void tuntap_log_default(int, const char *); -void tuntap_log_hexdump(void *, size_t); -void tuntap_log_chksum(void *, int); - -/* OS specific functions */ -int tuntap_sys_start(struct device *, int, int); -void tuntap_sys_destroy(struct device *); -int tuntap_sys_set_hwaddr(struct device *, struct ether_addr *); -int tuntap_sys_set_ipv4(struct device *, t_tun_in_addr *, uint32_t); - -#if defined(FreeBSD) -int tuntap_sys_set_ipv4_tap(struct device *, t_tun_in_addr *, uint32_t); -int tuntap_sys_set_ipv4_tun(struct device *dev, t_tun_in_addr *s4, t_tun_in_addr *s4dest, uint32_t bits); -#endif - -int tuntap_sys_set_ipv6(struct device *, t_tun_in6_addr *, uint32_t); -int tuntap_sys_set_ifname(struct device *, const char *, size_t); -int tuntap_sys_set_descr(struct device *, const char *, size_t); - -# ifdef __cplusplus -} -# endif - -#endif diff --git a/vendor/libtuntap-master/tuntap_log.c b/vendor/libtuntap-master/tuntap_log.c deleted file mode 100644 index 1cfb151d7..000000000 --- a/vendor/libtuntap-master/tuntap_log.c +++ /dev/null @@ -1,163 +0,0 @@ -/** - * Copyright (c) 2012, PICHOT Fabien Paul Leonard - * Copyright (c) 2012, Tristan Le Guern - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - **/ - -#if defined Windows -#include -#endif -#include -#include -#include -#include - -#include "tuntap.h" - -void -tuntap_log_set_cb(t_tuntap_log cb) -{ - if(cb == NULL) - { - tuntap_log = tuntap_log_default; - } - tuntap_log = cb; -} - -void -tuntap_log_default(int level, const char *errmsg) -{ - char *name; - - switch(level) - { - case TUNTAP_LOG_DEBUG: - name = "Debug"; - break; - case TUNTAP_LOG_INFO: - name = "Info"; - break; - case TUNTAP_LOG_NOTICE: - name = "Notice"; - break; - case TUNTAP_LOG_WARN: - name = "Warning"; - break; - case TUNTAP_LOG_ERR: - name = "Error"; - break; - case TUNTAP_LOG_NONE: - default: - name = NULL; - break; - } - if(name == NULL) - { - (void)fprintf(stderr, "%s\n", errmsg); - } - else - { - (void)fprintf(stderr, "%s: %s\n", name, errmsg); - } -} - -void -tuntap_log_hexdump(void *data, size_t size) -{ - unsigned char *p = (unsigned char *)data; - unsigned int c; - size_t n; - char bytestr[4] = {0}; - char addrstr[10] = {0}; - char hexstr[16 * 3 + 5] = {0}; - char charstr[16 * 1 + 5] = {0}; - char buf[1024]; - - for(n = 1; n <= size; n++) - { - if(n % 16 == 1) - { - /* store address for this line */ - snprintf(addrstr, sizeof(addrstr), "%.4lx", - ((uintptr_t)p - (uintptr_t)data)); - } - - c = *p; - if(isalnum(c) == 0) - { - c = '.'; - } - - /* store hex str (for left side) */ - snprintf(bytestr, sizeof(bytestr), "%02X ", *p); - strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1); - - /* store char str (for right side) */ - snprintf(bytestr, sizeof(bytestr), "%c", c); - strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1); - - if(n % 16 == 0) - { - /* line completed */ - (void)memset(buf, 0, sizeof buf); - (void)snprintf(buf, sizeof buf, "[%4.4s] %-50.50s %s", addrstr, hexstr, - charstr); - tuntap_log(TUNTAP_LOG_NONE, buf); - hexstr[0] = 0; - charstr[0] = 0; - } - else if(n % 8 == 0) - { - /* half line: add whitespaces */ - strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1); - strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1); - } - p++; /* next byte */ - } - - /* print the rest of the buffer if not empty */ - if(strlen(hexstr) > 0) - { - (void)memset(buf, 0, sizeof buf); - (void)snprintf(buf, sizeof buf, "[%4.4s] %-50.50s %s", addrstr, hexstr, - charstr); - tuntap_log(TUNTAP_LOG_NONE, buf); - } -} - -void -tuntap_log_chksum(void *addr, int count) -{ - int sum; - short *sptr; - char buf[32]; - - sum = 0; - sptr = (short *)addr; - while(count > 1) - { - sum = sum + *sptr; - count = count - 2; - sptr++; - } - - addr = (char *)sptr; - if(count > 0) - sum = sum + *((char *)addr); - sum = ~sum; - - (void)memset(buf, 0, sizeof buf); - (void)snprintf(buf, sizeof buf, "Checksum of this block: %0#4x", sum); - tuntap_log(TUNTAP_LOG_NONE, buf); -} diff --git a/vendor/libtuntap-master/tuntap_log.cpp b/vendor/libtuntap-master/tuntap_log.cpp new file mode 100644 index 000000000..1e93da089 --- /dev/null +++ b/vendor/libtuntap-master/tuntap_log.cpp @@ -0,0 +1,156 @@ +/** + * Copyright (c) 2012, PICHOT Fabien Paul Leonard + * Copyright (c) 2012, Tristan Le Guern + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + **/ + +#if defined Windows +#include +#endif +#include +#include +#include +#include + +#include +#include "tuntap.h" + +extern "C" +{ + void + tuntap_log_set_cb(t_tuntap_log cb) + { + if(cb == NULL) + { + tuntap_log = tuntap_log_default; + } + tuntap_log = cb; + } + + void + tuntap_log_default(int level, const char *errmsg) + { + switch(level) + { + case TUNTAP_LOG_DEBUG: + llarp::LogDebug(errmsg); + break; + case TUNTAP_LOG_INFO: + llarp::LogInfo(errmsg); + break; + case TUNTAP_LOG_NOTICE: + llarp::LogInfo(errmsg); + break; + case TUNTAP_LOG_WARN: + llarp::LogWarn(errmsg); + break; + case TUNTAP_LOG_ERR: + llarp::LogError(errmsg); + break; + case TUNTAP_LOG_NONE: + default: + return; + } + } + + void + tuntap_log_hexdump(void *data, size_t size) + { + unsigned char *p = (unsigned char *)data; + unsigned int c; + size_t n; + char bytestr[4] = {0}; + char addrstr[10] = {0}; + char hexstr[16 * 3 + 5] = {0}; + char charstr[16 * 1 + 5] = {0}; + char buf[1024]; + + for(n = 1; n <= size; n++) + { + if(n % 16 == 1) + { + /* store address for this line */ + snprintf(addrstr, sizeof(addrstr), "%.4lx", + ((uintptr_t)p - (uintptr_t)data)); + } + + c = *p; + if(isalnum(c) == 0) + { + c = '.'; + } + + /* store hex str (for left side) */ + snprintf(bytestr, sizeof(bytestr), "%02X ", *p); + strncat(hexstr, bytestr, sizeof(hexstr) - strlen(hexstr) - 1); + + /* store char str (for right side) */ + snprintf(bytestr, sizeof(bytestr), "%c", c); + strncat(charstr, bytestr, sizeof(charstr) - strlen(charstr) - 1); + + if(n % 16 == 0) + { + /* line completed */ + (void)memset(buf, 0, sizeof buf); + (void)snprintf(buf, sizeof buf, "[%4.4s] %-50.50s %s", addrstr, + hexstr, charstr); + tuntap_log(TUNTAP_LOG_NONE, buf); + hexstr[0] = 0; + charstr[0] = 0; + } + else if(n % 8 == 0) + { + /* half line: add whitespaces */ + strncat(hexstr, " ", sizeof(hexstr) - strlen(hexstr) - 1); + strncat(charstr, " ", sizeof(charstr) - strlen(charstr) - 1); + } + p++; /* next byte */ + } + + /* print the rest of the buffer if not empty */ + if(strlen(hexstr) > 0) + { + (void)memset(buf, 0, sizeof buf); + (void)snprintf(buf, sizeof buf, "[%4.4s] %-50.50s %s", addrstr, hexstr, + charstr); + tuntap_log(TUNTAP_LOG_NONE, buf); + } + } + + void + tuntap_log_chksum(void *addr, int count) + { + int sum; + short *sptr; + char buf[32]; + + sum = 0; + sptr = (short *)addr; + while(count > 1) + { + sum = sum + *sptr; + count = count - 2; + sptr++; + } + + addr = (char *)sptr; + if(count > 0) + sum = sum + *((char *)addr); + sum = ~sum; + + (void)memset(buf, 0, sizeof buf); + (void)snprintf(buf, sizeof buf, "Checksum of this block: %0#4x", sum); + tuntap_log(TUNTAP_LOG_NONE, buf); + } +} \ No newline at end of file