Normalized indentation for entire project to TABS
This commit is contained in:
@@ -41,123 +41,123 @@
|
||||
|
||||
// Debug output colors
|
||||
#if defined(__APPLE__)
|
||||
#include "TargetConditionals.h"
|
||||
#include "TargetConditionals.h"
|
||||
#endif
|
||||
#if defined(ZT_COLOR) && !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(__APP_FRAMEWORK__)
|
||||
#define ZT_RED "\x1B[31m"
|
||||
#define ZT_GRN "\x1B[32m"
|
||||
#define ZT_YEL "\x1B[33m"
|
||||
#define ZT_BLU "\x1B[34m"
|
||||
#define ZT_MAG "\x1B[35m"
|
||||
#define ZT_CYN "\x1B[36m"
|
||||
#define ZT_WHT "\x1B[37m"
|
||||
#define ZT_RESET "\x1B[0m"
|
||||
#define ZT_RED "\x1B[31m"
|
||||
#define ZT_GRN "\x1B[32m"
|
||||
#define ZT_YEL "\x1B[33m"
|
||||
#define ZT_BLU "\x1B[34m"
|
||||
#define ZT_MAG "\x1B[35m"
|
||||
#define ZT_CYN "\x1B[36m"
|
||||
#define ZT_WHT "\x1B[37m"
|
||||
#define ZT_RESET "\x1B[0m"
|
||||
#else
|
||||
#define ZT_RED
|
||||
#define ZT_GRN
|
||||
#define ZT_YEL
|
||||
#define ZT_BLU
|
||||
#define ZT_MAG
|
||||
#define ZT_CYN
|
||||
#define ZT_WHT
|
||||
#define ZT_RESET
|
||||
#define ZT_RED
|
||||
#define ZT_GRN
|
||||
#define ZT_YEL
|
||||
#define ZT_BLU
|
||||
#define ZT_MAG
|
||||
#define ZT_CYN
|
||||
#define ZT_WHT
|
||||
#define ZT_RESET
|
||||
#endif
|
||||
|
||||
#define ZT_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // short
|
||||
|
||||
/*
|
||||
#ifdef __linux__
|
||||
#define ZT_THREAD_ID 0 // (long)getpid()
|
||||
#define ZT_THREAD_ID 0 // (long)getpid()
|
||||
#elif __FreeBSD__
|
||||
#define ZT_THREAD_ID 0 // (long)getpid()
|
||||
#define ZT_THREAD_ID 0 // (long)getpid()
|
||||
#elif __APPLE__
|
||||
#define ZT_THREAD_ID 0 // (long)syscall(SYS_thread_selfid)
|
||||
#define ZT_THREAD_ID 0 // (long)syscall(SYS_thread_selfid)
|
||||
#endif
|
||||
*/
|
||||
|
||||
#define ZT_THREAD_ID 0
|
||||
|
||||
#if defined(__JNI_LIB__)
|
||||
#include <jni.h>
|
||||
#include <jni.h>
|
||||
#endif
|
||||
#if defined(__ANDROID__)
|
||||
#include <android/log.h>
|
||||
#define ZT_LOG_TAG "ZTSDK"
|
||||
#include <android/log.h>
|
||||
#define ZT_LOG_TAG "ZTSDK"
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_TEST
|
||||
#define DEBUG_TEST(fmt, args...) fprintf(stderr, ZT_CYN "TEST [%d] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#else
|
||||
#define DEBUG_ERROR(fmt, args...)
|
||||
#endif
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_TEST
|
||||
#define DEBUG_TEST(fmt, args...) fprintf(stderr, ZT_CYN "TEST [%d] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#else
|
||||
#define DEBUG_ERROR(fmt, args...)
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_ERROR
|
||||
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, ZT_RED "ERROR[%d] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#else
|
||||
#define DEBUG_ERROR(fmt, args...)
|
||||
#endif
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_ERROR
|
||||
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, ZT_RED "ERROR[%d] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#else
|
||||
#define DEBUG_ERROR(fmt, args...)
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_INFO
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_BLANK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:" fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_ATTN(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_STACK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"STACK: %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...) fprintf(stderr, \
|
||||
"INFO [%d] : %16s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_ATTN(fmt, args...) fprintf(stderr, ZT_CYN \
|
||||
"ATTN [%d] : %16s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL \
|
||||
"STACK[%d] : %16s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, \
|
||||
"INFO [%d] : %16s:%5d:" fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...)
|
||||
#define DEBUG_BLANK(fmt, args...)
|
||||
#define DEBUG_ATTN(fmt, args...)
|
||||
#define DEBUG_STACK(fmt, args...)
|
||||
#endif
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_INFO
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_BLANK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:" fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_ATTN(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"INFO : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#define DEBUG_STACK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"STACK: %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...) fprintf(stderr, \
|
||||
"INFO [%d] : %16s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_ATTN(fmt, args...) fprintf(stderr, ZT_CYN \
|
||||
"ATTN [%d] : %16s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL \
|
||||
"STACK[%d] : %16s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, \
|
||||
"INFO [%d] : %16s:%5d:" fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_INFO(fmt, args...)
|
||||
#define DEBUG_BLANK(fmt, args...)
|
||||
#define DEBUG_ATTN(fmt, args...)
|
||||
#define DEBUG_STACK(fmt, args...)
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"TRANS : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, ZT_GRN "TRANS[%ld] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...)
|
||||
#endif
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"TRANS : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, ZT_GRN "TRANS[%ld] : %16s:%5d:%25s: " fmt \
|
||||
"\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_TRANS(fmt, args...)
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_EXTRA
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"EXTRA : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, \
|
||||
"EXTRA[%d] : %16s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...)
|
||||
#endif
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_EXTRA
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"EXTRA : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, \
|
||||
"EXTRA[%d] : %16s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_EXTRA(fmt, args...)
|
||||
#endif
|
||||
|
||||
#if ZT_DEBUG_LEVEL >= ZT_MSG_FLOW
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_FLOW(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"FLOW : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_FLOW(fmt, args...) fprintf(stderr, "FLOW [%ld] : %16s:%5d:%25s: " fmt "\n", \
|
||||
ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_FLOW(fmt, args...)
|
||||
#endif
|
||||
#if defined(__ANDROID__)
|
||||
#define DEBUG_FLOW(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \
|
||||
"FLOW : %16s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args))
|
||||
#else
|
||||
#define DEBUG_FLOW(fmt, args...) fprintf(stderr, "FLOW [%ld] : %16s:%5d:%25s: " fmt "\n", \
|
||||
ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
|
||||
#endif
|
||||
#else
|
||||
#define DEBUG_FLOW(fmt, args...)
|
||||
#endif
|
||||
@@ -45,33 +45,33 @@
|
||||
// Normally defined in linux/if_packet.h, defined here so we can offer a linux-like
|
||||
// raw socket API on non-linux platforms
|
||||
struct sockaddr_ll {
|
||||
unsigned short sll_family; /* Always AF_PACKET */
|
||||
unsigned short sll_protocol; /* Physical layer protocol */
|
||||
int sll_ifindex; /* Interface number */
|
||||
unsigned short sll_hatype; /* ARP hardware type */
|
||||
unsigned char sll_pkttype; /* Packet type */
|
||||
unsigned char sll_halen; /* Length of address */
|
||||
unsigned char sll_addr[8]; /* Physical layer address */
|
||||
unsigned short sll_family; /* Always AF_PACKET */
|
||||
unsigned short sll_protocol; /* Physical layer protocol */
|
||||
int sll_ifindex; /* Interface number */
|
||||
unsigned short sll_hatype; /* ARP hardware type */
|
||||
unsigned char sll_pkttype; /* Packet type */
|
||||
unsigned char sll_halen; /* Length of address */
|
||||
unsigned char sll_addr[8]; /* Physical layer address */
|
||||
};
|
||||
|
||||
/*
|
||||
struct zts_ifreq {
|
||||
char ifr_name[IFNAMSIZ]; // Interface name
|
||||
union {
|
||||
struct sockaddr ifr_addr;
|
||||
struct sockaddr ifr_dstaddr;
|
||||
struct sockaddr ifr_broadaddr;
|
||||
struct sockaddr ifr_netmask;
|
||||
struct sockaddr ifr_hwaddr;
|
||||
short ifr_flags;
|
||||
int ifr_ifindex;
|
||||
int ifr_metric;
|
||||
int ifr_mtu;
|
||||
struct ifmap ifr_map;
|
||||
char ifr_slave[IFNAMSIZ];
|
||||
char ifr_newname[IFNAMSIZ];
|
||||
char *ifr_data;
|
||||
};
|
||||
char ifr_name[IFNAMSIZ]; // Interface name
|
||||
union {
|
||||
struct sockaddr ifr_addr;
|
||||
struct sockaddr ifr_dstaddr;
|
||||
struct sockaddr ifr_broadaddr;
|
||||
struct sockaddr ifr_netmask;
|
||||
struct sockaddr ifr_hwaddr;
|
||||
short ifr_flags;
|
||||
int ifr_ifindex;
|
||||
int ifr_metric;
|
||||
int ifr_mtu;
|
||||
struct ifmap ifr_map;
|
||||
char ifr_slave[IFNAMSIZ];
|
||||
char ifr_newname[IFNAMSIZ];
|
||||
char *ifr_data;
|
||||
};
|
||||
};
|
||||
*/
|
||||
|
||||
@@ -467,14 +467,14 @@ int zts_shutdown(ZT_SHUTDOWN_SIG);
|
||||
|
||||
namespace ZeroTier
|
||||
{
|
||||
class picoTCP;
|
||||
extern ZeroTier::picoTCP *picostack;
|
||||
class picoTCP;
|
||||
extern ZeroTier::picoTCP *picostack;
|
||||
|
||||
class lwIP;
|
||||
extern ZeroTier::lwIP *lwipstack;
|
||||
class lwIP;
|
||||
extern ZeroTier::lwIP *lwipstack;
|
||||
|
||||
class SocketTap;
|
||||
struct InetAddress;
|
||||
class SocketTap;
|
||||
struct InetAddress;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -32,158 +32,158 @@
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
template<typename T> class RingBuffer {
|
||||
template<typename T> class RingBuffer {
|
||||
|
||||
private:
|
||||
T * buf;
|
||||
size_t size;
|
||||
size_t begin;
|
||||
size_t end;
|
||||
bool wrap;
|
||||
private:
|
||||
T * buf;
|
||||
size_t size;
|
||||
size_t begin;
|
||||
size_t end;
|
||||
bool wrap;
|
||||
|
||||
public:
|
||||
/**
|
||||
* create a RingBuffer with space for up to size elements.
|
||||
*/
|
||||
explicit RingBuffer(size_t size)
|
||||
: size(size),
|
||||
begin(0),
|
||||
end(0),
|
||||
wrap(false)
|
||||
{
|
||||
buf = new T[size];
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* create a RingBuffer with space for up to size elements.
|
||||
*/
|
||||
explicit RingBuffer(size_t size)
|
||||
: size(size),
|
||||
begin(0),
|
||||
end(0),
|
||||
wrap(false)
|
||||
{
|
||||
buf = new T[size];
|
||||
}
|
||||
|
||||
RingBuffer(const RingBuffer<T> & ring)
|
||||
{
|
||||
this(ring.size);
|
||||
begin = ring.begin;
|
||||
end = ring.end;
|
||||
memcpy(buf, ring.buf, sizeof(T) * size);
|
||||
}
|
||||
RingBuffer(const RingBuffer<T> & ring)
|
||||
{
|
||||
this(ring.size);
|
||||
begin = ring.begin;
|
||||
end = ring.end;
|
||||
memcpy(buf, ring.buf, sizeof(T) * size);
|
||||
}
|
||||
|
||||
~RingBuffer()
|
||||
{
|
||||
delete[] buf;
|
||||
}
|
||||
~RingBuffer()
|
||||
{
|
||||
delete[] buf;
|
||||
}
|
||||
|
||||
// get a reference to the underlying buffer
|
||||
T* get_buf()
|
||||
{
|
||||
return buf + begin;
|
||||
}
|
||||
// get a reference to the underlying buffer
|
||||
T* get_buf()
|
||||
{
|
||||
return buf + begin;
|
||||
}
|
||||
|
||||
// adjust buffer index pointer as if we copied data in
|
||||
size_t produce(size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
// adjust buffer index pointer as if we copied data in
|
||||
size_t produce(size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
end = (end + first_chunk) % size;
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
end = (end + first_chunk) % size;
|
||||
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// adjust buffer index pointer as if we copied data out
|
||||
size_t consume(size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
// adjust buffer index pointer as if we copied data out
|
||||
size_t consume(size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
begin = (begin + first_chunk) % size;
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
begin = (begin + first_chunk) % size;
|
||||
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t write(const T * data, size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
size_t write(const T * data, size_t n)
|
||||
{
|
||||
n = std::min(n, getFree());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
memcpy(buf + end, data, first_chunk * sizeof(T));
|
||||
end = (end + first_chunk) % size;
|
||||
const size_t first_chunk = std::min(n, size - end);
|
||||
memcpy(buf + end, data, first_chunk * sizeof(T));
|
||||
end = (end + first_chunk) % size;
|
||||
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(buf + end, data + first_chunk, second_chunk * sizeof(T));
|
||||
end = (end + second_chunk) % size;
|
||||
}
|
||||
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
if (begin == end) {
|
||||
wrap = true;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t read(T * dest, size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
size_t read(T * dest, size_t n)
|
||||
{
|
||||
n = std::min(n, count());
|
||||
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
if (n == 0) {
|
||||
return n;
|
||||
}
|
||||
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
if (wrap) {
|
||||
wrap = false;
|
||||
}
|
||||
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
memcpy(dest, buf + begin, first_chunk * sizeof(T));
|
||||
begin = (begin + first_chunk) % size;
|
||||
const size_t first_chunk = std::min(n, size - begin);
|
||||
memcpy(dest, buf + begin, first_chunk * sizeof(T));
|
||||
begin = (begin + first_chunk) % size;
|
||||
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
if (first_chunk < n) {
|
||||
const size_t second_chunk = n - first_chunk;
|
||||
memcpy(dest + first_chunk, buf + begin, second_chunk * sizeof(T));
|
||||
begin = (begin + second_chunk) % size;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t count() {
|
||||
if (end == begin) {
|
||||
return wrap ? size : 0;
|
||||
}
|
||||
else if (end > begin) {
|
||||
return end - begin;
|
||||
}
|
||||
else {
|
||||
return size + end - begin;
|
||||
}
|
||||
}
|
||||
size_t count() {
|
||||
if (end == begin) {
|
||||
return wrap ? size : 0;
|
||||
}
|
||||
else if (end > begin) {
|
||||
return end - begin;
|
||||
}
|
||||
else {
|
||||
return size + end - begin;
|
||||
}
|
||||
}
|
||||
|
||||
size_t getFree() {
|
||||
return size - count();
|
||||
}
|
||||
};
|
||||
size_t getFree() {
|
||||
return size - count();
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // ZT_RINGBUFFER_HPP
|
||||
|
||||
@@ -81,7 +81,7 @@ namespace ZeroTier {
|
||||
void (*handler)(void *,void*,uint64_t,const MAC &,const MAC &,
|
||||
unsigned int,unsigned int,const void *,unsigned int),
|
||||
void *arg) :
|
||||
_handler(handler),
|
||||
_handler(handler),
|
||||
_homePath(homePath),
|
||||
_arg(arg),
|
||||
_enabled(true),
|
||||
@@ -185,12 +185,12 @@ namespace ZeroTier {
|
||||
const void *data,unsigned int len)
|
||||
{
|
||||
#if defined(STACK_PICO)
|
||||
if(picostack)
|
||||
picostack->pico_rx(this,from,to,etherType,data,len);
|
||||
if(picostack)
|
||||
picostack->pico_rx(this,from,to,etherType,data,len);
|
||||
#endif
|
||||
#if defined(STACK_LWIP)
|
||||
if(lwipstack)
|
||||
lwipstack->lwip_rx(this,from,to,etherType,data,len);
|
||||
if(lwipstack)
|
||||
lwipstack->lwip_rx(this,from,to,etherType,data,len);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -264,11 +264,11 @@ namespace ZeroTier {
|
||||
Connection *conn = (Connection*)*uptr;
|
||||
if(!conn)
|
||||
return;
|
||||
if(len){
|
||||
if(len){
|
||||
|
||||
Write(conn, data, len);
|
||||
}
|
||||
return;
|
||||
Write(conn, data, len);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void SocketTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool stack_invoked)
|
||||
@@ -337,13 +337,13 @@ namespace ZeroTier {
|
||||
|
||||
int SocketTap::Write(Connection *conn, void *data, ssize_t len) {
|
||||
if(conn->socket_type == SOCK_RAW) { // we don't want to use a stack, just VL2
|
||||
struct ether_header *eh = (struct ether_header *) data;
|
||||
MAC src_mac;
|
||||
MAC dest_mac;
|
||||
src_mac.setTo(eh->ether_shost, 6);
|
||||
dest_mac.setTo(eh->ether_dhost, 6);
|
||||
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
|
||||
return len;
|
||||
struct ether_header *eh = (struct ether_header *) data;
|
||||
MAC src_mac;
|
||||
MAC dest_mac;
|
||||
src_mac.setTo(eh->ether_shost, 6);
|
||||
dest_mac.setTo(eh->ether_dhost, 6);
|
||||
_handler(_arg,NULL,_nwid,src_mac,dest_mac, Utils::ntoh((uint16_t)eh->ether_type),0, ((char*)data) + sizeof(struct ether_header),len - sizeof(struct ether_header));
|
||||
return len;
|
||||
}
|
||||
|
||||
#if defined(STACK_PICO)
|
||||
|
||||
@@ -137,22 +137,22 @@ namespace ZeroTier {
|
||||
/*
|
||||
* For moving data onto the ZeroTier virtual wire
|
||||
*/
|
||||
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,
|
||||
const void *,unsigned int);
|
||||
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,
|
||||
const void *,unsigned int);
|
||||
|
||||
/*
|
||||
* Signals us to close the TcpConnection associated with this PhySocket
|
||||
*/
|
||||
* Signals us to close the TcpConnection associated with this PhySocket
|
||||
*/
|
||||
void phyOnUnixClose(PhySocket *sock,void **uptr);
|
||||
|
||||
/*
|
||||
* Notifies us that there is data to be read from an application's socket
|
||||
*/
|
||||
/*
|
||||
* Notifies us that there is data to be read from an application's socket
|
||||
*/
|
||||
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len);
|
||||
|
||||
/*
|
||||
* Notifies us that we can write to an application's socket
|
||||
*/
|
||||
* Notifies us that we can write to an application's socket
|
||||
*/
|
||||
void phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked);
|
||||
|
||||
/****************************************************************************/
|
||||
@@ -173,14 +173,14 @@ namespace ZeroTier {
|
||||
/* Guarded RX Frame Buffer for picoTCP */
|
||||
/****************************************************************************/
|
||||
|
||||
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
|
||||
int pico_frame_rxbuf_tot;
|
||||
Mutex _pico_frame_rxbuf_m;
|
||||
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
|
||||
int pico_frame_rxbuf_tot;
|
||||
Mutex _pico_frame_rxbuf_m;
|
||||
#endif
|
||||
|
||||
#if defined(STACK_LWIP)
|
||||
netif lwipdev;
|
||||
netif lwipdev6;
|
||||
netif lwipdev;
|
||||
netif lwipdev6;
|
||||
#endif
|
||||
|
||||
static int devno;
|
||||
@@ -190,13 +190,13 @@ namespace ZeroTier {
|
||||
std::vector<InetAddress> _ips;
|
||||
|
||||
std::string _homePath;
|
||||
void *_arg;
|
||||
void *_arg;
|
||||
volatile bool _enabled;
|
||||
volatile bool _run;
|
||||
MAC _mac;
|
||||
unsigned int _mtu;
|
||||
uint64_t _nwid;
|
||||
PhySocket *_unixListenSocket;
|
||||
unsigned int _mtu;
|
||||
uint64_t _nwid;
|
||||
PhySocket *_unixListenSocket;
|
||||
Phy<SocketTap *> _phy;
|
||||
|
||||
std::vector<Connection*> _Connections;
|
||||
@@ -245,14 +245,14 @@ namespace ZeroTier {
|
||||
int Read(PhySocket *sock,void **uptr,bool stack_invoked);
|
||||
|
||||
/*
|
||||
* Move data from application's "socket" into network stack
|
||||
*/
|
||||
* Move data from application's "socket" into network stack
|
||||
*/
|
||||
int Write(Connection *conn, void *data, ssize_t len);
|
||||
|
||||
/*
|
||||
* Closes a Connection
|
||||
*/
|
||||
int Close(Connection *conn);
|
||||
int Close(Connection *conn);
|
||||
|
||||
/*
|
||||
* Disposes of previously-closed Connections
|
||||
|
||||
@@ -38,14 +38,14 @@
|
||||
#define ip4_addr4b(ipaddr) (((u8_t*)(ipaddr))[3])
|
||||
inline ip_addr_t convert_ip(struct sockaddr_in * addr)
|
||||
{
|
||||
ip_addr_t conn_addr;
|
||||
struct sockaddr_in *ipv4 = addr;
|
||||
short a = ip4_addr1b(&(ipv4->sin_addr));
|
||||
short b = ip4_addr2b(&(ipv4->sin_addr));
|
||||
short c = ip4_addr3b(&(ipv4->sin_addr));
|
||||
short d = ip4_addr4b(&(ipv4->sin_addr));
|
||||
IP4_ADDR(&conn_addr, a,b,c,d);
|
||||
return conn_addr;
|
||||
ip_addr_t conn_addr;
|
||||
struct sockaddr_in *ipv4 = addr;
|
||||
short a = ip4_addr1b(&(ipv4->sin_addr));
|
||||
short b = ip4_addr2b(&(ipv4->sin_addr));
|
||||
short c = ip4_addr3b(&(ipv4->sin_addr));
|
||||
short d = ip4_addr4b(&(ipv4->sin_addr));
|
||||
IP4_ADDR(&conn_addr, a,b,c,d);
|
||||
return conn_addr;
|
||||
}
|
||||
|
||||
#endif // STACK_LWIP && LIBZT_IPV4
|
||||
|
||||
2754
src/libzt.cpp
2754
src/libzt.cpp
File diff suppressed because it is too large
Load Diff
600
src/lwIP.cpp
600
src/lwIP.cpp
@@ -42,352 +42,352 @@ err_t tapif_init(struct netif *netif)
|
||||
|
||||
err_t low_level_output(struct netif *netif, struct pbuf *p)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
struct pbuf *q;
|
||||
char buf[ZT_MAX_MTU+32];
|
||||
char *bufptr;
|
||||
int totalLength = 0;
|
||||
DEBUG_INFO();
|
||||
struct pbuf *q;
|
||||
char buf[ZT_MAX_MTU+32];
|
||||
char *bufptr;
|
||||
int totalLength = 0;
|
||||
|
||||
ZeroTier::SocketTap *tap = (ZeroTier::SocketTap*)netif->state;
|
||||
bufptr = buf;
|
||||
// Copy data from each pbuf, one at a time
|
||||
for(q = p; q != NULL; q = q->next) {
|
||||
memcpy(bufptr, q->payload, q->len);
|
||||
bufptr += q->len;
|
||||
totalLength += q->len;
|
||||
}
|
||||
// Split ethernet header and feed into handler
|
||||
struct eth_hdr *ethhdr;
|
||||
ethhdr = (struct eth_hdr *)buf;
|
||||
ZeroTier::SocketTap *tap = (ZeroTier::SocketTap*)netif->state;
|
||||
bufptr = buf;
|
||||
// Copy data from each pbuf, one at a time
|
||||
for(q = p; q != NULL; q = q->next) {
|
||||
memcpy(bufptr, q->payload, q->len);
|
||||
bufptr += q->len;
|
||||
totalLength += q->len;
|
||||
}
|
||||
// Split ethernet header and feed into handler
|
||||
struct eth_hdr *ethhdr;
|
||||
ethhdr = (struct eth_hdr *)buf;
|
||||
|
||||
ZeroTier::MAC src_mac;
|
||||
ZeroTier::MAC dest_mac;
|
||||
src_mac.setTo(ethhdr->src.addr, 6);
|
||||
dest_mac.setTo(ethhdr->dest.addr, 6);
|
||||
ZeroTier::MAC src_mac;
|
||||
ZeroTier::MAC dest_mac;
|
||||
src_mac.setTo(ethhdr->src.addr, 6);
|
||||
dest_mac.setTo(ethhdr->dest.addr, 6);
|
||||
|
||||
tap->_handler(tap->_arg,NULL,tap->_nwid,src_mac,dest_mac,
|
||||
ZeroTier::Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
|
||||
return ERR_OK;
|
||||
tap->_handler(tap->_arg,NULL,tap->_nwid,src_mac,dest_mac,
|
||||
ZeroTier::Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
namespace ZeroTier
|
||||
{
|
||||
void lwIP::lwip_init_interface(SocketTap *tap, const InetAddress &ip)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
Mutex::Lock _l(tap->_ips_m);
|
||||
void lwIP::lwip_init_interface(SocketTap *tap, const InetAddress &ip)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
Mutex::Lock _l(tap->_ips_m);
|
||||
|
||||
if (std::find(tap->_ips.begin(),tap->_ips.end(),ip) == tap->_ips.end()) {
|
||||
tap->_ips.push_back(ip);
|
||||
std::sort(tap->_ips.begin(),tap->_ips.end());
|
||||
if (std::find(tap->_ips.begin(),tap->_ips.end(),ip) == tap->_ips.end()) {
|
||||
tap->_ips.push_back(ip);
|
||||
std::sort(tap->_ips.begin(),tap->_ips.end());
|
||||
#if defined(LIBZT_IPV4)
|
||||
if (ip.isV4()) {
|
||||
// Set IP
|
||||
static ip_addr_t ipaddr, netmask, gw;
|
||||
IP4_ADDR(&gw,127,0,0,1);
|
||||
ipaddr.addr = *((u32_t *)ip.rawIpData());
|
||||
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
|
||||
netif_add(&(tap->lwipdev),&ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
|
||||
tap->lwipdev.state = tap;
|
||||
tap->lwipdev.output = etharp_output;
|
||||
tap->_mac.copyTo(tap->lwipdev.hwaddr, 6);
|
||||
tap->lwipdev.mtu = tap->_mtu;
|
||||
tap->lwipdev.name[0] = 'l';
|
||||
tap->lwipdev.name[1] = '4';
|
||||
tap->lwipdev.linkoutput = low_level_output;
|
||||
tap->lwipdev.hwaddr_len = 6;
|
||||
tap->lwipdev.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
|
||||
netif_set_default(&(tap->lwipdev));
|
||||
netif_set_up(&(tap->lwipdev));
|
||||
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
|
||||
}
|
||||
if (ip.isV4()) {
|
||||
// Set IP
|
||||
static ip_addr_t ipaddr, netmask, gw;
|
||||
IP4_ADDR(&gw,127,0,0,1);
|
||||
ipaddr.addr = *((u32_t *)ip.rawIpData());
|
||||
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
|
||||
netif_add(&(tap->lwipdev),&ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
|
||||
tap->lwipdev.state = tap;
|
||||
tap->lwipdev.output = etharp_output;
|
||||
tap->_mac.copyTo(tap->lwipdev.hwaddr, 6);
|
||||
tap->lwipdev.mtu = tap->_mtu;
|
||||
tap->lwipdev.name[0] = 'l';
|
||||
tap->lwipdev.name[1] = '4';
|
||||
tap->lwipdev.linkoutput = low_level_output;
|
||||
tap->lwipdev.hwaddr_len = 6;
|
||||
tap->lwipdev.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
|
||||
netif_set_default(&(tap->lwipdev));
|
||||
netif_set_up(&(tap->lwipdev));
|
||||
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
|
||||
}
|
||||
#endif
|
||||
#if defined(LIBZT_IPV6)
|
||||
if(ip.isV6()) {
|
||||
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
|
||||
static ip6_addr_t addr6;
|
||||
struct sockaddr_in6 in6;
|
||||
memcpy(in6.sin6_addr.s6_addr,ip.rawIpData(),16);
|
||||
in6_to_ip6((ip6_addr *)&addr6, &in6);
|
||||
tap->lwipdev6.mtu = tap->_mtu;
|
||||
tap->lwipdev6.name[0] = 'l';
|
||||
tap->lwipdev6.name[1] = '6';
|
||||
tap->lwipdev6.hwaddr_len = 6;
|
||||
tap->lwipdev6.linkoutput = low_level_output;
|
||||
tap->lwipdev6.ip6_autoconfig_enabled = 1;
|
||||
tap->_mac.copyTo(tap->lwipdev6.hwaddr, tap->lwipdev6.hwaddr_len);
|
||||
netif_create_ip6_linklocal_address(&(tap->lwipdev6), 1);
|
||||
netif_add(&(tap->lwipdev6), NULL, tapif_init, ethernet_input);
|
||||
netif_set_default(&(tap->lwipdev6));
|
||||
netif_set_up(&(tap->lwipdev6));
|
||||
netif_ip6_addr_set_state(&(tap->lwipdev6), 1, IP6_ADDR_TENTATIVE);
|
||||
ip6_addr_copy(ip_2_ip6(tap->lwipdev6.ip6_addr[1]), addr6);
|
||||
tap->lwipdev6.output_ip6 = ethip6_output;
|
||||
tap->lwipdev6.state = tap;
|
||||
tap->lwipdev6.flags = NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
|
||||
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
|
||||
}
|
||||
if(ip.isV6()) {
|
||||
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
|
||||
static ip6_addr_t addr6;
|
||||
struct sockaddr_in6 in6;
|
||||
memcpy(in6.sin6_addr.s6_addr,ip.rawIpData(),16);
|
||||
in6_to_ip6((ip6_addr *)&addr6, &in6);
|
||||
tap->lwipdev6.mtu = tap->_mtu;
|
||||
tap->lwipdev6.name[0] = 'l';
|
||||
tap->lwipdev6.name[1] = '6';
|
||||
tap->lwipdev6.hwaddr_len = 6;
|
||||
tap->lwipdev6.linkoutput = low_level_output;
|
||||
tap->lwipdev6.ip6_autoconfig_enabled = 1;
|
||||
tap->_mac.copyTo(tap->lwipdev6.hwaddr, tap->lwipdev6.hwaddr_len);
|
||||
netif_create_ip6_linklocal_address(&(tap->lwipdev6), 1);
|
||||
netif_add(&(tap->lwipdev6), NULL, tapif_init, ethernet_input);
|
||||
netif_set_default(&(tap->lwipdev6));
|
||||
netif_set_up(&(tap->lwipdev6));
|
||||
netif_ip6_addr_set_state(&(tap->lwipdev6), 1, IP6_ADDR_TENTATIVE);
|
||||
ip6_addr_copy(ip_2_ip6(tap->lwipdev6.ip6_addr[1]), addr6);
|
||||
tap->lwipdev6.output_ip6 = ethip6_output;
|
||||
tap->lwipdev6.state = tap;
|
||||
tap->lwipdev6.flags = NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
|
||||
DEBUG_INFO("addr=%s, netmask=%s", ip.toString().c_str(), ip.netmask().toString().c_str());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lwIP::lwip_loop(SocketTap *tap)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_discovery_time = 0;
|
||||
while(tap->_run)
|
||||
{
|
||||
uint64_t now = OSUtils::now();
|
||||
uint64_t since_tcp = now - prev_tcp_time;
|
||||
uint64_t since_discovery = now - prev_discovery_time;
|
||||
uint64_t since_status = now - prev_status_time;
|
||||
uint64_t tcp_remaining = LWIP_TCP_TIMER_INTERVAL;
|
||||
uint64_t discovery_remaining = 5000;
|
||||
void lwIP::lwip_loop(SocketTap *tap)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_discovery_time = 0;
|
||||
while(tap->_run)
|
||||
{
|
||||
uint64_t now = OSUtils::now();
|
||||
uint64_t since_tcp = now - prev_tcp_time;
|
||||
uint64_t since_discovery = now - prev_discovery_time;
|
||||
uint64_t since_status = now - prev_status_time;
|
||||
uint64_t tcp_remaining = LWIP_TCP_TIMER_INTERVAL;
|
||||
uint64_t discovery_remaining = 5000;
|
||||
|
||||
#if defined(LIBZT_IPV6)
|
||||
#define DISCOVERY_INTERVAL 1000
|
||||
#define DISCOVERY_INTERVAL 1000
|
||||
#elif defined(LIBZT_IPV4)
|
||||
#define DISCOVERY_INTERVAL ARP_TMR_INTERVAL
|
||||
#define DISCOVERY_INTERVAL ARP_TMR_INTERVAL
|
||||
#endif
|
||||
// Main TCP/ETHARP timer section
|
||||
if (since_tcp >= LWIP_TCP_TIMER_INTERVAL) {
|
||||
prev_tcp_time = now;
|
||||
tcp_tmr();
|
||||
}
|
||||
else {
|
||||
tcp_remaining = LWIP_TCP_TIMER_INTERVAL - since_tcp;
|
||||
}
|
||||
if (since_discovery >= DISCOVERY_INTERVAL) {
|
||||
prev_discovery_time = now;
|
||||
// Main TCP/ETHARP timer section
|
||||
if (since_tcp >= LWIP_TCP_TIMER_INTERVAL) {
|
||||
prev_tcp_time = now;
|
||||
tcp_tmr();
|
||||
}
|
||||
else {
|
||||
tcp_remaining = LWIP_TCP_TIMER_INTERVAL - since_tcp;
|
||||
}
|
||||
if (since_discovery >= DISCOVERY_INTERVAL) {
|
||||
prev_discovery_time = now;
|
||||
#if defined(LIBZT_IPV4)
|
||||
etharp_tmr();
|
||||
etharp_tmr();
|
||||
#endif
|
||||
#if defined(LIBZT_IPV6)
|
||||
nd6_tmr();
|
||||
nd6_tmr();
|
||||
#endif
|
||||
} else {
|
||||
discovery_remaining = DISCOVERY_INTERVAL - since_discovery;
|
||||
}
|
||||
tap->_phy.poll((unsigned long)std::min(tcp_remaining,discovery_remaining));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
discovery_remaining = DISCOVERY_INTERVAL - since_discovery;
|
||||
}
|
||||
tap->_phy.poll((unsigned long)std::min(tcp_remaining,discovery_remaining));
|
||||
}
|
||||
}
|
||||
|
||||
void lwIP::lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
struct pbuf *p,*q;
|
||||
if (!tap->_enabled)
|
||||
return;
|
||||
struct eth_hdr ethhdr;
|
||||
from.copyTo(ethhdr.src.addr, 6);
|
||||
to.copyTo(ethhdr.dest.addr, 6);
|
||||
ethhdr.type = ZeroTier::Utils::hton((uint16_t)etherType);
|
||||
void lwIP::lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
struct pbuf *p,*q;
|
||||
if (!tap->_enabled)
|
||||
return;
|
||||
struct eth_hdr ethhdr;
|
||||
from.copyTo(ethhdr.src.addr, 6);
|
||||
to.copyTo(ethhdr.dest.addr, 6);
|
||||
ethhdr.type = ZeroTier::Utils::hton((uint16_t)etherType);
|
||||
|
||||
p = pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
||||
if (p != NULL) {
|
||||
const char *dataptr = reinterpret_cast<const char *>(data);
|
||||
// First pbuf gets ethernet header at start
|
||||
q = p;
|
||||
if (q->len < sizeof(ethhdr)) {
|
||||
DEBUG_ERROR("dropped packet: first pbuf smaller than ethernet header");
|
||||
return;
|
||||
}
|
||||
memcpy(q->payload,ðhdr,sizeof(ethhdr));
|
||||
memcpy((char*)q->payload + sizeof(ethhdr),dataptr,q->len - sizeof(ethhdr));
|
||||
dataptr += q->len - sizeof(ethhdr);
|
||||
p = pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
||||
if (p != NULL) {
|
||||
const char *dataptr = reinterpret_cast<const char *>(data);
|
||||
// First pbuf gets ethernet header at start
|
||||
q = p;
|
||||
if (q->len < sizeof(ethhdr)) {
|
||||
DEBUG_ERROR("dropped packet: first pbuf smaller than ethernet header");
|
||||
return;
|
||||
}
|
||||
memcpy(q->payload,ðhdr,sizeof(ethhdr));
|
||||
memcpy((char*)q->payload + sizeof(ethhdr),dataptr,q->len - sizeof(ethhdr));
|
||||
dataptr += q->len - sizeof(ethhdr);
|
||||
|
||||
// Remaining pbufs (if any) get rest of data
|
||||
while ((q = q->next)) {
|
||||
memcpy(q->payload,dataptr,q->len);
|
||||
dataptr += q->len;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("dropped packet: no pbufs available");
|
||||
return;
|
||||
}
|
||||
{
|
||||
// Remaining pbufs (if any) get rest of data
|
||||
while ((q = q->next)) {
|
||||
memcpy(q->payload,dataptr,q->len);
|
||||
dataptr += q->len;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("dropped packet: no pbufs available");
|
||||
return;
|
||||
}
|
||||
{
|
||||
#if defined(LIBZT_IPV6)
|
||||
if(tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) {
|
||||
DEBUG_ERROR("error while feeding frame into stack lwipdev6");
|
||||
}
|
||||
if(tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) {
|
||||
DEBUG_ERROR("error while feeding frame into stack lwipdev6");
|
||||
}
|
||||
#endif
|
||||
#if defined(LIBZT_IPV4)
|
||||
if(tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) {
|
||||
DEBUG_ERROR("error while feeding frame into stack lwipdev");
|
||||
}
|
||||
if(tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) {
|
||||
DEBUG_ERROR("error while feeding frame into stack lwipdev");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol)
|
||||
{
|
||||
// TODO: check lwIP timers, and max sockets
|
||||
DEBUG_INFO();
|
||||
if(socket_type == SOCK_STREAM) {
|
||||
struct tcp_pcb *new_tcp_PCB = tcp_new();
|
||||
*pcb = new_tcp_PCB;
|
||||
return ERR_OK;
|
||||
}
|
||||
if(socket_type == SOCK_DGRAM) {
|
||||
struct udp_pcb *new_udp_PCB = udp_new();
|
||||
*pcb = new_udp_PCB;
|
||||
return ERR_OK;
|
||||
}
|
||||
if(socket_type == SOCK_RAW) {
|
||||
DEBUG_ERROR("SOCK_RAW, not currently supported.");
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol)
|
||||
{
|
||||
// TODO: check lwIP timers, and max sockets
|
||||
DEBUG_INFO();
|
||||
if(socket_type == SOCK_STREAM) {
|
||||
struct tcp_pcb *new_tcp_PCB = tcp_new();
|
||||
*pcb = new_tcp_PCB;
|
||||
return ERR_OK;
|
||||
}
|
||||
if(socket_type == SOCK_DGRAM) {
|
||||
struct udp_pcb *new_udp_PCB = udp_new();
|
||||
*pcb = new_udp_PCB;
|
||||
return ERR_OK;
|
||||
}
|
||||
if(socket_type == SOCK_RAW) {
|
||||
DEBUG_ERROR("SOCK_RAW, not currently supported.");
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int lwIP::lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
}
|
||||
int lwIP::lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
}
|
||||
|
||||
int lwIP::lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
ip_addr_t ba;
|
||||
char addrstr[INET6_ADDRSTRLEN];
|
||||
int port = 0, err = 0;
|
||||
int lwIP::lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
ip_addr_t ba;
|
||||
char addrstr[INET6_ADDRSTRLEN];
|
||||
int port = 0, err = 0;
|
||||
|
||||
#if defined(LIBZT_IPV4)
|
||||
DEBUG_ERROR("A");
|
||||
struct sockaddr_in *in4;
|
||||
if(addr->sa_family == AF_INET) {
|
||||
DEBUG_ERROR("A");
|
||||
in4 = (struct sockaddr_in *)addr;
|
||||
DEBUG_ERROR("A");
|
||||
inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN);
|
||||
DEBUG_ERROR("A");
|
||||
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(in4->sin_port));
|
||||
}
|
||||
ba = convert_ip(in4);
|
||||
port = lwip_ntohs(in4->sin_port);
|
||||
DEBUG_INFO("port=%d", port);
|
||||
DEBUG_INFO("port=%d", lwip_ntohs(port));
|
||||
DEBUG_ERROR("A");
|
||||
struct sockaddr_in *in4;
|
||||
if(addr->sa_family == AF_INET) {
|
||||
DEBUG_ERROR("A");
|
||||
in4 = (struct sockaddr_in *)addr;
|
||||
DEBUG_ERROR("A");
|
||||
inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN);
|
||||
DEBUG_ERROR("A");
|
||||
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(in4->sin_port));
|
||||
}
|
||||
ba = convert_ip(in4);
|
||||
port = lwip_ntohs(in4->sin_port);
|
||||
DEBUG_INFO("port=%d", port);
|
||||
DEBUG_INFO("port=%d", lwip_ntohs(port));
|
||||
#endif
|
||||
#if defined(LIBZT_IPV6)
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&addr;
|
||||
in6_to_ip6((ip6_addr *)&ba, in6);
|
||||
if(addr->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr;
|
||||
inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN);
|
||||
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(connaddr6->sin6_port));
|
||||
}
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&addr;
|
||||
in6_to_ip6((ip6_addr *)&ba, in6);
|
||||
if(addr->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr;
|
||||
inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN);
|
||||
DEBUG_INFO("%s:%d", addrstr, lwip_ntohs(connaddr6->sin6_port));
|
||||
}
|
||||
#endif
|
||||
if(conn->socket_type == SOCK_DGRAM) {
|
||||
err = udp_bind((struct udp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
|
||||
if(err == ERR_USE) {
|
||||
err = -1;
|
||||
errno = EADDRINUSE; // port in use
|
||||
}
|
||||
else {
|
||||
// set the recv callback
|
||||
udp_recv((struct udp_pcb*)conn->pcb, nc_udp_recved, new ConnectionPair(tap, conn));
|
||||
err = ERR_OK;
|
||||
errno = ERR_OK; // success
|
||||
}
|
||||
}
|
||||
else if (conn->socket_type == SOCK_STREAM) {
|
||||
err = tcp_bind((struct tcp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
|
||||
if(err != ERR_OK) {
|
||||
DEBUG_ERROR("err=%d", err);
|
||||
if(err == ERR_USE){
|
||||
err = -1;
|
||||
errno = EADDRINUSE;
|
||||
}
|
||||
if(err == ERR_MEM){
|
||||
err = -1;
|
||||
errno = ENOMEM;
|
||||
}
|
||||
if(err == ERR_BUF){
|
||||
err = -1;
|
||||
errno = ENOMEM;
|
||||
}
|
||||
}
|
||||
else {
|
||||
err = ERR_OK;
|
||||
errno = ERR_OK; // success
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
if(conn->socket_type == SOCK_DGRAM) {
|
||||
err = udp_bind((struct udp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
|
||||
if(err == ERR_USE) {
|
||||
err = -1;
|
||||
errno = EADDRINUSE; // port in use
|
||||
}
|
||||
else {
|
||||
// set the recv callback
|
||||
udp_recv((struct udp_pcb*)conn->pcb, nc_udp_recved, new ConnectionPair(tap, conn));
|
||||
err = ERR_OK;
|
||||
errno = ERR_OK; // success
|
||||
}
|
||||
}
|
||||
else if (conn->socket_type == SOCK_STREAM) {
|
||||
err = tcp_bind((struct tcp_pcb*)conn->pcb, (const ip_addr_t *)&ba, port);
|
||||
if(err != ERR_OK) {
|
||||
DEBUG_ERROR("err=%d", err);
|
||||
if(err == ERR_USE){
|
||||
err = -1;
|
||||
errno = EADDRINUSE;
|
||||
}
|
||||
if(err == ERR_MEM){
|
||||
err = -1;
|
||||
errno = ENOMEM;
|
||||
}
|
||||
if(err == ERR_BUF){
|
||||
err = -1;
|
||||
errno = ENOMEM;
|
||||
}
|
||||
}
|
||||
else {
|
||||
err = ERR_OK;
|
||||
errno = ERR_OK; // success
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int lwIP::lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
int lwIP::lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
|
||||
int lwIP::lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
// to be implemented
|
||||
}
|
||||
int lwIP::lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
// to be implemented
|
||||
}
|
||||
|
||||
int lwIP::lwip_Write(SocketTap *tap, Connection *conn)
|
||||
{
|
||||
DEBUG_EXTRA("conn=%p", (void*)&conn);
|
||||
// to be implemented
|
||||
}
|
||||
int lwIP::lwip_Write(SocketTap *tap, Connection *conn)
|
||||
{
|
||||
DEBUG_EXTRA("conn=%p", (void*)&conn);
|
||||
// to be implemented
|
||||
}
|
||||
|
||||
int lwIP::lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
int lwIP::lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* Callbacks from lwIP stack */
|
||||
/****************************************************************************/
|
||||
/****************************************************************************/
|
||||
/* Callbacks from lwIP stack */
|
||||
/****************************************************************************/
|
||||
|
||||
err_t lwIP::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
err_t lwIP::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t lwIP::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return -1;
|
||||
}
|
||||
err_t lwIP::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return -1;
|
||||
}
|
||||
|
||||
void lwIP::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
void lwIP::nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
|
||||
err_t lwIP::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
|
||||
{
|
||||
DEBUG_EXTRA("pcb=%p", (void*)&PCB);
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
err_t lwIP::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
|
||||
{
|
||||
DEBUG_EXTRA("pcb=%p", (void*)&PCB);
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t lwIP::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
|
||||
{
|
||||
DEBUG_ATTN("pcb=%p", (void*)&PCB);
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
err_t lwIP::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
|
||||
{
|
||||
DEBUG_ATTN("pcb=%p", (void*)&PCB);
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t lwIP::nc_poll(void* arg, struct tcp_pcb *PCB)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
err_t lwIP::nc_poll(void* arg, struct tcp_pcb *PCB)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void lwIP::nc_err(void *arg, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
void lwIP::nc_err(void *arg, err_t err)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
// to be implemented
|
||||
}
|
||||
}
|
||||
|
||||
88
src/lwIP.hpp
88
src/lwIP.hpp
@@ -51,16 +51,16 @@ struct tcp_pcb;
|
||||
struct netif;
|
||||
|
||||
#if defined(LIBZT_IPV4)
|
||||
#define LWIP_NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
|
||||
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr
|
||||
#define LWIP_NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
|
||||
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr
|
||||
#endif
|
||||
#if defined(LIBZT_IPV6)
|
||||
#include "lwip/ip6_addr.h"
|
||||
#define LWIP_NETIF_ADD_SIG struct netif *netif, void *state, netif_init_fn init, netif_input_fn input
|
||||
#define LWIP_ETHIP6_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr
|
||||
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr
|
||||
#define LWIP_NETIF_IP6_ADDR_SET_STATE_SIG struct netif* netif, s8_t addr_idx, u8_t state
|
||||
#define LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG struct netif *netif, u8_t from_mac_48bit
|
||||
#define LWIP_NETIF_ADD_SIG struct netif *netif, void *state, netif_init_fn init, netif_input_fn input
|
||||
#define LWIP_ETHIP6_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr
|
||||
#define LWIP_ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr
|
||||
#define LWIP_NETIF_IP6_ADDR_SET_STATE_SIG struct netif* netif, s8_t addr_idx, u8_t state
|
||||
#define LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG struct netif *netif, u8_t from_mac_48bit
|
||||
#endif
|
||||
|
||||
#define LWIP_PBUF_FREE_SIG struct pbuf *p
|
||||
@@ -105,13 +105,13 @@ struct netif;
|
||||
|
||||
|
||||
#if defined(LIBZT_IPV4)
|
||||
extern "C" err_t etharp_output(LWIP_ETHARP_OUTPUT_SIG);
|
||||
extern "C" err_t etharp_output(LWIP_ETHARP_OUTPUT_SIG);
|
||||
#endif
|
||||
#if defined(LIBZT_IPV6)
|
||||
extern "C" void nd6_tmr(void);
|
||||
extern "C" void netif_ip6_addr_set_state(LWIP_NETIF_IP6_ADDR_SET_STATE_SIG);
|
||||
extern "C" void netif_create_ip6_linklocal_address(LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG);
|
||||
extern "C" err_t _ethip6_output(LWIP_ETHIP6_OUTPUT_SIG);
|
||||
extern "C" void nd6_tmr(void);
|
||||
extern "C" void netif_ip6_addr_set_state(LWIP_NETIF_IP6_ADDR_SET_STATE_SIG);
|
||||
extern "C" void netif_create_ip6_linklocal_address(LWIP_NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG);
|
||||
extern "C" err_t _ethip6_output(LWIP_ETHIP6_OUTPUT_SIG);
|
||||
#endif
|
||||
|
||||
extern "C" void lwip_init();
|
||||
@@ -162,44 +162,44 @@ extern "C" err_t ip_input(LWIP_IP_INPUT_SIG);
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class SocketTap;
|
||||
struct Connection;
|
||||
class SocketTap;
|
||||
struct Connection;
|
||||
|
||||
class lwIP
|
||||
{
|
||||
public:
|
||||
class lwIP
|
||||
{
|
||||
public:
|
||||
|
||||
/*
|
||||
* Set up an interface in the network stack for the SocketTap
|
||||
*/
|
||||
void lwip_init_interface(SocketTap *tap, const InetAddress &ip);
|
||||
/*
|
||||
* Set up an interface in the network stack for the SocketTap
|
||||
*/
|
||||
void lwip_init_interface(SocketTap *tap, const InetAddress &ip);
|
||||
|
||||
/*
|
||||
* Main stack loop
|
||||
*/
|
||||
void lwip_loop(SocketTap *tap);
|
||||
/*
|
||||
* Main stack loop
|
||||
*/
|
||||
void lwip_loop(SocketTap *tap);
|
||||
|
||||
/*
|
||||
* Packets from the ZeroTier virtual wire enter the stack here
|
||||
*/
|
||||
void lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
||||
/*
|
||||
* Packets from the ZeroTier virtual wire enter the stack here
|
||||
*/
|
||||
void lwip_rx(SocketTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
||||
|
||||
int lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol);
|
||||
int lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc);
|
||||
int lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked);
|
||||
int lwip_Write(SocketTap *tap, Connection *conn);
|
||||
int lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn);
|
||||
int lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol);
|
||||
int lwip_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int lwip_Bind(SocketTap *tap, Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int lwip_Listen(SocketTap *tap, PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc);
|
||||
int lwip_Read(SocketTap *tap, PhySocket *sock, void **uptr, bool lwip_invoked);
|
||||
int lwip_Write(SocketTap *tap, Connection *conn);
|
||||
int lwip_Close(SocketTap *tap, PhySocket *sock, Connection *conn);
|
||||
|
||||
static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
|
||||
static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
|
||||
static void nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
|
||||
static void nc_err(void *arg, err_t err);
|
||||
static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
|
||||
static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
|
||||
static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
|
||||
};
|
||||
static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
|
||||
static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
|
||||
static void nc_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
|
||||
static void nc_err(void *arg, err_t err);
|
||||
static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
|
||||
static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
|
||||
static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
874
src/picoTCP.cpp
874
src/picoTCP.cpp
File diff suppressed because it is too large
Load Diff
@@ -73,12 +73,12 @@ namespace ZeroTier
|
||||
/*
|
||||
* Send raw frames from the stack to the ZeroTier virtual wire
|
||||
*/
|
||||
int pico_eth_send(struct pico_device *dev, void *buf, int len);
|
||||
int pico_eth_send(struct pico_device *dev, void *buf, int len);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Read raw frames from RX frame buffer into the stack
|
||||
*/
|
||||
int pico_eth_poll(struct pico_device *dev, int loop_score);
|
||||
int pico_eth_poll(struct pico_device *dev, int loop_score);
|
||||
|
||||
class SocketTap;
|
||||
struct Connection;
|
||||
@@ -88,38 +88,38 @@ namespace ZeroTier
|
||||
public:
|
||||
|
||||
/*
|
||||
* Set up an interface in the network stack for the SocketTap
|
||||
*/
|
||||
* Set up an interface in the network stack for the SocketTap
|
||||
*/
|
||||
bool pico_init_interface(ZeroTier::SocketTap *tap, const ZeroTier::InetAddress &ip);
|
||||
|
||||
/*
|
||||
* Main stack loop
|
||||
*/
|
||||
* Main stack loop
|
||||
*/
|
||||
void pico_loop(SocketTap *tap);
|
||||
|
||||
/*
|
||||
* Read bytes from the stack to the RX buffer (prepare to be read by app)
|
||||
*/
|
||||
* Read bytes from the stack to the RX buffer (prepare to be read by app)
|
||||
*/
|
||||
static void pico_cb_tcp_read(SocketTap *tap, struct pico_socket *s);
|
||||
|
||||
/*
|
||||
* Read bytes from the stack to the RX buffer (prepare to be read by app)
|
||||
*/
|
||||
static void pico_cb_udp_read(SocketTap *tap, struct pico_socket *s);
|
||||
* Read bytes from the stack to the RX buffer (prepare to be read by app)
|
||||
*/
|
||||
static void pico_cb_udp_read(SocketTap *tap, struct pico_socket *s);
|
||||
|
||||
/*
|
||||
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
|
||||
*/
|
||||
/*
|
||||
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
|
||||
*/
|
||||
static void pico_cb_tcp_write(SocketTap *tap, struct pico_socket *s);
|
||||
|
||||
/*
|
||||
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
|
||||
*/
|
||||
static void pico_cb_socket_activity(uint16_t ev, struct pico_socket *s);
|
||||
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
|
||||
*/
|
||||
static void pico_cb_socket_activity(uint16_t ev, struct pico_socket *s);
|
||||
|
||||
/*
|
||||
* Packets from the ZeroTier virtual wire enter the stack here
|
||||
*/
|
||||
* Packets from the ZeroTier virtual wire enter the stack here
|
||||
*/
|
||||
void pico_rx(SocketTap *tap, const ZeroTier::MAC &from,const ZeroTier::MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
||||
|
||||
/*
|
||||
@@ -130,39 +130,39 @@ namespace ZeroTier
|
||||
/*
|
||||
* Connect to remote host via userspace network stack interface - Called from SocketTap
|
||||
*/
|
||||
int pico_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int pico_Connect(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Bind to a userspace network stack interface - Called from SocketTap
|
||||
*/
|
||||
int pico_Bind(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int pico_Bind(Connection *conn, int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Listen for incoming connections - Called from SocketTap
|
||||
*/
|
||||
int pico_Listen(Connection *conn, int fd, int backlog);
|
||||
int pico_Listen(Connection *conn, int fd, int backlog);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Accept an incoming connection - Called from SocketTap
|
||||
*/
|
||||
Connection* pico_Accept(Connection *conn);
|
||||
Connection* pico_Accept(Connection *conn);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Read from RX buffer to application - Called from SocketTap
|
||||
*/
|
||||
int pico_Read(SocketTap *tap, ZeroTier::PhySocket *sock,Connection *conn,bool stack_invoked);
|
||||
int pico_Read(SocketTap *tap, ZeroTier::PhySocket *sock,Connection *conn,bool stack_invoked);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Write to userspace network stack - Called from SocketTap
|
||||
*/
|
||||
int pico_Write(Connection *conn, void *data, ssize_t len);
|
||||
int pico_Write(Connection *conn, void *data, ssize_t len);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Close a Connection - Called from SocketTap
|
||||
*/
|
||||
int pico_Close(Connection *conn);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Converts picoTCP error codes to pretty string
|
||||
*/
|
||||
static char *beautify_pico_error(int err);
|
||||
|
||||
@@ -68,9 +68,9 @@ void loadTestConfigFile(std::string filepath)
|
||||
prefix = value;
|
||||
}
|
||||
if(key[0] != '#' && key[0] != ';') {
|
||||
testConf[prefix + "." + key] = value; // format: alice.ipv4 172.30.30.1
|
||||
//fprintf(stderr, "%s.%s = %s\n", prefix.c_str(), key.c_str(), testConf[prefix + "." + key].c_str());
|
||||
}
|
||||
testConf[prefix + "." + key] = value; // format: alice.ipv4 172.30.30.1
|
||||
//fprintf(stderr, "%s.%s = %s\n", prefix.c_str(), key.c_str(), testConf[prefix + "." + key].c_str());
|
||||
}
|
||||
|
||||
}
|
||||
testFile.close();
|
||||
@@ -225,18 +225,18 @@ void start_echo_mode(std::string ipstr, int listen_port)
|
||||
|
||||
int main(int argc , char *argv[])
|
||||
{
|
||||
if(argc < 5) {
|
||||
fprintf(stderr, "usage: echotest <selftest.conf> <alice|bob|ted|carol> to <bob|alice|ted|carol>\n");
|
||||
fprintf(stderr, "e.g. : echotest test/selftest.conf bob to alice\n");
|
||||
return 1;
|
||||
}
|
||||
if(argc < 5) {
|
||||
fprintf(stderr, "usage: echotest <selftest.conf> <alice|bob|ted|carol> to <bob|alice|ted|carol>\n");
|
||||
fprintf(stderr, "e.g. : echotest test/selftest.conf bob to alice\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string from = argv[2];
|
||||
std::string to = argv[4];
|
||||
std::string me = from;
|
||||
|
||||
int start_port = 0;
|
||||
int port_offset = 0;
|
||||
int start_port = 0;
|
||||
int port_offset = 0;
|
||||
int echo_listen_port = 0;
|
||||
|
||||
std::string local_echo_ipv4;
|
||||
|
||||
@@ -27,22 +27,22 @@
|
||||
|
||||
unsigned short csum(unsigned short *buf, int nwords)
|
||||
{
|
||||
unsigned long sum;
|
||||
for(sum=0; nwords>0; nwords--)
|
||||
sum += *buf++;
|
||||
sum = (sum >> 16) + (sum &0xffff);
|
||||
sum += (sum >> 16);
|
||||
return (unsigned short)(~sum);
|
||||
unsigned long sum;
|
||||
for(sum=0; nwords>0; nwords--)
|
||||
sum += *buf++;
|
||||
sum = (sum >> 16) + (sum &0xffff);
|
||||
sum += (sum >> 16);
|
||||
return (unsigned short)(~sum);
|
||||
}
|
||||
|
||||
int main(int argc , char *argv[])
|
||||
{
|
||||
if(argc < 3) {
|
||||
fprintf(stderr, "usage: layer2 <zt_home_dir> <nwid>\n");
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "usage: layer2 <zt_home_dir> <nwid>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// initialize library
|
||||
// initialize library
|
||||
printf("Starting libzt...\n");
|
||||
zts_simple_start(argv[1], argv[2]);
|
||||
char device_id[11];
|
||||
@@ -181,7 +181,7 @@ int main(int argc , char *argv[])
|
||||
usleep(10000);
|
||||
// Send packet
|
||||
if (zts_sendto(fd, sendbuf, tx_len, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0)
|
||||
fprintf(stderr, "Send failed\n");
|
||||
fprintf(stderr, "Send failed\n");
|
||||
}
|
||||
|
||||
// dismantle all zt virtual taps (SocketTaps)
|
||||
|
||||
@@ -116,7 +116,7 @@ std::map<std::string, std::string> testConf;
|
||||
[OK] comprehensive server ipv6 - test all ipv4/6 server simple/sustained modes
|
||||
|
||||
Performance:
|
||||
(See libzt.h, compile libzt with appropriate ZT_TCP_TX_BUF_SZ, ZT_TCP_RX_BUF_SZ, ZT_UDP_TX_BUF_SZ, and ZT_UDO_RX_BUF_SZ for your test)
|
||||
(See libzt.h, compile libzt with appropriate ZT_TCP_TX_BUF_SZ, ZT_TCP_RX_BUF_SZ, ZT_UDP_TX_BUF_SZ, and ZT_UDO_RX_BUF_SZ for your test)
|
||||
|
||||
[OK] Throughput - Test maximum RX/TX speeds
|
||||
[ ] Memory Usage - Test memory consumption profile
|
||||
@@ -164,9 +164,9 @@ void loadTestConfigFile(std::string filepath)
|
||||
prefix = value;
|
||||
}
|
||||
if(key[0] != '#' && key[0] != ';') {
|
||||
testConf[prefix + "." + key] = value;
|
||||
fprintf(stderr, "%s.%s = %s\n", prefix.c_str(), key.c_str(), testConf[prefix + "." + key].c_str());
|
||||
}
|
||||
testConf[prefix + "." + key] = value;
|
||||
fprintf(stderr, "%s.%s = %s\n", prefix.c_str(), key.c_str(), testConf[prefix + "." + key].c_str());
|
||||
}
|
||||
|
||||
}
|
||||
testFile.close();
|
||||
@@ -870,7 +870,7 @@ int slam_api_test()
|
||||
int results[SLAM_NUMBER*SLAM_REPEAT];
|
||||
|
||||
struct hostent *server;
|
||||
struct sockaddr_in6 addr6;
|
||||
struct sockaddr_in6 addr6;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
// int start_stack_timer_count = pico_ntimers(); // number of picoTCP timers allocated
|
||||
@@ -946,17 +946,17 @@ int slam_api_test()
|
||||
|
||||
int port;
|
||||
while(!(std::find(used_ports.begin(),used_ports.end(),port) == used_ports.end())) {
|
||||
port = MIN_PORT + (rand() % (int)(MAX_PORT - MIN_PORT + 1));
|
||||
}
|
||||
used_ports.push_back(port);
|
||||
std::cout << "port = " << port << std::endl;
|
||||
port = MIN_PORT + (rand() % (int)(MAX_PORT - MIN_PORT + 1));
|
||||
}
|
||||
used_ports.push_back(port);
|
||||
std::cout << "port = " << port << std::endl;
|
||||
|
||||
if(false) {
|
||||
server = gethostbyname2("::",AF_INET6);
|
||||
memset((char *) &addr6, 0, sizeof(addr6));
|
||||
addr6.sin6_flowinfo = 0;
|
||||
addr6.sin6_family = AF_INET6;
|
||||
addr6.sin6_port = htons(port);
|
||||
memset((char *) &addr6, 0, sizeof(addr6));
|
||||
addr6.sin6_flowinfo = 0;
|
||||
addr6.sin6_family = AF_INET6;
|
||||
addr6.sin6_port = htons(port);
|
||||
addr6.sin6_addr = in6addr_any;
|
||||
err = zts_bind(sock, (struct sockaddr *)&addr6, (socklen_t)(sizeof addr6));
|
||||
}
|
||||
@@ -1020,10 +1020,10 @@ int slam_api_test()
|
||||
// connect()
|
||||
if(false) {
|
||||
server = gethostbyname2("::",AF_INET6);
|
||||
memset((char *) &addr6, 0, sizeof(addr6));
|
||||
addr6.sin6_flowinfo = 0;
|
||||
addr6.sin6_family = AF_INET6;
|
||||
addr6.sin6_port = htons(port);
|
||||
memset((char *) &addr6, 0, sizeof(addr6));
|
||||
addr6.sin6_flowinfo = 0;
|
||||
addr6.sin6_family = AF_INET6;
|
||||
addr6.sin6_port = htons(port);
|
||||
addr6.sin6_addr = in6addr_any;
|
||||
err = zts_connect(sock, (struct sockaddr *)&addr6, (socklen_t)(sizeof addr6));
|
||||
}
|
||||
@@ -1086,7 +1086,7 @@ int random_api_test()
|
||||
int calls_made = 0;
|
||||
|
||||
// how many calls we'll make
|
||||
int num_of_api_calls = 10;
|
||||
int num_of_api_calls = 10;
|
||||
|
||||
|
||||
zts_socket()
|
||||
@@ -1241,24 +1241,24 @@ void test_bad_args()
|
||||
|
||||
int main(int argc , char *argv[])
|
||||
{
|
||||
if(argc < 5) {
|
||||
fprintf(stderr, "usage: selftest <selftest.conf> <alice|bob|ted|carol> to <bob|alice|ted|carol>\n");
|
||||
fprintf(stderr, "e.g. : selftest test/selftest.conf alice to bob\n");
|
||||
return 1;
|
||||
}
|
||||
if(argc < 5) {
|
||||
fprintf(stderr, "usage: selftest <selftest.conf> <alice|bob|ted|carol> to <bob|alice|ted|carol>\n");
|
||||
fprintf(stderr, "e.g. : selftest test/selftest.conf alice to bob\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string from = argv[2];
|
||||
std::string to = argv[4];
|
||||
std::string me = from;
|
||||
|
||||
std::vector<std::string> results;
|
||||
std::vector<std::string> results;
|
||||
|
||||
int err = 0;
|
||||
int mode = 0;
|
||||
int port = 0;
|
||||
int operation = 0;
|
||||
int start_port = 0;
|
||||
int port_offset = 0;
|
||||
int err = 0;
|
||||
int mode = 0;
|
||||
int port = 0;
|
||||
int operation = 0;
|
||||
int start_port = 0;
|
||||
int port_offset = 0;
|
||||
int count = 0;
|
||||
int delay = 0;
|
||||
|
||||
|
||||
@@ -191,9 +191,9 @@ namespace ZeroTier {
|
||||
struct sockaddr_in6 in6;
|
||||
memset(&in6,0,sizeof(in6));
|
||||
in6.sin6_family = AF_INET;
|
||||
struct hostent *server;
|
||||
server = gethostbyname2((char*)host.c_str(),AF_INET6);
|
||||
memmove((char *) &in6.sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
|
||||
struct hostent *server;
|
||||
server = gethostbyname2((char*)host.c_str(),AF_INET6);
|
||||
memmove((char *) &in6.sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
|
||||
in6.sin6_port = Utils::hton(dest_port);
|
||||
conn->destination_sock = _phy.tcpConnect((const struct sockaddr *)&in6, connected, this);
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace ZeroTier {
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
TcpConnection *getConnection(PhySocket *sock);
|
||||
TcpConnection *getConnection(PhySocket *sock);
|
||||
|
||||
private:
|
||||
volatile bool _enabled;
|
||||
|
||||
Reference in New Issue
Block a user