mirror of https://github.com/oxen-io/lokinet
more boilerplate and enable tun/tap build
parent
c71d0b2bb3
commit
d5532e4de4
@ -1,18 +0,0 @@
|
|||||||
#ifndef LLARP_ENDPOINT_HANDLER_HPP
|
|
||||||
#define LLARP_ENDPOINT_HANDLER_HPP
|
|
||||||
|
|
||||||
#include <llarp/buffer.h>
|
|
||||||
|
|
||||||
namespace llarp
|
|
||||||
{
|
|
||||||
// hidden service endpoint handler
|
|
||||||
struct IEndpointHandler
|
|
||||||
{
|
|
||||||
~IEndpointHandler(){};
|
|
||||||
|
|
||||||
virtual void
|
|
||||||
HandleMessage(llarp_buffer_t buf) = 0;
|
|
||||||
};
|
|
||||||
} // namespace llarp
|
|
||||||
|
|
||||||
#endif
|
|
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef LLARP_HANDLERS_TUN_HPP
|
||||||
|
#define LLARP_HANDLERS_TUN_HPP
|
||||||
|
#include <llarp/service/handler.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace handlers
|
||||||
|
{
|
||||||
|
struct TunHandler : public service::IDataHandler
|
||||||
|
{
|
||||||
|
};
|
||||||
|
} // namespace handlers
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef LLARP_SERVICE_HANDLER_HPP
|
||||||
|
#define LLARP_SERVICE_HANDLER_HPP
|
||||||
|
#include <llarp/service/protocol.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace service
|
||||||
|
{
|
||||||
|
struct IDataHandler
|
||||||
|
{
|
||||||
|
virtual void
|
||||||
|
HandleDataMessage(ProtocolMessage* msg) = 0;
|
||||||
|
};
|
||||||
|
} // namespace service
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef LLARP_SERVICE_TUN_HPP
|
||||||
|
#define LLARP_SERVICE_TUN_HPP
|
||||||
|
#include <tuntap.h>
|
||||||
|
#include <llarp/service/endpoint.hpp>
|
||||||
|
|
||||||
|
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
|
@ -0,0 +1,252 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2012 Tristan Le Guern <leguern AT medu DOT se>
|
||||||
|
*
|
||||||
|
* 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 <mokhi64@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <sys/types.h>
|
||||||
|
#if defined Windows
|
||||||
|
#include <In6addr.h>
|
||||||
|
#else /* Unix */
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined Windows /* Unix :) */
|
||||||
|
#if defined Linux
|
||||||
|
#include <linux/if.h>
|
||||||
|
#else
|
||||||
|
#include <net/if.h>
|
||||||
|
#endif
|
||||||
|
#include <netinet/if_ether.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#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
|
@ -0,0 +1,14 @@
|
|||||||
|
#include <llarp/service/tun.hpp>
|
||||||
|
|
||||||
|
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
|
@ -1,213 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2012 Tristan Le Guern <leguern AT medu DOT se>
|
|
||||||
*
|
|
||||||
* 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 <mokhi64@gmail.com>
|
|
||||||
*
|
|
||||||
* 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 <sys/types.h>
|
|
||||||
#if defined Windows
|
|
||||||
# include <In6addr.h>
|
|
||||||
#else /* Unix */
|
|
||||||
# include <sys/socket.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined Windows /* Unix :) */
|
|
||||||
# if defined Linux
|
|
||||||
# include <linux/if.h>
|
|
||||||
# else
|
|
||||||
# include <net/if.h>
|
|
||||||
# endif
|
|
||||||
# include <netinet/in.h>
|
|
||||||
# include <netinet/if_ether.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#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
|
|
@ -1,163 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012, PICHOT Fabien Paul Leonard <pichot.fabienATgmail.com>
|
|
||||||
* Copyright (c) 2012, Tristan Le Guern <tleguern@bouledef.eu>
|
|
||||||
*
|
|
||||||
* 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 <windows.h>
|
|
||||||
#endif
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
@ -0,0 +1,156 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2012, PICHOT Fabien Paul Leonard <pichot.fabienATgmail.com>
|
||||||
|
* Copyright (c) 2012, Tristan Le Guern <tleguern@bouledef.eu>
|
||||||
|
*
|
||||||
|
* 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 <windows.h>
|
||||||
|
#endif
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <llarp/logger.hpp>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue