partially-working picoTCP integration

This commit is contained in:
Joseph Henry
2016-10-14 14:03:06 -07:00
parent d9033ba2f3
commit 8514c6bcab
164 changed files with 47767 additions and 56 deletions

View File

@@ -83,7 +83,7 @@ extern "C" {
//#if defined(SDK_DEBUG)
#if DEBUG_LEVEL >= MSG_ERROR
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, RED "ZT_ERROR: %20s:%4d:%20s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ERROR(fmt, args...) fprintf(stderr, RED "ZT_ERROR: %20s:%4d:%25s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#else
#define DEBUG_ERROR(fmt, args...)
#endif
@@ -92,8 +92,8 @@ extern "C" {
#define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_INFO : %20s:%4d:%20s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#define DEBUG_BLANK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_INFO : %20s:%4d:" fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#else
#define DEBUG_INFO(fmt, args...) fprintf(stderr, "ZT_INFO : %20s:%4d:%20s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ATTN(fmt, args...) fprintf(stderr, CYN "ZT_INFO : %20s:%4d:%20s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_INFO(fmt, args...) fprintf(stderr, "ZT_INFO : %20s:%4d:%25s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ATTN(fmt, args...) fprintf(stderr, CYN "ZT_INFO : %20s:%4d:%25s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, "ZT_INFO : %20s:%4d:" fmt "\n", __FILENAME__, __LINE__, ##args)
#endif
#else
@@ -102,18 +102,18 @@ extern "C" {
#endif
#if DEBUG_LEVEL >= MSG_TRANSFER
#if defined(__ANDROID__)
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_TRANS : %20s:%4d:%20s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_TRANS : %20s:%4d:%25s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#else
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, GRN "ZT_TRANS: %20s:%4d:%20s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_TRANS(fmt, args...) fprintf(stderr, GRN "ZT_TRANS: %20s:%4d:%25s: " fmt "\n" RESET, __FILENAME__, __LINE__, __FUNCTION__, ##args)
#endif
#else
#define DEBUG_TRANS(fmt, args...)
#endif
#if DEBUG_LEVEL >= MSG_EXTRA
#if defined(__ANDROID__)
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_EXTRA : %20s:%4d:%20s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "ZT_EXTRA : %20s:%4d:%25s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args))
#else
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, "ZT_EXTRA: %20s:%4d:%20s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_EXTRA(fmt, args...) fprintf(stderr, "ZT_EXTRA: %20s:%4d:%25s: " fmt "\n", __FILENAME__, __LINE__, __FUNCTION__, ##args)
#endif
#else
#define DEBUG_EXTRA(fmt, args...)

View File

@@ -38,8 +38,20 @@
#include "SDK_EthernetTap.hpp"
#include "SDK_Utils.hpp"
#include "SDK.h"
#include "SDK_defs.h"
#include "SDK_Debug.h"
#include "SDK_LWIPStack.hpp"
#if defined(SDK_LWIP)
#include "SDK_lwip.hpp"
#elif defined(SDK_PICOTCP)
#include "SDK_pico.hpp"
#include "pico_stack.h"
#include "pico_ipv4.h"
#include "pico_icmp4.h"
#include "pico_dev_tap.h"
#elif defined(SDK_JIP)
#include "SDK_jip.hpp"
#endif
#include "Utils.hpp"
#include "OSUtils.hpp"
@@ -71,6 +83,7 @@
namespace ZeroTier {
// ---------------------------------------------------------------------------
static ZeroTier::NetconEthernetTap *picotap;
static err_t tapif_init(struct netif *netif)
{
@@ -78,6 +91,73 @@ static err_t tapif_init(struct netif *netif)
return ERR_OK;
}
static void cb_ping(struct pico_icmp4_stats *s)
{
char host[30];
picotap->picostack->__pico_ipv4_to_string(host, s->dst.addr);
if (s->err == 0) {
// if all is well, print some pretty info
printf("%lu bytes from %s: icmp_req=%lu ttl=%lu time=%lu ms\n", s->size,
host, s->seq, s->ttl, (long unsigned int)s->time);
//if (s->seq >= NUM_PING)
//finished = 1;
} else {
// if something went wrong, print it and signal we want to stop
printf("PING %lu to %s: Error %d\n", s->seq, host, s->err);
//finished = 1;
}
}
static int pico_eth_send(struct pico_device *dev, void *buf, int len)
{
DEBUG_INFO("len = %d", len);
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);
picotap->_handler(picotap->_arg,picotap->_nwid,src_mac,dest_mac,
Utils::ntoh((uint16_t)ethhdr->type),0, ((char*)buf) + sizeof(struct eth_hdr),len - sizeof(struct eth_hdr));
return len;
}
static int pico_eth_poll(struct pico_device *dev, int loop_score)
{
// OPTIMIZATION: The copy logic and/or buffer structure should be reworked for better performance after the BETA
// DEBUG_INFO();
// ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
Mutex::Lock _l(picotap->_pico_frame_rxbuf_m);
uint8_t *buf = NULL;
uint32_t len = 0;
struct eth_hdr ethhdr;
unsigned char frame[ZT_MAX_MTU];
while (picotap->pico_frame_rxbuf_tot > 0) {
memset(frame, 0, sizeof(frame));
unsigned int len = 0;
memcpy(&len, picotap->pico_frame_rxbuf, sizeof(len)); // get frame len
DEBUG_ATTN("reading frame len = %ld", len);
memcpy(frame, picotap->pico_frame_rxbuf + sizeof(len), len); // get frame data
memmove(picotap->pico_frame_rxbuf, picotap->pico_frame_rxbuf + sizeof(len) + len, ZT_MAX_MTU-(sizeof(len) + len));
int rx_ret = picotap->picostack->__pico_stack_recv(dev, (uint8_t*)frame, len);
picotap->pico_frame_rxbuf_tot-=(sizeof(len) + len);
DEBUG_ATTN("rx_ret = %d", rx_ret);
DEBUG_EXTRA("RX frame buffer %3f full", (float)(picotap->pico_frame_rxbuf_tot) / (float)(MAX_PICO_FRAME_RX_BUF_SZ));
loop_score--;
}
//DEBUG_ATTN("loop_score = %d", loop_score);
return loop_score;
}
/*
* Outputs data from the pbuf queue to the interface
*/
@@ -107,7 +187,7 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
dest_mac.setTo(ethhdr->dest.addr, 6);
tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
return ERR_OK;
}
@@ -134,21 +214,42 @@ NetconEthernetTap::NetconEthernetTap(
_run(true)
{
sockstate = -1;
char sockPath[4096],lwipPath[4096];
char sockPath[4096],stackPath[4096];
Utils::snprintf(sockPath,sizeof(sockPath),"%s%snc_%.16llx",homePath,ZT_PATH_SEPARATOR_S,_nwid,ZT_PATH_SEPARATOR_S,(unsigned long long)nwid);
_dev = sockPath; // in SDK mode, set device to be just the network ID
Utils::snprintf(lwipPath,sizeof(lwipPath),"%s%sliblwip.so",homePath,ZT_PATH_SEPARATOR_S);
lwipstack = new LWIPStack(lwipPath);
if(!lwipstack)
throw std::runtime_error("unable to dynamically load a new instance of liblwip.so (searched ZeroTier home path)");
lwipstack->__lwip_init();
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
DEBUG_INFO("tap initialized on: path=%s", sockPath);
if (!_unixListenSocket)
DEBUG_ERROR("unable to bind to: path=%s", sockPath);
_thread = Thread::start(this);
// SIP-0
// Load and initialize network stack library
#if defined(SDK_LWIP)
Utils::snprintf(stackPath,sizeof(stackPath),"%s%sliblwip.so",homePath,ZT_PATH_SEPARATOR_S);
lwipstack = new lwIP_stack(stackPath);
#elif defined(SDK_PICOTCP)
Utils::snprintf(stackPath,sizeof(stackPath),"%s%slibpicotcp.so",homePath,ZT_PATH_SEPARATOR_S);
picostack = new picoTCP_stack(stackPath);
#elif defined(SDK_JIP)
Utils::snprintf(stackPath,sizeof(stackPath),"%s%slibjip.so",homePath,ZT_PATH_SEPARATOR_S);
jipstack = new jip_stack(stackPath);
#endif
if(!lwipstack && !picostack && !jipstack) {
DEBUG_ERROR("unable to dynamically load a new instance of (%s) (searched ZeroTier home path)", stackPath);
throw std::runtime_error("");
}
else {
if(lwipstack)
lwipstack->__lwip_init();
if(picostack)
picostack->__pico_stack_init();
//if(jipstack)
// jipstack->__jip_init();
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
DEBUG_INFO("tap initialized on: path=%s", sockPath);
if (!_unixListenSocket)
DEBUG_ERROR("unable to bind to: path=%s", sockPath);
_thread = Thread::start(this);
}
}
NetconEthernetTap::~NetconEthernetTap()
@@ -171,10 +272,14 @@ bool NetconEthernetTap::enabled() const
return _enabled;
}
bool NetconEthernetTap::addIp(const InetAddress &ip)
void NetconEthernetTap::lwIP_init_interface(const InetAddress &ip)
{
DEBUG_INFO("local_addr=%s", ip.toString().c_str());
Mutex::Lock _l(_ips_m);
// SIP-1
// Initialize network stack's interface, assign addresses
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
_ips.push_back(ip);
std::sort(_ips.begin(),_ips.end());
@@ -231,6 +336,62 @@ bool NetconEthernetTap::addIp(const InetAddress &ip)
interface6.flags = NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
}
}
}
void NetconEthernetTap::picoTCP_init_interface(const InetAddress &ip)
{
// TODO: Move this somewhere more appropriate
picotap = this;
DEBUG_INFO();
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
_ips.push_back(ip);
std::sort(_ips.begin(),_ips.end());
if(ip.isV4())
{
int id;
struct pico_ip4 ipaddr, netmask;
ipaddr.addr = *((u32_t *)ip.rawIpData());
netmask.addr = *((u32_t *)ip.netmask().rawIpData());
picostack->__pico_ipv4_link_add(&picodev, ipaddr, netmask);
picodev.send = pico_eth_send; // tx
picodev.poll = pico_eth_poll; // rx
// Register the device in picoTCP
uint8_t mac[PICO_SIZE_ETH];
_mac.copyTo(mac, PICO_SIZE_ETH);
DEBUG_ATTN("mac = %s", _mac.toString().c_str());
if( 0 != picostack->__pico_device_init(&picodev, "p0", mac)) {
DEBUG_ERROR("device init failed");
return;
}
DEBUG_INFO("successfully initialized device");
// picostack->__pico_icmp4_ping("10.8.8.1", 20, 1000, 10000, 64, cb_ping);
}
if(ip.isV6())
{
}
}
}
void NetconEthernetTap::jip_init_interface(const InetAddress &ip)
{
// will be similar to lwIP initialization process
}
bool NetconEthernetTap::addIp(const InetAddress &ip)
{
// SIP-3
// Initialize a new interface in the stack, assign an address
#if defined(SDK_LWIP)
lwIP_init_interface(ip);
#elif defined(SDK_PICOTCP)
picoTCP_init_interface(ip);
#elif defined(SDK_JIP)
jip_init_interface(ip);
#endif
return true;
}
@@ -253,13 +414,13 @@ std::vector<InetAddress> NetconEthernetTap::ips() const
return _ips;
}
void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
void NetconEthernetTap::lwIP_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_EXTRA("RX packet: len=%d, etherType=%d", len, etherType);
DEBUG_INFO();
struct pbuf *p,*q;
if (!_enabled)
return;
DEBUG_EXTRA("IPV6");
struct eth_hdr ethhdr;
from.copyTo(ethhdr.src.addr, 6);
to.copyTo(ethhdr.dest.addr, 6);
@@ -295,6 +456,58 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
}
}
void NetconEthernetTap::picoTCP_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_INFO();
// Since picoTCP only allows the reception of frames from within the polling function, we
// must enqueue each frame into a memory structure shared by both threads. This structure will
Mutex::Lock _l(_pico_frame_rxbuf_m);
if(len > ((1024 * 1024) - pico_frame_rxbuf_tot)) {
DEBUG_ERROR("dropping packet (len = %d) - not enough space left on RX frame buffer", len);
return;
}
//if(len != memcpy(pico_frame_rxbuf, data, len)) {
// DEBUG_ERROR("dropping packet (len = %d) - unable to copy contents of frame to RX frame buffer", len);
// return;
//}
// assemble new eth header
struct eth_hdr ethhdr;
from.copyTo(ethhdr.src.addr, 6);
to.copyTo(ethhdr.dest.addr, 6);
ethhdr.type = Utils::hton((uint16_t)etherType);
int newlen = len+sizeof(struct eth_hdr);
memcpy(pico_frame_rxbuf + pico_frame_rxbuf_tot, &newlen, sizeof(newlen)); // size of frame
memcpy(pico_frame_rxbuf + pico_frame_rxbuf_tot + sizeof(newlen), &ethhdr, sizeof(ethhdr)); // new eth header
memcpy(pico_frame_rxbuf + pico_frame_rxbuf_tot + sizeof(newlen) + sizeof(ethhdr), data, len); // frame data
pico_frame_rxbuf_tot += len + sizeof(len) + sizeof(ethhdr);
DEBUG_INFO("RX frame buffer %3f full", (float)pico_frame_rxbuf_tot / (float)(1024 * 1024));
}
void NetconEthernetTap::jip_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_INFO();
}
void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
DEBUG_EXTRA("RX packet: len=%d, etherType=%d", len, etherType);
// SIP-
// RX packet
#if defined(SDK_LWIP)
lwIP_rx(from,to,etherType,data,len);
#elif defined(SDK_PICOTCP)
picoTCP_rx(from,to,etherType,data,len);
#elif defined(SDK_JIP)
jip_rx(from,to,etherType,data,len);
#endif
}
std::string NetconEthernetTap::deviceName() const
{
return _dev;
@@ -326,13 +539,12 @@ void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,s
_multicastGroups.swap(newGroups);
}
void NetconEthernetTap::threadMain()
throw()
void NetconEthernetTap::lwIP_loop()
{
DEBUG_INFO();
uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_discovery_time = 0;
// Main timer loop
while (_run) {
uint64_t now = OSUtils::now();
uint64_t since_tcp = now - prev_tcp_time;
uint64_t since_discovery = now - prev_discovery_time;
@@ -407,6 +619,40 @@ void NetconEthernetTap::threadMain()
lwipstack->close();
}
void NetconEthernetTap::picoTCP_loop()
{
DEBUG_INFO();
while(_run)
{
//DEBUG_INFO("pico_tick");
usleep(1000);
picostack->__pico_stack_tick();
}
}
void NetconEthernetTap::jip_loop()
{
DEBUG_INFO();
while(_run)
{
}
}
void NetconEthernetTap::threadMain()
throw()
{
// SIP-2
// Enter main thread loop for network stack
#if defined(SDK_LWIP)
lwIP_loop();
#elif defined(SDK_PICOTCP)
picoTCP_loop();
#elif defined(SDK_JIP)
jip_loop();
#endif
}
Connection *NetconEthernetTap::getConnection(PhySocket *sock)
{
for(size_t i=0;i<_Connections.size();++i) {

View File

@@ -46,7 +46,11 @@
#include "netif/etharp.h"
#include "SDK_defs.h"
#include "SDK_RPC.h"
#include "SDK_lwip.hpp"
#include "SDK_pico.hpp"
#include "SDK_jip.hpp"
// lwIP structs
struct tcp_pcb;
@@ -60,25 +64,6 @@ struct connect_st;
struct getsockname_st;
struct accept_st;
// Timing
#define APPLICATION_POLL_FREQ 2
#define ZT_LWIP_TCP_TIMER_INTERVAL 50
#define STATUS_TMR_INTERVAL 500 // How often we check connection statuses (in ms)
// TCP Buffer sizes
#define DEFAULT_TCP_TX_BUF_SZ 1024 * 1024
#define DEFAULT_TCP_RX_BUF_SZ 1024 * 1024
// TCP RX/TX buffer soft boundaries
#define DEFAULT_TCP_TX_BUF_SOFTMAX DEFAULT_TCP_TX_BUF_SZ * 0.80
#define DEFAULT_TCP_TX_BUF_SOFTMIN DEFAULT_TCP_TX_BUF_SZ * 0.20
#define DEFAULT_TCP_RX_BUF_SOFTMAX DEFAULT_TCP_RX_BUF_SZ * 0.80
#define DEFAULT_TCP_RX_BUF_SOFTMIN DEFAULT_TCP_RX_BUF_SZ * 0.20
// UDP Buffer sizes (should be about the size of your MTU)
#define DEFAULT_UDP_TX_BUF_SZ ZT_MAX_MTU
#define DEFAULT_UDP_RX_BUF_SZ ZT_MAX_MTU * 128
namespace ZeroTier {
class NetconEthernetTap;
@@ -145,10 +130,29 @@ namespace ZeroTier {
void setFriendlyName(const char *friendlyName);
void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
// SIP-
struct pico_device picodev;
unsigned char pico_frame_rxbuf[MAX_PICO_FRAME_RX_BUF_SZ];
int pico_frame_rxbuf_tot = 0;
Mutex _pico_frame_rxbuf_m;
void lwIP_loop();
void picoTCP_loop();
void jip_loop();
// rx
void lwIP_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
void picoTCP_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
void jip_rx(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
void lwIP_init_interface(const InetAddress &ip);
void picoTCP_init_interface(const InetAddress &ip);
void jip_init_interface(const InetAddress &ip);
void threadMain()
throw();
LWIPStack *lwipstack;
uint64_t _nwid;
void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
void *_arg;
@@ -167,6 +171,10 @@ namespace ZeroTier {
std::string _homePath;
lwIP_stack *lwipstack;
picoTCP_stack *picostack;
jip_stack *jipstack;
private:
// LWIP callbacks
// NOTE: these are called from within LWIP, meaning that lwipstack->_lock is ALREADY
@@ -490,10 +498,9 @@ namespace ZeroTier {
PhySocket *_unixListenSocket;
std::vector<Connection*> _Connections;
std::map<uint64_t, std::pair<PhySocket*, void*> > jobmap;
pid_t rpcCounter;
netif interface;
netif interface6;

26
src/SDK_defs.h Normal file
View File

@@ -0,0 +1,26 @@
// --- lwIP
#define APPLICATION_POLL_FREQ 2
#define ZT_LWIP_TCP_TIMER_INTERVAL 50
#define STATUS_TMR_INTERVAL 500 // How often we check connection statuses (in ms)
// --- picoTCP
#define MAX_PICO_FRAME_RX_BUF_SZ ZT_MAX_MTU * 128
// --- jip
// --- General
// TCP Buffer sizes
#define DEFAULT_TCP_TX_BUF_SZ 1024 * 1024
#define DEFAULT_TCP_RX_BUF_SZ 1024 * 1024
// TCP RX/TX buffer soft boundaries
#define DEFAULT_TCP_TX_BUF_SOFTMAX DEFAULT_TCP_TX_BUF_SZ * 0.80
#define DEFAULT_TCP_TX_BUF_SOFTMIN DEFAULT_TCP_TX_BUF_SZ * 0.20
#define DEFAULT_TCP_RX_BUF_SOFTMAX DEFAULT_TCP_RX_BUF_SZ * 0.80
#define DEFAULT_TCP_RX_BUF_SOFTMIN DEFAULT_TCP_RX_BUF_SZ * 0.20
// UDP Buffer sizes (should be about the size of your MTU)
#define DEFAULT_UDP_TX_BUF_SZ ZT_MAX_MTU
#define DEFAULT_UDP_RX_BUF_SZ ZT_MAX_MTU * 128

126
src/SDK_jip.hpp Normal file
View File

@@ -0,0 +1,126 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#ifndef SDK_JIPSTACK_H
#define SDK_JIPSTACK_H
#include "Mutex.hpp"
#include "OSUtils.hpp"
#include "SDK_Debug.h"
#include <stdio.h>
#include <dlfcn.h>
#ifdef D_GNU_SOURCE
#define _GNU_SOURCE
#endif
namespace ZeroTier {
/**
* Loads an instance of picoTCP stack library in a private memory arena
*
* This uses dlmopen() to load an instance of the LWIP stack into its
* own private memory space. This is done to get around the stack's
* lack of thread-safety or multi-instance support. The alternative
* would be to massively refactor the stack so everything lives in a
* state object instead of static memory space.
*/
class jip_stack
{
public:
void *_libref;
void close() {
#if defined(__STATIC__LWIP__)
return;
#elif defined(__DYNAMIC_LWIP__)
dlclose(_libref);
#endif
}
//void (*_netif_init)(void);
Mutex _lock;
Mutex _lock_mem;
jip_stack(const char* path) :
_libref(NULL)
{
#if defined(__ANDROID__) || defined(__UNITY_3D__)
#define __STATIC_LWIP__
#elif defined(__linux__)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
#elif defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#include "node/Mutex.hpp"
#define __STATIC_LWIP__
// iOS Simulator or iOS device
// Do nothing, symbols are statically-linked
#elif TARGET_OS_MAC && !defined(SDK_BUNDLED)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlopen(path, RTLD_NOW);
#else
#define __STATIC_LWIP__
#endif
#endif
#ifdef __STATIC_LWIP__ // Set static references (for use in iOS)
//_netif_init = (void(*)(void))&netif_init;
#endif
#ifdef __DYNAMIC_LWIP__ // Use dynamically-loaded symbols (for use in normal desktop applications)
if(_libref == NULL)
DEBUG_ERROR("dlerror(): %s", dlerror());
//_netif_init = (void(*)(void))dlsym(_libref, "netif_init");
#endif
}
~jip_stack()
{
if (_libref)
dlclose(_libref);
}
//inline void __netif_init(void) throw() { Mutex::Lock _l(_lock); _netif_init(); }
};
} // namespace ZeroTier
#endif

384
src/SDK_lwip.hpp Normal file
View File

@@ -0,0 +1,384 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#ifndef SDK_LWIPSTACK_H
#define SDK_LWIPSTACK_H
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/init.h"
#include "lwip/udp.h"
#include "Mutex.hpp"
#include "OSUtils.hpp"
#include "SDK_Debug.h"
#include <stdio.h>
#include <dlfcn.h>
#ifdef D_GNU_SOURCE
#define _GNU_SOURCE
#endif
//typedef ip_addr ip_addr_t;
struct tcp_pcb;
// lwip General Stack API
#define PBUF_FREE_SIG struct pbuf *p
#define PBUF_ALLOC_SIG pbuf_layer layer, u16_t length, pbuf_type type
#define LWIP_HTONS_SIG u16_t x
#define LWIP_NTOHS_SIG u16_t x
#define IPADDR_NTOA_SIG const ip_addr_t *addr
#define ETHIP6_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr
#define ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip6_addr_t *ipaddr
//#define ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr
#define NETIF_ADD_SIG struct netif *netif, void *state, netif_init_fn init, netif_input_fn input
//#define 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 ETHERNET_INPUT_SIG struct pbuf *p, struct netif *netif
#define IP_INPUT_SIG struct pbuf *p, struct netif *inp
#define NETIF_SET_DEFAULT_SIG struct netif *netif
#define NETIF_SET_UP_SIG struct netif *netif
#define NETIF_POLL_SIG struct netif *netif
// lwIP UDP API
#define UDP_NEW_SIG void
#define UDP_CONNECT_SIG struct udp_pcb * pcb, ip_addr_t * ipaddr, u16_t port
#define UDP_SEND_SIG struct udp_pcb * pcb, struct pbuf * p
#define UDP_SENDTO_SIG struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip, u16_t dst_port
#define UDP_RECV_SIG struct udp_pcb * pcb, void (* recv)(void * arg, struct udp_pcb * upcb, struct pbuf * p, ip_addr_t * addr, u16_t port), void * recv_arg
#define UDP_RECVED_SIG struct udp_pcb * pcb, u16_t len
#define UDP_BIND_SIG struct udp_pcb * pcb, const ip_addr_t * ipaddr, u16_t port
#define UDP_REMOVE_SIG struct udp_pcb *pcb
// lwIP TCP API
#define TCP_WRITE_SIG struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags
#define TCP_SENT_SIG struct tcp_pcb * pcb, err_t (* sent)(void * arg, struct tcp_pcb * tpcb, u16_t len)
#define TCP_NEW_SIG void
#define TCP_RECV_SIG struct tcp_pcb * pcb, err_t (* recv)(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t err)
#define TCP_RECVED_SIG struct tcp_pcb * pcb, u16_t len
#define TCP_SNDBUF_SIG struct tcp_pcb * pcb
#define TCP_CONNECT_SIG struct tcp_pcb * pcb, ip_addr_t * ipaddr, u16_t port, err_t (* connected)(void * arg, struct tcp_pcb * tpcb, err_t err)
#define TCP_RECV_SIG struct tcp_pcb * pcb, err_t (* recv)(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t err)
#define TCP_ERR_SIG struct tcp_pcb * pcb, void (* err)(void * arg, err_t err)
#define TCP_POLL_SIG struct tcp_pcb * pcb, err_t (* poll)(void * arg, struct tcp_pcb * tpcb), u8_t interval
#define TCP_ARG_SIG struct tcp_pcb * pcb, void * arg
#define TCP_CLOSE_SIG struct tcp_pcb * pcb
#define TCP_ABORT_SIG struct tcp_pcb * pcb
#define TCP_OUTPUT_SIG struct tcp_pcb * pcb
#define TCP_ACCEPT_SIG struct tcp_pcb * pcb, err_t (* accept)(void * arg, struct tcp_pcb * newpcb, err_t err)
#define TCP_LISTEN_SIG struct tcp_pcb * pcb
#define TCP_LISTEN_WITH_BACKLOG_SIG struct tcp_pcb * pcb, u8_t backlog
#define TCP_BIND_SIG struct tcp_pcb * pcb, const ip_addr_t * ipaddr, u16_t port
#define TCP_INPUT_SIG struct pbuf *p, struct netif *inp
void dwr(int level, const char *fmt, ... );
#define NETIF_IP6_ADDR_SET_STATE_SIG struct netif* netif, s8_t addr_idx, u8_t state
#define NETIF_LOOPIF_INIT_SIG struct netif *netif
#define NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG struct netif *netif, u8_t from_mac_48bit
//#define NETIF_SET_ADDR_SIG struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw
namespace ZeroTier {
/**
* Loads an instance of liblwip.so in a private memory arena
*
* This uses dlmopen() to load an instance of the LWIP stack into its
* own private memory space. This is done to get around the stack's
* lack of thread-safety or multi-instance support. The alternative
* would be to massively refactor the stack so everything lives in a
* state object instead of static memory space.
*/
class lwIP_stack
{
public:
void *_libref;
void close() {
#if defined(__STATIC__LWIP__)
return;
#elif defined(__DYNAMIC_LWIP__)
dlclose(_libref);
#endif
}
void (*_netif_init)(void);
void (*_nd6_tmr)(void);
void (*_netif_ip6_addr_set_state)(NETIF_IP6_ADDR_SET_STATE_SIG);
void (*_netif_loopif_init)(NETIF_LOOPIF_INIT_SIG);
void (*_netif_create_ip6_linklocal_address)(NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG);
err_t (*_ethip6_output)(ETHIP6_OUTPUT_SIG);
// void (*_netif_set_addr)(NETIF_SET_ADDR_SIG);
void (*_lwip_init)();
err_t (*_tcp_write)(TCP_WRITE_SIG);
void (*_tcp_sent)(TCP_SENT_SIG);
struct tcp_pcb * (*_tcp_new)(TCP_NEW_SIG);
u16_t (*_tcp_sndbuf)(TCP_SNDBUF_SIG);
err_t (*_tcp_connect)(TCP_CONNECT_SIG);
struct udp_pcb * (*_udp_new)(UDP_NEW_SIG);
err_t (*_udp_connect)(UDP_CONNECT_SIG);
err_t (*_udp_send)(UDP_SEND_SIG);
err_t (*_udp_sendto)(UDP_SENDTO_SIG);
void (*_udp_recv)(UDP_RECV_SIG);
void (*_udp_recved)(UDP_RECVED_SIG);
err_t (*_udp_bind)(UDP_BIND_SIG);
void (*_udp_remove)(UDP_REMOVE_SIG);
void (*_tcp_recv)(TCP_RECV_SIG);
void (*_tcp_recved)(TCP_RECVED_SIG);
void (*_tcp_err)(TCP_ERR_SIG);
void (*_tcp_poll)(TCP_POLL_SIG);
void (*_tcp_arg)(TCP_ARG_SIG);
err_t (*_tcp_close)(TCP_CLOSE_SIG);
void (*_tcp_abort)(TCP_ABORT_SIG);
err_t (*_tcp_output)(TCP_OUTPUT_SIG);
void (*_tcp_accept)(TCP_ACCEPT_SIG);
struct tcp_pcb * (*_tcp_listen)(TCP_LISTEN_SIG);
struct tcp_pcb * (*_tcp_listen_with_backlog)(TCP_LISTEN_WITH_BACKLOG_SIG);
err_t (*_tcp_bind)(TCP_BIND_SIG);
void (*_etharp_tmr)(void);
void (*_tcp_tmr)(void);
u8_t (*_pbuf_free)(PBUF_FREE_SIG);
struct pbuf * (*_pbuf_alloc)(PBUF_ALLOC_SIG);
u16_t (*_lwip_htons)(LWIP_HTONS_SIG);
u16_t (*_lwip_ntohs)(LWIP_NTOHS_SIG);
char* (*_ipaddr_ntoa)(IPADDR_NTOA_SIG);
err_t (*_etharp_output)(ETHARP_OUTPUT_SIG);
err_t (*_ethernet_input)(ETHERNET_INPUT_SIG);
void (*_tcp_input)(TCP_INPUT_SIG);
err_t (*_ip_input)(IP_INPUT_SIG);
void (*_netif_set_default)(NETIF_SET_DEFAULT_SIG);
struct netif * (*_netif_add)(NETIF_ADD_SIG);
void (*_netif_set_up)(NETIF_SET_UP_SIG);
void (*_netif_poll)(NETIF_POLL_SIG);
Mutex _lock;
Mutex _lock_mem;
lwIP_stack(const char* path) :
_libref(NULL)
{
#if defined(__ANDROID__) || defined(__UNITY_3D__)
#define __STATIC_LWIP__
#elif defined(__linux__)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
#elif defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#include "node/Mutex.hpp"
#define __STATIC_LWIP__
// iOS Simulator or iOS device
// Do nothing, symbols are statically-linked
#elif TARGET_OS_MAC && !defined(SDK_BUNDLED)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlopen(path, RTLD_NOW);
#else
#define __STATIC_LWIP__
#endif
#endif
#ifdef __STATIC_LWIP__ // Set static references (for use in iOS)
_netif_init = (void(*)(void))&netif_init;
_nd6_tmr = (void(*)(void))&nd6_tmr;
_netif_ip6_addr_set_state = (void(*)(NETIF_IP6_ADDR_SET_STATE_SIG))&netif_ip6_addr_set_state;
_netif_loopif_init = (void(*)(NETIF_LOOPIF_INIT_SIG))&netif_loopif_init;
_netif_create_ip6_linklocal_address = (void(*)(NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG))&netif_create_ip6_linklocal_address;
_ethip6_output = (err_t(*)(ETHIP6_OUTPUT_SIG))&ethip6_output;
_ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))&ethernet_input;
_etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))&etharp_output;
_lwip_init = (void(*)(void))&lwip_init;
_tcp_write = (err_t(*)(TCP_WRITE_SIG))&tcp_write;
_tcp_sent = (void(*)(TCP_SENT_SIG))&tcp_sent;
_tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))&tcp_new;
_udp_new = (struct udp_pcb*(*)(UDP_NEW_SIG))&udp_new;
_udp_connect = (err_t(*)(UDP_CONNECT_SIG))&udp_connect;
_udp_send = (err_t(*)(UDP_SEND_SIG))&udp_send;
_udp_sendto = (err_t(*)(UDP_SENDTO_SIG))&udp_sendto;
_udp_recv = (void(*)(UDP_RECV_SIG))&udp_recv;
_udp_bind = (err_t(*)(UDP_BIND_SIG))&udp_bind;
_udp_remove = (void(*)(UDP_REMOVE_SIG))&udp_remove;
_tcp_connect = (err_t(*)(TCP_CONNECT_SIG))&tcp_connect;
_tcp_recv = (void(*)(TCP_RECV_SIG))&tcp_recv;
_tcp_recved = (void(*)(TCP_RECVED_SIG))&tcp_recved;
_tcp_err = (void(*)(TCP_ERR_SIG))&tcp_err;
_tcp_poll = (void(*)(TCP_POLL_SIG))&tcp_poll;
_tcp_arg = (void(*)(TCP_ARG_SIG))&tcp_arg;
_tcp_close = (err_t(*)(TCP_CLOSE_SIG))&tcp_close;
_tcp_abort = (void(*)(TCP_ABORT_SIG))&tcp_abort;
_tcp_output = (err_t(*)(TCP_OUTPUT_SIG))&tcp_output;
_tcp_accept = (void(*)(TCP_ACCEPT_SIG))&tcp_accept;
_tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))&tcp_listen_with_backlog;
_tcp_bind = (err_t(*)(TCP_BIND_SIG))&tcp_bind;
_etharp_tmr = (void(*)(void))&etharp_tmr;
_tcp_tmr = (void(*)(void))&tcp_tmr;
_pbuf_free = (u8_t(*)(PBUF_FREE_SIG))&pbuf_free;
_pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))&pbuf_alloc;
_lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))&lwip_htons;
_lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))&lwip_ntohs;
_ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))&ipaddr_ntoa;
_tcp_input = (void(*)(TCP_INPUT_SIG))&tcp_input;
_ip_input = (err_t(*)(IP_INPUT_SIG))&ip_input;
_netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))&netif_set_default;
_netif_add = (struct netif*(*)(NETIF_ADD_SIG))&netif_add;
_netif_set_up = (void(*)(NETIF_SET_UP_SIG))&netif_set_up;
#endif
#ifdef __DYNAMIC_LWIP__ // Use dynamically-loaded symbols (for use in normal desktop applications)
if(_libref == NULL)
DEBUG_ERROR("dlerror(): %s", dlerror());
_netif_init = (void(*)(void))dlsym(_libref, "netif_init");
_nd6_tmr = (void(*)(void))dlsym(_libref, "nd6_tmr");
_netif_ip6_addr_set_state = (void(*)(NETIF_IP6_ADDR_SET_STATE_SIG))dlsym(_libref, "netif_ip6_addr_set_state");
_netif_loopif_init = (void(*)(NETIF_LOOPIF_INIT_SIG))dlsym(_libref, "netif_loopif_init");
_netif_create_ip6_linklocal_address = (void(*)(NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG))dlsym(_libref, "netif_create_ip6_linklocal_address");
_ethip6_output = (err_t(*)(ETHIP6_OUTPUT_SIG))dlsym(_libref, "ethip6_output");
// _netif_set_addr = (void(*))(NETIF_SET_ADDR_SIG))dlsym(_libref, "netif_set_addr");
_ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(_libref, "ethernet_input");
_etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(_libref, "etharp_output");
_lwip_init = (void(*)(void))dlsym(_libref, "lwip_init");
_tcp_write = (err_t(*)(TCP_WRITE_SIG))dlsym(_libref, "tcp_write");
_tcp_sent = (void(*)(TCP_SENT_SIG))dlsym(_libref, "tcp_sent");
_tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))dlsym(_libref, "tcp_new");
_udp_new = (struct udp_pcb*(*)(UDP_NEW_SIG))dlsym(_libref, "udp_new");
_udp_connect = (err_t(*)(UDP_CONNECT_SIG))dlsym(_libref, "udp_connect");
_udp_send = (err_t(*)(UDP_SEND_SIG))dlsym(_libref, "udp_send");
_udp_sendto = (err_t(*)(UDP_SENDTO_SIG))dlsym(_libref, "udp_sendto");
_udp_recv = (void(*)(UDP_RECV_SIG))dlsym(_libref, "udp_recv");
_udp_bind = (err_t(*)(UDP_BIND_SIG))dlsym(_libref, "udp_bind");
_udp_remove = (void(*)(UDP_REMOVE_SIG))dlsym(_libref, "udp_remove");
_tcp_sndbuf = (u16_t(*)(TCP_SNDBUF_SIG))dlsym(_libref, "tcp_sndbuf");
_tcp_connect = (err_t(*)(TCP_CONNECT_SIG))dlsym(_libref, "tcp_connect");
_tcp_recv = (void(*)(TCP_RECV_SIG))dlsym(_libref, "tcp_recv");
_tcp_recved = (void(*)(TCP_RECVED_SIG))dlsym(_libref, "tcp_recved");
_tcp_err = (void(*)(TCP_ERR_SIG))dlsym(_libref, "tcp_err");
_tcp_poll = (void(*)(TCP_POLL_SIG))dlsym(_libref, "tcp_poll");
_tcp_arg = (void(*)(TCP_ARG_SIG))dlsym(_libref, "tcp_arg");
_tcp_close = (err_t(*)(TCP_CLOSE_SIG))dlsym(_libref, "tcp_close");
_tcp_abort = (void(*)(TCP_ABORT_SIG))dlsym(_libref, "tcp_abort");
_tcp_output = (err_t(*)(TCP_OUTPUT_SIG))dlsym(_libref, "tcp_output");
_tcp_accept = (void(*)(TCP_ACCEPT_SIG))dlsym(_libref, "tcp_accept");
_tcp_listen = (struct tcp_pcb*(*)(TCP_LISTEN_SIG))dlsym(_libref, "tcp_listen");
_tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))dlsym(_libref, "tcp_listen_with_backlog");
_tcp_bind = (err_t(*)(TCP_BIND_SIG))dlsym(_libref, "tcp_bind");
_etharp_tmr = (void(*)(void))dlsym(_libref, "etharp_tmr");
_tcp_tmr = (void(*)(void))dlsym(_libref, "tcp_tmr");
_pbuf_free = (u8_t(*)(PBUF_FREE_SIG))dlsym(_libref, "pbuf_free");
_pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))dlsym(_libref, "pbuf_alloc");
_lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))dlsym(_libref, "lwip_htons");
_lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))dlsym(_libref, "lwip_ntohs");
_ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))dlsym(_libref, "ipaddr_ntoa");
_tcp_input = (void(*)(TCP_INPUT_SIG))dlsym(_libref, "tcp_input");
_ip_input = (err_t(*)(IP_INPUT_SIG))dlsym(_libref, "ip_input");
_netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(_libref, "netif_set_default");
_netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(_libref, "netif_add");
_netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(_libref, "netif_set_up");
#endif
}
~lwIP_stack()
{
if (_libref)
dlclose(_libref);
}
inline void __netif_init(void) throw() { Mutex::Lock _l(_lock); _netif_init(); }
inline void __nd6_tmr(void) throw() { Mutex::Lock _l(_lock); _nd6_tmr(); }
inline void __netif_ip6_addr_set_state(NETIF_IP6_ADDR_SET_STATE_SIG) throw() { Mutex::Lock _l(_lock); _netif_ip6_addr_set_state(netif, addr_idx, state); }
inline void __netif_loopif_init(NETIF_LOOPIF_INIT_SIG) throw() { Mutex::Lock _l(_lock); _netif_loopif_init(netif); }
inline void __netif_create_ip6_linklocal_address(NETIF_CREATE_IP6_LINKLOCAL_ADDRESS_SIG) throw() { Mutex::Lock _l(_lock); _netif_create_ip6_linklocal_address(netif, from_mac_48bit); }
// inline void __netif_set_addr(NETIF_SET_ADDR_SIG) throw() { Mutex::Lock _l(_lock); _netif_set_addr(netif, ipaddr, netmask, gw); }
inline void __lwip_init() throw() { Mutex::Lock _l(_lock); return _lwip_init(); }
inline err_t __tcp_write(TCP_WRITE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_write(pcb,arg,len,apiflags); }
inline void __tcp_sent(TCP_SENT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sent(pcb,sent); }
inline struct tcp_pcb * __tcp_new(TCP_NEW_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_new(); }
inline struct udp_pcb * __udp_new(UDP_NEW_SIG) throw() { Mutex::Lock _l(_lock); return _udp_new(); }
inline err_t __udp_connect(UDP_CONNECT_SIG) throw() { Mutex::Lock _l(_lock); return _udp_connect(pcb,ipaddr,port); }
inline err_t __udp_send(UDP_SEND_SIG) throw() { Mutex::Lock _l(_lock); return _udp_send(pcb,p); }
inline err_t __udp_sendto(UDP_SENDTO_SIG) throw() { Mutex::Lock _l(_lock); return _udp_sendto(pcb,p,dst_ip,dst_port); }
inline void __udp_recv(UDP_RECV_SIG) throw() { Mutex::Lock _l(_lock); return _udp_recv(pcb,recv,recv_arg); }
inline err_t __udp_bind(UDP_BIND_SIG) throw() { Mutex::Lock _l(_lock); return _udp_bind(pcb,ipaddr,port); }
inline void __udp_remove(UDP_REMOVE_SIG) throw() { Mutex::Lock _l(_lock); return _udp_remove(pcb); }
inline u16_t __tcp_sndbuf(TCP_SNDBUF_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sndbuf(pcb); }
inline err_t __tcp_connect(TCP_CONNECT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_connect(pcb,ipaddr,port,connected); }
inline void __tcp_recv(TCP_RECV_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recv(pcb,recv); }
inline void __tcp_recved(TCP_RECVED_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recved(pcb,len); }
inline void __tcp_err(TCP_ERR_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_err(pcb,err); }
inline void __tcp_poll(TCP_POLL_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_poll(pcb,poll,interval); }
inline void __tcp_arg(TCP_ARG_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_arg(pcb,arg); }
inline err_t __tcp_close(TCP_CLOSE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_close(pcb); }
inline void __tcp_abort(TCP_ABORT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_abort(pcb); }
inline err_t __tcp_output(TCP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_output(pcb); }
inline void __tcp_accept(TCP_ACCEPT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_accept(pcb,accept); }
inline struct tcp_pcb * __tcp_listen(TCP_LISTEN_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_listen(pcb); }
inline struct tcp_pcb * __tcp_listen_with_backlog(TCP_LISTEN_WITH_BACKLOG_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_listen_with_backlog(pcb,backlog); }
inline err_t __tcp_bind(TCP_BIND_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_bind(pcb,ipaddr,port); }
inline void __etharp_tmr(void) throw() { Mutex::Lock _l(_lock); return _etharp_tmr(); }
inline void __tcp_tmr(void) throw() { Mutex::Lock _l(_lock); return _tcp_tmr(); }
inline u8_t __pbuf_free(PBUF_FREE_SIG) throw() { Mutex::Lock _l(_lock); return _pbuf_free(p); }
inline struct pbuf * __pbuf_alloc(PBUF_ALLOC_SIG) throw() { Mutex::Lock _l(_lock_mem); return _pbuf_alloc(layer,length,type); }
inline u16_t __lwip_htons(LWIP_HTONS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_htons(x); }
inline u16_t __lwip_ntohs(LWIP_NTOHS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_ntohs(x); }
inline char* __ipaddr_ntoa(IPADDR_NTOA_SIG) throw() { Mutex::Lock _l(_lock); return _ipaddr_ntoa(addr); }
inline err_t __ethip6_output(ETHIP6_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ethip6_output(netif,q,ip6addr); }
//inline err_t __etharp_output(ETHARP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _etharp_output(netif,q,ipaddr); }
inline struct netif * __netif_add(NETIF_ADD_SIG) throw() { Mutex::Lock _l(_lock); return _netif_add(netif,state,init,input); }
//inline struct netif * __netif_add(NETIF_ADD_SIG) throw() { Mutex::Lock _l(_lock); return _netif_add(netif,ipaddr,netmask,gw,state,init,input); }
inline err_t __ethernet_input(ETHERNET_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ethernet_input(p,netif); }
inline void __tcp_input(TCP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_input(p,inp); }
inline err_t __ip_input(IP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ip_input(p,inp); }
inline void __netif_set_default(NETIF_SET_DEFAULT_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_default(netif); }
inline void __netif_set_up(NETIF_SET_UP_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_up(netif); }
};
} // namespace ZeroTier
#endif

