Switch to MEM_LIBC_MALLOC usage in lwIP. Added event callbacks. Use of finer-grained locks in RX logic. CRCs disabled on inbound packets

This commit is contained in:
Joseph Henry
2019-01-25 12:42:53 -08:00
parent 2ac133b435
commit 068013d0f7
15 changed files with 3310 additions and 724 deletions

View File

@@ -34,15 +34,31 @@
#define LIBZT_CONSTANTS_HPP
//////////////////////////////////////////////////////////////////////////////
// Error codes returned by libzt API //
// Error codes returned by ZeroTier and the libzt API //
// See ext/ZeroTierOne/include/ZeroTierOne.h //
//////////////////////////////////////////////////////////////////////////////
typedef int zts_err_t;
#define ZTS_ERR_OK 0
#define ZTS_ERR_INVALID_ARG -1 // A parameter provided by the user application is invalid (e.g. our of range, NULL, etc)
#define ZTS_ERR_SERVICE -2 // The service isn't initialized or is for some other reason currently unavailable
#define ZTS_ERR_INVALID_OP -3 // For some reason this API operation is not permitted (perhaps the service is still starting?)
#define ZTS_ERR_OK 0 // Everything is ok
#define ZTS_ERR_INVALID_ARG -1 // A parameter provided by the user application is invalid (e.g. our of range, NULL, etc)
#define ZTS_ERR_SERVICE -2 // The service isn't initialized or is for some other reason currently unavailable
#define ZTS_ERR_INVALID_OP -3 // For some reason this API operation is not permitted (perhaps the service is still starting?)
#define ZTS_EVENT_NODE_ONLINE 0x01 // Node is online
#define ZTS_EVENT_NODE_OFFLINE 0x02 // Node is offline
#define ZTS_EVENT_NODE_DOWN 0x03 // Node is shutting down
#define ZTS_EVENT_NODE_IDENTITY_COLLISION 0x04 // Identity collision - check for duplicate instances
#define ZTS_EVENT_NODE_UNRECOVERABLE_ERROR 0x05 // Something is seriously wrong
#define ZTS_EVENT_NODE_NORMAL_TERMINATION 0x06 // Service thread has stopped
#define ZTS_EVENT_NETWORK_NOT_FOUND 0x07
#define ZTS_EVENT_NETWORK_CLIENT_TOO_OLD 0x08
#define ZTS_EVENT_NETWORK_REQUESTING_CONFIG 0x09
#define ZTS_EVENT_NETWORK_OK 0x0a
#define ZTS_EVENT_NETWORK_ACCESS_DENIED 0x0b
#define ZTS_EVENT_NETWORK_READY 0x0c
#define ZTS_EVENT_NETWORK_DOWN 0x0d
//////////////////////////////////////////////////////////////////////////////
// libzt config //
@@ -113,6 +129,16 @@ typedef int zts_err_t;
*/
#define ZTS_HOUSEKEEPING_INTERVAL 1000
/**
* Name of the service thread (for debug purposes only)
*/
#define ZTS_SERVICE_THREAD_NAME "ZeroTierServiceThread"
/**
* Name of the callback monitor thread (for debug purposes only)
*/
#define ZTS_EVENT_CALLBACK_THREAD_NAME "ZeroTierEventCallbackThread"
//////////////////////////////////////////////////////////////////////////////
// lwIP driver config //
// For more LWIP configuration options see: include/lwipopts.h //

View File

