diff --git a/include/Debug.hpp b/include/Debug.hpp
index f3a43df..2940811 100644
--- a/include/Debug.hpp
+++ b/include/Debug.hpp
@@ -33,7 +33,7 @@
#define ZT_MSG_INFO true // Information which is generally useful to any developer
#define ZT_MSG_TEST true // For use in selftest
#define ZT_MSG_TRANSFER true // RX/TX specific statements
-#define ZT_MSG_EXTRA true // If nothing in your world makes sense
+#define ZT_MSG_EXTRA false // If nothing in your world makes sense
#define ZT_COLOR true
diff --git a/src/ExampleStackDriver.cpp b/src/ExampleStackDriver.cpp
new file mode 100644
index 0000000..a8341c2
--- /dev/null
+++ b/src/ExampleStackDriver.cpp
@@ -0,0 +1,37 @@
+/*
+ * ZeroTier SDK - Network Virtualization Everywhere
+ * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
+ *
+ * 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 .
+ *
+ * --
+ *
+ * You can be released from the requirements of the license by purchasing
+ * a commercial license. Buying such a license is mandatory as soon as you
+ * develop commercial closed-source software that incorporates or links
+ * directly against ZeroTier software without disclosing the source code
+ * of your own application.
+ */
+
+
+/*
+
+This file is only meant for those who wish to replace the currently supported network stacks (lwIP and picoTCP) with
+one of their own choosing. If you're looking for information on how to integrate libzt with your application, you
+should instead go view the (examples) folder. Now let us get started.
+
+ TODO
+
+
+*/
\ No newline at end of file
diff --git a/src/VirtualSocket.hpp b/src/VirtualSocket.hpp
index 294e2cf..60e3760 100644
--- a/src/VirtualSocket.hpp
+++ b/src/VirtualSocket.hpp
@@ -45,12 +45,46 @@
#include "VirtualTap.hpp"
#include "RingBuffer.hpp"
-#define VS_OK 100
-#define VS_SHOULD_STOP 101
-#define VS_STOPPED 102
-#define VS_STATE_UNHANDLED_CONNECTED 103
-#define VS_STATE_CONNECTED 104
-#define VS_STATE_LISTENING 105
+#define VS_STATE_INACTIVE 0x000000u // Default value for newly created VirtualSocket
+#define VS_STATE_ACTIVE 0x000001u // VirtualSocket is RX'ing or TX'ing without issue
+#define VS_STATE_SHOULD_SHUTDOWN 0x000002u // Application, stack driver, or stack marked this VirtualSocket for death
+#define VS_STATE_SHUTDOWN 0x000004u // VirtualSocket and underlying protocol control structures will not RX/TX
+#define VS_STATE_CLOSED 0x000008u // VirtualSocket and underlying protocol control structures are closed
+#define VS_STATE_UNHANDLED_CONNECTED 0x000010u // stack callback has received a connection but we haven't dealt with it
+#define VS_STATE_CONNECTED 0x000020u // stack driver has akwnowledged new connection
+#define VS_STATE_LISTENING 0x000040u // virtual socket is listening for incoming connections
+
+#define VS_OPT_TCP_NODELAY 0x000000u // Nagle's algorithm
+#define VS_OPT_SO_LINGER 0x000001u // VirtualSocket waits for data transmission before closure
+/*
+#define VS_RESERVED 0x000002u //
+#define VS_RESERVED 0x000004u //
+#define VS_RESERVED 0x000008u //
+#define VS_RESERVED 0x000010u //
+#define VS_RESERVED 0x000020u //
+#define VS_RESERVED 0x000040u //
+*/
+#define VS_OPT_FD_NONBLOCKING 0x000080u // Whether the VirtualSocket exhibits non-blocking behaviour
+/*
+#define VS_RESERVED 0x000100u //
+#define VS_RESERVED 0x000200u //
+#define VS_RESERVED 0x000400u //
+#define VS_RESERVED 0x000800u //
+#define VS_RESERVED 0x001000u //
+#define VS_RESERVED 0x002000u //
+#define VS_RESERVED 0x004000u //
+#define VS_RESERVED 0x008000u //
+#define VS_RESERVED 0x010000u //
+#define VS_RESERVED 0x020000u //
+#define VS_RESERVED 0x040000u //
+#define VS_RESERVED 0x080000u //
+#define VS_RESERVED 0x100000u //
+#define VS_RESERVED 0x200000u //
+#define VS_RESERVED 0x400000u //
+#define VS_RESERVED 0x800000u //
+*/
+
+#define vs_is_nonblocking(vs) (((vs)->optflags & VS_OPT_FD_NONBLOCKING) != 0)
namespace ZeroTier {
@@ -58,20 +92,34 @@ namespace ZeroTier {
/**
* An abstraction of a socket that operates between the application-exposed platform-sockets
- * and the network stack's representation of a protocol control block. This object is used by
+ * and the network stack's representation of a protocol control structure. This object is used by
* the POSIX socket emulation layer and stack drivers.
*/
class VirtualSocket
{
private:
- int _state = VS_OK;
+ int _state = VS_STATE_INACTIVE;
public:
RingBuffer *TXbuf;
RingBuffer *RXbuf;
Mutex _tx_m, _rx_m, _op_m;
PhySocket *sock = NULL;
- // State control
+ /**
+ * Sets the VirtualSocket's state value
+ */
+ void apply_state(int state) {
+ // states may be set by application or by stack callbacks, thus this must be guarded
+ _op_m.lock();
+ _state &= state;
+#if defined (STACK_PICO)
+ DEBUG_EXTRA("APPLY STATE=%d (state=%d, vs=%p, ps=%p)", _state, state, this, picosock);
+#endif
+#if defined (STACK_LWIP)
+ DEBUG_EXTRA("APPLY STATE=%d (state=%d, vs=%p, pcb=%p)", _state, state, this, pcb);
+#endif
+ _op_m.unlock();
+ }
/**
* Sets the VirtualSocket's state value
*/
@@ -79,20 +127,32 @@ namespace ZeroTier {
// states may be set by application or by stack callbacks, thus this must be guarded
_op_m.lock();
_state = state;
- //DEBUG_EXTRA("SET STATE = %d (vs=%p)", _state, this);
+#if defined (STACK_PICO)
+ DEBUG_EXTRA("SET STATE=%d (state=%d, vs=%p, ps=%p)", _state, state, this, picosock);
+#endif
+#if defined (STACK_LWIP)
+ DEBUG_EXTRA("SET STATE=%d (state=%d, vs=%p, pcb=%p)", _state, state, this, pcb);
+#endif
_op_m.unlock();
}
/**
* Gets the VirtualSocket's state value
*/
int get_state() {
- //DEBUG_EXTRA("GET STATE = %d (vs=%p)", _state, this);
+#if defined (STACK_PICO)
+ DEBUG_EXTRA("GET STATE=%d (vs=%p, ps=%p)", _state, this, picosock);
+#endif
+#if defined (STACK_LWIP)
+ DEBUG_EXTRA("GET STATE=%d (vs=%p, pcb=%p)", _state, this, pcb);
+#endif
return _state;
}
#if defined(STACK_PICO)
struct pico_socket *picosock = NULL;
#endif
#if defined(STACK_LWIP)
+ int32_t optflags = 0;
+ int linger;
void *pcb = NULL; // Protocol Control Block
/*
- TCP_WRITE_FLAG_COPY: indicates whether the new memory should be allocated
@@ -119,10 +179,9 @@ namespace ZeroTier {
std::queue _AcceptedConnections;
VirtualTap *tap = NULL;
- std::time_t closure_ts = 0;
VirtualSocket() {
-
+ DEBUG_EXTRA("this=%p",this);
memset(&local_addr, 0, sizeof(sockaddr_storage));
memset(&peer_addr, 0, sizeof(sockaddr_storage));
@@ -131,7 +190,6 @@ namespace ZeroTier {
RXbuf = new RingBuffer(ZT_TCP_RX_BUF_SZ);
// socketpair, I/O channel between app and stack drivers
- closure_ts = -1;
ZT_PHY_SOCKFD_TYPE fdpair[2];
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) {
if (errno < 0) {
@@ -149,11 +207,19 @@ namespace ZeroTier {
}
}
~VirtualSocket() {
+ DEBUG_EXTRA("this=%p",this);
close(app_fd);
close(sdk_fd);
delete TXbuf;
delete RXbuf;
TXbuf = RXbuf = NULL;
+#if defined(STACK_PICO)
+ picosock->priv = NULL;
+ picosock = NULL;
+#endif
+#if defined(STACK_LWIP)
+ pcb = NULL;
+#endif
}
};
diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp
index a7690b0..0423cf9 100644
--- a/src/VirtualTap.cpp
+++ b/src/VirtualTap.cpp
@@ -296,18 +296,32 @@ namespace ZeroTier {
void VirtualTap::phyOnUnixClose(PhySocket *sock, void **uptr)
{
+ DEBUG_EXTRA();
+ /*
+ int err = 0;
if (sock) {
VirtualSocket *vs = (VirtualSocket*)uptr;
if (vs) {
- Close(vs);
+ if (vs->get_state() != VS_STATE_CLOSED && vs->get_state() != VS_STATE_LISTENING) {
+ DEBUG_EXTRA("vs=%p, vs->get_state()=%d, vs->picosock->state=%d", vs, vs->get_state(), vs->picosock->state);
+ // doesn't make sense to shut down a listening socket, just close it
+ if ((err = vs->tap->Shutdown(vs, SHUT_RDWR)) < 0) {
+ DEBUG_ERROR("error while shutting down socket");
+ handle_general_failure();
+ return;
+ }
+ }
+ //picostack->pico_Close(vs);
}
} else {
handle_general_failure();
}
+ */
}
void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
{
+ DEBUG_EXTRA();
VirtualSocket *vs = (VirtualSocket*)*uptr;
if (vs == NULL) {
handle_general_failure();
@@ -588,6 +602,7 @@ namespace ZeroTier {
// Write data from app socket to the virtual wire, either raw over VL2, or via network stack
int VirtualTap::Write(VirtualSocket *vs, void *data, ssize_t len)
{
+ DEBUG_EXTRA("vs=%p, fd=%d, data=%p, len=%d", vs, vs->app_fd, data, len);
int err = -1;
#if defined(NO_STACK)
#endif
@@ -668,14 +683,29 @@ namespace ZeroTier {
handle_general_failure();
return -1;
}
+ if (vs->sock) {
+ DEBUG_EXTRA("calling _phy.close()");
+ _phy.close(vs->sock, true);
+ }
removeVirtualSocket(vs);
#if defined(STACK_PICO)
- if (picostack) {
- err = picostack->pico_Close(vs);
- } else {
- handle_general_failure();
- return -1;
- }
+ if (vs->get_state() != VS_STATE_CLOSED && vs->get_state() != VS_STATE_LISTENING) {
+ DEBUG_EXTRA("vs=%p, vs->get_state()=%d, vs->picosock->state=%d", vs, vs->get_state(), vs->picosock->state);
+ // doesn't make sense to shut down a listening socket, just close it
+ if ((err = vs->tap->Shutdown(vs, SHUT_RDWR)) < 0) {
+ DEBUG_ERROR("error while shutting down socket");
+ handle_general_failure();
+ return - 1;
+ }
+ }
+ picostack->pico_Close(vs);
+ removeVirtualSocket(vs);
+ if (vs->socket_type == SOCK_STREAM) {
+ while (!(vs->picosock->state & PICO_SOCKET_STATE_CLOSED)) {
+ nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL);
+ DEBUG_EXTRA("virtual lingering on socket, ps=%p, buf remaining=%d",vs->picosock, vs->TXbuf->count());
+ }
+ }
#endif
#if defined(STACK_LWIP)
if (lwipstack) {
@@ -685,9 +715,6 @@ namespace ZeroTier {
return -1;
}
#endif
- if (vs->sock) {
- _phy.close(vs->sock, false);
- }
return err;
}
diff --git a/src/libzt.cpp b/src/libzt.cpp
index 9a3092e..d2e9928 100644
--- a/src/libzt.cpp
+++ b/src/libzt.cpp
@@ -74,41 +74,53 @@ for applications to use. See also: include/libzt.h */
#include "VirtualTap.hpp"
#include "libzt.h"
+
+
#ifdef __cplusplus
extern "C" {
#endif
namespace ZeroTier {
- static ZeroTier::OneService *zt1Service;
+ /**
+ * Reference to core ZeroTier One service
+ */
+ static ZeroTier::OneService *zt1Service;
- std::string homeDir; // Platform-specific dir we *must* use internally
- std::string netDir; // Where network .conf files are to be written
+ std::string homeDir; // Platform-specific dir we *must* use internally
+ std::string netDir; // Where network .conf files are to be written
#if defined(STACK_PICO)
- picoTCP *picostack = NULL;
+ /**
+ * Reference to picoTCP network stack
+ */
+ picoTCP *picostack = NULL;
#endif
#if defined(STACK_LWIP)
- lwIP *lwipstack = NULL;
+ /**
+ * Reference to lwIP network stack
+ */
+ lwIP *lwipstack = NULL;
#endif
- /*
- * VirtualSockets that have been created but not bound to a VirtualTap interface yet
- */
- std::map unmap;
+ /**
+ * Set of VirtualSocket objects that have been created but not bound to a VirtualTap interface object yet.
+ */
+ std::map unmap;
- /*
- * For fast lookup of VirtualSockets and VirtualTaps via given file descriptor
- */
- std::map*> fdmap;
+ /**
+ * Map of VirtualSocket objects to their respective VirtualTap objects. These have undergone bind(), or
+ * connected by use of a route associated with a specific VirtualTap.
+ */
+ std::map*> fdmap;
- /*
- * Virtual tap interfaces, one per virtual network
- */
- std::vector vtaps;
+ /**
+ * Set of all VirtualTap interface objects that exist. One per virtual network.
+ */
+ std::vector vtaps;
- ZeroTier::Mutex _vtaps_lock;
- ZeroTier::Mutex _multiplexer_lock;
+ ZeroTier::Mutex _vtaps_lock;
+ ZeroTier::Mutex _multiplexer_lock;
}
/****************************************************************************/
@@ -117,257 +129,257 @@ namespace ZeroTier {
void zts_start(const char *path)
{
- if (ZeroTier::zt1Service) {
- return;
- }
+ if (ZeroTier::zt1Service) {
+ return;
+ }
#if defined(STACK_PICO)
- if (ZeroTier::picostack) {
- return;
- }
- ZeroTier::picostack = new ZeroTier::picoTCP();
- pico_stack_init();
+ if (ZeroTier::picostack) {
+ return;
+ }
+ ZeroTier::picostack = new ZeroTier::picoTCP();
+ pico_stack_init();
#endif
#if defined(STACK_LWIP)
- ZeroTier::lwipstack = new ZeroTier::lwIP();
- lwip_init();
+ ZeroTier::lwipstack = new ZeroTier::lwIP();
+ lwip_init();
#endif
- if (path) {
- ZeroTier::homeDir = path;
- }
- pthread_t service_thread;
- pthread_create(&service_thread, NULL, zts_start_service, NULL);
+ if (path) {
+ ZeroTier::homeDir = path;
+ }
+ pthread_t service_thread;
+ pthread_create(&service_thread, NULL, zts_start_service, NULL);
}
void zts_simple_start(const char *path, const char *nwid)
{
- zts_start(path);
- while (zts_running() == false) {
- nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
- }
- while (true) {
- try {
- zts_join(nwid);
- break;
- }
- catch( ... ) {
- DEBUG_ERROR("there was a problem joining the virtual network");
- handle_general_failure();
- }
- }
- while (zts_has_address(nwid) == false) {
- nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
- }
+ zts_start(path);
+ while (zts_running() == false) {
+ nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
+ }
+ while (true) {
+ try {
+ zts_join(nwid);
+ break;
+ }
+ catch( ... ) {
+ DEBUG_ERROR("there was a problem joining the virtual network");
+ handle_general_failure();
+ }
+ }
+ while (zts_has_address(nwid) == false) {
+ nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
+ }
}
void zts_stop() {
- if (ZeroTier::zt1Service) {
- ZeroTier::zt1Service->terminate();
- disableTaps();
- }
+ if (ZeroTier::zt1Service) {
+ ZeroTier::zt1Service->terminate();
+ disableTaps();
+ }
}
void zts_join(const char * nwid) {
- if (ZeroTier::zt1Service) {
- std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
- if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) {
- DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str());
- handle_general_failure();
- }
- if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
- DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
- handle_general_failure();
- }
- ZeroTier::zt1Service->join(nwid);
- }
- // provide ZTO service reference to virtual taps
- // TODO: This might prove to be unreliable, but it works for now
- for (int i=0;izt1ServiceRef=(void*)ZeroTier::zt1Service;
- }
+ if (ZeroTier::zt1Service) {
+ std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
+ if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) {
+ DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str());
+ handle_general_failure();
+ }
+ if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
+ DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
+ handle_general_failure();
+ }
+ ZeroTier::zt1Service->join(nwid);
+ }
+ // provide ZTO service reference to virtual taps
+ // TODO: This might prove to be unreliable, but it works for now
+ for (int i=0;izt1ServiceRef=(void*)ZeroTier::zt1Service;
+ }
}
void zts_join_soft(const char * filepath, const char * nwid) {
- std::string net_dir = std::string(filepath) + "/networks.d/";
- std::string confFile = net_dir + std::string(nwid) + ".conf";
- if (ZeroTier::OSUtils::mkdir(net_dir) == false) {
- DEBUG_ERROR("unable to create: %s", net_dir.c_str());
- handle_general_failure();
- }
- if (ZeroTier::OSUtils::fileExists(confFile.c_str(), false) == false) {
- if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
- DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
- handle_general_failure();
- }
- }
+ std::string net_dir = std::string(filepath) + "/networks.d/";
+ std::string confFile = net_dir + std::string(nwid) + ".conf";
+ if (ZeroTier::OSUtils::mkdir(net_dir) == false) {
+ DEBUG_ERROR("unable to create: %s", net_dir.c_str());
+ handle_general_failure();
+ }
+ if (ZeroTier::OSUtils::fileExists(confFile.c_str(), false) == false) {
+ if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
+ DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
+ handle_general_failure();
+ }
+ }
}
void zts_leave(const char * nwid) {
- if (ZeroTier::zt1Service) {
- ZeroTier::zt1Service->leave(nwid);
- }
+ if (ZeroTier::zt1Service) {
+ ZeroTier::zt1Service->leave(nwid);
+ }
}
void zts_leave_soft(const char * filepath, const char * nwid) {
- std::string net_dir = std::string(filepath) + "/networks.d/";
- ZeroTier::OSUtils::rm((net_dir + nwid + ".conf").c_str());
+ std::string net_dir = std::string(filepath) + "/networks.d/";
+ ZeroTier::OSUtils::rm((net_dir + nwid + ".conf").c_str());
}
void zts_get_homepath(char *homePath, int len) {
- if (ZeroTier::homeDir.length()) {
- memset(homePath, 0, len);
- int buf_len = len < ZeroTier::homeDir.length() ? len : ZeroTier::homeDir.length();
- memcpy(homePath, ZeroTier::homeDir.c_str(), buf_len);
- }
+ if (ZeroTier::homeDir.length()) {
+ memset(homePath, 0, len);
+ int buf_len = len < ZeroTier::homeDir.length() ? len : ZeroTier::homeDir.length();
+ memcpy(homePath, ZeroTier::homeDir.c_str(), buf_len);
+ }
}
void zts_core_version(char *ver) {
- int major, minor, revision;
- ZT_version(&major, &minor, &revision);
- sprintf(ver, "%d.%d.%d", major, minor, revision);
+ int major, minor, revision;
+ ZT_version(&major, &minor, &revision);
+ sprintf(ver, "%d.%d.%d", major, minor, revision);
}
void zts_lib_version(char *ver) {
- sprintf(ver, "%d.%d.%d", ZT_LIB_VERSION_MAJOR, ZT_LIB_VERSION_MINOR, ZT_LIB_VERSION_REVISION);
+ sprintf(ver, "%d.%d.%d", ZT_LIB_VERSION_MAJOR, ZT_LIB_VERSION_MINOR, ZT_LIB_VERSION_REVISION);
}
int zts_get_device_id(char *devID) {
- if (ZeroTier::zt1Service) {
- char id[ZT_ID_LEN];
- sprintf(id, "%lx",ZeroTier::zt1Service->getNode()->address());
- memcpy(devID, id, ZT_ID_LEN);
- return 0;
- }
- else // Service isn't online, try to read ID from file
- {
- std::string fname("identity.public");
- std::string fpath(ZeroTier::homeDir);
- if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
- std::string oldid;
- ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
- memcpy(devID, oldid.c_str(), ZT_ID_LEN); // first 10 bytes of file
- return 0;
- }
- }
- return -1;
+ if (ZeroTier::zt1Service) {
+ char id[ZT_ID_LEN];
+ sprintf(id, "%lx",ZeroTier::zt1Service->getNode()->address());
+ memcpy(devID, id, ZT_ID_LEN);
+ return 0;
+ }
+ else // Service isn't online, try to read ID from file
+ {
+ std::string fname("identity.public");
+ std::string fpath(ZeroTier::homeDir);
+ if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
+ std::string oldid;
+ ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
+ memcpy(devID, oldid.c_str(), ZT_ID_LEN); // first 10 bytes of file
+ return 0;
+ }
+ }
+ return -1;
}
int zts_running() {
- return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
+ return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
}
int zts_has_ipv4_address(const char *nwid)
{
- char ipv4_addr[INET_ADDRSTRLEN];
- memset(ipv4_addr, 0, INET_ADDRSTRLEN);
- zts_get_ipv4_address(nwid, ipv4_addr, INET_ADDRSTRLEN);
- return strcmp(ipv4_addr, "\0");
+ char ipv4_addr[INET_ADDRSTRLEN];
+ memset(ipv4_addr, 0, INET_ADDRSTRLEN);
+ zts_get_ipv4_address(nwid, ipv4_addr, INET_ADDRSTRLEN);
+ return strcmp(ipv4_addr, "\0");
}
int zts_has_ipv6_address(const char *nwid)
{
- char ipv6_addr[INET6_ADDRSTRLEN];
- memset(ipv6_addr, 0, INET6_ADDRSTRLEN);
- zts_get_ipv6_address(nwid, ipv6_addr, INET6_ADDRSTRLEN);
- return strcmp(ipv6_addr, "\0");
+ char ipv6_addr[INET6_ADDRSTRLEN];
+ memset(ipv6_addr, 0, INET6_ADDRSTRLEN);
+ zts_get_ipv6_address(nwid, ipv6_addr, INET6_ADDRSTRLEN);
+ return strcmp(ipv6_addr, "\0");
}
int zts_has_address(const char *nwid)
{
- return zts_has_ipv4_address(nwid) || zts_has_ipv6_address(nwid);
+ return zts_has_ipv4_address(nwid) || zts_has_ipv6_address(nwid);
}
void zts_get_ipv4_address(const char *nwid, char *addrstr, const int addrlen)
{
- if (ZeroTier::zt1Service) {
- uint64_t nwid_int = strtoull(nwid, NULL, 16);
- ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
- if (tap && tap->_ips.size()) {
- for (int i=0; i_ips.size(); i++) {
- if (tap->_ips[i].isV4()) {
- char ipbuf[INET_ADDRSTRLEN];
- std::string addr = tap->_ips[i].toString(ipbuf);
- int len = addrlen < addr.length() ? addrlen : addr.length();
- memset(addrstr, 0, len);
- memcpy(addrstr, addr.c_str(), len);
- return;
- }
- }
- }
- }
- else
- memcpy(addrstr, "\0", 1);
+ if (ZeroTier::zt1Service) {
+ uint64_t nwid_int = strtoull(nwid, NULL, 16);
+ ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
+ if (tap && tap->_ips.size()) {
+ for (int i=0; i_ips.size(); i++) {
+ if (tap->_ips[i].isV4()) {
+ char ipbuf[INET_ADDRSTRLEN];
+ std::string addr = tap->_ips[i].toString(ipbuf);
+ int len = addrlen < addr.length() ? addrlen : addr.length();
+ memset(addrstr, 0, len);
+ memcpy(addrstr, addr.c_str(), len);
+ return;
+ }
+ }
+ }
+ }
+ else
+ memcpy(addrstr, "\0", 1);
}
void zts_get_ipv6_address(const char *nwid, char *addrstr, const int addrlen)
{
- if (ZeroTier::zt1Service) {
- uint64_t nwid_int = strtoull(nwid, NULL, 16);
- ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
- if (tap && tap->_ips.size()) {
- for (int i=0; i_ips.size(); i++) {
- if (tap->_ips[i].isV6()) {
- char ipbuf[INET6_ADDRSTRLEN];
- std::string addr = tap->_ips[i].toString(ipbuf);
- int len = addrlen < addr.length() ? addrlen : addr.length();
- memset(addrstr, 0, len);
- memcpy(addrstr, addr.c_str(), len);
- return;
- }
- }
- }
- }
- else
- memcpy(addrstr, "\0", 1);
+ if (ZeroTier::zt1Service) {
+ uint64_t nwid_int = strtoull(nwid, NULL, 16);
+ ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
+ if (tap && tap->_ips.size()) {
+ for (int i=0; i_ips.size(); i++) {
+ if (tap->_ips[i].isV6()) {
+ char ipbuf[INET6_ADDRSTRLEN];
+ std::string addr = tap->_ips[i].toString(ipbuf);
+ int len = addrlen < addr.length() ? addrlen : addr.length();
+ memset(addrstr, 0, len);
+ memcpy(addrstr, addr.c_str(), len);
+ return;
+ }
+ }
+ }
+ }
+ else
+ memcpy(addrstr, "\0", 1);
}
void zts_get_6plane_addr(char *addr, const char *nwid, const char *devID)
{
- ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(
- ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
- char ipbuf[INET6_ADDRSTRLEN];
- memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
+ ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(
+ ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
+ char ipbuf[INET6_ADDRSTRLEN];
+ memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
}
void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
{
- ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv6rfc4193(
- ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
- char ipbuf[INET6_ADDRSTRLEN];
- memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
+ ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv6rfc4193(
+ ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
+ char ipbuf[INET6_ADDRSTRLEN];
+ memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
}
unsigned long zts_get_peer_count() {
- if (ZeroTier::zt1Service) {
- return ZeroTier::zt1Service->getNode()->peers()->peerCount;
- }
- else {
- return 0;
- }
+ if (ZeroTier::zt1Service) {
+ return ZeroTier::zt1Service->getNode()->peers()->peerCount;
+ }
+ else {
+ return 0;
+ }
}
int zts_get_peer_address(char *peer, const char *devID) {
- if (ZeroTier::zt1Service) {
- ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers();
- // uint64_t addr;
- for (int i=0; ipeerCount; i++) {
- // ZT_Peer *p = &(pl->peers[i]);
- // DEBUG_INFO("peer[%d] = %lx", i, p->address);
- }
- return pl->peerCount;
- }
- else
- return -1;
+ if (ZeroTier::zt1Service) {
+ ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers();
+ // uint64_t addr;
+ for (int i=0; ipeerCount; i++) {
+ // ZT_Peer *p = &(pl->peers[i]);
+ // DEBUG_INFO("peer[%d] = %lx", i, p->address);
+ }
+ return pl->peerCount;
+ }
+ else
+ return -1;
}
void zts_enable_http_control_plane()
{
- // TODO
+ // TODO
}
void zts_disable_http_control_plane()
{
- // TODO
+ // TODO
}
/****************************************************************************/
@@ -379,1359 +391,1012 @@ void zts_disable_http_control_plane()
/* bind() call. This enables multi-network support */
/****************************************************************************/
-/*
-
-Darwin:
-
- [ ] [EACCES] Permission to create a socket of the specified type and/or protocol is denied.
- [ ] [EAFNOSUPPORT] The specified address family is not supported.
- [--] [EMFILE] The per-process descriptor table is full.
- [NA] [ENFILE] The system file table is full.
- [ ] [ENOBUFS] Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.
- [--] [ENOMEM] Insufficient memory was available to fulfill the request.
- [--] [EPROTONOSUPPORT] The protocol type or the specified protocol is not supported within this domain.
- [ ] [EPROTOTYPE] The socket type is not supported by the protocol.
-*/
-
// int socket_family, int socket_type, int protocol
-int zts_socket(ZT_SOCKET_SIG) {
- DEBUG_EXTRA();
- int err = errno = 0;
- if (socket_family < 0 || socket_type < 0 || protocol < 0) {
- errno = EINVAL;
- return -1;
- }
- if (ZeroTier::zt1Service == NULL) {
- DEBUG_ERROR("cannot create socket, no service running. call zts_start() first.");
- errno = EMFILE; // could also be ENFILE
- return -1;
- }
- if (socket_type == SOCK_SEQPACKET) {
- DEBUG_ERROR("SOCK_SEQPACKET not yet supported.");
- errno = EPROTONOSUPPORT; // seemingly closest match
- return -1;
- }
- if (socket_type == SOCK_RAW) {
- // VirtualSocket is only used to associate a socket with a VirtualTap, it has no other implication
- ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
- vs->socket_family = socket_family;
- vs->socket_type = socket_type;
- vs->protocol = protocol;
- add_unassigned_virt_socket(vs->app_fd, vs);
- return vs->app_fd;
- }
+int zts_socket(int socket_family, int socket_type, int protocol) {
+ DEBUG_EXTRA();
+ int err = errno = 0;
+ if (socket_family < 0 || socket_type < 0 || protocol < 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (ZeroTier::zt1Service == NULL) {
+ DEBUG_ERROR("cannot create socket, no service running. call zts_start() first.");
+ errno = EMFILE; // could also be ENFILE
+ return -1;
+ }
+ if (socket_type == SOCK_SEQPACKET) {
+ DEBUG_ERROR("SOCK_SEQPACKET not yet supported.");
+ errno = EPROTONOSUPPORT; // seemingly closest match
+ return -1;
+ }
+ if (socket_type == SOCK_RAW) {
+ // VirtualSocket is only used to associate a socket with a VirtualTap, it has no other implication
+ ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
+ vs->socket_family = socket_family;
+ vs->socket_type = socket_type;
+ vs->protocol = protocol;
+ add_unassigned_virt_socket(vs->app_fd, vs);
+ return vs->app_fd;
+ }
#if defined(STACK_PICO)
- struct pico_socket *p;
- err = ZeroTier::picostack->pico_Socket(&p, socket_family, socket_type, protocol);
- if (err == false && p) {
- ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
- vs->socket_family = socket_family;
- vs->socket_type = socket_type;
- vs->picosock = p;
- add_unassigned_virt_socket(vs->app_fd, vs);
- err = vs->app_fd; // return one end of the socketpair
- }
- else {
- DEBUG_ERROR("failed to create pico_socket");
- errno = ENOMEM;
- err = -1;
- }
+ struct pico_socket *p;
+ err = ZeroTier::picostack->pico_Socket(&p, socket_family, socket_type, protocol);
+ if (err == false && p) {
+ ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
+ vs->socket_family = socket_family;
+ vs->socket_type = socket_type;
+ vs->picosock = p;
+ add_unassigned_virt_socket(vs->app_fd, vs);
+ err = vs->app_fd; // return one end of the socketpair
+ }
+ else {
+ DEBUG_ERROR("failed to create pico_socket");
+ errno = ENOMEM;
+ err = -1;
+ }
#endif
#if defined(STACK_LWIP)
- // TODO: check for max lwIP timers/sockets
- void *pcb;
- err = ZeroTier::lwipstack->lwip_Socket(&pcb, socket_family, socket_type, protocol);
- if (pcb) {
- ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
- vs->socket_family = socket_family;
- vs->socket_type = socket_type;
- vs->pcb = pcb;
- add_unassigned_virt_socket(vs->app_fd, vs);
- // return one end of the socketpair for the app to use
- err = vs->app_fd;
- }
- else {
- DEBUG_ERROR("failed to create lwip pcb");
- errno = ENOMEM;
- err = -1;
- }
+ // TODO: check for max lwIP timers/sockets
+ void *pcb;
+ err = ZeroTier::lwipstack->lwip_Socket(&pcb, socket_family, socket_type, protocol);
+ if (pcb) {
+ ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
+ vs->socket_family = socket_family;
+ vs->socket_type = socket_type;
+ vs->pcb = pcb;
+ add_unassigned_virt_socket(vs->app_fd, vs);
+ // return one end of the socketpair for the app to use
+ err = vs->app_fd;
+ }
+ else {
+ DEBUG_ERROR("failed to create lwip pcb");
+ errno = ENOMEM;
+ err = -1;
+ }
#endif
- return err;
+
+//#if defined(DEFAULT_VS_LINGER)
+ /*
+ if (socket_type == SOCK_STREAM) {
+ linger lin;
+ unsigned int y=sizeof(lin);
+ lin.l_onoff=1;
+ lin.l_linger=10;
+ int fd = err;
+ if((err = zts_setsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)(&lin), y)) < 0) {
+ DEBUG_ERROR("error while setting default linger time on socket");
+ errno = -1; // TODO
+ handle_general_failure();
+ return -1;
+ }
+ err = fd;
+ }
+ */
+//#endif
+ return err;
}
+int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen) {
+ DEBUG_INFO("fd=%d",fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (ZeroTier::zt1Service == NULL) {
+ DEBUG_ERROR("service not started. call zts_start(path) first");
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid socket, unable to locate VirtualSocket for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ if (addr == NULL) {
+ DEBUG_ERROR("invalid address for fd=%d", fd);
+ errno = EINVAL;
+ return -1;
+ }
+ if (addrlen <= 0) {
+ DEBUG_ERROR("invalid address length for fd=%d", fd);
+ errno = EINVAL;
+ return -1;
+ }
+ // TODO: Handle bad address lengths, right now this call will still
+ // succeed with a complete connect despite a bad address length.
-/*
-
-Darwin:
-
- [ ] [EACCES] The destination address is a broadcast address and the socket option SO_BROADCAST is not set.
- [ ] [EADDRINUSE] The address is already in use.
- [ ] [EADDRNOTAVAIL] The specified address is not available on this machine.
- [ ] [EAFNOSUPPORT] Addresses in the specified address family cannot be used with this socket.
- [ ] [EALREADY] The socket is non-blocking and a previous VirtualSocket attempt has not yet been completed.
- [--] [EBADF] socket is not a valid descriptor.
- [ ] [ECONNREFUSED] The attempt to connect was ignored (because the target is not listening for VirtualSockets) or explicitly rejected.
- [ ] [EFAULT] The address parameter specifies an area outside the process address space.
- [ ] [EHOSTUNREACH] The target host cannot be reached (e.g., down, disconnected).
- [--] [EINPROGRESS] The socket is non-blocking and the VirtualSocket cannot be completed immediately.
- It is possible to select(2) for completion by selecting the socket for writing.
- [NA] [EINTR] Its execution was interrupted by a signal.
- [ ] [EINVAL] An invalid argument was detected (e.g., address_len is not valid for the address family, the specified address family is invalid).
- [ ] [EISCONN] The socket is already connected.
- [ ] [ENETDOWN] The local network interface is not functioning.
- [--] [ENETUNREACH] The network isn't reachable from this host.
- [ ] [ENOBUFS] The system call was unable to allocate a needed memory buffer.
- [ ] [ENOTSOCK] socket is not a file descriptor for a socket.
- [ ] [EOPNOTSUPP] Because socket is listening, no VirtualSocket is allowed.
- [ ] [EPROTOTYPE] address has a different type than the socket that is bound to the specified peer address.
- [ ] [ETIMEDOUT] VirtualSocket establishment timed out without establishing a VirtualSocket.
- [ ] [ECONNRESET] Remote host reset the VirtualSocket request.
-
-Linux:
-
- [ ] [EACCES] For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket file,
- or search permission is denied for one of the directories in the path prefix. (See also path_resolution(7).)
- [ ] [EACCES, EPERM] The user tried to connect to a broadcast address without having the socket broadcast flag enabled or the
- VirtualSocket request failed because of a local firewall rule.
- [ ] [EADDRINUSE] Local address is already in use.
- [ ] [EAFNOSUPPORT] The passed address didn't have the correct address family in its sa_family field.
- [ ] [EAGAIN] No more free local ports or insufficient entries in the routing cache. For AF_INET see the description
- of /proc/sys/net/ipv4/ip_local_port_range ip(7) for information on how to increase the number of local ports.
- [ ] [EALREADY] The socket is nonblocking and a previous VirtualSocket attempt has not yet been completed.
- [ ] [EBADF] The file descriptor is not a valid index in the descriptor table.
- [ ] [ECONNREFUSED] No-one listening on the remote address.
- [ ] [EFAULT] The socket structure address is outside the user's address space.
- [ ] [EINPROGRESS] The socket is nonblocking and the VirtualSocket cannot be completed immediately. It is possible to select(2) or
- poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2)
- to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero)
- or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).
- [ ] [EINTR] The system call was interrupted by a signal that was caught; see signal(7).
- [ ] [EISCONN] The socket is already connected.
- [ ] [ENETUNREACH] Network is unreachable.
- [ ] [ENOTSOCK] The file descriptor is not associated with a socket.
- [ ] [ETIMEDOUT] Timeout while attempting VirtualSocket. The server may be too busy to accept new VirtualSockets. Note that for
- IP sockets the timeout may be very long when syncookies are enabled on the server.
-
-*/
-
-int zts_connect(ZT_CONNECT_SIG) {
- DEBUG_INFO("fd=%d",fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (ZeroTier::zt1Service == NULL) {
- DEBUG_ERROR("service not started. call zts_start(path) first");
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid socket, unable to locate VirtualSocket for fd=%d", fd);
- errno = EBADF;
- return -1;
- }
- if (addr == NULL) {
- DEBUG_ERROR("invalid address for fd=%d", fd);
- errno = EINVAL;
- return -1;
- }
- if (addrlen <= 0) {
- DEBUG_ERROR("invalid address length for fd=%d", fd);
- errno = EINVAL;
- return -1;
- }
- // TODO: Handle bad address lengths, right now this call will still
- // succeed with a complete connect despite a bad address length.
-
- // DEBUG_EXTRA("fd = %d, %s : %d", fd, ipstr, ntohs(port));
- ZeroTier::InetAddress inet;
- sockaddr2inet(vs->socket_family, addr, &inet);
- ZeroTier::VirtualTap *tap = getTapByAddr(&inet);
- if (tap == NULL) {
- DEBUG_ERROR("no route to host, could not find appropriate VirtualTap for fd=%d", fd);
- errno = ENETUNREACH;
- return -1;
- }
+ // DEBUG_EXTRA("fd = %d, %s : %d", fd, ipstr, ntohs(port));
+ ZeroTier::InetAddress inet;
+ sockaddr2inet(vs->socket_family, addr, &inet);
+ ZeroTier::VirtualTap *tap = getTapByAddr(&inet);
+ if (tap == NULL) {
+ DEBUG_ERROR("no route to host, could not find appropriate VirtualTap for fd=%d", fd);
+ errno = ENETUNREACH;
+ return -1;
+ }
#if defined(STACK_PICO)
- // pointer to virtual tap we use in callbacks from the stack
- vs->picosock->priv = new ZeroTier::VirtualBindingPair(tap, vs);
+ // pointer to virtual tap we use in callbacks from the stack
+ vs->picosock->priv = new ZeroTier::VirtualBindingPair(tap, vs);
#endif
#if defined(STACK_LWIP)
#endif
- if ((err = tap->Connect(vs, addr, addrlen)) < 0) {
- DEBUG_ERROR("error while connecting socket");
- // errno will be set by tap->Connect
- return -1;
- }
- // assign this VirtualSocket to the tap we decided on
- tap->_VirtualSockets.push_back(vs);
- vs->tap = tap;
- vs->sock = tap->_phy.wrapSocket(vs->sdk_fd, vs);
+ if ((err = tap->Connect(vs, addr, addrlen)) < 0) {
+ DEBUG_ERROR("error while connecting socket");
+ // errno will be set by tap->Connect
+ return -1;
+ }
+ // assign this VirtualSocket to the tap we decided on
+ tap->_VirtualSockets.push_back(vs);
+ vs->tap = tap;
+ vs->sock = tap->_phy.wrapSocket(vs->sdk_fd, vs);
- // TODO: Consolidate these calls
- del_unassigned_virt_socket(fd);
- add_assigned_virt_socket(tap, vs, fd);
+ // TODO: Consolidate these calls
+ del_unassigned_virt_socket(fd);
+ add_assigned_virt_socket(tap, vs, fd);
- // save peer addr, for calls like getpeername
- memcpy(&(vs->peer_addr), addr, sizeof(vs->peer_addr));
+ // save peer addr, for calls like getpeername
+ memcpy(&(vs->peer_addr), addr, sizeof(vs->peer_addr));
- // Below will simulate BLOCKING/NON-BLOCKING behaviour
+ // Below will simulate BLOCKING/NON-BLOCKING behaviour
- // NOTE: pico_socket_connect() will return 0 if no error happens immediately, but that doesn't indicate
- // the connection was completed, for that we must wait for a callback from the stack. During that
- // callback we will place the VirtualSocket in a VS_STATE_UNHANDLED_CONNECTED state to signal
- // to the multiplexer logic that this connection is complete and a success value can be sent to the
- // user application
+ // NOTE: pico_socket_connect() will return 0 if no error happens immediately, but that doesn't indicate
+ // the connection was completed, for that we must wait for a callback from the stack. During that
+ // callback we will place the VirtualSocket in a VS_STATE_UNHANDLED_CONNECTED state to signal
+ // to the multiplexer logic that this connection is complete and a success value can be sent to the
+ // user application
- int f_err, blocking = 1;
- if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) {
- DEBUG_ERROR("fcntl error, err=%s, errno=%d", f_err, errno);
- // errno will be set by fcntl
- return -1;
- }
- blocking = !(f_err & O_NONBLOCK);
- if (blocking == false) {
- errno = EINPROGRESS; // can't connect immediately
- err = -1;
- }
- if (blocking == true) {
- bool complete = false;
- while (true) {
- // FIXME: locking and unlocking so often might cause significant performance overhead while outgoing VirtualSockets
- // are being established (also applies to accept())
- nanosleep((const struct timespec[]) {{0, (ZT_CONNECT_RECHECK_DELAY * 1000000)}}, NULL);
- tap->_tcpconns_m.lock();
- for (int i=0; i_VirtualSockets.size(); i++) {
+ int f_err, blocking = 1;
+ if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) {
+ DEBUG_ERROR("fcntl error, err=%s, errno=%d", f_err, errno);
+ // errno will be set by fcntl
+ return -1;
+ }
+ blocking = !(f_err & O_NONBLOCK);
+ if (blocking == false) {
+ errno = EINPROGRESS; // can't connect immediately
+ err = -1;
+ }
+ if (blocking == true) {
+ bool complete = false;
+ while (true) {
+ // FIXME: locking and unlocking so often might cause significant performance overhead while outgoing VirtualSockets
+ // are being established (also applies to accept())
+ nanosleep((const struct timespec[]) {{0, (ZT_CONNECT_RECHECK_DELAY * 1000000)}}, NULL);
+ tap->_tcpconns_m.lock();
+ for (int i=0; i_VirtualSockets.size(); i++) {
#if defined(STACK_PICO)
- if (tap->_VirtualSockets[i]->get_state() == PICO_ERR_ECONNRESET) {
- errno = ECONNRESET;
- DEBUG_ERROR("ECONNRESET");
- err = -1;
- }
+ DEBUG_EXTRA("checking tap->_VirtualSockets[i]=%p", tap->_VirtualSockets[i]);
+ if (tap->_VirtualSockets[i]->get_state() == PICO_ERR_ECONNRESET) {
+ errno = ECONNRESET;
+ DEBUG_ERROR("ECONNRESET");
+ err = -1;
+ }
#endif
#if defined(STACK_LWIP)
#endif
- if (tap->_VirtualSockets[i]->get_state() == VS_STATE_UNHANDLED_CONNECTED) {
- tap->_VirtualSockets[i]->set_state(VS_STATE_CONNECTED);
- complete = true;
- }
- }
- tap->_tcpconns_m.unlock();
- if (complete) {
- break;
- }
- }
- }
- return err;
+ if (tap->_VirtualSockets[i]->get_state() == VS_STATE_UNHANDLED_CONNECTED) {
+ tap->_VirtualSockets[i]->set_state(VS_STATE_CONNECTED);
+ complete = true;
+ }
+ }
+ tap->_tcpconns_m.unlock();
+ if (complete) {
+ break;
+ }
+ }
+ }
+ return err;
}
-/*
+int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (ZeroTier::zt1Service == NULL) {
+ DEBUG_ERROR("service not started. call zts_start(path) first");
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("no VirtualSocket for fd=%d", fd);
+ errno = ENOTSOCK;
+ return -1;
+ }
+ // detect local interface binds
+ ZeroTier::VirtualTap *tap = NULL;
+ if (vs->socket_family == AF_INET) {
+ struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
+ if (in4->sin_addr.s_addr == INADDR_ANY) {
+ DEBUG_EXTRA("AF_INET, INADDR_ANY, binding to all interfaces");
+ // grab first vtap
+ if (ZeroTier::vtaps.size()) {
+ tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
+ }
+ }
+ if (in4->sin_addr.s_addr == 0x7f000001) {
+ DEBUG_EXTRA("127.0.0.1, will bind to appropriate vtap when connection is inbound");
+ if (ZeroTier::vtaps.size()) {
+ tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
+ }
+ }
+ }
+ if (vs->socket_family == AF_INET6) {
+ struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
+ if (memcmp((void*)&(in6->sin6_addr), (void*)&(in6addr_any), sizeof(in6addr_any)) == 0) {
+ DEBUG_EXTRA("AF_INET6, in6addr_any, binding to all interfaces");
+ if (ZeroTier::vtaps.size()) {
+ tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
+ }
+ }
+ }
-Darwin:
-
- [--] [EBADF] S is not a valid descriptor.
- [--] [ENOTSOCK] S is not a socket.
- [--] [EADDRNOTAVAIL] The specified address is not available from the local
- machine.
- [ ] [EADDRINUSE] The specified address is already in use.
- [ ] [EINVAL] The socket is already bound to an address.
- [ ] [EACCES] The requested address is protected, and the current
- user has inadequate permission to access it.
- [ ] [EFAULT] The name parameter is not in a valid part of the user
- address space.
-*/
-int zts_bind(ZT_BIND_SIG) {
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (ZeroTier::zt1Service == NULL) {
- DEBUG_ERROR("service not started. call zts_start(path) first");
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("no VirtualSocket for fd=%d", fd);
- errno = ENOTSOCK;
- return -1;
- }
- // detect local interface binds
- ZeroTier::VirtualTap *tap = NULL;
- if (vs->socket_family == AF_INET) {
- struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
- if (in4->sin_addr.s_addr == INADDR_ANY) {
- DEBUG_EXTRA("AF_INET, INADDR_ANY, binding to all interfaces");
- // grab first vtap
- if (ZeroTier::vtaps.size()) {
- tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
- }
- }
- if (in4->sin_addr.s_addr == 0x7f000001) {
- DEBUG_EXTRA("127.0.0.1, will bind to appropriate vtap when connection is inbound");
- if (ZeroTier::vtaps.size()) {
- tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
- }
- }
- }
- if (vs->socket_family == AF_INET6) {
- struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
- if (memcmp((void*)&(in6->sin6_addr), (void*)&(in6addr_any), sizeof(in6addr_any)) == 0) {
- DEBUG_EXTRA("AF_INET6, in6addr_any, binding to all interfaces");
- if (ZeroTier::vtaps.size()) {
- tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap
- }
- }
- }
-
- ZeroTier::InetAddress inet;
- sockaddr2inet(vs->socket_family, addr, &inet);
- if (tap == NULL) {
- tap = getTapByAddr(&inet);
- }
- if (tap == NULL) {
- DEBUG_ERROR("no matching interface to bind to, could not find appropriate VirtualTap for fd=%d", fd);
- errno = ENETUNREACH;
- return -1;
- }
+ ZeroTier::InetAddress inet;
+ sockaddr2inet(vs->socket_family, addr, &inet);
+ if (tap == NULL) {
+ tap = getTapByAddr(&inet);
+ }
+ if (tap == NULL) {
+ DEBUG_ERROR("no matching interface to bind to, could not find appropriate VirtualTap for fd=%d", fd);
+ errno = ENETUNREACH;
+ return -1;
+ }
#if defined(STACK_PICO)
- // used in callbacks from network stack
- vs->picosock->priv = new ZeroTier::VirtualBindingPair(tap, vs);
+ // used in callbacks from network stack
+ vs->picosock->priv = new ZeroTier::VirtualBindingPair(tap, vs);
#endif
- tap->addVirtualSocket(vs);
- err = tap->Bind(vs, addr, addrlen);
- if (err == 0) { // success
- vs->tap = tap;
- del_unassigned_virt_socket(fd);
- add_assigned_virt_socket(tap, vs, fd);
- }
- return err;
+ tap->addVirtualSocket(vs);
+ err = tap->Bind(vs, addr, addrlen);
+ if (err == 0) { // success
+ vs->tap = tap;
+ del_unassigned_virt_socket(fd);
+ add_assigned_virt_socket(tap, vs, fd);
+ }
+ return err;
}
-/*
-
-Darwin:
-
- [--] [EACCES] The current process has insufficient privileges.
- [--] [EBADF] The argument socket is not a valid file descriptor.
- [--] [EDESTADDRREQ] The socket is not bound to a local address and the protocol does not support listening on an unbound socket.
- [ ] [EINVAL] socket is already connected.
- [ ] [ENOTSOCK] The argument socket does not reference a socket.
- [ ] [EOPNOTSUPP] The socket is not of a type that supports the operation listen().
-
-Linux:
-
- [ ] [EADDRINUSE] Another socket is already listening on the same port.
- [--] [EBADF] The argument sockfd is not a valid descriptor.
- [ ] [ENOTSOCK] The argument sockfd is not a socket.
- [ ] [EOPNOTSUPP] The socket is not of a type that supports the listen() operation.
-*/
-int zts_listen(ZT_LISTEN_SIG) {
- DEBUG_EXTRA("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (ZeroTier::zt1Service == NULL) {
- DEBUG_ERROR("service not started. call zts_start(path) first");
- errno = EACCES;
- return -1;
- }
- std::pair *p = get_assigned_virtual_pair(fd);
- ZeroTier::_multiplexer_lock.lock();
- if (p == NULL) {
- DEBUG_ERROR("unable to locate VirtualSocket pair. did you bind?");
- errno = EDESTADDRREQ;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = p->first;
- ZeroTier::VirtualTap *tap = p->second;
- if (tap == NULL || vs == NULL) {
- DEBUG_ERROR("unable to locate tap interface for file descriptor");
- errno = EBADF;
- return -1;
- }
- backlog = backlog > 128 ? 128 : backlog; // See: /proc/sys/net/core/somaxconn
- err = tap->Listen(vs, backlog);
- vs->set_state(VS_STATE_LISTENING);
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+int zts_listen(int fd, int backlog) {
+ DEBUG_EXTRA("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (ZeroTier::zt1Service == NULL) {
+ DEBUG_ERROR("service not started. call zts_start(path) first");
+ errno = EACCES;
+ return -1;
+ }
+ std::pair *p = get_assigned_virtual_pair(fd);
+ ZeroTier::_multiplexer_lock.lock();
+ if (p == NULL) {
+ DEBUG_ERROR("unable to locate VirtualSocket pair. did you bind?");
+ errno = EDESTADDRREQ;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = p->first;
+ ZeroTier::VirtualTap *tap = p->second;
+ if (tap == NULL || vs == NULL) {
+ DEBUG_ERROR("unable to locate tap interface for file descriptor");
+ errno = EBADF;
+ return -1;
+ }
+ backlog = backlog > 128 ? 128 : backlog; // See: /proc/sys/net/core/somaxconn
+ err = tap->Listen(vs, backlog);
+ vs->set_state(VS_STATE_LISTENING);
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
-/*
-
-Darwin:
-
- [--] [EBADF] The descriptor is invalid.
- [ ] [ENOTSOCK] The descriptor references a file, not a socket.
- [ ] [EOPNOTSUPP] The referenced socket is not of type SOCK_STREAM.
- [ ] [EFAULT] The addr parameter is not in a writable part of the
- user address space.
- [--] [EWOULDBLOCK] The socket is marked non-blocking and no VirtualSockets
- are present to be accepted.
- [--] [EMFILE] The per-process descriptor table is full.
- [ ] [ENFILE] The system file table is full.
-*/
-int zts_accept(ZT_ACCEPT_SIG) {
- int err = errno = 0;
- DEBUG_EXTRA("fd=%d", fd);
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (addr && *addrlen <= 0) {
- DEBUG_ERROR("invalid address length given");
- errno = EINVAL; // TODO, not actually a valid error for this function
- return -1;
- }
- if (can_provision_new_socket(SOCK_STREAM) == false) {
- DEBUG_ERROR("cannot provision additional socket due to limitation of network stack");
- errno = EMFILE;
- return -1;
- }
- std::pair *p = get_assigned_virtual_pair(fd);
- if (p == NULL) {
- DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?");
- errno = EBADF;
- err = -1;
- }
- else {
- ZeroTier::VirtualSocket *vs = p->first;
- ZeroTier::VirtualTap *tap = p->second;
- // BLOCKING: loop and keep checking until we find a newly accepted VirtualSocket
- int f_err, blocking = 1;
- if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) {
- DEBUG_ERROR("fcntl error, err = %s, errno = %d", f_err, errno);
- err = -1;
- }
- else {
- blocking = !(f_err & O_NONBLOCK);
- }
- ZeroTier::VirtualSocket *accepted_vs;
- if (err == false) {
- if (blocking == false) { // non-blocking
- DEBUG_EXTRA("EWOULDBLOCK, assuming non-blocking mode");
- errno = EWOULDBLOCK;
- err = -1;
- accepted_vs = tap->Accept(vs);
- }
- else { // blocking
- while (true) {
- nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL);
- accepted_vs = tap->Accept(vs);
- if (accepted_vs)
- break; // accepted fd = err
- }
- }
- if (accepted_vs) {
- add_assigned_virt_socket(tap, accepted_vs, accepted_vs->app_fd);
- err = accepted_vs->app_fd;
- }
- }
- if (err > 0) {
- if (addr && *addrlen) {
- *addrlen = *addrlen < sizeof(accepted_vs->peer_addr) ? *addrlen : sizeof(accepted_vs->peer_addr);
- // copy address into provided address buffer and len buffer
- memcpy(addr, &(accepted_vs->peer_addr), *addrlen);
- }
- }
- }
- return err;
+int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen) {
+ int err = errno = 0;
+ DEBUG_EXTRA("fd=%d", fd);
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (addr && *addrlen <= 0) {
+ DEBUG_ERROR("invalid address length given");
+ errno = EINVAL; // TODO, not actually a valid error for this function
+ return -1;
+ }
+ if (can_provision_new_socket(SOCK_STREAM) == false) {
+ DEBUG_ERROR("cannot provision additional socket due to limitation of network stack");
+ errno = EMFILE;
+ return -1;
+ }
+ std::pair *p = get_assigned_virtual_pair(fd);
+ if (p == NULL) {
+ DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?");
+ errno = EBADF;
+ err = -1;
+ }
+ else {
+ ZeroTier::VirtualSocket *vs = p->first;
+ ZeroTier::VirtualTap *tap = p->second;
+ // BLOCKING: loop and keep checking until we find a newly accepted VirtualSocket
+ int f_err, blocking = 1;
+ if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) {
+ DEBUG_ERROR("fcntl error, err = %s, errno = %d", f_err, errno);
+ err = -1;
+ }
+ else {
+ blocking = !(f_err & O_NONBLOCK);
+ }
+ ZeroTier::VirtualSocket *accepted_vs;
+ if (err == false) {
+ if (blocking == false) { // non-blocking
+ DEBUG_EXTRA("EWOULDBLOCK, assuming non-blocking mode");
+ errno = EWOULDBLOCK;
+ err = -1;
+ accepted_vs = tap->Accept(vs);
+ }
+ else { // blocking
+ while (true) {
+ nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL);
+ accepted_vs = tap->Accept(vs);
+ if (accepted_vs)
+ break; // accepted fd = err
+ }
+ }
+ if (accepted_vs) {
+ add_assigned_virt_socket(tap, accepted_vs, accepted_vs->app_fd);
+ err = accepted_vs->app_fd;
+ }
+ }
+ if (err > 0) {
+ if (addr && *addrlen) {
+ *addrlen = *addrlen < sizeof(accepted_vs->peer_addr) ? *addrlen : sizeof(accepted_vs->peer_addr);
+ // copy address into provided address buffer and len buffer
+ memcpy(addr, &(accepted_vs->peer_addr), *addrlen);
+ }
+ }
+ }
+ return err;
}
-
-/*
-Linux accept() (and accept4()) passes already-pending network errors on the new socket as an error code from accept(). This behavior differs from other BSD socket implementations. For reliable operation the application should detect the network errors defined for the protocol after accept() and treat them like EAGAIN by retrying. In the case of TCP/IP, these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
-Errors
-
- [ ] [EAGAIN or EWOULDBLOCK] The socket is marked nonblocking and no VirtualSockets are present to be accepted. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
- [--] [EBADF] The descriptor is invalid.
- [ ] [ECONNABORTED] A VirtualSocket has been aborted.
- [ ] [EFAULT] The addr argument is not in a writable part of the user address space.
- [NA] [EINTR] The system call was interrupted by a signal that was caught before a valid VirtualSocket arrived; see signal(7).
- [ ] [EINVAL] Socket is not listening for VirtualSockets, or addrlen is invalid (e.g., is negative).
- [ ] [EINVAL] (accept4()) invalid value in flags.
- [ ] [EMFILE] The per-process limit of open file descriptors has been reached.
- [ ] [ENFILE] The system limit on the total number of open files has been reached.
- [ ] [ENOBUFS, ENOMEM] Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
- [ ] [ENOTSOCK] The descriptor references a file, not a socket.
- [ ] [EOPNOTSUPP] The referenced socket is not of type SOCK_STREAM.
- [ ] [EPROTO] Protocol error.
-
-In addition, Linux accept() may fail if:
-
-EPERM Firewall rules forbid VirtualSocket.
-
-
-
- SOCK_NONBLOCK Set the O_NONBLOCK file status flag on the new open
- file description. Using this flag saves extra calls
- to fcntl(2) to achieve the same result.
-
- SOCK_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new
- file descriptor. See the description of the
- O_CLOEXEC flag in open(2) for reasons why this may be
- useful.
-
- int fd, struct sockaddr *addr, socklen_t *addrlen, int flags
-*/
#if defined(__linux__)
-int zts_accept4(ZT_ACCEPT4_SIG)
+int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
- errno = 0;
- //DEBUG_INFO("fd=%d", fd);
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (flags & SOCK_NONBLOCK) {
- fcntl(fd, F_SETFL, O_NONBLOCK);
- }
- if (flags & SOCK_CLOEXEC) {
- fcntl(fd, F_SETFL, FD_CLOEXEC);
- }
- addrlen = !addr ? 0 : addrlen;
- return zts_accept(fd, addr, addrlen);
+ errno = 0;
+ //DEBUG_INFO("fd=%d", fd);
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (flags & SOCK_NONBLOCK) {
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+ }
+ if (flags & SOCK_CLOEXEC) {
+ fcntl(fd, F_SETFL, FD_CLOEXEC);
+ }
+ addrlen = !addr ? 0 : addrlen;
+ return zts_accept(fd, addr, addrlen);
}
#endif
-
-/*
- [--] [EBADF] The argument s is not a valid descriptor.
- [ ] [ENOTSOCK] The argument s is a file, not a socket.
- [--] [ENOPROTOOPT] The option is unknown at the level indicated.
- [ ] [EFAULT] The address pointed to by optval is not in a valid
- part of the process address space. For getsockopt(),
- this error may also be returned if optlen is not in a
- valid part of the process address space.
- [ ] [EDOM] The argument value is out of bounds.
-
- int fd, int level, int optname, const void *optval, socklen_t optlen
-*/
-int zts_setsockopt(ZT_SETSOCKOPT_SIG)
+int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
{
- DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid fd=%d", fd);
- errno = EBADF;
- return -1;
- }
+ DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
#if defined(STACK_PICO)
- if (ZeroTier::picostack) {
- err = ZeroTier::picostack->pico_setsockopt(vs, level, optname, optval, optlen);
- } else {
- handle_general_failure();
- // TODO: errno?
- err = -1;
- }
+ if (ZeroTier::picostack) {
+ err = ZeroTier::picostack->pico_setsockopt(vs, level, optname, optval, optlen);
+ } else {
+ handle_general_failure();
+ // TODO: errno?
+ err = -1;
+ }
#endif
#if defined(STACK_LWIP)
- if (ZeroTier::lwipstack) {
- err = ZeroTier::lwipstack->lwip_setsockopt(vs, level, optname, optval, optlen);
- } else {
- handle_general_failure();
- // TODO: errno?
- err = -1;
- }
+ if (ZeroTier::lwipstack) {
+ err = ZeroTier::lwipstack->lwip_setsockopt(vs, level, optname, optval, optlen);
+ } else {
+ handle_general_failure();
+ // TODO: errno?
+ err = -1;
+ }
#endif
- return err;
+ return err;
}
-/*
- [--] [EBADF] The argument s is not a valid descriptor.
- [ ] [ENOTSOCK] The argument s is a file, not a socket.
- [ ] [ENOPROTOOPT] The option is unknown at the level indicated.
- [ ] [EFAULT] The address pointed to by optval is not in a valid
- part of the process address space. For getsockopt(),
- this error may also be returned if optlen is not in a
- valid part of the process address space.
- [ ] [EDOM] The argument value is out of bounds.
-
-*/
-int zts_getsockopt(ZT_GETSOCKOPT_SIG)
+int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
{
- DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid fd=%d", fd);
- errno = EBADF;
- return -1;
- }
+ DEBUG_EXTRA("fd=%d, level=%d, optname=%d", fd, level, optname);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
#if defined(STACK_PICO)
- if (ZeroTier::picostack) {
- err = ZeroTier::picostack->pico_getsockopt(vs, level, optname, optval, optlen);
- } else {
- handle_general_failure();
- // TODO: errno?
- err = -1;
- }
+ if (ZeroTier::picostack) {
+ err = ZeroTier::picostack->pico_getsockopt(vs, level, optname, optval, optlen);
+ } else {
+ handle_general_failure();
+ // TODO: errno?
+ err = -1;
+ }
#endif
#if defined(STACK_LWIP)
- if (ZeroTier::lwipstack) {
- err = ZeroTier::lwipstack->lwip_getsockopt(vs, level, optname, optval, optlen);
- } else {
- handle_general_failure();
- // TODO: errno?
- err = -1;
- }
+ if (ZeroTier::lwipstack) {
+ err = ZeroTier::lwipstack->lwip_getsockopt(vs, level, optname, optval, optlen);
+ } else {
+ handle_general_failure();
+ // TODO: errno?
+ err = -1;
+ }
#endif
- return err;
+ return err;
}
-/*
- [--] [EBADF] The argument s is not a valid descriptor.
- [ ] [ENOTSOCK] The argument s is a file, not a socket.
- [ ] [ENOBUFS] Insufficient resources were available in the system to
- perform the operation.
- [ ] [EFAULT] The name parameter points to memory not in a valid
- part of the process address space.
-*/
-int zts_getsockname(ZT_GETSOCKNAME_SIG)
+int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
{
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- // TODO
- return err;
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ // TODO
+ return err;
}
-/*
-
-Linux:
-
- [--] [EBADF] The argument s is not a valid descriptor.
- [ ] [ENOTSOCK] The argument s is a file, not a socket.
- [--] [ENOTCONN] The socket is not connected.
- [ ] [ENOBUFS] Insufficient resources were available in the system to
- perform the operation.
- [ ] [EFAULT] The name parameter points to memory not in a valid
- part of the process address space.
-*/
-int zts_getpeername(ZT_GETPEERNAME_SIG)
+int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
{
- DEBUG_INFO("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- errno = ENOTCONN;
- return -1;
- }
- memcpy(addr, &(vs->peer_addr), sizeof(struct sockaddr_storage));
- return err;
+ DEBUG_INFO("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ errno = ENOTCONN;
+ return -1;
+ }
+ memcpy(addr, &(vs->peer_addr), sizeof(struct sockaddr_storage));
+ return err;
}
-/*
-
-Linux:
-
- [ ] [EFAULT] name is an invalid address.
- [ ] [EINVAL] len is negative or, for sethostname(), len is larger than the
- maximum allowed size.
- [ ] [ENAMETOOLONG] (glibc gethostname()) len is smaller than the actual size.
- (Before version 2.1, glibc uses EINVAL for this case.)
- [ ] [EPERM] For sethostname(), the caller did not have the CAP_SYS_ADMIN
- capability in the user namespace associated with its UTS
- namespace (see namespaces(7)).
-*/
-
-int zts_gethostname(ZT_GETHOSTNAME_SIG)
+int zts_gethostname(char *name, size_t len)
{
- errno = 0;
- return gethostname(name, len);
+ errno = 0;
+ return gethostname(name, len);
}
-/*
-
-Linux:
-
- [ ] [EFAULT] name is an invalid address.
- [ ] [EINVAL] len is negative or, for sethostname(), len is larger than the
- maximum allowed size.
- [ ] [ENAMETOOLONG] (glibc gethostname()) len is smaller than the actual size.
- (Before version 2.1, glibc uses EINVAL for this case.)
- [ ] [EPERM] For sethostname(), the caller did not have the CAP_SYS_ADMIN
- capability in the user namespace associated with its UTS
- namespace (see namespaces(7)).
-*/
-
-int zts_sethostname(ZT_SETHOSTNAME_SIG)
+int zts_sethostname(const char *name, size_t len)
{
- errno = 0;
- return sethostname(name, len);
+ errno = 0;
+ return sethostname(name, len);
}
-/*
-
-Linux:
-
- See: http://yarchive.net/comp/linux/close_return_value.html
-
-Linux / Darwin:
-
- [--] [EBADF] fildes is not a valid, active file descriptor.
- [NA] [EINTR] Its execution was interrupted by a signal.
- [ ] [EIO] A previously-uncommitted write(2) encountered an input/output error.
-*/
-
-int zts_close(ZT_CLOSE_SIG)
+int zts_close(int fd)
{
- DEBUG_EXTRA("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (ZeroTier::zt1Service == NULL) {
- DEBUG_ERROR("cannot close socket. service not started. call zts_start(path) first");
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("no vs found for fd=%d", fd);
- handle_general_failure();
- errno = EBADF;
- return -1;
- }
- del_virt_socket(fd);
- if (vs->tap) {
- vs->tap->Close(vs);
- }
- delete vs;
- vs = NULL;
- return err;
+ DEBUG_EXTRA("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (ZeroTier::zt1Service == NULL) {
+ DEBUG_ERROR("cannot close socket. service not started. call zts_start(path) first");
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("no vs found for fd=%d", fd);
+ handle_general_failure();
+ errno = EBADF;
+ return -1;
+ }
+ del_virt_socket(fd);
+ if (vs->tap) {
+ vs->tap->Close(vs);
+ }
+ delete vs;
+ vs = NULL;
+ return err;
}
-int zts_poll(ZT_POLL_SIG)
+int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
- errno = 0;
- return poll(fds, nfds, timeout);
+ errno = 0;
+ return poll(fds, nfds, timeout);
}
-int zts_select(ZT_SELECT_SIG)
+int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
- errno = 0;
- return select(nfds, readfds, writefds, exceptfds, timeout);
+ errno = 0;
+ return select(nfds, readfds, writefds, exceptfds, timeout);
}
-int zts_fcntl(ZT_FCNTL_SIG)
+int zts_fcntl(int fd, int cmd, int flags)
{
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- else {
- err = fcntl(fd, cmd, flags);
- }
- return err;
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ err = fcntl(fd, cmd, flags);
+ return err;
}
-/*
- [ ] [BADF] fd is not a valid file descriptor.
- [ ] [EFAULT] argp references an inaccessible memory area.
- [ ] [EINVAL] request or argp is not valid.
- [ ] [ENOTTY] The specified request does not apply to the kind of object that the file descriptor fd references.
-*/
-
-int zts_ioctl(ZT_IOCTL_SIG)
+int zts_ioctl(int fd, unsigned long request, void *argp)
{
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- else {
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ else {
#if defined(__linux__)
- if (argp)
- {
- struct ifreq *ifr = (struct ifreq *)argp;
- ZeroTier::VirtualTap *tap = getTapByName(ifr->ifr_name);
- if (tap == NULL) {
- DEBUG_ERROR("unable to locate tap interface with that name");
- err = -1;
- errno = EINVAL;
- }
- // index of VirtualTap interface
- if (request == SIOCGIFINDEX) {
- ifr->ifr_ifindex = tap->ifindex;
- err = 0;
- }
- // MAC addres or VirtualTap
- if (request == SIOCGIFHWADDR) {
- tap->_mac.copyTo(&(ifr->ifr_hwaddr.sa_data), sizeof(ifr->ifr_hwaddr.sa_data));
- err = 0;
- }
- // IP address of VirtualTap
- if (request == SIOCGIFADDR) {
- struct sockaddr_in *in4 = (struct sockaddr_in *)&(ifr->ifr_addr);
- memcpy(&(in4->sin_addr.s_addr), tap->_ips[0].rawIpData(), sizeof(ifr->ifr_addr));
- err = 0;
- }
- }
- else {
- DEBUG_INFO("!argp");
- }
+ if (argp)
+ {
+ struct ifreq *ifr = (struct ifreq *)argp;
+ ZeroTier::VirtualTap *tap = getTapByName(ifr->ifr_name);
+ if (tap == NULL) {
+ DEBUG_ERROR("unable to locate tap interface with that name");
+ err = -1;
+ errno = EINVAL;
+ }
+ // index of VirtualTap interface
+ if (request == SIOCGIFINDEX) {
+ ifr->ifr_ifindex = tap->ifindex;
+ err = 0;
+ }
+ // MAC addres or VirtualTap
+ if (request == SIOCGIFHWADDR) {
+ tap->_mac.copyTo(&(ifr->ifr_hwaddr.sa_data), sizeof(ifr->ifr_hwaddr.sa_data));
+ err = 0;
+ }
+ // IP address of VirtualTap
+ if (request == SIOCGIFADDR) {
+ struct sockaddr_in *in4 = (struct sockaddr_in *)&(ifr->ifr_addr);
+ memcpy(&(in4->sin_addr.s_addr), tap->_ips[0].rawIpData(), sizeof(ifr->ifr_addr));
+ err = 0;
+ }
+ }
+ else {
+ DEBUG_INFO("!argp");
+ }
#else
- err = ioctl(fd, request, argp);
+ err = ioctl(fd, request, argp);
#endif
- }
- return err;
+ }
+ return err;
}
-
-/*
-
-Linux:
-
- [ ] [EAGAIN or EWOULDBLOCK] The socket is marked nonblocking and the requested operation would block.
- POSIX.1-2001 allows either error to be returned for this case, and does not
- require these constants to have the same value, so a portable application
- should check for both possibilities.
- [--] [EBADF] An invalid descriptor was specified.
- [ ] [ECONNRESET] VirtualSocket reset by peer.
- [ ] [EDESTADDRREQ] The socket is not VirtualSocket-mode, and no peer address is set.
- [ ] [EFAULT] An invalid user space address was specified for an argument.
- [ ] [EINTR] A signal occurred before any data was transmitted; see signal(7).
- [ ] [EINVAL] Invalid argument passed.
- [ ] [EISCONN] The VirtualSocket-mode socket was connected already but a recipient was
- specified. (Now either this error is returned, or the recipient
- specification is ignored.)
- [ ] [EMSGSIZE] The socket type requires that message be sent atomically, and the size
- of the message to be sent made this impossible.
- [ ] [ENOBUFS] The output queue for a network interface was full. This generally indicates
- that the interface has stopped sending, but may be caused by transient congestion.
- (Normally, this does not occur in Linux. Packets are just silently
- dropped when a device queue overflows.)
- [ ] [ENOMEM] No memory available.
- [ ] [ENOTCONN] The socket is not connected, and no target has been given.
- [ ] [ENOTSOCK] The argument sockfd is not a socket.
- [ ] [EOPNOTSUPP] Some bit in the flags argument is inappropriate for the socket type.
- [ ] [EPIPE] The local end has been shut down on a VirtualSocket oriented socket.
- In this case the process will also receive a SIGPIPE unless MSG_NOSIGNAL is set.
-
- ZT_SENDTO_SIG int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen
-*/
-
-ssize_t zts_sendto(ZT_SENDTO_SIG)
+ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
{
- //DEBUG_TRANS("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (len == 0) {
- return 0;
- }
- if (len > ZT_SOCKET_MSG_BUF_SZ) {
- DEBUG_ERROR("msg is too long to be sent atomically (len=%d)", len);
- errno = EMSGSIZE;
- return -1;
- }
- if (buf == NULL) {
- DEBUG_ERROR("msg buf is null");
- errno = EINVAL;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("no vs found for fd=%x", fd);
- handle_general_failure();
- errno = EBADF;
- return -1;
- }
- ZeroTier::InetAddress iaddr;
- ZeroTier::VirtualTap *tap;
+ //DEBUG_TRANS("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (len == 0) {
+ return 0;
+ }
+ if (len > ZT_SOCKET_MSG_BUF_SZ) {
+ DEBUG_ERROR("msg is too long to be sent atomically (len=%d)", len);
+ errno = EMSGSIZE;
+ return -1;
+ }
+ if (buf == NULL) {
+ DEBUG_ERROR("msg buf is null");
+ errno = EINVAL;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("no vs found for fd=%x", fd);
+ handle_general_failure();
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::InetAddress iaddr;
+ ZeroTier::VirtualTap *tap;
- if (vs->socket_type == SOCK_DGRAM) {
- if (vs->socket_family == AF_INET)
- {
- char ipstr[INET_ADDRSTRLEN];
- memset(ipstr, 0, INET_ADDRSTRLEN);
- inet_ntop(AF_INET,
- (const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN);
- iaddr.fromString(ipstr);
- }
- if (vs->socket_family == AF_INET6)
- {
- char ipstr[INET6_ADDRSTRLEN];
- memset(ipstr, 0, INET6_ADDRSTRLEN);
- inet_ntop(AF_INET6,
- (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, ipstr, INET6_ADDRSTRLEN);
- // TODO: This is a hack, determine a proper way to do this
- char addrstr[INET6_ADDRSTRLEN];
- sprintf(addrstr, "%s%s", ipstr, std::string("/40").c_str());
- iaddr.fromString(addrstr);
- }
- // get tap
- tap = getTapByAddr(&iaddr);
- if (tap == NULL) {
- DEBUG_INFO("SOCK_DGRAM, tap not found");
- errno = EDESTADDRREQ; // TODO: double check this is the best errno to report
- return -1;
- }
- // write
- if ((err = tap->SendTo(vs, buf, len, flags, addr, addrlen)) < 0) {
- DEBUG_ERROR("error while attempting to sendto");
- errno = EINVAL; // TODO: Not correct, but what else could we use?
- }
- }
- if (vs->socket_type == SOCK_RAW)
- {
- struct sockaddr_ll *socket_address = (struct sockaddr_ll *)addr;
- ZeroTier::VirtualTap *tap = getTapByIndex(socket_address->sll_ifindex);
- if (tap) {
- DEBUG_INFO("found interface of ifindex=%d", tap->ifindex);
- err = tap->Write(vs, (void*)buf, len);
- }
- else {
- DEBUG_ERROR("unable to locate tap of ifindex=%d", socket_address->sll_ifindex);
- err = -1;
- errno = EINVAL;
- }
- }
- return err;
+ if (vs->socket_type == SOCK_DGRAM) {
+ if (vs->socket_family == AF_INET)
+ {
+ char ipstr[INET_ADDRSTRLEN];
+ memset(ipstr, 0, INET_ADDRSTRLEN);
+ inet_ntop(AF_INET,
+ (const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN);
+ iaddr.fromString(ipstr);
+ }
+ if (vs->socket_family == AF_INET6)
+ {
+ char ipstr[INET6_ADDRSTRLEN];
+ memset(ipstr, 0, INET6_ADDRSTRLEN);
+ inet_ntop(AF_INET6,
+ (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, ipstr, INET6_ADDRSTRLEN);
+ // TODO: This is a hack, determine a proper way to do this
+ char addrstr[INET6_ADDRSTRLEN];
+ sprintf(addrstr, "%s%s", ipstr, std::string("/40").c_str());
+ iaddr.fromString(addrstr);
+ }
+ // get tap
+ tap = getTapByAddr(&iaddr);
+ if (tap == NULL) {
+ DEBUG_INFO("SOCK_DGRAM, tap not found");
+ errno = EDESTADDRREQ; // TODO: double check this is the best errno to report
+ return -1;
+ }
+ // write
+ if ((err = tap->SendTo(vs, buf, len, flags, addr, addrlen)) < 0) {
+ DEBUG_ERROR("error while attempting to sendto");
+ errno = EINVAL; // TODO: Not correct, but what else could we use?
+ }
+ }
+ if (vs->socket_type == SOCK_RAW)
+ {
+ struct sockaddr_ll *socket_address = (struct sockaddr_ll *)addr;
+ ZeroTier::VirtualTap *tap = getTapByIndex(socket_address->sll_ifindex);
+ if (tap) {
+ DEBUG_INFO("found interface of ifindex=%d", tap->ifindex);
+ err = tap->Write(vs, (void*)buf, len);
+ }
+ else {
+ DEBUG_ERROR("unable to locate tap of ifindex=%d", socket_address->sll_ifindex);
+ err = -1;
+ errno = EINVAL;
+ }
+ }
+ return err;
}
-/*
- Linux:
-
- [ ] EACCES (For UNIX domain sockets, which are identified by pathname)
- Write permission is denied on the destination socket file, or
- search permission is denied for one of the directories the
- path prefix. (See path_resolution(7).)
-
- (For UDP sockets) An attempt was made to send to a
- network/broadcast address as though it was a unicast address.
- [ ] EAGAIN or EWOULDBLOCK The socket is marked nonblocking and the requested operation
- would block. POSIX.1-2001 allows either error to be returned
- for this case, and does not require these constants to have
- the same value, so a portable application should check for
- both possibilities.
- [ ] EAGAIN (Internet domain datagram sockets) The socket referred to by
- sockfd had not previously been bound to an address and, upon
- attempting to bind it to an ephemeral port, it was determined
- that all port numbers in the ephemeral port range are
- currently in use. See the discussion of
- /proc/sys/net/ipv4/ip_local_port_range in ip(7).
- [--] EBADF sockfd is not a valid open file descriptor.
- [ ] ECONNRESET Connection reset by peer.
- [--] EDESTADDRREQ The socket is not connection-mode, and no peer address is set.
- [ ] EFAULT An invalid user space address was specified for an argument.
- [ ] EINTR A signal occurred before any data was transmitted
- [--] EINVAL Invalid argument passed.
- [ ] EISCONN The connection-mode socket was connected already but a
- recipient was specified. (Now either this error is returned,
- or the recipient specification is ignored.)
- [ ] EMSGSIZE The socket type requires that message be sent atomically, and
- the size of the message to be sent made this impossible.
- [ ] ENOBUFS The output queue for a network interface was full. This
- generally indicates that the interface has stopped sending,
- but may be caused by transient congestion. (Normally, this
- does not occur in Linux. Packets are just silently dropped
- when a device queue overflows.)
- [ ] ENOMEM No memory available.
- [ ] ENOTCONN The socket is not connected, and no target has been given.
- [ ] ENOTSOCK The file descriptor sockfd does not refer to a socket.
- [ ] EOPNOTSUPP Some bit in the flags argument is inappropriate for the socket
- type.
- [ ] EPIPE The local end has been shut down on a connection oriented
- socket. In this case, the process will also receive a SIGPIPE
- unless MSG_NOSIGNAL is set.
-
- ZT_SEND_SIG int fd, const void *buf, size_t len, int flags
-*/
-
-ssize_t zts_send(ZT_SEND_SIG)
+ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
{
- // DEBUG_TRANS("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (len == 0) {
- return 0;
- }
- if (len > ZT_SOCKET_MSG_BUF_SZ) {
- DEBUG_ERROR("msg is too long to be sent atomically (len=%d)", len);
- errno = EMSGSIZE;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid vs for fd=%d", fd);
- errno = EBADF;
- return -1;
- }
- if (vs->socket_type != SOCK_STREAM) {
- DEBUG_ERROR("the socket is not connection-mode, and no peer address is set for fd=%d", fd);
- errno = EDESTADDRREQ;
- return -1;
- }
- if (flags & MSG_DONTROUTE) {
- DEBUG_INFO("MSG_DONTROUTE not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_DONTWAIT) {
- // The stack drivers and stack are inherently non-blocking by design, but we
- // still need to modify the unix pipe connecting them to the application:
- fcntl(fd, F_SETFL, O_NONBLOCK);
- }
- if (flags & MSG_EOR) {
- DEBUG_INFO("MSG_EOR not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_OOB) {
- DEBUG_INFO("MSG_OOB not implemented yet");
- errno = EINVAL;
- return -1;
- }
+ // DEBUG_TRANS("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (len == 0) {
+ return 0;
+ }
+ if (len > ZT_SOCKET_MSG_BUF_SZ) {
+ DEBUG_ERROR("msg is too long to be sent atomically (len=%d)", len);
+ errno = EMSGSIZE;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ if (vs->socket_type != SOCK_STREAM) {
+ DEBUG_ERROR("the socket is not connection-mode, and no peer address is set for fd=%d", fd);
+ errno = EDESTADDRREQ;
+ return -1;
+ }
+ if (flags & MSG_DONTROUTE) {
+ DEBUG_INFO("MSG_DONTROUTE not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_DONTWAIT) {
+ // The stack drivers and stack are inherently non-blocking by design, but we
+ // still need to modify the unix pipe connecting them to the application:
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+ }
+ if (flags & MSG_EOR) {
+ DEBUG_INFO("MSG_EOR not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_OOB) {
+ DEBUG_INFO("MSG_OOB not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
#if defined(__linux__)
- if (flags & MSG_CONFIRM) {
- DEBUG_INFO("MSG_CONFIRM not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_MORE) {
- DEBUG_INFO("MSG_MORE not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_NOSIGNAL) {
- DEBUG_INFO("MSG_NOSIGNAL not implemented yet");
- errno = EINVAL;
- return -1;
- }
+ if (flags & MSG_CONFIRM) {
+ DEBUG_INFO("MSG_CONFIRM not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_MORE) {
+ DEBUG_INFO("MSG_MORE not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_NOSIGNAL) {
+ DEBUG_INFO("MSG_NOSIGNAL not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
#endif
- err = write(fd, buf, len);
- // restore "per-call" flags
+ err = write(fd, buf, len);
+ // restore "per-call" flags
- if (flags & MSG_DONTWAIT) {
- int saved_flags = fcntl(fd, F_GETFL);
- if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag
- handle_general_failure();
- return -1;
- }
- }
- return err;
+ if (flags & MSG_DONTWAIT) {
+ int saved_flags = fcntl(fd, F_GETFL);
+ if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag
+ handle_general_failure();
+ return -1;
+ }
+ }
+ return err;
}
// TODO
-ssize_t zts_sendmsg(ZT_SENDMSG_SIG)
+ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags)
{
- DEBUG_TRANS("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- else {
- err = sendmsg(fd, msg, flags);
- }
- return err;
+ DEBUG_TRANS("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ else {
+ err = sendmsg(fd, msg, flags);
+ }
+ return err;
}
-/*
-
- Linux:
-
- These are some standard errors generated by the socket layer.
- Additional errors may be generated and returned from the underlying
- protocol modules; see their manual pages.
-
- [ ] EAGAIN or EWOULDBLOCK The socket is marked nonblocking and the receive operation
- would block, or a receive timeout had been set and the timeout
- expired before data was received.
- [--] EBADF The argument sockfd is an invalid file descriptor.
- [ ] ECONNREFUSED
- A remote host refused to allow the network connection
- (typically because it is not running the requested service).
- [ ] EFAULT The receive buffer pointer(s) point outside the process's
- address space.
- [ ] EINTR The receive was interrupted by delivery of a signal before any
- data were available; see signal(7).
- [--] EINVAL Invalid argument passed.
- [ ] ENOMEM Could not allocate memory for recvmsg().
- [ ] ENOTCONN The socket is associated with a connection-oriented protocol and has not been connected (see connect(2) and accept(2)).
- [ ] ENOTSOCK The file descriptor sockfd does not refer to a socket.
-
- ZT_RECV_SIG int fd, void *buf, size_t len, int flags
-*/
-
-ssize_t zts_recv(ZT_RECV_SIG)
+ssize_t zts_recv(int fd, void *buf, size_t len, int flags)
{
- DEBUG_TRANS("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid vs for fd=%d", fd);
- errno = EBADF;
- return -1;
- }
- if (vs->socket_type != SOCK_STREAM) {
- DEBUG_ERROR("the socket is not connection-mode, and no peer address is set for fd=%d", fd);
- errno = EDESTADDRREQ;
- return -1;
- }
- if (vs->get_state() != VS_STATE_CONNECTED) {
- DEBUG_ERROR("the socket is not in a connected state, fd=%d", fd);
- errno = ENOTCONN;
- return -1;
- }
- if (flags & MSG_DONTWAIT) {
- // The stack drivers and stack are inherently non-blocking by design, but we
- // still need to modify the unix pipe connecting them to the application:
- fcntl(fd, F_SETFL, O_NONBLOCK);
- }
- if (flags & MSG_OOB) {
- DEBUG_INFO("MSG_OOB not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_TRUNC) {
- DEBUG_INFO("MSG_TRUNC not implemented yet");
- errno = EINVAL;
- return -1;
- }
- if (flags & MSG_WAITALL) {
- DEBUG_INFO("MSG_WAITALL not implemented yet");
- errno = EINVAL;
- return -1;
- }
+ DEBUG_TRANS("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ if (vs->socket_type != SOCK_STREAM) {
+ DEBUG_ERROR("the socket is not connection-mode, and no peer address is set for fd=%d", fd);
+ errno = EDESTADDRREQ;
+ return -1;
+ }
+ if (vs->get_state() != VS_STATE_CONNECTED) {
+ DEBUG_ERROR("the socket is not in a connected state, fd=%d", fd);
+ errno = ENOTCONN;
+ return -1;
+ }
+ if (flags & MSG_DONTWAIT) {
+ // The stack drivers and stack are inherently non-blocking by design, but we
+ // still need to modify the unix pipe connecting them to the application:
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+ }
+ if (flags & MSG_OOB) {
+ DEBUG_INFO("MSG_OOB not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_TRUNC) {
+ DEBUG_INFO("MSG_TRUNC not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
+ if (flags & MSG_WAITALL) {
+ DEBUG_INFO("MSG_WAITALL not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
#if defined(__linux__)
- if (flags & MSG_ERRQUEUE) {
- DEBUG_INFO("MSG_ERRQUEUE not implemented yet");
- errno = EINVAL;
- return -1;
- }
+ if (flags & MSG_ERRQUEUE) {
+ DEBUG_INFO("MSG_ERRQUEUE not implemented yet");
+ errno = EINVAL;
+ return -1;
+ }
#endif
- if (flags & MSG_PEEK) {
- // MSG_PEEK doesn't require any special stack-related machinery so we can just
- // pass it to a regular recv() call with no issue
- err = recv(fd, buf, len, MSG_PEEK);
- }
+ if (flags & MSG_PEEK) {
+ // MSG_PEEK doesn't require any special stack-related machinery so we can just
+ // pass it to a regular recv() call with no issue
+ err = recv(fd, buf, len, MSG_PEEK);
+ }
- // restore "per-call" flags
+ // restore "per-call" flags
- if (flags & MSG_DONTWAIT) {
- int saved_flags = fcntl(fd, F_GETFL);
- if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag
- handle_general_failure();
- return -1;
- }
- }
- return err;
+ if (flags & MSG_DONTWAIT) {
+ int saved_flags = fcntl(fd, F_GETFL);
+ if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag
+ handle_general_failure();
+ return -1;
+ }
+ }
+ return err;
}
-/*
- Linux:
-
- [ ] [EAGAIN or EWOULDBLOCK] The socket is marked nonblocking and the receive operation
- would block, or a receive timeout had been set and the
- timeout expired before data was received. POSIX.1-2001
- allows either error to be returned for this case, and does
- not require these constants to have the same value, so a
- portable application should check for both possibilities.
- [--] [EBADF] The argument sockfd is an invalid descriptor.
- [ ] [ECONNREFUSED] A remote host refused to allow the network connection
- (typically because it is not running the requested service).
- [ ] [EFAULT] The receive buffer pointer(s) point outside the process's
- address space.
- [ ] [EINTR] The receive was interrupted by delivery of a signal before any
- data were available; see signal(7).
- [--] [EINVAL] Invalid argument passed.
- [ ] [ENOMEM] Could not allocate memory for recvmsg().
- [ ] [ENOTCONN] The socket is associated with a connection-oriented protocol
- and has not been connected (see connect(2) and accept(2)).
- [NA] [ENOTSOCK] The argument sockfd does not refer to a socket.
-
- ZT_RECVFROM_SIG int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen
-*/
-
-ssize_t zts_recvfrom(ZT_RECVFROM_SIG)
+ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen)
{
- //DEBUG_TRANS("fd=%d", fd);
- int32_t r = 0;
- errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (len == 0) {
- return 0;
- }
- if (buf == NULL) {
- DEBUG_ERROR("buf is null");
- errno = EINVAL;
- return -1;
- }
- char udp_msg_buf[ZT_SOCKET_MSG_BUF_SZ];
- char *msg_ptr = udp_msg_buf;
- memset(msg_ptr, 0, sizeof(int32_t)); // zero only len portion
+ //DEBUG_TRANS("fd=%d", fd);
+ int32_t r = 0;
+ errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (len == 0) {
+ return 0;
+ }
+ if (buf == NULL) {
+ DEBUG_ERROR("buf is null");
+ errno = EINVAL;
+ return -1;
+ }
+ char udp_msg_buf[ZT_SOCKET_MSG_BUF_SZ];
+ char *msg_ptr = udp_msg_buf;
+ memset(msg_ptr, 0, sizeof(int32_t)); // zero only len portion
- int32_t udp_msg_len = 0;
+ int32_t udp_msg_len = 0;
- // PEEK at the buffer and see if we can read a length, if not, err out
- r = recv(fd, msg_ptr, sizeof(int32_t), MSG_PEEK);
- if (r != sizeof(int32_t)) {
- errno = EIO; // TODO: test for this
- return -1;
- }
- // read of sizeof(int32_t) for the length of the datagram (including address)
- r = read(fd, msg_ptr, sizeof(int32_t));
- // copy to length variable
- memcpy(&udp_msg_len, msg_ptr, sizeof(int32_t));
- msg_ptr+=sizeof(int32_t);
+ // PEEK at the buffer and see if we can read a length, if not, err out
+ r = recv(fd, msg_ptr, sizeof(int32_t), MSG_PEEK);
+ if (r != sizeof(int32_t)) {
+ errno = EIO; // TODO: test for this
+ return -1;
+ }
+ // read of sizeof(int32_t) for the length of the datagram (including address)
+ r = read(fd, msg_ptr, sizeof(int32_t));
+ // copy to length variable
+ memcpy(&udp_msg_len, msg_ptr, sizeof(int32_t));
+ msg_ptr+=sizeof(int32_t);
- if (udp_msg_len <= 0) {
- DEBUG_ERROR("invalid datagram");
- errno = EIO; // TODO: test for this
- return -1;
- }
- // there is a datagram to read, so let's read it
- // zero remainder of buffer
- memset(msg_ptr, 0, ZT_SOCKET_MSG_BUF_SZ- sizeof(int32_t));
- if ((r = read(fd, msg_ptr, udp_msg_len)) < 0) {
- DEBUG_ERROR("invalid datagram");
- errno = EIO; // TODO: test for this
- return -1;
- }
- // get address
- if (addr) {
- if (*addrlen < sizeof(struct sockaddr_storage)) {
- DEBUG_ERROR("invalid address length provided");
- errno = EINVAL;
- return -1;
- }
- *addrlen = sizeof(struct sockaddr_storage);
- memcpy(addr, msg_ptr, *addrlen);
- }
- msg_ptr+=sizeof(struct sockaddr_storage);
- // get payload
- int32_t payload_sz = udp_msg_len - *addrlen;
- int32_t write_sz = len < payload_sz ? len : payload_sz;
- memcpy(buf, msg_ptr, write_sz);
- return write_sz;
+ if (udp_msg_len <= 0) {
+ DEBUG_ERROR("invalid datagram");
+ errno = EIO; // TODO: test for this
+ return -1;
+ }
+ // there is a datagram to read, so let's read it
+ // zero remainder of buffer
+ memset(msg_ptr, 0, ZT_SOCKET_MSG_BUF_SZ- sizeof(int32_t));
+ if ((r = read(fd, msg_ptr, udp_msg_len)) < 0) {
+ DEBUG_ERROR("invalid datagram");
+ errno = EIO; // TODO: test for this
+ return -1;
+ }
+ // get address
+ if (addr) {
+ if (*addrlen < sizeof(struct sockaddr_storage)) {
+ DEBUG_ERROR("invalid address length provided");
+ errno = EINVAL;
+ return -1;
+ }
+ *addrlen = sizeof(struct sockaddr_storage);
+ memcpy(addr, msg_ptr, *addrlen);
+ }
+ msg_ptr+=sizeof(struct sockaddr_storage);
+ // get payload
+ int32_t payload_sz = udp_msg_len - *addrlen;
+ int32_t write_sz = len < payload_sz ? len : payload_sz;
+ memcpy(buf, msg_ptr, write_sz);
+ return write_sz;
}
// TODO
-ssize_t zts_recvmsg(ZT_RECVMSG_SIG)
+ssize_t zts_recvmsg(int fd, struct msghdr *msg,int flags)
{
- //DEBUG_TRANS("fd=%d", fd);
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- else {
- err = recvmsg(fd, msg, flags);
- }
- return err;
+ //DEBUG_TRANS("fd=%d", fd);
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ else {
+ err = recvmsg(fd, msg, flags);
+ }
+ return err;
}
-int zts_read(ZT_READ_SIG) {
- //DEBUG_TRANS("fd=%d", fd);
- return read(fd, buf, len);
+int zts_read(int fd, void *buf, size_t len) {
+ DEBUG_TRANS("fd=%d", fd);
+ errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ return read(fd, buf, len);
}
-int zts_write(ZT_WRITE_SIG) {
- DEBUG_TRANS("fd=%d", fd);
- return write(fd, buf, len);
+int zts_write(int fd, const void *buf, size_t len) {
+ DEBUG_TRANS("fd=%d", fd);
+ errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ return write(fd, buf, len);
}
-
-/*
-
-Linux:
-
- [--] [EBADF] The socket argument is not a valid file descriptor.
- [--] [EINVAL] The how argument is invalid.
- [--] [ENOTCONN] The socket is not connected.
- [NA] [ENOTSOCK] The socket argument does not refer to a socket.
- [NA] [ENOBUFS] Insufficient resources were available in the system to perform the operation.
-
- ZT_SHUTDOWN_SIG int fd, int how
-*/
-
-int zts_shutdown(ZT_SHUTDOWN_SIG)
+int zts_shutdown(int fd, int how)
{
- int err = errno = 0;
- if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
- errno = EBADF;
- return -1;
- }
- if (how != SHUT_RD && how != SHUT_WR && how != SHUT_RDWR) {
- errno = EINVAL;
- return -1;
- }
- ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
- if (vs == NULL) {
- DEBUG_ERROR("invalid vs for fd=%d", fd);
- errno = EBADF;
- return -1;
- }
- if (vs->get_state() != VS_STATE_CONNECTED || vs->socket_type != SOCK_STREAM) {
- DEBUG_ERROR("the socket is either not in a connected state, or isn't connection-based, fd=%d", fd);
- errno = ENOTCONN;
- return -1;
- }
- if (vs->tap) {
- err = vs->tap->Shutdown(vs, how);
- }
- return err;
+ int err = errno = 0;
+ if (fd < 0 || fd >= ZT_MAX_SOCKETS) {
+ errno = EBADF;
+ return -1;
+ }
+ if (how != SHUT_RD && how != SHUT_WR && how != SHUT_RDWR) {
+ errno = EINVAL;
+ return -1;
+ }
+ ZeroTier::VirtualSocket *vs = get_virt_socket(fd);
+ if (vs == NULL) {
+ DEBUG_ERROR("invalid vs for fd=%d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ if (vs->get_state() != VS_STATE_CONNECTED || vs->socket_type != SOCK_STREAM) {
+ DEBUG_ERROR("the socket is either not in a connected state, or isn't connection-based, fd=%d", fd);
+ errno = ENOTCONN;
+ return -1;
+ }
+ if (vs->tap) {
+ err = vs->tap->Shutdown(vs, how);
+ }
+ return err;
}
int zts_add_dns_nameserver(struct sockaddr *addr)
{
- ZeroTier::VirtualTap *vtap = getAnyTap();
- if (vtap) {
- return vtap->add_DNS_Nameserver(addr);
- }
- DEBUG_ERROR("unable to locate virtual tap to add DNS nameserver");
- return -1;
+ ZeroTier::VirtualTap *vtap = getAnyTap();
+ if (vtap) {
+ return vtap->add_DNS_Nameserver(addr);
+ }
+ DEBUG_ERROR("unable to locate virtual tap to add DNS nameserver");
+ return -1;
}
int zts_del_dns_nameserver(struct sockaddr *addr)
{
- ZeroTier::VirtualTap *vtap = getAnyTap();
- if (vtap) {
- return vtap->del_DNS_Nameserver(addr);
- }
- DEBUG_ERROR("unable to locate virtual tap to remove DNS nameserver");
- return -1;
+ ZeroTier::VirtualTap *vtap = getAnyTap();
+ if (vtap) {
+ return vtap->del_DNS_Nameserver(addr);
+ }
+ DEBUG_ERROR("unable to locate virtual tap to remove DNS nameserver");
+ return -1;
}
/****************************************************************************/
@@ -1744,266 +1409,266 @@ int zts_del_dns_nameserver(struct sockaddr *addr)
namespace ZeroTier {
- #include
+ #include
- JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1start(JNIEnv *env, jobject thisObj, jstring path) {
- if (path) {
- homeDir = env->GetStringUTFChars(path, NULL);
- zts_start(homeDir.c_str());
- }
- }
- // Shuts down ZeroTier service and SOCKS5 Proxy server
- JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1stop(JNIEnv *env, jobject thisObj) {
- if (ZeroTier::zt1Service) {
- zts_stop();
- }
- }
+ JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1start(JNIEnv *env, jobject thisObj, jstring path) {
+ if (path) {
+ homeDir = env->GetStringUTFChars(path, NULL);
+ zts_start(homeDir.c_str());
+ }
+ }
+ // Shuts down ZeroTier service and SOCKS5 Proxy server
+ JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1stop(JNIEnv *env, jobject thisObj) {
+ if (ZeroTier::zt1Service) {
+ zts_stop();
+ }
+ }
- // Returns whether the ZeroTier service is running
- JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_ztjni_1running(
- JNIEnv *env, jobject thisObj)
- {
- return zts_running();
- }
- // Returns path for ZT config/data files
- JNIEXPORT jstring JNICALL Java_zerotier_ZeroTier_ztjni_1homepath(
- JNIEnv *env, jobject thisObj)
- {
- // TODO: fix, should copy into given arg
- // return (*env).NewStringUTF(zts_get_homepath());
- return (*env).NewStringUTF("");
- }
- // Join a network
- JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1join(
- JNIEnv *env, jobject thisObj, jstring nwid)
- {
- const char *nwidstr;
- if (nwid) {
- nwidstr = env->GetStringUTFChars(nwid, NULL);
- zts_join(nwidstr);
- }
- }
- // Leave a network
- JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1leave(
- JNIEnv *env, jobject thisObj, jstring nwid)
- {
- const char *nwidstr;
- if (nwid) {
- nwidstr = env->GetStringUTFChars(nwid, NULL);
- zts_leave(nwidstr);
- }
- }
- // FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations
- // Now only returns first assigned address per network. Shouldn't normally be a problem
- JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_ztjni_1get_1ipv4_1address(
- JNIEnv *env, jobject thisObj, jstring nwid)
- {
- const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
- char address_string[INET_ADDRSTRLEN];
- memset(address_string, 0, INET_ADDRSTRLEN);
- zts_get_ipv4_address(nwid_str, address_string, INET_ADDRSTRLEN);
- jclass clazz = (*env).FindClass("java/util/ArrayList");
- jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "", "()V"));
- jstring _str = (*env).NewStringUTF(address_string);
- env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
- return addresses;
- }
+ // Returns whether the ZeroTier service is running
+ JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_ztjni_1running(
+ JNIEnv *env, jobject thisObj)
+ {
+ return zts_running();
+ }
+ // Returns path for ZT config/data files
+ JNIEXPORT jstring JNICALL Java_zerotier_ZeroTier_ztjni_1homepath(
+ JNIEnv *env, jobject thisObj)
+ {
+ // TODO: fix, should copy into given arg
+ // return (*env).NewStringUTF(zts_get_homepath());
+ return (*env).NewStringUTF("");
+ }
+ // Join a network
+ JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1join(
+ JNIEnv *env, jobject thisObj, jstring nwid)
+ {
+ const char *nwidstr;
+ if (nwid) {
+ nwidstr = env->GetStringUTFChars(nwid, NULL);
+ zts_join(nwidstr);
+ }
+ }
+ // Leave a network
+ JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1leave(
+ JNIEnv *env, jobject thisObj, jstring nwid)
+ {
+ const char *nwidstr;
+ if (nwid) {
+ nwidstr = env->GetStringUTFChars(nwid, NULL);
+ zts_leave(nwidstr);
+ }
+ }
+ // FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations
+ // Now only returns first assigned address per network. Shouldn't normally be a problem
+ JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_ztjni_1get_1ipv4_1address(
+ JNIEnv *env, jobject thisObj, jstring nwid)
+ {
+ const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
+ char address_string[INET_ADDRSTRLEN];
+ memset(address_string, 0, INET_ADDRSTRLEN);
+ zts_get_ipv4_address(nwid_str, address_string, INET_ADDRSTRLEN);
+ jclass clazz = (*env).FindClass("java/util/ArrayList");
+ jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "", "()V"));
+ jstring _str = (*env).NewStringUTF(address_string);
+ env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
+ return addresses;
+ }
- JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_ztjni_1get_1ipv6_1address(
- JNIEnv *env, jobject thisObj, jstring nwid)
- {
- const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
- char address_string[INET6_ADDRSTRLEN];
- memset(address_string, 0, INET6_ADDRSTRLEN);
- zts_get_ipv6_address(nwid_str, address_string, INET6_ADDRSTRLEN);
- jclass clazz = (*env).FindClass("java/util/ArrayList");
- jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "", "()V"));
- jstring _str = (*env).NewStringUTF(address_string);
- env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
- return addresses;
- }
+ JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_ztjni_1get_1ipv6_1address(
+ JNIEnv *env, jobject thisObj, jstring nwid)
+ {
+ const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
+ char address_string[INET6_ADDRSTRLEN];
+ memset(address_string, 0, INET6_ADDRSTRLEN);
+ zts_get_ipv6_address(nwid_str, address_string, INET6_ADDRSTRLEN);
+ jclass clazz = (*env).FindClass("java/util/ArrayList");
+ jobject addresses = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "", "()V"));
+ jstring _str = (*env).NewStringUTF(address_string);
+ env->CallBooleanMethod(addresses, env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
+ return addresses;
+ }
- // Returns the device is in integer form
- JNIEXPORT jint Java_zerotier_ZeroTier_ztjni_1get_1device_1id()
- {
- return zts_get_device_id(NULL); // TODO
- }
+ // Returns the device is in integer form
+ JNIEXPORT jint Java_zerotier_ZeroTier_ztjni_1get_1device_1id()
+ {
+ return zts_get_device_id(NULL); // TODO
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags)
- {
- jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
- char * bufp = (char *)malloc(sizeof(char)*len);
- memcpy(bufp, body, len);
- (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
- int written_bytes = zts_write(fd, body, len);
- return written_bytes;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1send(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, int flags)
+ {
+ jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
+ char * bufp = (char *)malloc(sizeof(char)*len);
+ memcpy(bufp, body, len);
+ (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
+ int written_bytes = zts_write(fd, body, len);
+ return written_bytes;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1sendto(
- JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr)
- {
- struct sockaddr_in addr;
- jclass cls = (*env).GetObjectClass( ztaddr);
- jfieldID f = (*env).GetFieldID( cls, "port", "I");
- addr.sin_port = htons((*env).GetIntField( ztaddr, f));
- f = (*env).GetFieldID( cls, "_rawAddr", "J");
- addr.sin_addr.s_addr = (*env).GetLongField( ztaddr, f);
- addr.sin_family = AF_INET;
- //LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
- // TODO: Optimize this
- jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
- char * bufp = (char *)malloc(sizeof(char)*len);
- memcpy(bufp, body, len);
- (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
- // "connect" and send buffer contents
- int sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
- return sent_bytes;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1sendto(
+ JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len, jint flags, jobject ztaddr)
+ {
+ struct sockaddr_in addr;
+ jclass cls = (*env).GetObjectClass( ztaddr);
+ jfieldID f = (*env).GetFieldID( cls, "port", "I");
+ addr.sin_port = htons((*env).GetIntField( ztaddr, f));
+ f = (*env).GetFieldID( cls, "_rawAddr", "J");
+ addr.sin_addr.s_addr = (*env).GetLongField( ztaddr, f);
+ addr.sin_family = AF_INET;
+ //LOGV("zt_sendto(): fd = %d\naddr = %s\nport=%d", fd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
+ // TODO: Optimize this
+ jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
+ char * bufp = (char *)malloc(sizeof(char)*len);
+ memcpy(bufp, body, len);
+ (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
+ // "connect" and send buffer contents
+ int sent_bytes = zts_sendto(fd, body, len, flags, (struct sockaddr *)&addr, sizeof(addr));
+ return sent_bytes;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1recvfrom(
- JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint len, jint flags, jobject ztaddr)
- {
- struct sockaddr_in addr;
- jbyte *body = (*env).GetByteArrayElements( buf, 0);
- unsigned char buffer[ZT_SDK_MTU];
- int payload_offset = sizeof(int32_t) + sizeof(struct sockaddr_storage);
- int rxbytes = zts_recvfrom(fd, &buffer, len, flags, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr_storage));
- if (rxbytes > 0)
- memcpy(body, (jbyte*)buffer + payload_offset, rxbytes);
- (*env).ReleaseByteArrayElements( buf, body, 0);
- // Update fields of Java ZTAddress object
- jfieldID fid;
- jclass cls = (*env).GetObjectClass( ztaddr);
- fid = (*env).GetFieldID( cls, "port", "I");
- (*env).SetIntField( ztaddr, fid, addr.sin_port);
- fid = (*env).GetFieldID( cls,"_rawAddr", "J");
- (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
- return rxbytes;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1recvfrom(
+ JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint len, jint flags, jobject ztaddr)
+ {
+ struct sockaddr_in addr;
+ jbyte *body = (*env).GetByteArrayElements( buf, 0);
+ unsigned char buffer[ZT_SDK_MTU];
+ int payload_offset = sizeof(int32_t) + sizeof(struct sockaddr_storage);
+ int rxbytes = zts_recvfrom(fd, &buffer, len, flags, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr_storage));
+ if (rxbytes > 0)
+ memcpy(body, (jbyte*)buffer + payload_offset, rxbytes);
+ (*env).ReleaseByteArrayElements( buf, body, 0);
+ // Update fields of Java ZTAddress object
+ jfieldID fid;
+ jclass cls = (*env).GetObjectClass( ztaddr);
+ fid = (*env).GetFieldID( cls, "port", "I");
+ (*env).SetIntField( ztaddr, fid, addr.sin_port);
+ fid = (*env).GetFieldID( cls,"_rawAddr", "J");
+ (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
+ return rxbytes;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1write(JNIEnv *env, jobject thisObj,
- jint fd, jarray buf, jint len)
- {
- jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
- char * bufp = (char *)malloc(sizeof(char)*len);
- memcpy(bufp, body, len);
- (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
- int written_bytes = zts_write(fd, body, len);
- return written_bytes;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1write(JNIEnv *env, jobject thisObj,
+ jint fd, jarray buf, jint len)
+ {
+ jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
+ char * bufp = (char *)malloc(sizeof(char)*len);
+ memcpy(bufp, body, len);
+ (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
+ int written_bytes = zts_write(fd, body, len);
+ return written_bytes;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1read(JNIEnv *env, jobject thisObj,
- jint fd, jarray buf, jint len)
- {
- jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
- int read_bytes = read(fd, body, len);
- (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
- return read_bytes;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1read(JNIEnv *env, jobject thisObj,
+ jint fd, jarray buf, jint len)
+ {
+ jbyte *body = (*env).GetByteArrayElements((_jbyteArray *)buf, 0);
+ int read_bytes = read(fd, body, len);
+ (*env).ReleaseByteArrayElements((_jbyteArray *)buf, body, 0);
+ return read_bytes;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1setsockopt(
- JNIEnv *env, jobject thisObj,
- jint fd, jint level, jint optname, jint optval, jint optlen) {
- return zts_setsockopt(fd, level, optname, (const void*)optval, optlen);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1setsockopt(
+ JNIEnv *env, jobject thisObj,
+ jint fd, jint level, jint optname, jint optval, jint optlen) {
+ return zts_setsockopt(fd, level, optname, (const void*)optval, optlen);
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getsockopt(JNIEnv *env, jobject thisObj,
- jint fd, jint level, jint optname, jint optval, jint optlen) {
- return zts_getsockopt(fd, level, optname, (void*)optval, (socklen_t *)optlen);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getsockopt(JNIEnv *env, jobject thisObj,
+ jint fd, jint level, jint optname, jint optval, jint optlen) {
+ return zts_getsockopt(fd, level, optname, (void*)optval, (socklen_t *)optlen);
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1socket(JNIEnv *env, jobject thisObj,
- jint family, jint type, jint protocol) {
- return zts_socket(family, type, protocol);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1socket(JNIEnv *env, jobject thisObj,
+ jint family, jint type, jint protocol) {
+ return zts_socket(family, type, protocol);
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1connect(JNIEnv *env, jobject thisObj,
- jint fd, jstring addrstr, jint port) {
- struct sockaddr_in addr;
- const char *str = (*env).GetStringUTFChars( addrstr, 0);
- addr.sin_addr.s_addr = inet_addr(str);
- addr.sin_family = AF_INET;
- addr.sin_port = htons( port );
- (*env).ReleaseStringUTFChars( addrstr, str);
- return zts_connect(fd, (struct sockaddr *)&addr, sizeof(addr));
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1connect(JNIEnv *env, jobject thisObj,
+ jint fd, jstring addrstr, jint port) {
+ struct sockaddr_in addr;
+ const char *str = (*env).GetStringUTFChars( addrstr, 0);
+ addr.sin_addr.s_addr = inet_addr(str);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons( port );
+ (*env).ReleaseStringUTFChars( addrstr, str);
+ return zts_connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1bind(JNIEnv *env, jobject thisObj,
- jint fd, jstring addrstr, jint port) {
- struct sockaddr_in addr;
- const char *str = (*env).GetStringUTFChars( addrstr, 0);
- DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
- addr.sin_addr.s_addr = inet_addr(str);
- addr.sin_family = AF_INET;
- addr.sin_port = htons( port );
- (*env).ReleaseStringUTFChars( addrstr, str);
- return zts_bind(fd, (struct sockaddr *)&addr, sizeof(addr));
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1bind(JNIEnv *env, jobject thisObj,
+ jint fd, jstring addrstr, jint port) {
+ struct sockaddr_in addr;
+ const char *str = (*env).GetStringUTFChars( addrstr, 0);
+ DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
+ addr.sin_addr.s_addr = inet_addr(str);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons( port );
+ (*env).ReleaseStringUTFChars( addrstr, str);
+ return zts_bind(fd, (struct sockaddr *)&addr, sizeof(addr));
+ }
#if defined(__linux__)
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1accept4(JNIEnv *env, jobject thisObj,
- jint fd, jstring addrstr, jint port, jint flags) {
- struct sockaddr_in addr;
- char *str;
- // = env->GetStringUTFChars(addrstr, NULL);
- (*env).ReleaseStringUTFChars( addrstr, str);
- addr.sin_addr.s_addr = inet_addr(str);
- addr.sin_family = AF_INET;
- addr.sin_port = htons( port );
- return zts_accept4(fd, (struct sockaddr *)&addr, sizeof(addr), flags);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1accept4(JNIEnv *env, jobject thisObj,
+ jint fd, jstring addrstr, jint port, jint flags) {
+ struct sockaddr_in addr;
+ char *str;
+ // = env->GetStringUTFChars(addrstr, NULL);
+ (*env).ReleaseStringUTFChars( addrstr, str);
+ addr.sin_addr.s_addr = inet_addr(str);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons( port );
+ return zts_accept4(fd, (struct sockaddr *)&addr, sizeof(addr), flags);
+ }
#endif
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1accept(JNIEnv *env, jobject thisObj,
- jint fd, jstring addrstr, jint port) {
- struct sockaddr_in addr;
- // TODO: Send addr info back to Javaland
- addr.sin_addr.s_addr = inet_addr("");
- addr.sin_family = AF_INET;
- addr.sin_port = htons( port );
- return zts_accept(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr));
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1accept(JNIEnv *env, jobject thisObj,
+ jint fd, jstring addrstr, jint port) {
+ struct sockaddr_in addr;
+ // TODO: Send addr info back to Javaland
+ addr.sin_addr.s_addr = inet_addr("");
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons( port );
+ return zts_accept(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(addr));
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1listen(JNIEnv *env, jobject thisObj,
- jint fd, int backlog) {
- return zts_listen(fd, backlog);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1listen(JNIEnv *env, jobject thisObj,
+ jint fd, int backlog) {
+ return zts_listen(fd, backlog);
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1close(JNIEnv *env, jobject thisObj,
- jint fd) {
- return zts_close(fd);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1close(JNIEnv *env, jobject thisObj,
+ jint fd) {
+ return zts_close(fd);
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getsockname(JNIEnv *env, jobject thisObj,
- jint fd, jobject ztaddr) {
- struct sockaddr_in addr;
- int err = zts_getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
- jfieldID fid;
- jclass cls = (*env).GetObjectClass(ztaddr);
- fid = (*env).GetFieldID( cls, "port", "I");
- (*env).SetIntField( ztaddr, fid, addr.sin_port);
- fid = (*env).GetFieldID( cls,"_rawAddr", "J");
- (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
- return err;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getsockname(JNIEnv *env, jobject thisObj,
+ jint fd, jobject ztaddr) {
+ struct sockaddr_in addr;
+ int err = zts_getsockname(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
+ jfieldID fid;
+ jclass cls = (*env).GetObjectClass(ztaddr);
+ fid = (*env).GetFieldID( cls, "port", "I");
+ (*env).SetIntField( ztaddr, fid, addr.sin_port);
+ fid = (*env).GetFieldID( cls,"_rawAddr", "J");
+ (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
+ return err;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getpeername(JNIEnv *env, jobject thisObj,
- jint fd, jobject ztaddr) {
- struct sockaddr_in addr;
- int err = zts_getpeername(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
- jfieldID fid;
- jclass cls = (*env).GetObjectClass( ztaddr);
- fid = (*env).GetFieldID( cls, "port", "I");
- (*env).SetIntField( ztaddr, fid, addr.sin_port);
- fid = (*env).GetFieldID( cls,"_rawAddr", "J");
- (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
- return err;
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1getpeername(JNIEnv *env, jobject thisObj,
+ jint fd, jobject ztaddr) {
+ struct sockaddr_in addr;
+ int err = zts_getpeername(fd, (struct sockaddr *)&addr, (socklen_t *)sizeof(struct sockaddr));
+ jfieldID fid;
+ jclass cls = (*env).GetObjectClass( ztaddr);
+ fid = (*env).GetFieldID( cls, "port", "I");
+ (*env).SetIntField( ztaddr, fid, addr.sin_port);
+ fid = (*env).GetFieldID( cls,"_rawAddr", "J");
+ (*env).SetLongField( ztaddr, fid,addr.sin_addr.s_addr);
+ return err;
+ }
- JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1fcntl(JNIEnv *env, jobject thisObj,
- jint fd, jint cmd, jint flags) {
- return zts_fcntl(fd,cmd,flags);
- }
+ JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_ztjni_1fcntl(JNIEnv *env, jobject thisObj,
+ jint fd, jint cmd, jint flags) {
+ return zts_fcntl(fd,cmd,flags);
+ }
}
#endif
@@ -2013,63 +1678,63 @@ namespace ZeroTier {
bool can_provision_new_socket(int socket_type)
{
- int can = false;
+ int can = false;
#if defined(STACK_PICO)
- return !(pico_ntimers()+1 > PICO_MAX_TIMERS);
+ return !(pico_ntimers()+1 > PICO_MAX_TIMERS);
#endif
#if defined(STACK_LWIP)
- if (socket_type == SOCK_STREAM) {
- return !(ZeroTier::lwIP::lwip_num_current_tcp_pcbs()+1 > MEMP_NUM_TCP_PCB);
- }
- if (socket_type == SOCK_DGRAM) {
- return !(ZeroTier::lwIP::lwip_num_current_udp_pcbs()+1 > MEMP_NUM_UDP_PCB);
- }
- if (socket_type == SOCK_RAW) {
- return !(ZeroTier::lwIP::lwip_num_current_raw_pcbs()+1 > MEMP_NUM_RAW_PCB);
- }
- can = true;
+ if (socket_type == SOCK_STREAM) {
+ return !(ZeroTier::lwIP::lwip_num_current_tcp_pcbs()+1 > MEMP_NUM_TCP_PCB);
+ }
+ if (socket_type == SOCK_DGRAM) {
+ return !(ZeroTier::lwIP::lwip_num_current_udp_pcbs()+1 > MEMP_NUM_UDP_PCB);
+ }
+ if (socket_type == SOCK_RAW) {
+ return !(ZeroTier::lwIP::lwip_num_current_raw_pcbs()+1 > MEMP_NUM_RAW_PCB);
+ }
+ can = true;
#endif
#if defined(NO_STACK)
- // always true since there's no network stack timer/memory limitation
- can = true;
+ // always true since there's no network stack timer/memory limitation
+ can = true;
#endif
- return can;
+ return can;
}
int zts_num_active_virt_sockets()
{
- ZeroTier::_multiplexer_lock.lock();
- int num = ZeroTier::unmap.size() + ZeroTier::fdmap.size();
- ZeroTier::_multiplexer_lock.unlock();
- return num;
+ ZeroTier::_multiplexer_lock.lock();
+ int num = ZeroTier::unmap.size() + ZeroTier::fdmap.size();
+ ZeroTier::_multiplexer_lock.unlock();
+ return num;
}
int zts_maxsockets(int socket_type)
{
- int max = 0;
+ int max = 0;
#if defined(STACK_PICO)
- // TODO: This is only an approximation
- // TODO: distinquish by type
- max = PICO_MAX_TIMERS - 10;
+ // TODO: This is only an approximation
+ // TODO: distinquish by type
+ max = PICO_MAX_TIMERS - 10;
#endif
#if defined(STACK_LWIP)
- if (socket_type == SOCK_STREAM) {
- max = MEMP_NUM_TCP_PCB;
- }
- if (socket_type == SOCK_DGRAM) {
- max = MEMP_NUM_UDP_PCB;
- }
+ if (socket_type == SOCK_STREAM) {
+ max = MEMP_NUM_TCP_PCB;
+ }
+ if (socket_type == SOCK_DGRAM) {
+ max = MEMP_NUM_UDP_PCB;
+ }
#endif
#if defined(NO_STACK)
- // arbitrary
+ // arbitrary
#if defined(__linux__)
- max = RLIMIT_NOFILE;
+ max = RLIMIT_NOFILE;
#endif
#if defined(__APPLE__)
- max = 1024;
+ max = 1024;
#endif
#endif
- return max;
+ return max;
}
/****************************************************************************/
@@ -2078,102 +1743,102 @@ int zts_maxsockets(int socket_type)
std::vector *zts_get_network_routes(char *nwid)
{
- uint64_t nwid_int = strtoull(nwid, NULL, 16);
- return ZeroTier::zt1Service->getRoutes(nwid_int);
+ uint64_t nwid_int = strtoull(nwid, NULL, 16);
+ return ZeroTier::zt1Service->getRoutes(nwid_int);
}
ZeroTier::VirtualTap *getTapByNWID(uint64_t nwid)
{
- ZeroTier::_vtaps_lock.lock();
- ZeroTier::VirtualTap *s, *tap = nullptr;
- for (int i=0; i_nwid == nwid) { tap = s; }
- }
- ZeroTier::_vtaps_lock.unlock();
- return tap;
+ ZeroTier::_vtaps_lock.lock();
+ ZeroTier::VirtualTap *s, *tap = nullptr;
+ for (int i=0; i_nwid == nwid) { tap = s; }
+ }
+ ZeroTier::_vtaps_lock.unlock();
+ return tap;
}
ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
{
- ZeroTier::_vtaps_lock.lock();
- ZeroTier::VirtualTap *s, *tap = nullptr;
- //char ipbuf[64], ipbuf2[64], ipbuf3[64];
- for (int i=0; i_ips.size(); j++) {
- if ((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) {
- //DEBUG_EXTRA("looking at tap %s, --- for <%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2));
- if (s->_ips[j].isEqualPrefix(addr)
- || s->_ips[j].ipsEqual(addr)
- || s->_ips[j].containsAddress(addr)
- || (addr->isV6() && ipv6_in_subnet(&s->_ips[j], addr))
- )
- {
- //DEBUG_EXTRA("selected tap %s, ", s->_dev.c_str(), s->_ips[j].toString(ipbuf));
- ZeroTier::_vtaps_lock.unlock();
- return s;
- }
- }
- }
- // check managed routes
- if (tap == NULL) {
- std::vector *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid);
- ZeroTier::InetAddress target, nm, via;
- for (int i=0; isize(); i++) {
- target = managed_routes->at(i).target;
- nm = target.netmask();
- via = managed_routes->at(i).via;
- if (target.containsAddress(addr)) {
- //DEBUG_EXTRA("chose tap with route ", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3));
- ZeroTier::_vtaps_lock.unlock();
- return s;
- }
- }
- }
- }
- ZeroTier::_vtaps_lock.unlock();
- return tap;
+ ZeroTier::_vtaps_lock.lock();
+ ZeroTier::VirtualTap *s, *tap = nullptr;
+ //char ipbuf[64], ipbuf2[64], ipbuf3[64];
+ for (int i=0; i_ips.size(); j++) {
+ if ((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) {
+ //DEBUG_EXTRA("looking at tap %s, --- for <%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2));
+ if (s->_ips[j].isEqualPrefix(addr)
+ || s->_ips[j].ipsEqual(addr)
+ || s->_ips[j].containsAddress(addr)
+ || (addr->isV6() && ipv6_in_subnet(&s->_ips[j], addr))
+ )
+ {
+ //DEBUG_EXTRA("selected tap %s, ", s->_dev.c_str(), s->_ips[j].toString(ipbuf));
+ ZeroTier::_vtaps_lock.unlock();
+ return s;
+ }
+ }
+ }
+ // check managed routes
+ if (tap == NULL) {
+ std::vector *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid);
+ ZeroTier::InetAddress target, nm, via;
+ for (int i=0; isize(); i++) {
+ target = managed_routes->at(i).target;
+ nm = target.netmask();
+ via = managed_routes->at(i).via;
+ if (target.containsAddress(addr)) {
+ //DEBUG_EXTRA("chose tap with route ", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3));
+ ZeroTier::_vtaps_lock.unlock();
+ return s;
+ }
+ }
+ }
+ }
+ ZeroTier::_vtaps_lock.unlock();
+ return tap;
}
ZeroTier::VirtualTap *getTapByName(char *ifname)
{
- ZeroTier::_vtaps_lock.lock();
- ZeroTier::VirtualTap *s, *tap = nullptr;
- for (int i=0; i_dev.c_str(), ifname) == false) {
- tap = s;
- }
- }
- ZeroTier::_vtaps_lock.unlock();
- return tap;
+ ZeroTier::_vtaps_lock.lock();
+ ZeroTier::VirtualTap *s, *tap = nullptr;
+ for (int i=0; i_dev.c_str(), ifname) == false) {
+ tap = s;
+ }
+ }
+ ZeroTier::_vtaps_lock.unlock();
+ return tap;
}
ZeroTier::VirtualTap *getTapByIndex(int index)
{
- ZeroTier::_vtaps_lock.lock();
- ZeroTier::VirtualTap *s, *tap = nullptr;
- for (int i=0; iifindex == index) {
- tap = s;
- }
- }
- ZeroTier::_vtaps_lock.unlock();
- return tap;
+ ZeroTier::_vtaps_lock.lock();
+ ZeroTier::VirtualTap *s, *tap = nullptr;
+ for (int i=0; iifindex == index) {
+ tap = s;
+ }
+ }
+ ZeroTier::_vtaps_lock.unlock();
+ return tap;
}
ZeroTier::VirtualTap *getAnyTap()
{
- ZeroTier::_vtaps_lock.lock();
- ZeroTier::VirtualTap *vtap = NULL;
- if (ZeroTier::vtaps.size()) {
- vtap = (ZeroTier::VirtualTap *)ZeroTier::vtaps[0];
- }
- ZeroTier::_vtaps_lock.unlock();
- return vtap;
+ ZeroTier::_vtaps_lock.lock();
+ ZeroTier::VirtualTap *vtap = NULL;
+ if (ZeroTier::vtaps.size()) {
+ vtap = (ZeroTier::VirtualTap *)ZeroTier::vtaps[0];
+ }
+ ZeroTier::_vtaps_lock.unlock();
+ return vtap;
}
/****************************************************************************/
@@ -2182,239 +1847,258 @@ ZeroTier::VirtualTap *getAnyTap()
ZeroTier::VirtualSocket *get_virt_socket(int fd)
{
- ZeroTier::_multiplexer_lock.lock();
- // try to locate in unmapped set
- ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd];
- if (vs == NULL) {
- // if not, try to find in mapped set (bind to vtap has been performed)
- std::pair *p = ZeroTier::fdmap[fd];
- if (p) {
- vs = p->first;
- }
- else {
- DEBUG_ERROR("unable to locate virtual socket");
- }
- }
- ZeroTier::_multiplexer_lock.unlock();
- return vs;
+ DEBUG_EXTRA("fd=%d", fd);
+ ZeroTier::_multiplexer_lock.lock();
+ // try to locate in unmapped set
+ ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd];
+ if (vs == NULL) {
+ // if not, try to find in mapped set (bind to vtap has been performed)
+ std::pair *p = ZeroTier::fdmap[fd];
+ if (p) {
+ vs = p->first;
+ }
+ else {
+ DEBUG_ERROR("unable to locate virtual socket");
+ }
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return vs;
}
int del_virt_socket(int fd)
{
- int err = 0;
- ZeroTier::_multiplexer_lock.lock();
- try {
- std::map::iterator un_iter = ZeroTier::unmap.find(fd);
- if (un_iter != ZeroTier::unmap.end()) {
- ZeroTier::unmap.erase(un_iter);
- }
- std::map*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
- if (fd_iter != ZeroTier::fdmap.end()) {
- ZeroTier::fdmap.erase(fd_iter);
- }
- }
- catch( ... ) {
- DEBUG_ERROR("unable to remove virtual socket");
- handle_general_failure();
- err = -1;
- }
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+ DEBUG_EXTRA("fd=%d", fd);
+ int err = 0;
+ ZeroTier::_multiplexer_lock.lock();
+ try {
+ std::map::iterator un_iter = ZeroTier::unmap.find(fd);
+ if (un_iter != ZeroTier::unmap.end()) {
+ ZeroTier::unmap.erase(un_iter);
+ }
+ std::map*>::iterator fd_iter = ZeroTier::fdmap.find(fd);
+ if (fd_iter != ZeroTier::fdmap.end()) {
+ ZeroTier::fdmap.erase(fd_iter);
+ }
+ }
+ catch( ... ) {
+ DEBUG_ERROR("unable to remove virtual socket");
+ handle_general_failure();
+ err = -1;
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
int add_unassigned_virt_socket(int fd, ZeroTier::VirtualSocket *vs)
{
- int err = 0;
- ZeroTier::_multiplexer_lock.lock();
- try {
- std::map::iterator un_iter = ZeroTier::unmap.find(fd);
- if (un_iter == ZeroTier::unmap.end()) {
- ZeroTier::unmap[fd] = vs;
- }
- else {
- DEBUG_ERROR("fd=%d already contained in map", fd);
- handle_general_failure();
- }
- }
- catch( ... ) {
- DEBUG_ERROR("unable to add virtual socket");
- handle_general_failure();
- err = -1;
- }
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+ DEBUG_EXTRA("fd=%d, vs=%p", fd, vs);
+ int err = 0;
+ ZeroTier::_multiplexer_lock.lock();
+ try {
+ std::map::iterator un_iter = ZeroTier::unmap.find(fd);
+ if (un_iter == ZeroTier::unmap.end()) {
+ ZeroTier::unmap[fd] = vs;
+ }
+ else {
+ DEBUG_ERROR("fd=%d already contained in map", fd);
+ handle_general_failure();
+ }
+ }
+ catch( ... ) {
+ DEBUG_ERROR("unable to add virtual socket");
+ handle_general_failure();
+ err = -1;
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
int del_unassigned_virt_socket(int fd)
{
- int err = 0;
- ZeroTier::_multiplexer_lock.lock();
- try {
- std::map::iterator un_iter = ZeroTier::unmap.find(fd);
- if (un_iter != ZeroTier::unmap.end()) {
- ZeroTier::unmap.erase(un_iter);
- }
- }
- catch( ... ) {
- DEBUG_ERROR("unable to remove virtual socket");
- handle_general_failure();
- err = -1;
- }
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+ DEBUG_EXTRA("fd=%d", fd);
+ int err = 0;
+ ZeroTier::_multiplexer_lock.lock();
+ try {
+ std::map::iterator un_iter = ZeroTier::unmap.find(fd);
+ if (un_iter != ZeroTier::unmap.end()) {
+ ZeroTier::unmap.erase(un_iter);
+ }
+ }
+ catch( ... ) {
+ DEBUG_ERROR("unable to remove virtual socket");
+ handle_general_failure();
+ err = -1;
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
int add_assigned_virt_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
{
- int err = 0;
- ZeroTier::_multiplexer_lock.lock();
- try {
- std::map*>::iterator fd_iter;
- fd_iter = ZeroTier::fdmap.find(fd);
- if (fd_iter == ZeroTier::fdmap.end()) {
- ZeroTier::fdmap[fd] = new std::pair(vs, tap);
- }
- else {
- DEBUG_ERROR("fd=%d already contained in > map", fd);
- handle_general_failure();
- }
- }
- catch( ... ) {
- DEBUG_ERROR("unable to add virtual socket");
- handle_general_failure();
- err = -1;
- }
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+ DEBUG_EXTRA("tap=%p, vs=%p, fd=%d", tap, vs, fd);
+ int err = 0;
+ ZeroTier::_multiplexer_lock.lock();
+ try {
+ std::map*>::iterator fd_iter;
+ fd_iter = ZeroTier::fdmap.find(fd);
+ if (fd_iter == ZeroTier::fdmap.end()) {
+ ZeroTier::fdmap[fd] = new std::pair(vs, tap);
+ }
+ else {
+ DEBUG_ERROR("fd=%d already contained in > map", fd);
+ handle_general_failure();
+ }
+ }
+ catch( ... ) {
+ DEBUG_ERROR("unable to add virtual socket");
+ handle_general_failure();
+ err = -1;
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
int del_assigned_virt_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd)
{
- int err = 0;
- ZeroTier::_multiplexer_lock.lock();
- try {
- std::map*>::iterator fd_iter;
- fd_iter = ZeroTier::fdmap.find(fd);
- if (fd_iter != ZeroTier::fdmap.end()) {
- ZeroTier::fdmap.erase(fd_iter);
- }
- }
- catch( ... ) {
- DEBUG_ERROR("unable to remove virtual socket");
- handle_general_failure();
- err = -1;
- }
- ZeroTier::_multiplexer_lock.unlock();
- return err;
+ DEBUG_EXTRA("tap=%p, vs=%p, fd=%d", tap, vs, fd);
+ int err = 0;
+ ZeroTier::_multiplexer_lock.lock();
+ try {
+ std::map*>::iterator fd_iter;
+ fd_iter = ZeroTier::fdmap.find(fd);
+ if (fd_iter != ZeroTier::fdmap.end()) {
+ ZeroTier::fdmap.erase(fd_iter);
+ }
+ }
+ catch( ... ) {
+ DEBUG_ERROR("unable to remove virtual socket");
+ handle_general_failure();
+ err = -1;
+ }
+ ZeroTier::_multiplexer_lock.unlock();
+ return err;
}
std::pair *get_assigned_virtual_pair(int fd)
{
- ZeroTier::_multiplexer_lock.lock();
- std::pair *p = ZeroTier::fdmap[fd];
- ZeroTier::_multiplexer_lock.unlock();
- return p;
+ ZeroTier::_multiplexer_lock.lock();
+ std::pair *p = ZeroTier::fdmap[fd];
+ ZeroTier::_multiplexer_lock.unlock();
+ return p;
}
void disableTaps()
{
- ZeroTier::_vtaps_lock.lock();
- for (int i=0; i_enabled = false;
- }
- ZeroTier::_vtaps_lock.unlock();
+ ZeroTier::_vtaps_lock.lock();
+ for (int i=0; i_enabled = false;
+ }
+ ZeroTier::_vtaps_lock.unlock();
}
int zts_get_device_id_from_file(const char *filepath, char *devID) {
- std::string fname("identity.public");
- std::string fpath(filepath);
- if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
- std::string oldid;
- ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
- memcpy(devID, oldid.c_str(), 10); // first 10 bytes of file
- return 0;
- }
- return -1;
+ std::string fname("identity.public");
+ std::string fpath(filepath);
+ if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
+ std::string oldid;
+ ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
+ memcpy(devID, oldid.c_str(), 10); // first 10 bytes of file
+ return 0;
+ }
+ return -1;
}
// Starts a ZeroTier service in the background
void *zts_start_service(void *thread_id)
{
- DEBUG_INFO("homeDir=%s", ZeroTier::homeDir.c_str());
- // Where network .conf files will be stored
- ZeroTier::netDir = ZeroTier::homeDir + "/networks.d";
- ZeroTier::zt1Service = (ZeroTier::OneService *)0;
- // Construct path for network config and supporting service files
- if (ZeroTier::homeDir.length()) {
- std::vector hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
- std::string ptmp;
- if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) {
- ptmp.push_back(ZT_PATH_SEPARATOR);
- }
- for (std::vector::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) {
- if (ptmp.length() > 0) {
- ptmp.push_back(ZT_PATH_SEPARATOR);
- }
- ptmp.append(*pi);
- if ((*pi != ".")&&(*pi != "..")) {
- if (ZeroTier::OSUtils::mkdir(ptmp) == false) {
- DEBUG_ERROR("home path does not exist, and could not create");
- handle_general_failure();
- perror("error\n");
- }
- }
- }
- }
- else {
- DEBUG_ERROR("homeDir is empty, could not construct path");
- handle_general_failure();
- return NULL;
- }
+ DEBUG_INFO("homeDir=%s", ZeroTier::homeDir.c_str());
+ // Where network .conf files will be stored
+ ZeroTier::netDir = ZeroTier::homeDir + "/networks.d";
+ ZeroTier::zt1Service = (ZeroTier::OneService *)0;
+ // Construct path for network config and supporting service files
+ if (ZeroTier::homeDir.length()) {
+ std::vector hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
+ std::string ptmp;
+ if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) {
+ ptmp.push_back(ZT_PATH_SEPARATOR);
+ }
+ for (std::vector::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) {
+ if (ptmp.length() > 0) {
+ ptmp.push_back(ZT_PATH_SEPARATOR);
+ }
+ ptmp.append(*pi);
+ if ((*pi != ".")&&(*pi != "..")) {
+ if (ZeroTier::OSUtils::mkdir(ptmp) == false) {
+ DEBUG_ERROR("home path does not exist, and could not create");
+ handle_general_failure();
+ perror("error\n");
+ }
+ }
+ }
+ }
+ else {
+ DEBUG_ERROR("homeDir is empty, could not construct path");
+ handle_general_failure();
+ return NULL;
+ }
- // Generate random port for new service instance
- unsigned int randp = 0;
- ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));
- // TODO: Better port random range selection
- int servicePort = 9000 + (randp % 1000);
- for (;;) {
- ZeroTier::zt1Service = ZeroTier::OneService::newInstance(ZeroTier::homeDir.c_str(),servicePort);
- switch(ZeroTier::zt1Service->run()) {
- case ZeroTier::OneService::ONE_STILL_RUNNING:
- case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
- break;
- case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
- DEBUG_ERROR("ZTO service port = %d", servicePort);
- DEBUG_ERROR("fatal error: %s",ZeroTier::zt1Service->fatalErrorMessage().c_str());
- break;
- case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
- delete ZeroTier::zt1Service;
- ZeroTier::zt1Service = (ZeroTier::OneService *)0;
- std::string oldid;
- ZeroTier::OSUtils::readFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
- + "identity.secret").c_str(),oldid);
- if (oldid.length()) {
- ZeroTier::OSUtils::writeFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
- + "identity.secret.saved_after_collision").c_str(),oldid);
- ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
- + "identity.secret").c_str());
- ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
- + "identity.public").c_str());
- }
- }
- continue; // restart!
- }
- break; // terminate loop -- normally we don't keep restarting
- }
- delete ZeroTier::zt1Service;
- ZeroTier::zt1Service = (ZeroTier::OneService *)0;
- return NULL;
+ // Generate random port for new service instance
+ unsigned int randp = 0;
+ ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));
+ // TODO: Better port random range selection
+ int servicePort = 9000 + (randp % 1000);
+ for (;;) {
+ ZeroTier::zt1Service = ZeroTier::OneService::newInstance(ZeroTier::homeDir.c_str(),servicePort);
+ switch(ZeroTier::zt1Service->run()) {
+ case ZeroTier::OneService::ONE_STILL_RUNNING:
+ case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
+ break;
+ case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
+ DEBUG_ERROR("ZTO service port = %d", servicePort);
+ DEBUG_ERROR("fatal error: %s",ZeroTier::zt1Service->fatalErrorMessage().c_str());
+ break;
+ case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
+ delete ZeroTier::zt1Service;
+ ZeroTier::zt1Service = (ZeroTier::OneService *)0;
+ std::string oldid;
+ ZeroTier::OSUtils::readFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
+ + "identity.secret").c_str(),oldid);
+ if (oldid.length()) {
+ ZeroTier::OSUtils::writeFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
+ + "identity.secret.saved_after_collision").c_str(),oldid);
+ ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
+ + "identity.secret").c_str());
+ ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
+ + "identity.public").c_str());
+ }
+ }
+ continue; // restart!
+ }
+ break; // terminate loop -- normally we don't keep restarting
+ }
+ delete ZeroTier::zt1Service;
+ ZeroTier::zt1Service = (ZeroTier::OneService *)0;
+ return NULL;
}
void handle_general_failure() {
#ifdef ZT_EXIT_ON_GENERAL_FAIL
- DEBUG_ERROR("exiting (ZT_EXIT_ON_GENERAL_FAIL==1)");
- exit(-1);
+ DEBUG_ERROR("exiting (ZT_EXIT_ON_GENERAL_FAIL==1)");
+ exit(-1);
+#endif
+}
+
+inline unsigned int gettid()
+{
+#ifdef _WIN32
+ return GetCurrentThreadId();
+#elif defined(__unix__)
+ return static_cast(::syscall(__NR_gettid));
+#elif defined(__APPLE__)
+ uint64_t tid64;
+ pthread_threadid_np(NULL, &tid64);
+ return static_cast(tid64);
#endif
}
diff --git a/src/lwIP.cpp b/src/lwIP.cpp
index 5f3c365..08c8473 100644
--- a/src/lwIP.cpp
+++ b/src/lwIP.cpp
@@ -675,13 +675,13 @@ namespace ZeroTier
}
else {
// place a request for the stack to close this VirtualSocket's PCB
- vs->set_state(VS_SHOULD_STOP);
+ vs->set_state(VS_STATE_SHOULD_SHUTDOWN);
// wait for indication of success, this will block if the PCB can't close
while (true) {
sleep(1);
nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
DEBUG_EXTRA("checking closure state... pcb->state=%d", tpcb->state);
- if (vs->get_state() == VS_STOPPED || tpcb->state == CLOSED) {
+ if (vs->get_state() == VS_STATE_CLOSED || tpcb->state == CLOSED) {
return ERR_OK;
}
}
@@ -689,7 +689,7 @@ namespace ZeroTier
}
if (vs->socket_type == SOCK_DGRAM) {
// place a request for the stack to close this VirtualSocket's PCB
- vs->set_state(VS_SHOULD_STOP);
+ vs->set_state(VS_STATE_SHOULD_SHUTDOWN);
}
return err;
}
@@ -742,7 +742,7 @@ namespace ZeroTier
struct pbuf* q = p;
if (p == NULL) {
DEBUG_INFO("p=0x0 for pcb=%p, vs->pcb=%p, this indicates a closure. No need to call tcp_close()", PCB, vs->pcb);
- vs->set_state(VS_SHOULD_STOP);
+ vs->set_state(VS_STATE_SHOULD_SHUTDOWN);
return ERR_ABRT;
}
vs->tap->_tcpconns_m.lock();
@@ -953,9 +953,8 @@ namespace ZeroTier
DEBUG_INFO("fd=%d, vs=%p, pcb=%p", vs->app_fd, vs, PCB, vs->pcb);
}
-
// Handle PCB closure requests (set in lwip_Close())
- if (vs->get_state() == VS_SHOULD_STOP) {
+ if (vs->get_state() == VS_STATE_SHOULD_SHUTDOWN) {
DEBUG_EXTRA("closing pcb=%p, fd=%d, vs=%p", PCB, vs->app_fd, vs);
int err = 0;
errno = 0;
@@ -996,7 +995,7 @@ namespace ZeroTier
err = -1;
}
else {
- vs->set_state(VS_STOPPED); // success
+ vs->set_state(VS_STATE_CLOSED); // success
}
}
}
@@ -1097,9 +1096,9 @@ namespace ZeroTier
/* Lingers on a close() if data is present. */
if (optname == SO_LINGER)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ // we do this at the VirtualSocket layer since lwIP's raw API doesn't currently have a way to do this
+ vs->optflags &= VS_OPT_SO_LINGER;
+ return 0;
}
/* Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int
value. This is a Boolean option. */
@@ -1356,7 +1355,12 @@ namespace ZeroTier
}
/* If set, disable the Nagle algorithm. */
if (optname == TCP_NODELAY) {
- pcb->flags |= TF_NODELAY;
+ int enable_nagle = *((const int*)optval);
+ if (enable_nagle == true) {
+ tcp_nagle_enable(pcb);
+ } else {
+ tcp_nagle_disable(pcb);
+ }
return 0;
}
/* Enable quickack mode if set or disable quickack mode if cleared. */
@@ -1436,12 +1440,13 @@ namespace ZeroTier
errno = ENOPROTOOPT;
return -1;
}
- /* Lingers on a close() if data is present. */
+ /* Get SO_LINGER flag value */
if (optname == SO_LINGER)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ // we do this at the VirtualSocket layer since lwIP's raw API doesn't currently have a way to do this
+ optval_tmp = (vs->optflags & VS_OPT_SO_LINGER);
+ memcpy(optval, &optval_tmp, *optlen);
+ return 0;
}
/* Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int
value. This is a Boolean option. */
@@ -1695,9 +1700,9 @@ namespace ZeroTier
errno = ENOPROTOOPT;
err = -1;
}
- /* If set, disable the Nagle algorithm. */
+ /* Get value of Nagle algorithm flag */
if (optname == TCP_NODELAY) {
- optval_tmp = pcb->flags & TF_NODELAY;
+ optval_tmp = tcp_nagle_disabled(pcb);
memcpy(optval, &optval_tmp, *optlen);
err = 0;
}
diff --git a/src/lwIP.hpp b/src/lwIP.hpp
index ae4d468..e4be8a4 100644
--- a/src/lwIP.hpp
+++ b/src/lwIP.hpp
@@ -193,106 +193,109 @@ namespace ZeroTier {
class VirtualTap;
class VirtualSocket;
+ /**
+ * lwIP network stack driver class
+ */
class lwIP
{
public:
- /*
+ /**
* Set up an interface in the network stack for the VirtualTap
*/
void lwip_init_interface(VirtualTap *tap, const InetAddress &ip);
- /*
+ /**
* Returns the number of TCP PCBs currently allocated
*/
static int lwip_num_current_tcp_pcbs();
- /*
+ /**
* Returns the number of UDP PCBs currently allocated
*/
static int lwip_num_current_udp_pcbs();
- /*
+ /**
* Returns the number of RAW PCBs currently allocated
*/
static int lwip_num_current_raw_pcbs();
- /*
+ /**
* Returns the total number of PCBs of any time or state
- */
+ */
int lwip_num_total_pcbs();
- /*
+ /**
* Registers a DNS nameserver with the network stack
*/
int lwip_add_dns_nameserver(struct sockaddr *addr);
- /*
+ /**
* Un-registers a DNS nameserver from the network stack
*/
int lwip_del_dns_nameserver(struct sockaddr *addr);
- /*
+ /**
* Main stack loop
*/
void lwip_loop(VirtualTap *tap);
- /*
+ /**
* Packets from the ZeroTier virtual wire enter the stack here
*/
void lwip_eth_rx(VirtualTap *tap, const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
- /*
+ /**
* Creates a stack-specific "socket" or "VirtualSocket object"
*/
int lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol);
- /*
+ /**
* Connect to remote host via userspace network stack interface - Called from VirtualTap
*/
int lwip_Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
- /*
+ /**
* Bind to a userspace network stack interface - Called from VirtualTap
*/
int lwip_Bind(VirtualTap *tap, VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
- /*
+ /**
* Listen for incoming VirtualSockets - Called from VirtualTap
*/
int lwip_Listen(VirtualSocket *vs, int backlog);
- /*
+ /**
* Accept an incoming VirtualSocket - Called from VirtualTap
*/
VirtualSocket* lwip_Accept(VirtualSocket *vs);
- /*
+ /**
* Read from RX buffer to application - Called from VirtualTap
*/
int lwip_Read(VirtualSocket *vs, bool lwip_invoked);
- /*
+ /**
* Write to userspace network stack - Called from VirtualTap
*/
int lwip_Write(VirtualSocket *vs, void *data, ssize_t len);
- /*
+ /**
* Close a VirtualSocket - Called from VirtualTap
*/
int lwip_Close(VirtualSocket *vs);
- /*
+ /**
* Shuts down some aspect of a VirtualSocket - Called from VirtualTap
*/
int lwip_Shutdown(VirtualSocket *vs, int how);
- /*
+ /**
* Sets a property of a socket
*/
static int lwip_setsockopt(VirtualSocket *vs, int level, int optname, const void *optval, socklen_t optlen);
- /*
+ /**
* Gets a property of a socket
*/
static int lwip_getsockopt(VirtualSocket *vs, int level, int optname, void *optval, socklen_t *optlen);
@@ -301,37 +304,37 @@ namespace ZeroTier {
//static void netif_status_callback(struct netif *nif);
- /*
+ /**
* Callback for handling received UDP packets (already processed by network stack)
*/
static err_t lwip_cb_tcp_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
- /*
+ /**
* Callback for handling accepted connection
*/
static err_t lwip_cb_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
- /*
+ /**
* Callback for handling received TCP packets (already processed by stack)
*/
static void lwip_cb_udp_recved(void * arg, struct udp_pcb * upcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);
- /*
+ /**
* Callback for handling errors from within the network stack
*/
static void lwip_cb_err(void *arg, err_t err);
- /*
+ /**
* Callback for handling periodic background tasks
*/
static err_t lwip_cb_poll(void* arg, struct tcp_pcb *PCB);
- /*
+ /**
* Callback for handling confirmation of sent packets
*/
static err_t lwip_cb_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
- /*
+ /**
* Callback for handling successful connections
*/
static err_t lwip_cb_connected(void *arg, struct tcp_pcb *PCB, err_t err);
diff --git a/src/picoTCP.cpp b/src/picoTCP.cpp
index 02c768d..84b196c 100644
--- a/src/picoTCP.cpp
+++ b/src/picoTCP.cpp
@@ -236,9 +236,7 @@ namespace ZeroTier {
while (tap->_run)
{
tap->_phy.poll(ZT_PHY_POLL_INTERVAL);
- //_picostack_driver_lock.lock();
pico_stack_tick();
- //_picostack_driver_lock.unlock();
tap->Housekeeping();
}
}
@@ -375,40 +373,15 @@ namespace ZeroTier {
{
VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs);
if (vs == NULL) {
- DEBUG_ERROR("s->priv yielded no valid vs");
- handle_general_failure();
+ DEBUG_EXTRA("vs == NULL");
return;
}
- Mutex::Lock _l(vs->_tx_m);
- if (vs == NULL) {
- DEBUG_ERROR("invalid VirtualSocket");
- handle_general_failure();
+ if (vs->picosock != s) {
+ DEBUG_ERROR("vs->picosock != s, bad callback");
return;
}
- int txsz = vs->TXbuf->count();
- if (txsz <= 0)
- return;
- //DEBUG_INFO("TXbuf->count()=%d", vs->TXbuf->count());
-
- int r, max_write_len = std::min(std::min(txsz, ZT_SDK_MTU),ZT_STACK_SOCKET_WR_MAX);
- if ((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) {
- DEBUG_ERROR("unable to write to pico_socket=%p, err=%d, pico_err=%d, %s",
- vs->picosock, r, pico_err, beautify_pico_error(pico_err));
- handle_general_failure();
- return;
- }
- if (vs->socket_type == SOCK_STREAM) {
- DEBUG_TRANS("len=%5d buf_len=%13d [VSTXBF --> NSPICO] proto=0x%04x (TCP)", r, vs->TXbuf->count(), PICO_PROTO_TCP);
- }
- if (r == 0) {
- // DEBUG_ERROR("err=%d, pico_err=%d, %s", r, pico_err, beautify_pico_error(pico_err));
- // This is a peciliarity of the picoTCP network stack, if we receive no error code, and the size of
- // the byte stream written is 0, this is an indication that the buffer for this pico_socket is too small
- // DEBUG_ERROR("pico_socket buffer is too small (adjust ZT_STACK_SOCKET_TX_SZ, ZT_STACK_SOCKET_RX_SZ)");
- // handle_general_failure();
- }
- if (r>0)
- vs->TXbuf->consume(r);
+ // we will get the vs->TXBuf->get_buf() reference from within pico_Write
+ picostack->pico_Write(vs, NULL, vs->TXbuf->count());
}
void picoTCP::pico_cb_socket_ev(uint16_t ev, struct pico_socket *s)
@@ -423,7 +396,6 @@ namespace ZeroTier {
if (ev & PICO_SOCK_EV_FIN) {
DEBUG_EXTRA("PICO_SOCK_EV_FIN (socket closed), picosock=%p", s);
//DEBUG_EXTRA("PICO_SOCK_EV_FIN (socket closed), picosock=%p, vs=%p, app_fd=%d, sdk_fd=%d", s, vs, vs->app_fd, vs->sdk_fd);
- //vs->closure_ts = std::time(nullptr);
}
// PICO_SOCK_EV_ERR - triggered when an error occurs.
@@ -439,26 +411,30 @@ namespace ZeroTier {
// allowed to send new data until a local shutdown or close is initiated. PicoTCP is able to
// keep the VirtualSocket half-open (only for sending) after the FIN packet has been received,
// allowing new data to be sent in the TCP CLOSE WAIT state.
- if (ev & PICO_SOCK_EV_CLOSE) {
- if ((err = pico_socket_close(s)) < 0) {
- DEBUG_ERROR("pico_socket_close()=%d, pico_err=%d, %s", err, pico_err, beautify_pico_error(pico_err));
- }
- DEBUG_EXTRA("PICO_SOCK_EV_CLOSE (socket closure) err=%d (%s), picosock=%p", pico_err, beautify_pico_error(pico_err), s);
- //DEBUG_EXTRA("PICO_SOCK_EV_CLOSE (socket closure) err=%d, picosock=%p, vs=%p, app_fd=%d, sdk_fd=%d", err, s, vs, vs->app_fd, vs->sdk_fd);
- //vs->closure_ts = std::time(nullptr);
- return;
- }
-
- // --- handle non-error events ---
VirtualBindingPair *vbp = (VirtualBindingPair*)(s->priv);
if (vbp == NULL) {
- DEBUG_ERROR("s->priv yielded no valid VirtualBindingPair");
+ DEBUG_ERROR("s->priv yielded no valid vbp");
handle_general_failure();
return;
}
VirtualTap *tap = static_cast(vbp->tap);
VirtualSocket *vs = static_cast(vbp->vs);
+
+ if (ev & PICO_SOCK_EV_CLOSE) {
+ vs->set_state(VS_STATE_CLOSED);
+ if ((err = pico_socket_shutdown(s, PICO_SHUT_RDWR)) < 0) {
+ DEBUG_ERROR("error while shutting down socket");
+ }
+ if ((err = pico_socket_close(s)) < 0) {
+ DEBUG_ERROR("pico_socket_close()=%d, pico_err=%d, %s", err, pico_err, beautify_pico_error(pico_err));
+ }
+ DEBUG_EXTRA("PICO_SOCK_EV_CLOSE (socket closure) err=%d (%s), picosock=%p", pico_err, beautify_pico_error(pico_err), s);
+ return;
+ }
+
+ // --- handle non-error events ---
+
if (vs == NULL) {
DEBUG_ERROR("invalid VirtualSocket");
handle_general_failure();
@@ -551,6 +527,7 @@ namespace ZeroTier {
int pico_eth_tx(struct pico_device *dev, void *buf, int len)
{
+ //DEBUG_TRANS();
//_picostack_driver_lock.lock();
VirtualTap *tap = static_cast(dev->tap);
if (tap == NULL) {
@@ -573,9 +550,9 @@ namespace ZeroTier {
char flagbuf[32];
memset(&flagbuf, 0, 32);
+/*
struct pico_tcp_hdr *hdr;
void * tcp_hdr_ptr;
-
if (Utils::ntoh(ethhdr->proto) == 0x86dd) { // tcp, ipv6
tcp_hdr_ptr = ðhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR;
}
@@ -610,7 +587,7 @@ namespace ZeroTier {
}
}
}
-
+*/
DEBUG_TRANS("len=%5d dst=%s [%s TX <-- %s] proto=0x%04x %s %s", len, macBuf, nodeBuf, tap->nodeId().c_str(),
Utils::ntoh(ethhdr->proto), beautify_eth_proto_nums(Utils::ntoh(ethhdr->proto)), flagbuf);
}
@@ -625,6 +602,7 @@ namespace ZeroTier {
void picoTCP::pico_eth_rx(VirtualTap *tap, const MAC &from,const MAC &to,unsigned int etherType,
const void *data,unsigned int len)
{
+ //DEBUG_TRANS();
//_picostack_driver_lock.lock();
if (tap == NULL) {
DEBUG_ERROR("invalid tap");
@@ -648,11 +626,11 @@ namespace ZeroTier {
mac.setTo(ethhdr.saddr, 6);
mac.toAddress(tap->_nwid).toString(nodeBuf);
- char flagbuf[32];
- memset(&flagbuf, 0, 32);
+ char flagbuf[64];
+ memset(&flagbuf, 0, 64);
+/*
struct pico_tcp_hdr *hdr;
void * tcp_hdr_ptr;
-
if (etherType == 0x86dd) { // tcp, ipv6
tcp_hdr_ptr = ðhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR;
}
@@ -686,6 +664,7 @@ namespace ZeroTier {
}
}
}
+*/
DEBUG_TRANS("len=%5d src=%s [%s RX --> %s] proto=0x%04x %s %s", len, macBuf, nodeBuf, tap->nodeId().c_str(),
etherType, beautify_eth_proto_nums(etherType), flagbuf);
}
@@ -773,15 +752,6 @@ namespace ZeroTier {
DEBUG_ERROR("unable to set RCVBUF size, err=%d, pico_err=%d, %s",
t_err, pico_err, beautify_pico_error(pico_err));
}
- /*
- if (ZT_SOCK_BEHAVIOR_LINGER) {
- int linger_time_ms = ZT_SOCK_BEHAVIOR_LINGER_TIME;
- if ((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_LINGER, &linger_time_ms)) < 0) {
- DEBUG_ERROR("unable to set LINGER, err=%d, pico_err=%d, %s",
- t_err, pico_err, beautify_pico_error(pico_err));
- }
- }
- */
}
}
*p = psock;
@@ -912,21 +882,27 @@ namespace ZeroTier {
int picoTCP::pico_Write(VirtualSocket *vs, void *data, ssize_t len)
{
int err = 0;
+ void *src_buf = NULL;
// TODO: Add RingBuffer overflow checks
- // DEBUG_INFO("vs=%p, len=%d", vs, len);
+ DEBUG_EXTRA("vs=%p, fd=%d, data=%p, len=%d", vs, vs->app_fd, data, len);
if (vs == NULL) {
DEBUG_ERROR("invalid vs");
handle_general_failure();
return ZT_ERR_GENERAL_FAILURE;
}
Mutex::Lock _l(vs->_tx_m);
- if (len <= 0) {
- DEBUG_ERROR("invalid write length (len=%d)", len);
+ if (vs->picosock == NULL) {
+ DEBUG_ERROR("ps == NULL");
handle_general_failure();
return -1;
}
- if (vs->picosock->state == PICO_SOCKET_STATE_CLOSED) {
- DEBUG_ERROR("socket is CLOSED, this wrpico_cb_tcp_writeite() will fail");
+ if (vs->app_fd <= 0) {
+ DEBUG_EXTRA("invalid fd");
+ handle_general_failure();
+ return -1;
+ }
+ if (vs->picosock->state & PICO_SOCKET_STATE_CLOSED) {
+ DEBUG_ERROR("socket is PICO_SOCKET_STATE_CLOSED, this pico_tcp_write() will fail");
return -1;
}
if (vs == NULL) {
@@ -935,6 +911,16 @@ namespace ZeroTier {
return -1;
}
if (vs->socket_type == SOCK_DGRAM) {
+ if (data == NULL) {
+ DEBUG_ERROR("data == NULL");
+ handle_general_failure();
+ return -1;
+ }
+ if (len <= 0) {
+ DEBUG_ERROR("invalid write len=%d for SOCK_DGRAM", len);
+ handle_general_failure();
+ return -1;
+ }
int r;
if ((r = pico_socket_write(vs->picosock, data, len)) < 0) {
DEBUG_ERROR("unable to write to picosock=%p, err=%d, pico_err=%d, %s",
@@ -944,25 +930,43 @@ namespace ZeroTier {
else {
err = r; // successful write
}
+ if (vs->socket_type == SOCK_DGRAM) {
+ DEBUG_TRANS("len=%5d buf_len=N/A [APPFDS --> NSPICO] proto=0x%04x (UDP)", r, PICO_PROTO_TCP);
+ }
}
if (vs->socket_type == SOCK_STREAM) {
- int original_txsz = vs->TXbuf->count();
- if (original_txsz + len >= ZT_TCP_TX_BUF_SZ) {
- DEBUG_ERROR("txsz=%d, len=%d", original_txsz, len);
- DEBUG_ERROR("TX buffer is too small, try increasing ZT_TCP_TX_BUF_SZ in libzt.h");
- handle_general_failure();
- return ZT_ERR_GENERAL_FAILURE;
- }
- int buf_w = vs->TXbuf->write((const unsigned char*)data, len);
+ if (len > 0 && data != NULL) {
+
+ src_buf = data; // --- Data source: poll loop I/O buffer ---
+
+ // in this case, we've recieved data on the 'data' buffer, add it to TX ringbuffer, then try to handle it from there
+ int original_txsz = vs->TXbuf->count();
+ if (original_txsz + len >= ZT_TCP_TX_BUF_SZ) {
+ DEBUG_ERROR("txsz=%d, len=%d", original_txsz, len);
+ DEBUG_ERROR("TX buffer is too small, try increasing ZT_TCP_TX_BUF_SZ in libzt.h");
+ handle_general_failure();
+ return ZT_ERR_GENERAL_FAILURE;
+ }
+ int buf_w = vs->TXbuf->write((const unsigned char*)data, len);
if (buf_w != len) {
- // because we checked ZT_TCP_TX_BUF_SZ above, this should not happen
- DEBUG_ERROR("TX wrote only %d but expected to write %d", buf_w, len);
- handle_general_failure();
- return ZT_ERR_GENERAL_FAILURE;
+ // because we checked ZT_TCP_TX_BUF_SZ above, this should not happen
+ DEBUG_ERROR("wrote only len=%d but expected to write len=%d", buf_w, len);
+ handle_general_failure();
+ return ZT_ERR_GENERAL_FAILURE;
+ }
+ } else if (len == 0 && data == NULL) {
+ DEBUG_EXTRA("len=0 => write request from poll loop or callback");
+
+ src_buf = vs->TXbuf->get_buf(); // --- Data source: TX ringbuffer ---
+
+ // do nothing, all the data we need is already on the TX ringbuffer
+ } else if (len < 0) {
+ DEBUG_ERROR("invalid write len=%d for SOCK_STREAM", len);
}
+
int txsz = vs->TXbuf->count();
int r, max_write_len = std::min(std::min(txsz, ZT_SDK_MTU),ZT_STACK_SOCKET_WR_MAX);
- if ((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) {
+ if ((r = pico_socket_write(vs->picosock, src_buf, max_write_len)) < 0) {
DEBUG_ERROR("unable to write to picosock=%p, err=%d, pico_err=%d, %s",
vs->picosock, r, pico_err, beautify_pico_error(pico_err));
err = -1;
@@ -972,12 +976,9 @@ namespace ZeroTier {
}
if (r>0) {
vs->TXbuf->consume(r);
- }
- if (vs->socket_type == SOCK_STREAM) {
- DEBUG_TRANS("len=%5d buf_len=%13d [VSTXBF --> NSPICO] proto=0x%04x (TCP)", r, vs->TXbuf->count(), PICO_PROTO_TCP);
- }
- if (vs->socket_type == SOCK_DGRAM) {
- DEBUG_TRANS("len=%5d buf_len= [APPFDS --> NSPICO] proto=0x%04x (UDP)", r, PICO_PROTO_TCP);
+ if (vs->socket_type == SOCK_STREAM) {
+ DEBUG_TRANS("len=%5d buf_len=%13d [VSTXBF --> NSPICO] proto=0x%04x (TCP)", r, vs->TXbuf->count(), PICO_PROTO_TCP);
+ }
}
}
return err;
@@ -985,21 +986,32 @@ namespace ZeroTier {
int picoTCP::pico_Close(VirtualSocket *vs)
{
+ DEBUG_EXTRA();
if (vs == NULL) {
DEBUG_ERROR("invalid vs");
handle_general_failure();
return ZT_ERR_GENERAL_FAILURE;
}
+ if (vs->get_state() == VS_STATE_CLOSED) {
+ DEBUG_EXTRA("socket already in VS_STATE_CLOSED state");
+ return 0;
+ }
+ if (vs->picosock == NULL) {
+ DEBUG_EXTRA("ps == NULL");
+ return 0;
+ }
+ if (vs->picosock->state & PICO_SOCKET_STATE_CLOSED) {
+ DEBUG_EXTRA("ps already closed, ps=%p", vs->picosock);
+ return 0;
+ }
DEBUG_EXTRA("vs=%p, picosock=%p, fd=%d", vs, vs->picosock, vs->app_fd);
if (vs == NULL || vs->picosock == NULL)
return ZT_ERR_GENERAL_FAILURE;
int err = 0;
Mutex::Lock _l(vs->tap->_tcpconns_m);
- if (vs->closure_ts != -1) // it was closed at some point in the past, it'll work itself out
- return ZT_ERR_OK;
if ((err = pico_socket_close(vs->picosock)) < 0) {
errno = pico_err;
- DEBUG_ERROR("error closing pico_socket, err=%d, pico_err=%s, %s",
+ DEBUG_ERROR("error closing pico_socket, err=%d, pico_err=%d, %s",
err, pico_err, beautify_pico_error(pico_err));
}
return err;
@@ -1007,6 +1019,7 @@ namespace ZeroTier {
int picoTCP::pico_Shutdown(VirtualSocket *vs, int how)
{
+ DEBUG_EXTRA("vs=%p, how=%d", vs, how);
int err = 0, mode = 0;
if (how == SHUT_RD) {
mode = PICO_SHUT_RD;
@@ -1069,9 +1082,12 @@ namespace ZeroTier {
/* Lingers on a close() if data is present. */
if (optname == SO_LINGER)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ int linger_time_ms = *((const int*)optval);
+ if ((err = pico_socket_setoption(vs->picosock, PICO_SOCKET_OPT_LINGER, &linger_time_ms)) < 0) {
+ DEBUG_ERROR("unable to set LINGER, err=%d, pico_err=%d, %s",
+ err, pico_err, beautify_pico_error(pico_err));
+ }
+ return err;
}
/* Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int
value. This is a Boolean option. */
@@ -1092,16 +1108,28 @@ namespace ZeroTier {
/* Sets send buffer size. This option takes an int value. */
if (optname == SO_SNDBUF)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ int no_delay = *((const int*)optval);
+ if ((err = pico_socket_setoption(vs->picosock, PICO_SOCKET_OPT_SNDBUF, &no_delay) < 0)) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while setting PICO_SOCKET_OPT_SNDBUF");
+ errno = EINVAL;
+ err = -1;
+ }
+ }
+ return err;
}
/* Sets receive buffer size. This option takes an int value. */
if (optname == SO_RCVBUF)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ int no_delay = *((const int*)optval);
+ if ((err = pico_socket_setoption(vs->picosock, PICO_SOCKET_OPT_RCVBUF, &no_delay) < 0)) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while setting PICO_SOCKET_OPT_RCVBUF");
+ errno = EINVAL;
+ err = -1;
+ }
+ }
+ return err;
}
/* */
if (optname == SO_STYLE)
@@ -1188,29 +1216,31 @@ namespace ZeroTier {
return -1;
}
if (optname == IP_MULTICAST_IF) {
- /*
- if ((err = pico_socket_getoption(p, PICO_TCP_NODELAY, &optval_tmp)) < 0) {
- if (err == PICO_ERR_EINVAL) {
- DEBUG_ERROR("error while disabling Nagle's algorithm");
- errno = ENOPROTOOPT;
- err = -1;
- }
- }
- memcpy(optval, &optval_tmp, *optlen);
- */
// TODO
errno = ENOPROTOOPT;
return -1;
}
if (optname == IP_MULTICAST_LOOP) {
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ int loop = *((const int*)optval);
+ if ((err = pico_socket_setoption(vs->picosock, PICO_IP_MULTICAST_LOOP, &loop) < 0)) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while setting PICO_IP_MULTICAST_TTL");
+ errno = EINVAL;
+ err = -1;
+ }
+ }
+ return err;
}
if (optname == IP_MULTICAST_TTL) {
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ int ttl = *((const int*)optval);
+ if ((err = pico_socket_setoption(vs->picosock, PICO_IP_MULTICAST_TTL, &ttl) < 0)) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while setting PICO_IP_MULTICAST_TTL");
+ errno = EINVAL;
+ err = -1;
+ }
+ }
+ return err;
}
if (optname == IP_NODEFRAG) {
// TODO
@@ -1339,7 +1369,7 @@ namespace ZeroTier {
/* If set, disable the Nagle algorithm. */
if (optname == TCP_NODELAY) {
int no_delay = *((const int*)optval);
- if ((err = pico_socket_setoption(p, PICO_TCP_NODELAY, &no_delay) < 0)) {
+ if ((err = pico_socket_setoption(vs->picosock, PICO_TCP_NODELAY, &no_delay) < 0)) {
if (err == PICO_ERR_EINVAL) {
DEBUG_ERROR("error while disabling Nagle's algorithm");
errno = EINVAL;
@@ -1451,16 +1481,26 @@ namespace ZeroTier {
/* Sets send buffer size. This option takes an int value. */
if (optname == SO_SNDBUF)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ if ((err = pico_socket_getoption(vs->picosock, PICO_SOCKET_OPT_SNDBUF, &optval_tmp)) < 0) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while getting PICO_SOCKET_OPT_SNDBUF");
+ errno = ENOPROTOOPT;
+ err = -1;
+ }
+ }
+ memcpy(optval, &optval_tmp, *optlen);
}
/* Sets receive buffer size. This option takes an int value. */
if (optname == SO_RCVBUF)
{
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ if ((err = pico_socket_getoption(vs->picosock, PICO_SOCKET_OPT_SNDBUF, &optval_tmp)) < 0) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while getting PICO_SOCKET_OPT_RCVBUF");
+ errno = ENOPROTOOPT;
+ err = -1;
+ }
+ }
+ memcpy(optval, &optval_tmp, *optlen);
}
/* */
if (optname == SO_STYLE)
@@ -1552,14 +1592,24 @@ namespace ZeroTier {
return -1;
}
if (optname == IP_MULTICAST_LOOP) {
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ if ((err = pico_socket_getoption(vs->picosock, PICO_IP_MULTICAST_LOOP, &optval_tmp)) < 0) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while getting PICO_IP_MULTICAST_TTL");
+ errno = ENOPROTOOPT;
+ err = -1;
+ }
+ }
+ memcpy(optval, &optval_tmp, *optlen);
}
if (optname == IP_MULTICAST_TTL) {
- // TODO
- errno = ENOPROTOOPT;
- return -1;
+ if ((err = pico_socket_getoption(vs->picosock, PICO_IP_MULTICAST_TTL, &optval_tmp)) < 0) {
+ if (err == PICO_ERR_EINVAL) {
+ DEBUG_ERROR("error while getting PICO_IP_MULTICAST_TTL");
+ errno = ENOPROTOOPT;
+ err = -1;
+ }
+ }
+ memcpy(optval, &optval_tmp, *optlen);
}
if (optname == IP_NODEFRAG) {
// TODO
@@ -1686,21 +1736,7 @@ namespace ZeroTier {
}
/* If set, disable the Nagle algorithm. */
if (optname == TCP_NODELAY) {
-
- /*
-
- TODO:
-
- PICO TCP NODELAY - Nagle algorithm, value casted to (int *) (0 = disabled, 1 = enabled)
- • PICO SOCKET OPT RCVBUF - Read current receive buffer size for the socket
- • PICO SOCKET OPT SNDBUF - Read current receive buffer size for the socket
- • PICO IP MULTICAST IF - (Not supported) Link multicast datagrams are sent from
- • PICO IP MULTICAST TTL - TTL (0-255) of multicast datagrams
- • PICO IP MULTICAST LOOP - Loop back a copy of an outgoing multicast datagram, as long
- as it is a member of the multicast group, or not
-
- */
- if ((err = pico_socket_getoption(p, PICO_TCP_NODELAY, &optval_tmp)) < 0) {
+ if ((err = pico_socket_getoption(vs->picosock, PICO_TCP_NODELAY, &optval_tmp)) < 0) {
if (err == PICO_ERR_EINVAL) {
DEBUG_ERROR("error while disabling Nagle's algorithm");
errno = ENOPROTOOPT;
diff --git a/src/picoTCP.hpp b/src/picoTCP.hpp
index 8ff908d..3de6eb0 100644
--- a/src/picoTCP.hpp
+++ b/src/picoTCP.hpp
@@ -75,12 +75,12 @@
namespace ZeroTier
{
- /*
+ /**
* Send raw frames from the stack to the ZeroTier virtual wire
*/
int pico_eth_tx(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);
@@ -88,137 +88,140 @@ namespace ZeroTier
class VirtualTap;
class VirtualSocket;
+ /**
+ * picoTCP network stack driver class
+ */
class picoTCP
{
public:
- /*
+ /**
* Set up an interface in the network stack for the VirtualTap
*/
bool pico_init_interface(ZeroTier::VirtualTap *tap);
- /*
+ /**
* Register an address with the stack
*/
bool pico_register_address(VirtualTap *tap, const InetAddress &ip);
- /*
+ /**
* Adds a route to the picoTCP device
*/
bool pico_route_add(VirtualTap *tap, const InetAddress &addr, const InetAddress &nm, const InetAddress &gw, int metric);
- /*
+ /**
* Deletes a route from the picoTCP device
*/
bool pico_route_del(VirtualTap *tap, const InetAddress &addr, const InetAddress &nm, int metric);
- /*
+ /**
* Registers a DNS nameserver with the network stack
*/
int pico_add_dns_nameserver(struct sockaddr *addr);
- /*
+ /**
* Un-registers a DNS nameserver from the network stack
*/
int pico_del_dns_nameserver(struct sockaddr *addr);
- /*
+ /**
* Main stack loop
*/
void pico_loop(VirtualTap *tap);
- /*
+ /**
* Read bytes from the stack to the RX buffer (prepare to be read by app)
*/
static void pico_cb_tcp_read(VirtualTap *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(VirtualTap *tap, struct pico_socket *s);
- /*
+ /**
* Write bytes from TX buffer to stack (prepare to be sent to ZT virtual wire)
*/
static void pico_cb_tcp_write(VirtualTap *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_ev(uint16_t ev, struct pico_socket *s);
- /*
+ /**
* Packets from the ZeroTier virtual wire enter the stack here
*/
void pico_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC &to,
unsigned int etherType, const void *data, unsigned int len);
- /*
+ /**
* Creates a stack-specific "socket" or "VirtualSocket object"
*/
int pico_Socket(struct pico_socket **p, int socket_family, int socket_type, int protocol);
- /*
+ /**
* Connect to remote host via userspace network stack interface - Called from VirtualTap
*/
int pico_Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
- /*
+ /**
* Bind to a userspace network stack interface - Called from VirtualTap
*/
int pico_Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen);
- /*
+ /**
* Listen for incoming VirtualSockets - Called from VirtualTap
*/
int pico_Listen(VirtualSocket *vs, int backlog);
- /*
+ /**
* Accept an incoming VirtualSocket - Called from VirtualTap
*/
VirtualSocket* pico_Accept(VirtualSocket *vs);
- /*
+ /**
* Read from RX buffer to application - Called from VirtualTap
*/
int pico_Read(VirtualTap *tap, ZeroTier::PhySocket *sock, VirtualSocket *vs, bool stack_invoked);
- /*
+ /**
* Write to userspace network stack - Called from VirtualTap
*/
int pico_Write(VirtualSocket *vs, void *data, ssize_t len);
- /*
+ /**
* Close a VirtualSocket - Called from VirtualTap
*/
int pico_Close(VirtualSocket *vs);
- /*
+ /**
* Shuts down some aspect of a VirtualSocket - Called from VirtualTap
*/
int pico_Shutdown(VirtualSocket *vs, int how);
- /*
+ /**
* Sets a property of a socket
*/
static int pico_setsockopt(VirtualSocket *vs, int level, int optname, const void *optval, socklen_t optlen);
- /*
+ /**
* Gets a property of a socket
*/
static int pico_getsockopt(VirtualSocket *vs, int level, int optname, void *optval, socklen_t *optlen);
- /*
+ /**
* Converts a pico_err to its most closely-related errno, and sets errno
*/
static int map_pico_err_to_errno(int err);
- /*
+ /**
* Converts picoTCP error codes to pretty string
*/
static char *beautify_pico_error(int err);
- /*
+ /**
* Converts picoTCP socket states into pretty string
*/
static char *beautify_pico_state(int state);
diff --git a/test/selftest.cpp b/test/selftest.cpp
index f97fcfe..ccbc1fd 100644
--- a/test/selftest.cpp
+++ b/test/selftest.cpp
@@ -63,7 +63,7 @@
#define SLAM_INTERVAL 500000 // microseconds
#define WAIT_FOR_TEST_TO_CONCLUDE 0
-#define WAIT_FOR_TRANSMISSION_TO_COMPLETE 5
+#define ARTIFICIAL_SOCKET_LINGER 1
#define STR_SIZE 32
@@ -130,6 +130,19 @@
// If running a native instance to test against, use system calls
#if defined(__NATIVETEST__)
+inline unsigned int gettid()
+{
+#ifdef _WIN32
+ return GetCurrentThreadId();
+#elif defined(__unix__)
+ return static_cast(::syscall(__NR_gettid));
+#elif defined(__APPLE__)
+ uint64_t tid64;
+ pthread_threadid_np(NULL, &tid64);
+ return static_cast(tid64);
+#endif
+}
+
#define SOCKET socket
#define BIND bind
#define LISTEN listen
@@ -334,7 +347,7 @@ void tcp_client_4(TCP_UNIT_TEST_SIG_4)
{
std::string testname = "tcp_client_4";
std::string msg = "tcp_cs_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "connect to remote host with IPv4 address, write string, read string, compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -367,6 +380,7 @@ void tcp_client_4(TCP_UNIT_TEST_SIG_4)
r = READ(fd, rbuf, len);
DEBUG_TEST("Sent : %s", msg.c_str());
DEBUG_TEST("Received : %s", rbuf);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
*passed = (w == len && r == len && !err) && !strcmp(rbuf, msg.c_str());
@@ -381,7 +395,7 @@ void tcp_server_4(TCP_UNIT_TEST_SIG_4)
{
std::string testname = "tcp_server_4";
std::string msg = "tcp_cs_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "accept connection with IPv4 address, read string, write string, compare.\n");
int w=0, r=0, fd, client_fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -426,6 +440,7 @@ void tcp_server_4(TCP_UNIT_TEST_SIG_4)
r = READ(client_fd, rbuf, len);
w = WRITE(client_fd, rbuf, len);
DEBUG_TEST("Received : %s, r=%d, w=%d", rbuf, r, w);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
err = CLOSE(client_fd);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
@@ -441,7 +456,7 @@ void tcp_client_6(TCP_UNIT_TEST_SIG_6)
{
std::string testname = "tcp_client_6";
std::string msg = "tcp_cs_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "connect to remote host with IPv6 address, write string, read string, compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -473,6 +488,7 @@ void tcp_client_6(TCP_UNIT_TEST_SIG_6)
w = WRITE(fd, msg.c_str(), len);
r = READ(fd, rbuf, len);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
@@ -489,7 +505,7 @@ void tcp_server_6(TCP_UNIT_TEST_SIG_6)
{
std::string testname = "tcp_server_6";
std::string msg = "tcp_cs_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "accept connection with IPv6 address, read string, write string, compare.\n");
int w=0, r=0, fd, client_fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -537,6 +553,7 @@ void tcp_server_6(TCP_UNIT_TEST_SIG_6)
r = READ(client_fd, rbuf, sizeof rbuf);
w = WRITE(client_fd, rbuf, len);
DEBUG_TEST("Received : %s", rbuf);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
err = CLOSE(client_fd);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
@@ -554,7 +571,7 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
{
std::string testname = "udp_client_4";
std::string msg = "udp_cs_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv4 address, send string until response is seen. compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -590,6 +607,7 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4)
// rx
r = RECVFROM(fd, rbuf, STR_SIZE, 0, (struct sockaddr *)&saddr, (socklen_t *)&serverlen);
if (r == strlen(msg.c_str())) {
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
DEBUG_TEST("%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
@@ -610,7 +628,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4)
{
std::string testname = "udp_server_4";
std::string msg = "udp_cs_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv4 address, read single string, send many responses. compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -654,6 +672,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4)
break;
}
}
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
DEBUG_TEST("%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
@@ -671,7 +690,7 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6)
{
std::string testname = "udp_client_6";
std::string msg = "udp_cs_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv6 address, send string until response is seen. compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -711,7 +730,7 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6)
r = RECVFROM(fd, rbuf, len, 0, (struct sockaddr *)&saddr, (socklen_t *)&serverlen);
if (r == len) {
DEBUG_TEST("[2] complete");
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
DEBUG_TEST("%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
@@ -731,7 +750,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
{
std::string testname = "udp_server_6";
std::string msg = "udp_cs_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv6 address, read single string, send many responses. compare.\n");
int r, w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -779,8 +798,8 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6)
break;
}
}
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
- //err = CLOSE(fd);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
+ err = CLOSE(fd);
DEBUG_TEST("[3/3] complete, %s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
sprintf(details, "%s, err=%d, r=%d, w=%d", testname.c_str(), err, r, w);
DEBUG_TEST("Sent : %s", msg.c_str());
@@ -805,7 +824,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
{
std::string testname = "tcp_client_sustained_4";
std::string msg = "tcp_sustained_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "connect to remote host with IPv4 address, exchange a sequence of packets, check order.\n");
int n=0, w=0, r=0, fd, err;
char *rxbuf = (char*)malloc(cnt*sizeof(char));
@@ -831,7 +850,9 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
while (wrem) {
int next_write = std::min(4096, wrem);
signal(SIGPIPE, SIG_IGN);
+ DEBUG_TEST("writing...");
n = WRITE(fd, &txbuf[w], next_write);
+ DEBUG_TEST("wrote=%d", n);
if (n > 0) {
w += n;
wrem -= n;
@@ -856,7 +877,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4)
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
// Compare RX and TX buffer and detect mismatches
bool match = true;
@@ -887,7 +908,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
{
std::string testname = "tcp_client_sustained_6";
std::string msg = "tcp_sustained_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "connect to remote host with IPv6 address, exchange a sequence of packets, check order.\n");
int n=0, w=0, r=0, fd, err;
char *rxbuf = (char*)malloc(cnt*sizeof(char));
@@ -936,7 +957,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6)
}
long int rx_tf = get_now_ts();
DEBUG_TEST("read=%d", r);
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
// Compare RX and TX buffer and detect mismatches
bool match = true;
@@ -967,7 +988,7 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
{
std::string testname = "tcp_server_sustained_4";
std::string msg = "tcp_sustained_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "accept connection from host with IPv4 address, exchange a sequence of packets, check order.\n");
int n=0, w=0, r=0, fd, client_fd, err;
char *rxbuf = (char*)malloc(cnt*sizeof(char));
@@ -1028,6 +1049,7 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
err = CLOSE(client_fd);
// Compute time deltas and transfer rates
@@ -1037,7 +1059,6 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4)
float rx_rate = (float)cnt / (float)rx_dt;
sprintf(details, "%s, n=%d, tx_dt=%.2f, rx_dt=%.2f, r=%d, w=%d, tx_rate=%.2f MB/s, rx_rate=%.2f MB/s",
testname.c_str(), cnt, tx_dt, rx_dt, r, w, (tx_rate / float(ONE_MEGABYTE) ), (rx_rate / float(ONE_MEGABYTE) ));
-
*passed = (r == cnt && w == cnt && err>=0);
}
free(rxbuf);
@@ -1051,7 +1072,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
{
std::string testname = "tcp_server_sustained_6";
std::string msg = "tcp_sustained_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "accept connection from host with IPv6 address, exchange a sequence of packets, check order.\n");
int n=0, w=0, r=0, fd, client_fd, err;
char *rxbuf = (char*)malloc(cnt*sizeof(char));
@@ -1115,6 +1136,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6)
}
long int tx_tf = get_now_ts();
DEBUG_TEST("wrote=%d", w);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
err = CLOSE(client_fd);
// Compute time deltas and transfer rates
@@ -1138,7 +1160,7 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4)
{
std::string testname = "udp_client_sustained_4";
std::string msg = "udp_sustained_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv4 address, TX n-datagrams\n");
int w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -1169,7 +1191,7 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4)
DEBUG_ERROR("error sending packet, err=%d", errno);
}
}
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, w=%d", testname.c_str(), cnt, err, w);
sprintf(details, "%s, n=%d, err=%d, w=%d", testname.c_str(), cnt, err, w);
@@ -1186,7 +1208,7 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4)
{
std::string testname = "udp_server_sustained_4";
std::string msg = "udp_sustained_4";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv4 address, RX (n/x)-datagrams\n");
int r, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -1218,8 +1240,7 @@ void udp_server_sustained_4(UDP_UNIT_TEST_SIG_4)
DEBUG_TEST("received DGRAM from %s : %d", inet_ntoa(in4->sin_addr), ntohs(in4->sin_port));
DEBUG_TEST("sending DGRAM(s) to %s : %d", inet_ntoa(remote_addr->sin_addr), ntohs(remote_addr->sin_port));
}
-
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
//err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
sprintf(details, "%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
@@ -1235,7 +1256,7 @@ void udp_client_sustained_6(UDP_UNIT_TEST_SIG_6)
{
std::string testname = "udp_client_sustained_6";
std::string msg = "udp_sustained_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv6 address, TX n-datagrams\n");
int w, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -1267,7 +1288,7 @@ void udp_client_sustained_6(UDP_UNIT_TEST_SIG_6)
DEBUG_ERROR("error sending packet, err=%d", errno);
}
}
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, w=%d", testname.c_str(), cnt, err, w);
sprintf(details, "%s, n=%d, err=%d, w=%d", testname.c_str(), cnt, err, w);
@@ -1284,7 +1305,7 @@ void udp_server_sustained_6(UDP_UNIT_TEST_SIG_6)
{
std::string testname = "udp_server_sustained_6";
std::string msg = "udp_sustained_6";
- fprintf(stderr, "\n\n%s\n", testname.c_str());
+ fprintf(stderr, "\n\n%s (ts=%lu)\n", testname.c_str(), get_now_ts);
fprintf(stderr, "bind to interface with IPv6 address, RX (n/x)-datagrams\n");
int r, fd, err, len = strlen(msg.c_str());
char rbuf[STR_SIZE];
@@ -1316,8 +1337,8 @@ void udp_server_sustained_6(UDP_UNIT_TEST_SIG_6)
//DEBUG_TEST("received DGRAM from %s : %d", inet_ntoa(in6->sin6_addr), ntohs(in6->sin6_port));
//DEBUG_TEST("sending DGRAM(s) to %s : %d", inet_ntoa(remote_addr->sin6_addr), ntohs(remote_addr->sin6_port));
}
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
- //err = CLOSE(fd);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
+ err = CLOSE(fd);
DEBUG_TEST("%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
sprintf(details, "%s, n=%d, err=%d, r=%d", testname.c_str(), cnt, err, r);
DEBUG_TEST("Received : %s", rbuf);
@@ -1485,7 +1506,7 @@ void tcp_perf_tx_echo_4(TCP_UNIT_TEST_SIG_4)
float rate = (float)tot / (float)ts_delta;
sprintf(details, "%s, tot=%d, dt=%.2f, rate=%.2f MB/s", msg.c_str(), tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
*passed = (tot == cnt && !err) ? PASSED : FAILED;
}
@@ -1553,7 +1574,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4)
float rate = (float)tot / (float)ts_delta;
sprintf(details, "%s, tot=%d, dt=%.2f, rate=%.2f MB/s", msg.c_str(), tot, ts_delta, (rate / float(ONE_MEGABYTE) ));
- sleep(WAIT_FOR_TRANSMISSION_TO_COMPLETE);
+ sleep(ARTIFICIAL_SOCKET_LINGER);
err = CLOSE(fd);
*passed = (tot == cnt && !err) ? PASSED : FAILED;
}
@@ -1567,6 +1588,7 @@ void tcp_perf_rx_echo_4(TCP_UNIT_TEST_SIG_4)
int obscure_api_test(bool *passed)
{
+ int err = -1;
fprintf(stderr, "\n\nobscure API test\n\n");
/*
@@ -1610,52 +1632,71 @@ int obscure_api_test(bool *passed)
// TODO: write an ipv6 version of the above ^^^
*/
+/*
+int levels[] = {
+ IPPROTO_TCP,
+ IPPROTO_UDP,
+ IPPROTO_IP
+ };
+ int num_levels = sizeof(levels) / sizeof(int);
- // ---
- // Disable Nagle's Algorithm on a socket (TCP_NODELAY)
- int level = IPPROTO_TCP;
- int optname = TCP_NODELAY;
- int optval = 1;
- socklen_t flag_len = sizeof(optval);
- int fd = SOCKET(AF_INET, SOCK_STREAM, 0);
- DEBUG_TEST("setting level=%d, optname=%d, optval=%d...", level, optname, optval);
- int err = SETSOCKOPT(fd, level, optname, (char *)&optval, sizeof(int));
- if (err < 0) {
- DEBUG_ERROR("error while setting optval on socket");
- *passed = false;
- err = -1;
- }
- optval = -99; // set junk value to test against
- if ((err = GETSOCKOPT(fd, level, optname, &optval, &flag_len)) < 0) {
- DEBUG_ERROR("error while getting the optval");
- *passed = false;
- err = -1;
- }
- DEBUG_TEST("flag_len=%d", flag_len);
- if (optval <= 0) {
- DEBUG_ERROR("incorrect optval=%d (from getsockopt)", optval);
- *passed = false;
- err = -1;
- } else {
- DEBUG_TEST("correctly read optval=%d, now reversing it", optval);
- if (optval > 0) { // TODO: what should be expected for each platform? Should this mirror them?
- optval = 0;
- DEBUG_TEST("setting level=%d, optname=%d, optval=%d...", level, optname, optval);
- if ((err = SETSOCKOPT(fd, level, optname, (char *) &optval, (socklen_t)sizeof(int))) < 0) {
- DEBUG_ERROR("error while setting on socket");
- *passed = false;
- err = -1;
- }
- else {
- DEBUG_TEST("success");
- *passed = true;
- }
- } else {
- DEBUG_ERROR("the optval wasn't set correctly");
+ int optnames[] = {
+ TCP_NODELAY,
+ SO_LINGER
+ };
+ int num_optnames = sizeof(optnames) / sizeof(int);
+
+
+ for (int i=0; i 0) { // TODO: what should be expected for each platform? Should this mirror them?
+ optval = 0;
+ DEBUG_TEST("setting level=%d, optname=%d, optval=%d...", level, optname, optval);
+ if ((err = SETSOCKOPT(fd, level, optname, (char *) &optval, (socklen_t)sizeof(int))) < 0) {
+ DEBUG_ERROR("error while setting on socket");
+ *passed = false;
+ err = -1;
+ }
+ else {
+ DEBUG_TEST("success");
+ *passed = true;
+ }
+ } else {
+ DEBUG_ERROR("the optval wasn't set correctly");
+ *passed = false;
+ err = -1;
+ }
+ }
}
+ */
return err;
}
@@ -2468,18 +2509,23 @@ for (int i=0; i