diff --git a/src/SocketTap.cpp b/src/SocketTap.cpp index 73a4fe2..e67f347 100644 --- a/src/SocketTap.cpp +++ b/src/SocketTap.cpp @@ -24,7 +24,6 @@ * of your own application. */ -// picoTCP #include #include #include @@ -32,19 +31,16 @@ #include #include -// SDK #include "SocketTap.hpp" #include "libzt.h" -// stack drivers #if defined(STACK_PICO) - #include "picoTCP.hpp" +#include "picoTCP.hpp" #endif #if defined(STACK_LWIP) - #include "lwIP.hpp" +#include "lwIP.hpp" #endif -// ZT #include "Utils.hpp" #include "OSUtils.hpp" #include "Constants.hpp" @@ -84,8 +80,6 @@ namespace ZeroTier { SocketTap::~SocketTap() { - // TODO: Verify that stack is fully stopped before - // deleting Connection objects _run = false; _phy.whack(); Thread::join(_thread); @@ -108,16 +102,12 @@ namespace ZeroTier { #if defined(STACK_PICO) if(picostack){ picostack->pico_init_interface(this, ip); - _ips.push_back(ip); - std::sort(_ips.begin(),_ips.end()); return true; } #endif #if defined(STACK_LWIP) if(lwipstack){ lwipstack->lwip_init_interface(this, ip); - _ips.push_back(ip); - std::sort(_ips.begin(),_ips.end()); return true; } #endif @@ -126,10 +116,18 @@ namespace ZeroTier { bool SocketTap::addIp(const InetAddress &ip) { +#if defined(NO_STACK) + DEBUG_INFO("addIp (%s)", ip.toString().c_str()); + _ips.push_back(ip); + std::sort(_ips.begin(),_ips.end()); + return true; +#endif if(registerIpWithStack(ip)) { // only start the stack if we successfully registered and initialized a device to // the given address + _ips.push_back(ip); + std::sort(_ips.begin(),_ips.end()); return true; } return false; @@ -137,7 +135,6 @@ namespace ZeroTier { bool SocketTap::removeIp(const InetAddress &ip) { - DEBUG_INFO(); Mutex::Lock _l(_ips_m); std::vector::iterator i(std::find(_ips.begin(),_ips.end(),ip)); if (i == _ips.end()) @@ -176,8 +173,9 @@ namespace ZeroTier { return _dev; } - void SocketTap::setFriendlyName(const char *friendlyName) { - DEBUG_INFO(); + void SocketTap::setFriendlyName(const char *friendlyName) + { + // Someday } void SocketTap::scanMulticastGroups(std::vector &added, @@ -235,7 +233,7 @@ namespace ZeroTier { void SocketTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len) { - //DEBUG_ATTN("sock->fd=%d", _phy.getDescriptor(sock)); + DEBUG_ATTN("sock->fd=%d", _phy.getDescriptor(sock)); Connection *conn = (Connection*)*uptr; if(!conn) return; @@ -248,7 +246,6 @@ namespace ZeroTier { void SocketTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool stack_invoked) { - DEBUG_INFO(); if(sock) Read(sock,uptr,stack_invoked); } diff --git a/src/libzt.cpp b/src/libzt.cpp index b5a3484..d97cd9a 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -24,7 +24,6 @@ * of your own application. */ - /* This file implements the libzt library API, it talks to the network stack driver and core ZeroTier service to create a socket-like interface for applications to use. See also: include/libzt.h */ @@ -42,22 +41,19 @@ for applications to use. See also: include/libzt.h */ #include #include -// stack #if defined(STACK_PICO) - #include "pico_stack.h" +#include "pico_stack.h" #endif #if defined(STACK_LWIP) - #include "lwIP.hpp" +#include "lwIP.hpp" #endif -// ZT #include "OneService.hpp" #include "Utils.hpp" #include "OSUtils.hpp" #include "InetAddress.hpp" #include "ZeroTierOne.h" -// SDK #include "SocketTap.hpp" #include "libzt.h" @@ -68,12 +64,9 @@ extern "C" { static ZeroTier::OneService *zt1Service; namespace ZeroTier { - std::string homeDir; // The resultant platform-specific dir we *must* use internally + std::string homeDir; // Platform-specific dir we *must* use internally std::string netDir; // Where network .conf files are to be written - /* - * Global reference to stack - */ #if defined(STACK_PICO) picoTCP *picostack = NULL; #endif @@ -336,16 +329,6 @@ void zts_disable_http_control_plane() /* bind() call. This enables multi-network support */ /****************************************************************************/ -/* - - socket fd = 0 (nwid=X)---\ /--- SocketTap=A // e.x. 172.27.0.0 / 16 - \ / - socket fd = 1 (nwid=Y)--------Multiplexed z* calls-------- SocketTap=B // e.x. 192.168.0.1 / 16 - / \ - socket fd = 2 (nwid=Z)---/ \--- SocketTap=C // e.x. 10.9.9.0 / 24 - -*/ - /* Darwin: @@ -378,6 +361,13 @@ int zts_socket(ZT_SOCKET_SIG) { errno = EPROTONOSUPPORT; // seemingly closest match return -1; } + if(socket_type == SOCK_RAW) + { + // TODO: + // - create Connection object, handle as you please + // - we will need to hook this up to SocketTap::put and SocketTap::_handler at some point + return -1; + } ZeroTier::_multiplexer_lock.lock(); #if defined(STACK_PICO) diff --git a/test/layer2.cpp b/test/layer2.cpp new file mode 100644 index 0000000..912d78f --- /dev/null +++ b/test/layer2.cpp @@ -0,0 +1,33 @@ +// This file is built with libzt.a via `make tests` + +#include +#include +#include +#include +#include + +#include "libzt.h" + +int main(int argc , char *argv[]) +{ + if(argc < 3) { + fprintf(stderr, "usage: layer2 \n"); + return 1; + } + + printf("Starting libzt...\n"); + zts_simple_start(argv[1], argv[2]); + char device_id[11]; + zts_get_device_id(device_id); + printf("Complete. I am %s\n", device_id); + + // layer2 example code + int rawsock; + if((rawsock = zts_socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) + { + printf("There was a problem creating the raw socket\n"); + return -1; + } + printf("Created raw socket (%d)\n", rawsock); + return 0; +} \ No newline at end of file