@@ -89,6 +89,22 @@ ZT_SOCKET_API int ZTCALL zts_get_service_port();
*/
ZT_SOCKET_API int ZTCALL zts_start(const char *path, int blocking);
/**
* @brief Starts the ZeroTier service and notifies user application of events via callback
*
* @usage Should be called at the beginning of your application. Will blocks until all of the following conditions are met:
* - ZeroTier core service has been initialized
* - Cryptographic identity has been generated or loaded from directory specified by `path`
* - Virtual network is successfully joined
* - IP address is assigned by network controller service
* @param path path directory where cryptographic identities and network configuration files are stored and retrieved
* (`identity.public`, `identity.secret`)
* @param userCallbackFunc User-specified callback for ZeroTier events
* @param blocking whether or not this call will block until the entire service is up and running
* @return 0 if successful; or 1 if failed
*/
ZT_SOCKET_API int ZTCALL zts_start_with_callback(const char *path, void (*userCallbackFunc)(uint64_t, int), int blocking);
/**
* @brief Starts the ZeroTier service
*

View File

@@ -77,6 +77,25 @@ public:
~VirtualTap();
/**
* A state will only be reported via callback if it differs from this value. Subsequently this
* value will be updated.
*/
int _lastReportedStatus;
/**
* The last time that this virtual tap received a network config update from the core
*/
uint64_t _lastConfigUpdateTime = 0;
/**
* The last time that a callback notification was sent to the user application signalling
* that this interface is ready to process traffic.
*/
uint64_t _lastReadyReportTime = 0;
void lastConfigUpdate(uint64_t lastConfigUpdateTime);
void setEnabled(bool en);
bool enabled() const;
@@ -163,7 +182,6 @@ public:
//////////////////////////////////////////////////////////////////////////////
std::vector<std::pair<InetAddress, InetAddress> > routes;
void *zt1ServiceRef = NULL;
char vtap_full_name[64];
char vtap_abbr_name[16];

View File