188
src/SDK_pico.hpp Normal file
View File

@@ -0,0 +1,188 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#ifndef SDK_PICOSTACK_H
#define SDK_PICOSTACK_H
#include "Mutex.hpp"
#include "OSUtils.hpp"
#include "SDK_Debug.h"
#include <stdio.h>
#include <dlfcn.h>
#ifdef D_GNU_SOURCE
#define _GNU_SOURCE
#endif
#if defined(SDK_LWIP)
#include "SDK_lwip.hpp"
#elif defined(SDK_PICOTCP)
#include "SDK_pico.hpp"
#include "pico_stack.h"
#include "pico_ipv4.h"
#include "pico_icmp4.h"
#include "pico_dev_tap.h"
#elif defined(SDK_JIP)
#include "SDK_jip.hpp"
#endif
#define PICO_STRING_TO_IPV4_SIG const char *ipstr, uint32_t *ip
#define PICO_IPV4_TO_STRING_SIG char *ipbuf, const uint32_t ip
#define PICO_TAP_CREATE_SIG char *name
#define PICO_IPV4_LINK_ADD_SIG struct pico_device *dev, struct pico_ip4 address, struct pico_ip4 netmask
#define PICO_DEVICE_INIT_SIG struct pico_device *dev, const char *name, uint8_t *mac
#define PICO_STACK_RECV_SIG struct pico_device *dev, uint8_t *buffer, uint32_t len
#define PICO_ICMP4_PING_SIG char *dst, int count, int interval, int timeout, int size, void (*cb)(struct pico_icmp4_stats *)
namespace ZeroTier {
/**
* Loads an instance of picoTCP stack library in a private memory arena
*
* This uses dlmopen() to load an instance of the LWIP stack into its
* own private memory space. This is done to get around the stack's
* lack of thread-safety or multi-instance support. The alternative
* would be to massively refactor the stack so everything lives in a
* state object instead of static memory space.
*/
class picoTCP_stack
{
public:
void *_libref;
void close() {
#if defined(__STATIC__LWIP__)
return;
#elif defined(__DYNAMIC_LWIP__)
dlclose(_libref);
#endif
}
// SIP-
void (*_pico_stack_init)(void);
void (*_pico_stack_tick)(void);
int (*_pico_string_to_ipv4)(PICO_STRING_TO_IPV4_SIG);
int (*_pico_ipv4_to_string)(PICO_IPV4_TO_STRING_SIG);
struct pico_device* (*_pico_tap_create)(PICO_TAP_CREATE_SIG);
int (*_pico_ipv4_link_add)(PICO_IPV4_LINK_ADD_SIG);
int (*_pico_device_init)(PICO_DEVICE_INIT_SIG);
int32_t (*_pico_stack_recv)(PICO_STACK_RECV_SIG);
int (*_pico_icmp4_ping)(PICO_ICMP4_PING_SIG);
Mutex _lock;
Mutex _lock_mem;
picoTCP_stack(const char* path) :
_libref(NULL)
{
#if defined(__ANDROID__) || defined(__UNITY_3D__)
#define __STATIC_LWIP__
#elif defined(__linux__)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
#elif defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#include "node/Mutex.hpp"
#define __STATIC_LWIP__
// iOS Simulator or iOS device
// Do nothing, symbols are statically-linked
#elif TARGET_OS_MAC && !defined(SDK_BUNDLED)
#define __DYNAMIC_LWIP__
// Dynamically load liblwip.so
_libref = dlopen(path, RTLD_NOW);
#else
#define __STATIC_LWIP__
#endif
#endif
#ifdef __STATIC_LWIP__ // Set static references (for use in iOS)
// SIP-
_pico_stack_init = (void(*)(void))&pico_stack_init;
_pico_stack_tick = (void(*)(void))&pico_stack_tick;
_pico_tap_create = (struct pico_device*(*)(PICO_TAP_CREATE_SIG)&pico_tap_create;
_pico_string_to_ipv4 = (int(*)(PICO_STRING_TO_IPV4_SIG))&pico_string_to_ipv4;
_pico_ipv4_to_string = (int(*)(PICO_IPV4_TO_STRING_SIG))&pico_ipv4_to_string;
_pico_ipv4_link_add = (int(*)(PICO_IPV4_LINK_ADD_SIG))&pico_ipv4_link_add;
_pico_device_init = (int(*)(PICO_DEVICE_INIT_SIG))&pico_device_init;
_pico_stack_recv = (int32_t(*)(PICO_STACK_RECV_SIG))&pico_stack_recv;
_pico_icmp4_ping = (int(*)(PICO_ICMP4_PING_SIG))&pico_icmp4_ping;
#endif
#ifdef __DYNAMIC_LWIP__ // Use dynamically-loaded symbols (for use in normal desktop applications)
if(_libref == NULL)
DEBUG_ERROR("dlerror(): %s", dlerror());
// SIP-
_pico_stack_init = (void(*)(void))dlsym(_libref, "pico_stack_init");
_pico_stack_tick = (void(*)(void))dlsym(_libref, "pico_stack_tick");
_pico_tap_create = (struct pico_device*(*)(PICO_TAP_CREATE_SIG))dlsym(_libref, "pico_tap_create");
_pico_string_to_ipv4 = (int(*)(PICO_STRING_TO_IPV4_SIG))dlsym(_libref, "pico_string_to_ipv4");
_pico_ipv4_to_string = (int(*)(PICO_IPV4_TO_STRING_SIG))dlsym(_libref, "pico_ipv4_to_string");
_pico_ipv4_link_add = (int(*)(PICO_IPV4_LINK_ADD_SIG))dlsym(_libref, "pico_ipv4_link_add");
_pico_device_init = (int(*)(PICO_DEVICE_INIT_SIG))dlsym(_libref, "pico_device_init");
_pico_stack_recv = (int32_t(*)(PICO_STACK_RECV_SIG))dlsym(_libref, "pico_stack_recv");
_pico_icmp4_ping = (int(*)(PICO_ICMP4_PING_SIG))dlsym(_libref, "pico_icmp4_ping");
#endif
}
~picoTCP_stack()
{
if (_libref)
dlclose(_libref);
}
// SIP-
inline void __pico_stack_init(void) throw() { Mutex::Lock _l(_lock); _pico_stack_init(); }
inline void __pico_stack_tick(void) throw() { Mutex::Lock _l(_lock); _pico_stack_tick(); }
inline struct pico_device * __pico_tap_create(PICO_TAP_CREATE_SIG) throw() { Mutex::Lock _l(_lock); return _pico_tap_create(name); }
inline int __pico_string_to_ipv4(PICO_STRING_TO_IPV4_SIG) throw() { Mutex::Lock _l(_lock); return _pico_string_to_ipv4(ipstr, ip); }
inline int __pico_ipv4_to_string(PICO_IPV4_TO_STRING_SIG) throw() { Mutex::Lock _l(_lock); return _pico_ipv4_to_string(ipbuf, ip); }
inline int __pico_ipv4_link_add(PICO_IPV4_LINK_ADD_SIG) throw() { Mutex::Lock _l(_lock); return _pico_ipv4_link_add(dev, address, netmask); }
inline int __pico_device_init(PICO_DEVICE_INIT_SIG) throw() { Mutex::Lock _l(_lock); return _pico_device_init(dev, name, mac); }
inline int __pico_stack_recv(PICO_STACK_RECV_SIG) throw() { /*Mutex::Lock _l(_lock);*/ return _pico_stack_recv(dev, buffer, len); }
inline int __pico_icmp4_ping(PICO_ICMP4_PING_SIG) throw() { Mutex::Lock _l(_lock); return _pico_icmp4_ping(dst, count, interval, timeout, size, cb); }
};
} // namespace ZeroTier
#endif