Added layer2 SOCK_RAW example code and stubs

This commit is contained in:
Joseph Henry
2017-07-26 02:13:13 -07:00
parent b105ddb060
commit 409c2dc9de
3 changed files with 57 additions and 37 deletions

View File

@@ -24,7 +24,6 @@
* of your own application. * of your own application.
*/ */
// picoTCP
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <sys/poll.h> #include <sys/poll.h>
@@ -32,19 +31,16 @@
#include <utility> #include <utility>
#include <string> #include <string>
// SDK
#include "SocketTap.hpp" #include "SocketTap.hpp"
#include "libzt.h" #include "libzt.h"
// stack drivers
#if defined(STACK_PICO) #if defined(STACK_PICO)
#include "picoTCP.hpp" #include "picoTCP.hpp"
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
#include "lwIP.hpp" #include "lwIP.hpp"
#endif #endif
// ZT
#include "Utils.hpp" #include "Utils.hpp"
#include "OSUtils.hpp" #include "OSUtils.hpp"
#include "Constants.hpp" #include "Constants.hpp"
@@ -84,8 +80,6 @@ namespace ZeroTier {
SocketTap::~SocketTap() SocketTap::~SocketTap()
{ {
// TODO: Verify that stack is fully stopped before
// deleting Connection objects
_run = false; _run = false;
_phy.whack(); _phy.whack();
Thread::join(_thread); Thread::join(_thread);
@@ -108,16 +102,12 @@ namespace ZeroTier {
#if defined(STACK_PICO) #if defined(STACK_PICO)
if(picostack){ if(picostack){
picostack->pico_init_interface(this, ip); picostack->pico_init_interface(this, ip);
_ips.push_back(ip);
std::sort(_ips.begin(),_ips.end());
return true; return true;
} }
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
if(lwipstack){ if(lwipstack){
lwipstack->lwip_init_interface(this, ip); lwipstack->lwip_init_interface(this, ip);
_ips.push_back(ip);
std::sort(_ips.begin(),_ips.end());
return true; return true;
} }
#endif #endif
@@ -126,10 +116,18 @@ namespace ZeroTier {
bool SocketTap::addIp(const InetAddress &ip) 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)) if(registerIpWithStack(ip))
{ {
// only start the stack if we successfully registered and initialized a device to // only start the stack if we successfully registered and initialized a device to
// the given address // the given address
_ips.push_back(ip);
std::sort(_ips.begin(),_ips.end());
return true; return true;
} }
return false; return false;
@@ -137,7 +135,6 @@ namespace ZeroTier {
bool SocketTap::removeIp(const InetAddress &ip) bool SocketTap::removeIp(const InetAddress &ip)
{ {
DEBUG_INFO();
Mutex::Lock _l(_ips_m); Mutex::Lock _l(_ips_m);
std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip)); std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
if (i == _ips.end()) if (i == _ips.end())
@@ -176,8 +173,9 @@ namespace ZeroTier {
return _dev; return _dev;
} }
void SocketTap::setFriendlyName(const char *friendlyName) { void SocketTap::setFriendlyName(const char *friendlyName)
DEBUG_INFO(); {
// Someday
} }
void SocketTap::scanMulticastGroups(std::vector<MulticastGroup> &added, void SocketTap::scanMulticastGroups(std::vector<MulticastGroup> &added,
@@ -235,7 +233,7 @@ namespace ZeroTier {
void SocketTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len) 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; Connection *conn = (Connection*)*uptr;
if(!conn) if(!conn)
return; return;
@@ -248,7 +246,6 @@ namespace ZeroTier {
void SocketTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool stack_invoked) void SocketTap::phyOnUnixWritable(PhySocket *sock,void **uptr,bool stack_invoked)
{ {
DEBUG_INFO();
if(sock) if(sock)
Read(sock,uptr,stack_invoked); Read(sock,uptr,stack_invoked);
} }

View File

@@ -24,7 +24,6 @@
* of your own application. * of your own application.
*/ */
/* This file implements the libzt library API, it talks to the network /* This file implements the libzt library API, it talks to the network
stack driver and core ZeroTier service to create a socket-like interface stack driver and core ZeroTier service to create a socket-like interface
for applications to use. See also: include/libzt.h */ for applications to use. See also: include/libzt.h */
@@ -42,22 +41,19 @@ for applications to use. See also: include/libzt.h */
#include <pthread.h> #include <pthread.h>
#include <poll.h> #include <poll.h>
// stack
#if defined(STACK_PICO) #if defined(STACK_PICO)
#include "pico_stack.h" #include "pico_stack.h"
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
#include "lwIP.hpp" #include "lwIP.hpp"
#endif #endif
// ZT
#include "OneService.hpp" #include "OneService.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "OSUtils.hpp" #include "OSUtils.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "ZeroTierOne.h" #include "ZeroTierOne.h"
// SDK
#include "SocketTap.hpp" #include "SocketTap.hpp"
#include "libzt.h" #include "libzt.h"
@@ -68,12 +64,9 @@ extern "C" {
static ZeroTier::OneService *zt1Service; static ZeroTier::OneService *zt1Service;
namespace ZeroTier { 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 std::string netDir; // Where network .conf files are to be written
/*
* Global reference to stack
*/
#if defined(STACK_PICO) #if defined(STACK_PICO)
picoTCP *picostack = NULL; picoTCP *picostack = NULL;
#endif #endif
@@ -336,16 +329,6 @@ void zts_disable_http_control_plane()
/* bind() call. This enables multi-network support */ /* 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: Darwin:
@@ -378,6 +361,13 @@ int zts_socket(ZT_SOCKET_SIG) {
errno = EPROTONOSUPPORT; // seemingly closest match errno = EPROTONOSUPPORT; // seemingly closest match
return -1; 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(); ZeroTier::_multiplexer_lock.lock();
#if defined(STACK_PICO) #if defined(STACK_PICO)

33
test/layer2.cpp Normal file
View File

@@ -0,0 +1,33 @@
// This file is built with libzt.a via `make tests`
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "libzt.h"
int main(int argc , char *argv[])
{
if(argc < 3) {
fprintf(stderr, "usage: layer2 <zt_home_dir> <nwid>\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;
}