@@ -30,14 +30,14 @@
* Management of virtual tap interfaces
*/
#ifndef LIBZT_VIRTUAL_TAP_MANAGER_H
#define LIBZT_VIRTUAL_TAP_MANAGER_H
#include "VirtualTap.hpp"
#include "OneService.hpp"
namespace ZeroTier {
extern std::vector<void*> vtaps;
extern Mutex _vtaps_lock;
class VirtualTap;
/**
@@ -47,94 +47,16 @@ class VirtualTapManager
{
public:
static void add_tap(VirtualTap *tap) {
_vtaps_lock.lock();
vtaps.push_back((void*)tap);
_vtaps_lock.unlock();
}
static VirtualTap *getTapByNWID(uint64_t nwid)
{
_vtaps_lock.lock();
VirtualTap *s, *tap = nullptr;
for (size_t i=0; i<vtaps.size(); i++) {
s = (VirtualTap*)vtaps[i];
if (s->_nwid == nwid) { tap = s; }
}
_vtaps_lock.unlock();
return tap;
}
static size_t get_vtaps_size() {
size_t sz;
_vtaps_lock.lock();
sz = vtaps.size();
_vtaps_lock.unlock();
return sz;
}
// TODO: We shouldn't re-apply the reference to everything all the time
static void update_service_references(void *serviceRef) {
_vtaps_lock.lock();
for (size_t i=0;i<vtaps.size(); i++) {
VirtualTap *s = (VirtualTap*)vtaps[i];
s->zt1ServiceRef=serviceRef;
}
_vtaps_lock.unlock();
}
static void remove_by_nwid(uint64_t nwid) {
_vtaps_lock.lock();
for (size_t i=0;i<vtaps.size(); i++) {
VirtualTap *s = (VirtualTap*)vtaps[i];
if (s->_nwid == nwid) {
vtaps.erase(vtaps.begin() + i);
}
}
_vtaps_lock.unlock();
}
static void clear() {
_vtaps_lock.lock();
vtaps.clear();
_vtaps_lock.unlock();
}
static void get_network_details(uint64_t nwid, struct zts_network_details *nd)
{
VirtualTap *tap;
socklen_t addrlen;
_vtaps_lock.lock();
for (size_t i=0; i<vtaps.size(); i++) {
tap = (VirtualTap*)vtaps[i];
if (tap->_nwid == nwid) {
nd->nwid = tap->_nwid;
nd->mtu = tap->_mtu;
// assigned addresses
nd->num_addresses = tap->_ips.size() < ZTS_MAX_ASSIGNED_ADDRESSES ? tap->_ips.size() : ZTS_MAX_ASSIGNED_ADDRESSES;
for (int j=0; j<nd->num_addresses; j++) {
addrlen = tap->_ips[j].isV4() ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
memcpy(&(nd->addr[j]), &(tap->_ips[j]), addrlen);
}
// routes
nd->num_routes = ZTS_MAX_NETWORK_ROUTES;
OneService *zt1Service = (OneService*)tap->zt1ServiceRef;
zt1Service->getRoutes(nwid, (ZT_VirtualNetworkRoute*)&(nd->routes)[0], &(nd->num_routes));
break;
}
}
_vtaps_lock.unlock();
}
static void get_all_network_details(struct zts_network_details *nds, int *num)
{
VirtualTap *tap;
*num = get_vtaps_size();
for (size_t i=0; i<vtaps.size(); i++) {
tap = (VirtualTap*)vtaps[i];
get_network_details(tap->_nwid, &nds[i]);
}
}
static void add_tap(VirtualTap *tap);
static VirtualTap *getTapByNWID(uint64_t nwid);
static size_t get_vtaps_size();
static void remove_by_nwid(uint64_t nwid);
static void clear();
static void get_network_details_helper(void *zt1ServiceRef, uint64_t nwid, struct zts_network_details *nd);
static void get_network_details(void *zt1ServiceRef, uint64_t nwid, struct zts_network_details *nd);
static void get_all_network_details(void *zt1ServiceRef, struct zts_network_details *nds, int *num);
};
} // namespace ZeroTier
} // namespace ZeroTier
#endif // _H

View File

@@ -100,7 +100,7 @@ typedef SSIZE_T ssize_t;
#define ZTS_PF_INET ZTS_AF_INET
#define ZTS_PF_INET6 ZTS_AF_INET6
#define ZTS_PF_UNSPEC ZTS_AF_UNSPEC
//
// Protocol command types
#define ZTS_IPPROTO_IP 0x0000
#define ZTS_IPPROTO_ICMP 0x0001
#define ZTS_IPPROTO_TCP 0x0006
@@ -121,7 +121,7 @@ typedef SSIZE_T ssize_t;
// fnctl() flags
#define ZTS_O_NONBLOCK 0x0001
#define ZTS_O_NDELAY 0x0001
//
// Shutdown commands
#define ZTS_SHUT_RD 0x0000
#define ZTS_SHUT_WR 0x0001
#define ZTS_SHUT_RDWR 0x0002
@@ -161,7 +161,7 @@ typedef SSIZE_T ssize_t;
// IPPROTO_IPV6 options
#define ZTS_IPV6_CHECKSUM 0x0007 // RFC3542
#define ZTS_IPV6_V6ONLY 0x001b // RFC3493
//
// Macro's for defining ioctl() command values
#define ZTS_IOCPARM_MASK 0x7fU
#define ZTS_IOC_VOID 0x20000000UL
#define ZTS_IOC_OUT 0x40000000UL
@@ -170,11 +170,10 @@ typedef SSIZE_T ssize_t;
#define ZTS_IO(x,y) (ZTS_IOC_VOID | ((x)<<8)|(y))
#define ZTS_IOR(x,y,t) (ZTS_IOC_OUT | (((long)sizeof(t) & ZTS_IOCPARM_MASK)<<16) | ((x)<<8) | (y))
#define ZTS_IOW(x,y,t) (ZTS_IOC_IN | (((long)sizeof(t) & ZTS_IOCPARM_MASK)<<16) | ((x)<<8) | (y))
//
// ioctl() commands
#define ZTS_FIONREAD ZTS_IOR('f', 127, unsigned long)
#define ZTS_FIONBIO ZTS_IOW('f', 126, unsigned long)
/* FD_SET used for lwip_select */
#ifndef ZTS_FD_SET
@@ -330,6 +329,9 @@ struct sockaddr_ll {
extern "C" {
#endif
// Custom errno to prevent conflicts with platform's own errno
extern int zts_errno;
/**
* @brief Create a socket
*

File diff suppressed because it is too large Load Diff