standardization pass: conditional statement spacing, removed NULL shorthand making checks more explicit

This commit is contained in:
Joseph Henry
2017-09-14 13:17:37 -07:00
parent 476f162a19
commit 7c586471f4
10 changed files with 603 additions and 634 deletions

View File

@@ -77,7 +77,6 @@
#if defined(NS_DEBUG) #if defined(NS_DEBUG)
#define DEBUG_LWIP(fmt, args...) fprintf(stderr, ZT_YEL "LWIP : %17s:%5d:%25s: " fmt \ #define DEBUG_LWIP(fmt, args...) fprintf(stderr, ZT_YEL "LWIP : %17s:%5d:%25s: " fmt \
ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
#define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL "STACK: %17s:%5d:%25s: " fmt \ #define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL "STACK: %17s:%5d:%25s: " fmt \
ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
#else #else
@@ -116,8 +115,6 @@
"INFO : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args) "INFO : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ATTN(fmt, args...) fprintf(stderr, ZT_CYN \ #define DEBUG_ATTN(fmt, args...) fprintf(stderr, ZT_CYN \
"ATTN : %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) "ATTN : %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
#define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL \
"STACK: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)
#define DEBUG_BLANK(fmt, args...) fprintf(stderr, \ #define DEBUG_BLANK(fmt, args...) fprintf(stderr, \
"INFO : %17s:%5d:" fmt "\n", ZT_FILENAME, __LINE__, ##args) "INFO : %17s:%5d:" fmt "\n", ZT_FILENAME, __LINE__, ##args)
#endif #endif

View File

@@ -43,7 +43,6 @@
* will be set to standard values. Override anything you dont like! * will be set to standard values. Override anything you dont like!
*/ */
#include "lwip/debug.h" #include "lwip/debug.h"
#include "Debug.hpp"
// IP Protocol version // IP Protocol version
#if defined(LIBZT_IPV6) #if defined(LIBZT_IPV6)

View File

@@ -27,13 +27,12 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <net/if_arp.h> #include <net/if_arp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h>
#include <algorithm>
#include <utility> #include <utility>
#include <sys/poll.h> #include <sys/poll.h>
#include <stdint.h> #include <stdint.h>
#include <utility> #include <utility>
#include <string>
#include "VirtualTap.hpp" #include "VirtualTap.hpp"
#include "libzt.h" #include "libzt.h"
@@ -196,12 +195,14 @@ namespace ZeroTier {
const void *data,unsigned int len) const void *data,unsigned int len)
{ {
#if defined(STACK_PICO) #if defined(STACK_PICO)
if(picostack) if (picostack) {
picostack->pico_eth_rx(this,from,to,etherType,data,len); picostack->pico_eth_rx(this,from,to,etherType,data,len);
}
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
if(lwipstack) if (lwipstack) {
lwipstack->lwip_eth_rx(this,from,to,etherType,data,len); lwipstack->lwip_eth_rx(this,from,to,etherType,data,len);
}
#endif #endif
} }
@@ -286,18 +287,20 @@ namespace ZeroTier {
{ {
if (sock) { if (sock) {
VirtualSocket *vs = (VirtualSocket*)uptr; VirtualSocket *vs = (VirtualSocket*)uptr;
if(vs) if (vs) {
Close(vs); Close(vs);
} }
} }
}
void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len) void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
{ {
//DEBUG_ATTN("sock->fd=%d", _phy.getDescriptor(sock)); //DEBUG_ATTN("sock->fd=%d", _phy.getDescriptor(sock));
VirtualSocket *vs = (VirtualSocket*)*uptr; VirtualSocket *vs = (VirtualSocket*)*uptr;
if(!vs) if (vs == NULL) {
return; return;
if(len){ }
if (len > 0) {
Write(vs, data, len); Write(vs, data, len);
} }
return; return;
@@ -561,7 +564,7 @@ namespace ZeroTier {
// respective protocol control structures // respective protocol control structures
int VirtualTap::Close(VirtualSocket *vs) { int VirtualTap::Close(VirtualSocket *vs) {
int err = 0; int err = 0;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid VirtualSocket"); DEBUG_ERROR("invalid VirtualSocket");
return -1; return -1;
} }
@@ -631,8 +634,8 @@ namespace ZeroTier {
found=true; found=true;
} }
} }
if(!found) { if (found == false) {
if(!via_addr.ipsEqual(null_addr)) { if (via_addr.ipsEqual(null_addr) == false) {
DEBUG_INFO("adding route <target=%s, nm=%s, via=%s>", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3)); DEBUG_INFO("adding route <target=%s, nm=%s, via=%s>", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3));
routes.push_back(std::pair<ZeroTier::InetAddress,ZeroTier::InetAddress>(target_addr, nm)); routes.push_back(std::pair<ZeroTier::InetAddress,ZeroTier::InetAddress>(target_addr, nm));
routeAdd(target_addr, nm, via_addr); routeAdd(target_addr, nm, via_addr);
@@ -650,7 +653,7 @@ namespace ZeroTier {
found=true; found=true;
} }
} }
if(!found) { if (found == false) {
DEBUG_INFO("removing route to <target=%s>", routes[i].first.toString(ipbuf), routes[i].second.toString(ipbuf2)); DEBUG_INFO("removing route to <target=%s>", routes[i].first.toString(ipbuf), routes[i].second.toString(ipbuf2));
routes.erase(routes.begin() + i); routes.erase(routes.begin() + i);
routeDelete(routes[i].first, routes[i].second); routeDelete(routes[i].first, routes[i].second);

View File

@@ -35,6 +35,7 @@
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
#include <stdint.h> #include <stdint.h>
#include <errno.h>
#include "Constants.hpp" #include "Constants.hpp"
#include "MulticastGroup.hpp" #include "MulticastGroup.hpp"
@@ -58,7 +59,8 @@
#include "pico_ipv6.h" #include "pico_ipv6.h"
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
#include "lwIP.hpp" #include "lwip/netif.h"
struct netif;
#endif #endif
namespace ZeroTier { namespace ZeroTier {

View File

@@ -116,11 +116,13 @@ namespace ZeroTier {
void zts_start(const char *path) void zts_start(const char *path)
{ {
if(ZeroTier::zt1Service) if (ZeroTier::zt1Service) {
return; return;
}
#if defined(STACK_PICO) #if defined(STACK_PICO)
if(ZeroTier::picostack) if (ZeroTier::picostack) {
return; return;
}
ZeroTier::picostack = new ZeroTier::picoTCP(); ZeroTier::picostack = new ZeroTier::picoTCP();
pico_stack_init(); pico_stack_init();
#endif #endif
@@ -128,8 +130,9 @@ void zts_start(const char *path)
ZeroTier::lwipstack = new ZeroTier::lwIP(); ZeroTier::lwipstack = new ZeroTier::lwIP();
lwip_init(); lwip_init();
#endif #endif
if(path) if (path) {
ZeroTier::homeDir = path; ZeroTier::homeDir = path;
}
pthread_t service_thread; pthread_t service_thread;
pthread_create(&service_thread, NULL, zts_start_service, NULL); pthread_create(&service_thread, NULL, zts_start_service, NULL);
} }
@@ -137,10 +140,10 @@ void zts_start(const char *path)
void zts_simple_start(const char *path, const char *nwid) void zts_simple_start(const char *path, const char *nwid)
{ {
zts_start(path); zts_start(path);
while(!zts_running()) { while(zts_running() == false) {
nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
} }
while(1) { while(true) {
try { try {
zts_join(nwid); zts_join(nwid);
break; break;
@@ -150,7 +153,7 @@ void zts_simple_start(const char *path, const char *nwid)
handle_general_failure(); handle_general_failure();
} }
} }
while(!zts_has_address(nwid)) { while(zts_has_address(nwid) == false) {
nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL);
} }
} }
@@ -165,11 +168,11 @@ void zts_stop() {
void zts_join(const char * nwid) { void zts_join(const char * nwid) {
if (ZeroTier::zt1Service) { if (ZeroTier::zt1Service) {
std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf"; std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
if(!ZeroTier::OSUtils::mkdir(ZeroTier::netDir)) { if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) {
DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str()); DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str());
handle_general_failure(); handle_general_failure();
} }
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) { if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str()); DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
handle_general_failure(); handle_general_failure();
} }
@@ -186,12 +189,12 @@ void zts_join(const char * nwid) {
void zts_join_soft(const char * filepath, const char * nwid) { void zts_join_soft(const char * filepath, const char * nwid) {
std::string net_dir = std::string(filepath) + "/networks.d/"; std::string net_dir = std::string(filepath) + "/networks.d/";
std::string confFile = net_dir + std::string(nwid) + ".conf"; std::string confFile = net_dir + std::string(nwid) + ".conf";
if(!ZeroTier::OSUtils::mkdir(net_dir)) { if (ZeroTier::OSUtils::mkdir(net_dir) == false) {
DEBUG_ERROR("unable to create: %s", net_dir.c_str()); DEBUG_ERROR("unable to create: %s", net_dir.c_str());
handle_general_failure(); handle_general_failure();
} }
if(!ZeroTier::OSUtils::fileExists(confFile.c_str(),false)) { if (ZeroTier::OSUtils::fileExists(confFile.c_str(), false) == false) {
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) { if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str()); DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
handle_general_failure(); handle_general_failure();
} }
@@ -199,9 +202,10 @@ void zts_join_soft(const char * filepath, const char * nwid) {
} }
void zts_leave(const char * nwid) { void zts_leave(const char * nwid) {
if(ZeroTier::zt1Service) if (ZeroTier::zt1Service) {
ZeroTier::zt1Service->leave(nwid); ZeroTier::zt1Service->leave(nwid);
} }
}
void zts_leave_soft(const char * filepath, const char * nwid) { void zts_leave_soft(const char * filepath, const char * nwid) {
std::string net_dir = std::string(filepath) + "/networks.d/"; std::string net_dir = std::string(filepath) + "/networks.d/";
@@ -236,7 +240,6 @@ int zts_get_device_id(char *devID) {
{ {
std::string fname("identity.public"); std::string fname("identity.public");
std::string fpath(ZeroTier::homeDir); std::string fpath(ZeroTier::homeDir);
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) { if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
std::string oldid; std::string oldid;
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid); ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
@@ -248,7 +251,7 @@ int zts_get_device_id(char *devID) {
} }
int zts_running() { int zts_running() {
return !ZeroTier::zt1Service ? false : ZeroTier::zt1Service->isRunning(); return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
} }
int zts_has_ipv4_address(const char *nwid) int zts_has_ipv4_address(const char *nwid)
@@ -333,11 +336,13 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
} }
unsigned long zts_get_peer_count() { unsigned long zts_get_peer_count() {
if(ZeroTier::zt1Service) if (ZeroTier::zt1Service) {
return ZeroTier::zt1Service->getNode()->peers()->peerCount; return ZeroTier::zt1Service->getNode()->peers()->peerCount;
else }
else {
return 0; return 0;
} }
}
int zts_get_peer_address(char *peer, const char *devID) { int zts_get_peer_address(char *peer, const char *devID) {
if (ZeroTier::zt1Service) { if (ZeroTier::zt1Service) {
@@ -393,7 +398,7 @@ int zts_socket(ZT_SOCKET_SIG) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if(!ZeroTier::zt1Service) { if (ZeroTier::zt1Service == NULL) {
DEBUG_ERROR("cannot create socket, no service running. call zts_start() first."); DEBUG_ERROR("cannot create socket, no service running. call zts_start() first.");
errno = EMFILE; // could also be ENFILE errno = EMFILE; // could also be ENFILE
return -1; return -1;
@@ -415,7 +420,7 @@ int zts_socket(ZT_SOCKET_SIG) {
#if defined(STACK_PICO) #if defined(STACK_PICO)
struct pico_socket *p; struct pico_socket *p;
err = ZeroTier::picostack->pico_Socket(&p, socket_family, socket_type, protocol); err = ZeroTier::picostack->pico_Socket(&p, socket_family, socket_type, protocol);
if(!err && p) { if (err == false && p) {
ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket(); ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket();
vs->socket_family = socket_family; vs->socket_family = socket_family;
vs->socket_type = socket_type; vs->socket_type = socket_type;
@@ -513,18 +518,18 @@ int zts_connect(ZT_CONNECT_SIG) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
if(!ZeroTier::zt1Service) { if (ZeroTier::zt1Service == NULL) {
DEBUG_ERROR("service not started. call zts_start(path) first"); DEBUG_ERROR("service not started. call zts_start(path) first");
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid socket, unable to locate VirtualSocket for fd=%d", fd); DEBUG_ERROR("invalid socket, unable to locate VirtualSocket for fd=%d", fd);
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
if(!addr) { if (addr == NULL) {
DEBUG_ERROR("invalid address for fd=%d", fd); DEBUG_ERROR("invalid address for fd=%d", fd);
errno = EINVAL; errno = EINVAL;
return -1; return -1;
@@ -541,7 +546,7 @@ int zts_connect(ZT_CONNECT_SIG) {
ZeroTier::InetAddress inet; ZeroTier::InetAddress inet;
sockaddr2inet(vs->socket_family, addr, &inet); sockaddr2inet(vs->socket_family, addr, &inet);
ZeroTier::VirtualTap *tap = getTapByAddr(&inet); ZeroTier::VirtualTap *tap = getTapByAddr(&inet);
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("no route to host, could not find appropriate VirtualTap for fd=%d", fd); DEBUG_ERROR("no route to host, could not find appropriate VirtualTap for fd=%d", fd);
errno = ENETUNREACH; errno = ENETUNREACH;
return -1; return -1;
@@ -586,20 +591,18 @@ int zts_connect(ZT_CONNECT_SIG) {
return -1; return -1;
} }
blocking = !(f_err & O_NONBLOCK); blocking = !(f_err & O_NONBLOCK);
if(!blocking) { if (blocking == false) {
errno = EINPROGRESS; // can't connect immediately errno = EINPROGRESS; // can't connect immediately
err = -1; err = -1;
} }
if(blocking) { if (blocking == true) {
bool complete = false; bool complete = false;
while(true) while(true) {
{
// FIXME: locking and unlocking so often might cause significant performance overhead while outgoing VirtualSockets // FIXME: locking and unlocking so often might cause significant performance overhead while outgoing VirtualSockets
// are being established (also applies to accept()) // are being established (also applies to accept())
nanosleep((const struct timespec[]) {{0, (ZT_CONNECT_RECHECK_DELAY * 1000000)}}, NULL); nanosleep((const struct timespec[]) {{0, (ZT_CONNECT_RECHECK_DELAY * 1000000)}}, NULL);
tap->_tcpconns_m.lock(); tap->_tcpconns_m.lock();
for(int i=0; i<tap->_VirtualSockets.size(); i++) for(int i=0; i<tap->_VirtualSockets.size(); i++) {
{
#if defined(STACK_PICO) #if defined(STACK_PICO)
if (tap->_VirtualSockets[i]->state == PICO_ERR_ECONNRESET) { if (tap->_VirtualSockets[i]->state == PICO_ERR_ECONNRESET) {
errno = ECONNRESET; errno = ECONNRESET;
@@ -644,21 +647,19 @@ int zts_bind(ZT_BIND_SIG) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
if(!ZeroTier::zt1Service) { if (ZeroTier::zt1Service == NULL) {
DEBUG_ERROR("service not started. call zts_start(path) first"); DEBUG_ERROR("service not started. call zts_start(path) first");
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no VirtualSocket for fd=%d", fd); DEBUG_ERROR("no VirtualSocket for fd=%d", fd);
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
// detect local interface binds // detect local interface binds
ZeroTier::VirtualTap *tap = NULL; ZeroTier::VirtualTap *tap = NULL;
if (vs->socket_family == AF_INET) { if (vs->socket_family == AF_INET) {
struct sockaddr_in *in4 = (struct sockaddr_in *)addr; struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
if (in4->sin_addr.s_addr == INADDR_ANY) { if (in4->sin_addr.s_addr == INADDR_ANY) {
@@ -687,10 +688,10 @@ int zts_bind(ZT_BIND_SIG) {
ZeroTier::InetAddress inet; ZeroTier::InetAddress inet;
sockaddr2inet(vs->socket_family, addr, &inet); sockaddr2inet(vs->socket_family, addr, &inet);
if(!tap) if (tap == NULL) {
tap = getTapByAddr(&inet); tap = getTapByAddr(&inet);
}
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("no matching interface to bind to, could not find appropriate VirtualTap for fd=%d", fd); DEBUG_ERROR("no matching interface to bind to, could not find appropriate VirtualTap for fd=%d", fd);
errno = ENETUNREACH; errno = ENETUNREACH;
return -1; return -1;
@@ -701,8 +702,8 @@ int zts_bind(ZT_BIND_SIG) {
#endif #endif
tap->addVirtualSocket(vs); tap->addVirtualSocket(vs);
err = tap->Bind(vs, addr, addrlen); err = tap->Bind(vs, addr, addrlen);
vs->tap = tap;
if (err == 0) { // success if (err == 0) { // success
vs->tap = tap;
del_unassigned_virtual_socket(fd); del_unassigned_virtual_socket(fd);
add_assigned_virtual_socket(tap, vs, fd); add_assigned_virtual_socket(tap, vs, fd);
} }
@@ -734,21 +735,21 @@ int zts_listen(ZT_LISTEN_SIG) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
if(!ZeroTier::zt1Service) { if (ZeroTier::zt1Service == NULL) {
DEBUG_ERROR("service not started. call zts_start(path) first"); DEBUG_ERROR("service not started. call zts_start(path) first");
errno = EACCES; errno = EACCES;
return -1; return -1;
} }
std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = get_assigned_virtual_pair(fd); std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = get_assigned_virtual_pair(fd);
ZeroTier::_multiplexer_lock.lock(); ZeroTier::_multiplexer_lock.lock();
if(!p) { if (p == NULL) {
DEBUG_ERROR("unable to locate VirtualSocket pair. did you bind?"); DEBUG_ERROR("unable to locate VirtualSocket pair. did you bind?");
errno = EDESTADDRREQ; errno = EDESTADDRREQ;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = p->first; ZeroTier::VirtualSocket *vs = p->first;
ZeroTier::VirtualTap *tap = p->second; ZeroTier::VirtualTap *tap = p->second;
if(!tap || !vs) { if (tap == NULL || vs == NULL) {
DEBUG_ERROR("unable to locate tap interface for file descriptor"); DEBUG_ERROR("unable to locate tap interface for file descriptor");
errno = EBADF; errno = EBADF;
return -1; return -1;
@@ -786,13 +787,13 @@ int zts_accept(ZT_ACCEPT_SIG) {
errno = EINVAL; // TODO, not actually a valid error for this function errno = EINVAL; // TODO, not actually a valid error for this function
return -1; return -1;
} }
if(!can_provision_new_socket(SOCK_STREAM)) { if (can_provision_new_socket(SOCK_STREAM) == false) {
DEBUG_ERROR("cannot provision additional socket due to limitation of network stack"); DEBUG_ERROR("cannot provision additional socket due to limitation of network stack");
errno = EMFILE; errno = EMFILE;
return -1; return -1;
} }
std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = get_assigned_virtual_pair(fd); std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = get_assigned_virtual_pair(fd);
if(!p) { if (p == NULL) {
DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?"); DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?");
errno = EBADF; errno = EBADF;
err = -1; err = -1;
@@ -800,7 +801,6 @@ int zts_accept(ZT_ACCEPT_SIG) {
else { else {
ZeroTier::VirtualSocket *vs = p->first; ZeroTier::VirtualSocket *vs = p->first;
ZeroTier::VirtualTap *tap = p->second; ZeroTier::VirtualTap *tap = p->second;
// BLOCKING: loop and keep checking until we find a newly accepted VirtualSocket // BLOCKING: loop and keep checking until we find a newly accepted VirtualSocket
int f_err, blocking = 1; int f_err, blocking = 1;
if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) { if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) {
@@ -811,8 +811,8 @@ int zts_accept(ZT_ACCEPT_SIG) {
blocking = !(f_err & O_NONBLOCK); blocking = !(f_err & O_NONBLOCK);
} }
ZeroTier::VirtualSocket *accepted_vs; ZeroTier::VirtualSocket *accepted_vs;
if(!err) { if (err == false) {
if(!blocking) { // non-blocking if (blocking == false) { // non-blocking
DEBUG_EXTRA("EWOULDBLOCK, assuming non-blocking mode"); DEBUG_EXTRA("EWOULDBLOCK, assuming non-blocking mode");
errno = EWOULDBLOCK; errno = EWOULDBLOCK;
err = -1; err = -1;
@@ -920,7 +920,7 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG)
} }
#if defined(STACK_PICO) #if defined(STACK_PICO)
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid fd=%d", fd); DEBUG_ERROR("invalid fd=%d", fd);
errno = EBADF; errno = EBADF;
return -1; return -1;
@@ -942,7 +942,7 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG)
return err; return err;
#endif #endif
#if defined(STACK_LWIP) #if defined(STACK_LWIP)
// TODO
#endif #endif
return 0; return 0;
} }
@@ -1009,7 +1009,7 @@ int zts_getpeername(ZT_GETPEERNAME_SIG)
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
errno = ENOTCONN; errno = ENOTCONN;
return -1; return -1;
} }
@@ -1078,13 +1078,13 @@ int zts_close(ZT_CLOSE_SIG)
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
if(!ZeroTier::zt1Service) { if (ZeroTier::zt1Service == NULL) {
DEBUG_ERROR("cannot close socket. service not started. call zts_start(path) first"); DEBUG_ERROR("cannot close socket. service not started. call zts_start(path) first");
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no vs found for fd=%d", fd); DEBUG_ERROR("no vs found for fd=%d", fd);
handle_general_failure(); handle_general_failure();
errno = EBADF; errno = EBADF;
@@ -1097,25 +1097,6 @@ int zts_close(ZT_CLOSE_SIG)
delete vs; delete vs;
vs = NULL; vs = NULL;
return err; return err;
/*
// check if socket is blocking
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);
}
if(blocking) {
for(int i=0; i<ZT_SDK_CLTIME; i++) {
if(vs->TXbuf->count() == 0)
break;
usleep(ZT_API_CHECK_INTERVAL * 1000);
}
}
*/
} }
int zts_poll(ZT_POLL_SIG) int zts_poll(ZT_POLL_SIG)
@@ -1163,7 +1144,7 @@ int zts_ioctl(ZT_IOCTL_SIG)
{ {
struct ifreq *ifr = (struct ifreq *)argp; struct ifreq *ifr = (struct ifreq *)argp;
ZeroTier::VirtualTap *tap = getTapByName(ifr->ifr_name); ZeroTier::VirtualTap *tap = getTapByName(ifr->ifr_name);
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("unable to locate tap interface with that name"); DEBUG_ERROR("unable to locate tap interface with that name");
err = -1; err = -1;
errno = EINVAL; errno = EINVAL;
@@ -1185,8 +1166,7 @@ int zts_ioctl(ZT_IOCTL_SIG)
err = 0; err = 0;
} }
} }
else else {
{
DEBUG_INFO("!argp"); DEBUG_INFO("!argp");
} }
#else #else
@@ -1246,24 +1226,22 @@ ssize_t zts_sendto(ZT_SENDTO_SIG)
errno = EMSGSIZE; errno = EMSGSIZE;
return -1; return -1;
} }
if(!buf) { if (buf == NULL) {
DEBUG_ERROR("msg buf is null"); DEBUG_ERROR("msg buf is null");
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no vs found for fd=%x", fd); DEBUG_ERROR("no vs found for fd=%x", fd);
handle_general_failure(); handle_general_failure();
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
ZeroTier::InetAddress iaddr; ZeroTier::InetAddress iaddr;
ZeroTier::VirtualTap *tap; ZeroTier::VirtualTap *tap;
if(vs->socket_type == SOCK_DGRAM) if (vs->socket_type == SOCK_DGRAM) {
{
if (vs->socket_family == AF_INET) if (vs->socket_family == AF_INET)
{ {
char ipstr[INET_ADDRSTRLEN]; char ipstr[INET_ADDRSTRLEN];
@@ -1285,7 +1263,7 @@ ssize_t zts_sendto(ZT_SENDTO_SIG)
} }
// get tap // get tap
tap = getTapByAddr(&iaddr); tap = getTapByAddr(&iaddr);
if(!tap) { if (tap == NULL) {
DEBUG_INFO("SOCK_DGRAM, tap not found"); DEBUG_INFO("SOCK_DGRAM, tap not found");
errno = EDESTADDRREQ; // TODO: double check this is the best errno to report errno = EDESTADDRREQ; // TODO: double check this is the best errno to report
return -1; return -1;
@@ -1378,9 +1356,8 @@ ssize_t zts_send(ZT_SEND_SIG)
errno = EMSGSIZE; errno = EMSGSIZE;
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs for fd=%d", fd); DEBUG_ERROR("invalid vs for fd=%d", fd);
errno = EBADF; errno = EBADF;
return -1; return -1;
@@ -1429,7 +1406,6 @@ ssize_t zts_send(ZT_SEND_SIG)
#endif #endif
err = write(fd, buf, len); err = write(fd, buf, len);
// restore "per-call" flags // restore "per-call" flags
if (flags & MSG_DONTWAIT) { if (flags & MSG_DONTWAIT) {
@@ -1493,7 +1469,7 @@ ssize_t zts_recv(ZT_RECV_SIG)
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs for fd=%d", fd); DEBUG_ERROR("invalid vs for fd=%d", fd);
errno = EBADF; errno = EBADF;
return -1; return -1;
@@ -1591,7 +1567,7 @@ ssize_t zts_recvfrom(ZT_RECVFROM_SIG)
if (len == 0) { if (len == 0) {
return 0; return 0;
} }
if(!buf) { if (buf == NULL) {
DEBUG_ERROR("buf is null"); DEBUG_ERROR("buf is null");
errno = EINVAL; errno = EINVAL;
return -1; return -1;
@@ -1696,7 +1672,7 @@ int zts_shutdown(ZT_SHUTDOWN_SIG)
return -1; return -1;
} }
ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); ZeroTier::VirtualSocket *vs = get_virtual_socket(fd);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs for fd=%d", fd); DEBUG_ERROR("invalid vs for fd=%d", fd);
errno = EBADF; errno = EBADF;
return -1; return -1;
@@ -1752,9 +1728,10 @@ namespace ZeroTier {
} }
// Shuts down ZeroTier service and SOCKS5 Proxy server // Shuts down ZeroTier service and SOCKS5 Proxy server
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1stop(JNIEnv *env, jobject thisObj) { JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1stop(JNIEnv *env, jobject thisObj) {
if(ZeroTier::zt1Service) if (ZeroTier::zt1Service) {
zts_stop(); zts_stop();
} }
}
// Returns whether the ZeroTier service is running // Returns whether the ZeroTier service is running
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_ztjni_1running( JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_ztjni_1running(
@@ -2085,7 +2062,7 @@ ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
} }
} }
// check managed routes // check managed routes
if(!tap) { if (tap == NULL) {
std::vector<ZT_VirtualNetworkRoute> *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid); std::vector<ZT_VirtualNetworkRoute> *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid);
ZeroTier::InetAddress target, nm, via; ZeroTier::InetAddress target, nm, via;
for(int i=0; i<managed_routes->size(); i++) { for(int i=0; i<managed_routes->size(); i++) {
@@ -2110,7 +2087,7 @@ ZeroTier::VirtualTap *getTapByName(char *ifname)
ZeroTier::VirtualTap *s, *tap = nullptr; ZeroTier::VirtualTap *s, *tap = nullptr;
for(int i=0; i<ZeroTier::vtaps.size(); i++) { for(int i=0; i<ZeroTier::vtaps.size(); i++) {
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i]; s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
if(!strcmp(s->_dev.c_str(), ifname)) { if (strcmp(s->_dev.c_str(), ifname) == false) {
tap = s; tap = s;
} }
} }
@@ -2152,7 +2129,7 @@ ZeroTier::VirtualSocket *get_virtual_socket(int fd)
ZeroTier::_multiplexer_lock.lock(); ZeroTier::_multiplexer_lock.lock();
// try to locate in unmapped set // try to locate in unmapped set
ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd]; ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd];
if(!vs) { if (vs == NULL) {
// if not, try to find in mapped set (bind to vtap has been performed) // if not, try to find in mapped set (bind to vtap has been performed)
std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = ZeroTier::fdmap[fd]; std::pair<ZeroTier::VirtualSocket*, ZeroTier::VirtualTap*> *p = ZeroTier::fdmap[fd];
if (p) { if (p) {
@@ -2296,7 +2273,6 @@ void dismantleTaps()
int zts_get_device_id_from_file(const char *filepath, char *devID) { int zts_get_device_id_from_file(const char *filepath, char *devID) {
std::string fname("identity.public"); std::string fname("identity.public");
std::string fpath(filepath); std::string fpath(filepath);
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) { if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
std::string oldid; std::string oldid;
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid); ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
@@ -2307,26 +2283,26 @@ int zts_get_device_id_from_file(const char *filepath, char *devID) {
} }
// Starts a ZeroTier service in the background // Starts a ZeroTier service in the background
void *zts_start_service(void *thread_id) { void *zts_start_service(void *thread_id)
{
DEBUG_INFO("homeDir=%s", ZeroTier::homeDir.c_str()); DEBUG_INFO("homeDir=%s", ZeroTier::homeDir.c_str());
// Where network .conf files will be stored // Where network .conf files will be stored
ZeroTier::netDir = ZeroTier::homeDir + "/networks.d"; ZeroTier::netDir = ZeroTier::homeDir + "/networks.d";
ZeroTier::zt1Service = (ZeroTier::OneService *)0; ZeroTier::zt1Service = (ZeroTier::OneService *)0;
// Construct path for network config and supporting service files // Construct path for network config and supporting service files
if (ZeroTier::homeDir.length()) { if (ZeroTier::homeDir.length()) {
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), std::vector<std::string> hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
ZT_PATH_SEPARATOR_S,"",""));
std::string ptmp; std::string ptmp;
if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) {
ptmp.push_back(ZT_PATH_SEPARATOR); ptmp.push_back(ZT_PATH_SEPARATOR);
}
for(std::vector<std::string>::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) { for(std::vector<std::string>::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) {
if (ptmp.length() > 0) if (ptmp.length() > 0) {
ptmp.push_back(ZT_PATH_SEPARATOR); ptmp.push_back(ZT_PATH_SEPARATOR);
}
ptmp.append(*pi); ptmp.append(*pi);
if ((*pi != ".")&&(*pi != "..")) { if ((*pi != ".")&&(*pi != "..")) {
if (!ZeroTier::OSUtils::mkdir(ptmp)) { if (ZeroTier::OSUtils::mkdir(ptmp) == false) {
DEBUG_ERROR("home path does not exist, and could not create"); DEBUG_ERROR("home path does not exist, and could not create");
handle_general_failure(); handle_general_failure();
perror("error\n"); perror("error\n");

View File

@@ -307,7 +307,7 @@ namespace ZeroTier
const void *data, unsigned int len) const void *data, unsigned int len)
{ {
struct pbuf *p,*q; struct pbuf *p,*q;
if (!tap->_enabled){ if (tap->_enabled == false) {
return; return;
} }
struct eth_hdr ethhdr; struct eth_hdr ethhdr;
@@ -364,7 +364,7 @@ namespace ZeroTier
int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol) int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol)
{ {
if(!can_provision_new_socket(socket_type)) { if (can_provision_new_socket(socket_type) == false) {
DEBUG_ERROR("unable to create socket due to limitation of network stack, PCBs=%d", lwip_num_total_pcbs()); DEBUG_ERROR("unable to create socket due to limitation of network stack, PCBs=%d", lwip_num_total_pcbs());
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@@ -507,7 +507,7 @@ namespace ZeroTier
VirtualSocket* lwIP::lwip_Accept(VirtualSocket *vs) VirtualSocket* lwIP::lwip_Accept(VirtualSocket *vs)
{ {
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid virtual socket"); DEBUG_ERROR("invalid virtual socket");
handle_general_failure(); handle_general_failure();
return NULL; return NULL;
@@ -526,11 +526,11 @@ namespace ZeroTier
{ {
DEBUG_EXTRA("vs=%p", vs); DEBUG_EXTRA("vs=%p", vs);
int err = 0; int err = 0;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no virtual socket"); DEBUG_ERROR("no virtual socket");
return -1; return -1;
} }
if(!lwip_invoked) { if (lwip_invoked == false) {
DEBUG_INFO("!lwip_invoked"); DEBUG_INFO("!lwip_invoked");
vs->tap->_tcpconns_m.lock(); vs->tap->_tcpconns_m.lock();
vs->_rx_m.lock(); vs->_rx_m.lock();
@@ -548,7 +548,7 @@ namespace ZeroTier
if (vs->RXbuf->count() == 0) { if (vs->RXbuf->count() == 0) {
vs->tap->_phy.setNotifyWritable(vs->sock, false); // nothing else to send to the app vs->tap->_phy.setNotifyWritable(vs->sock, false); // nothing else to send to the app
} }
if(!lwip_invoked) { if (lwip_invoked == false) {
vs->tap->_tcpconns_m.unlock(); vs->tap->_tcpconns_m.unlock();
vs->_rx_m.unlock(); vs->_rx_m.unlock();
} }
@@ -558,7 +558,7 @@ namespace ZeroTier
int lwIP::lwip_Write(VirtualSocket *vs, void *data, ssize_t len) int lwIP::lwip_Write(VirtualSocket *vs, void *data, ssize_t len)
{ {
int err = 0; int err = 0;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no virtual socket"); DEBUG_ERROR("no virtual socket");
return -1; return -1;
} }
@@ -568,7 +568,7 @@ namespace ZeroTier
// TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks // TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks
int udp_trans_len = std::min(len, (ssize_t)ZT_MAX_MTU); int udp_trans_len = std::min(len, (ssize_t)ZT_MAX_MTU);
struct pbuf * pb = pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL); struct pbuf * pb = pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL);
if(!pb) { if (pb == NULL) {
DEBUG_ERROR("unable to allocate new pbuf of len=%d", udp_trans_len); DEBUG_ERROR("unable to allocate new pbuf of len=%d", udp_trans_len);
return -1; return -1;
} }
@@ -590,7 +590,7 @@ namespace ZeroTier
if (vs->socket_type == SOCK_STREAM) { if (vs->socket_type == SOCK_STREAM) {
// How much we are currently allowed to write to the VirtualSocket // How much we are currently allowed to write to the VirtualSocket
ssize_t sndbuf = ((struct tcp_pcb*)vs->pcb)->snd_buf; ssize_t sndbuf = ((struct tcp_pcb*)vs->pcb)->snd_buf;
if(!sndbuf) { if (sndbuf == 0) {
// PCB send buffer is full, turn off readability notifications for the // PCB send buffer is full, turn off readability notifications for the
// corresponding PhySocket until lwip_cb_sent() is called and confirms that there is // corresponding PhySocket until lwip_cb_sent() is called and confirms that there is
// now space on the buffer // now space on the buffer
@@ -608,7 +608,7 @@ namespace ZeroTier
if (vs->TXbuf->count() <= 0) { if (vs->TXbuf->count() <= 0) {
err = -1; // nothing to write err = -1; // nothing to write
} }
if(!err) { if (err == ERR_OK) {
int r = std::min((ssize_t)vs->TXbuf->count(), sndbuf); int r = std::min((ssize_t)vs->TXbuf->count(), sndbuf);
// Writes data pulled from the client's socket buffer to LWIP. This merely sends the // Writes data pulled from the client's socket buffer to LWIP. This merely sends the
// data to LWIP to be enqueued and eventually sent to the network. // data to LWIP to be enqueued and eventually sent to the network.
@@ -643,7 +643,7 @@ namespace ZeroTier
int lwIP::lwip_Close(VirtualSocket *vs) int lwIP::lwip_Close(VirtualSocket *vs)
{ {
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return -1; return -1;
@@ -732,7 +732,7 @@ namespace ZeroTier
//DEBUG_INFO(); //DEBUG_INFO();
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
int tot = 0; int tot = 0;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("no virtual socket"); DEBUG_ERROR("no virtual socket");
handle_general_failure(); handle_general_failure();
return ERR_OK; return ERR_OK;
@@ -827,12 +827,12 @@ namespace ZeroTier
{ {
//DEBUG_EXTRA("arg(vs)=%p, pcb=%p, port=%d)", arg, upcb, port); //DEBUG_EXTRA("arg(vs)=%p, pcb=%p, port=%d)", arg, upcb, port);
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid virtual socket"); DEBUG_ERROR("invalid virtual socket");
return; return;
} }
if(!p) { if (p == NULL) {
DEBUG_ERROR("!p"); DEBUG_ERROR("p == NULL");
return; return;
} }
struct pbuf* q = p; struct pbuf* q = p;
@@ -851,10 +851,8 @@ namespace ZeroTier
char udp_payload_buf[ZT_SOCKET_MSG_BUF_SZ]; char udp_payload_buf[ZT_SOCKET_MSG_BUF_SZ];
char *msg_ptr = udp_payload_buf; char *msg_ptr = udp_payload_buf;
int tot_len = 0; int tot_len = 0;
while(p != NULL) while(p != NULL) {
{
if (p->len <= 0) { if (p->len <= 0) {
break; break;
} }
@@ -887,10 +885,10 @@ namespace ZeroTier
{ {
//DEBUG_EXTRA("pcb=%p", PCB); //DEBUG_EXTRA("pcb=%p", PCB);
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
if(!vs){ if (vs == NULL) {
DEBUG_ERROR("invalid vs for PCB=%p, len=%d", PCB, len); DEBUG_ERROR("invalid vs for PCB=%p, len=%d", PCB, len);
} }
if(!(vs->copymode & TCP_WRITE_FLAG_COPY)) { if ((vs->copymode & TCP_WRITE_FLAG_COPY) == false) {
/* /*
From lwIP docs: From lwIP docs:
@@ -926,7 +924,7 @@ namespace ZeroTier
{ {
DEBUG_ATTN("pcb=%p", PCB); DEBUG_ATTN("pcb=%p", PCB);
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid virtual socket"); DEBUG_ERROR("invalid virtual socket");
return -1; return -1;
} }
@@ -941,7 +939,7 @@ namespace ZeroTier
err_t lwIP::lwip_cb_poll(void* arg, struct tcp_pcb *PCB) err_t lwIP::lwip_cb_poll(void* arg, struct tcp_pcb *PCB)
{ {
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return ERR_OK; // TODO: determine appropriate error value, if any return ERR_OK; // TODO: determine appropriate error value, if any
@@ -999,7 +997,7 @@ namespace ZeroTier
void lwIP::lwip_cb_err(void *arg, err_t err) void lwIP::lwip_cb_err(void *arg, err_t err)
{ {
VirtualSocket *vs = (VirtualSocket *)arg; VirtualSocket *vs = (VirtualSocket *)arg;
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("err=%d, invalid virtual socket", err); DEBUG_ERROR("err=%d, invalid virtual socket", err);
errno = -1; errno = -1;
} }

View File

@@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <errno.h>
#include "lwip/mem.h" #include "lwip/mem.h"
#include "lwip/pbuf.h" #include "lwip/pbuf.h"

View File

@@ -101,7 +101,7 @@ namespace ZeroTier {
_picostack_driver_lock.lock(); _picostack_driver_lock.lock();
// give right to vtap to start the stack // give right to vtap to start the stack
// only one stack loop is permitted // only one stack loop is permitted
if(!picodev_initialized) { if (picodev_initialized == false) {
tap->should_start_stack = true; tap->should_start_stack = true;
picodev.send = pico_eth_tx; // tx picodev.send = pico_eth_tx; // tx
picodev.poll = pico_eth_poll; // calls pico_eth_rx picodev.poll = pico_eth_poll; // calls pico_eth_rx
@@ -247,19 +247,18 @@ namespace ZeroTier {
void picoTCP::pico_cb_tcp_read(ZeroTier::VirtualTap *tap, struct pico_socket *s) void picoTCP::pico_cb_tcp_read(ZeroTier::VirtualTap *tap, struct pico_socket *s)
{ {
VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("s->priv yielded no valid vs"); DEBUG_ERROR("s->priv yielded no valid vs");
handle_general_failure(); handle_general_failure();
return; return;
} }
Mutex::Lock _l(vs->_rx_m); Mutex::Lock _l(vs->_rx_m);
if (tap == NULL) {
if(!tap) {
DEBUG_ERROR("invalid tap"); DEBUG_ERROR("invalid tap");
handle_general_failure(); handle_general_failure();
return; return;
} }
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return; return;
@@ -306,19 +305,18 @@ namespace ZeroTier {
{ {
// DEBUG_INFO(); // DEBUG_INFO();
VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("s->priv yielded no valid vs"); DEBUG_ERROR("s->priv yielded no valid vs");
handle_general_failure(); handle_general_failure();
return; return;
} }
Mutex::Lock _l(vs->_rx_m); Mutex::Lock _l(vs->_rx_m);
if (tap == NULL) {
if(!tap) {
DEBUG_ERROR("invalid tap"); DEBUG_ERROR("invalid tap");
handle_general_failure(); handle_general_failure();
return; return;
} }
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return; return;
@@ -376,13 +374,13 @@ namespace ZeroTier {
void picoTCP::pico_cb_tcp_write(VirtualTap *tap, struct pico_socket *s) void picoTCP::pico_cb_tcp_write(VirtualTap *tap, struct pico_socket *s)
{ {
VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("s->priv yielded no valid vs"); DEBUG_ERROR("s->priv yielded no valid vs");
handle_general_failure(); handle_general_failure();
return; return;
} }
Mutex::Lock _l(vs->_tx_m); Mutex::Lock _l(vs->_tx_m);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid VirtualSocket"); DEBUG_ERROR("invalid VirtualSocket");
handle_general_failure(); handle_general_failure();
return; return;
@@ -454,14 +452,14 @@ namespace ZeroTier {
// --- handle non-error events --- // --- handle non-error events ---
VirtualBindingPair *vbp = (VirtualBindingPair*)(s->priv); VirtualBindingPair *vbp = (VirtualBindingPair*)(s->priv);
if(!vbp) { if (vbp == NULL) {
DEBUG_ERROR("s->priv yielded no valid VirtualBindingPair"); DEBUG_ERROR("s->priv yielded no valid VirtualBindingPair");
handle_general_failure(); handle_general_failure();
return; return;
} }
VirtualTap *tap = static_cast<VirtualTap*>(vbp->tap); VirtualTap *tap = static_cast<VirtualTap*>(vbp->tap);
VirtualSocket *vs = static_cast<VirtualSocket*>(vbp->vs); VirtualSocket *vs = static_cast<VirtualSocket*>(vbp->vs);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid VirtualSocket"); DEBUG_ERROR("invalid VirtualSocket");
handle_general_failure(); handle_general_failure();
return; return;
@@ -472,24 +470,21 @@ namespace ZeroTier {
// may now be issued in order to accept the incoming VirtualSocket from a remote host. // may now be issued in order to accept the incoming VirtualSocket from a remote host.
if (ev & PICO_SOCK_EV_CONN) { if (ev & PICO_SOCK_EV_CONN) {
DEBUG_EXTRA("PICO_SOCK_EV_CONN"); DEBUG_EXTRA("PICO_SOCK_EV_CONN");
if(vs->state == ZT_SOCK_STATE_LISTENING) if (vs->state == ZT_SOCK_STATE_LISTENING) {
{
uint16_t port; uint16_t port;
struct pico_socket *client_psock = nullptr; struct pico_socket *client_psock = nullptr;
struct pico_ip4 orig4; struct pico_ip4 orig4;
struct pico_ip6 orig6; struct pico_ip6 orig6;
if (vs->socket_family == AF_INET) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 if (vs->socket_family == AF_INET) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4
client_psock = pico_socket_accept(s, &orig4, &port); client_psock = pico_socket_accept(s, &orig4, &port);
} }
if (vs->socket_family == AF_INET6) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 if (vs->socket_family == AF_INET6) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4
client_psock = pico_socket_accept(s, &orig6, &port); client_psock = pico_socket_accept(s, &orig6, &port);
} }
if(!client_psock) { if (client_psock == NULL) {
DEBUG_ERROR("pico_socket_accept(): pico_socket=%p, pico_err=%d, %s", s, pico_err, beautify_pico_error(pico_err)); DEBUG_ERROR("pico_socket_accept(): pico_socket=%p, pico_err=%d, %s", s, pico_err, beautify_pico_error(pico_err));
return; return;
} }
// Create a new VirtualSocket and add it to the queue, // Create a new VirtualSocket and add it to the queue,
// some time in the future a call to zts_multiplex_accept() will pick up // some time in the future a call to zts_multiplex_accept() will pick up
// this new VirtualSocket, add it to the VirtualSocket list and return its // this new VirtualSocket, add it to the VirtualSocket list and return its
@@ -497,7 +492,6 @@ namespace ZeroTier {
VirtualSocket *new_vs = new VirtualSocket(); VirtualSocket *new_vs = new VirtualSocket();
new_vs->socket_type = SOCK_STREAM; new_vs->socket_type = SOCK_STREAM;
new_vs->picosock = client_psock; new_vs->picosock = client_psock;
// TODO: Condense this // TODO: Condense this
if (vs->socket_family == AF_INET) { if (vs->socket_family == AF_INET) {
char addrstr[INET_ADDRSTRLEN]; char addrstr[INET_ADDRSTRLEN];
@@ -524,7 +518,7 @@ namespace ZeroTier {
inet.fromString(addrstr); inet.fromString(addrstr);
new_vs->tap = getTapByAddr(&inet); // assign to tap based on incoming address new_vs->tap = getTapByAddr(&inet); // assign to tap based on incoming address
} }
if(!new_vs->tap) { if (new_vs->tap == NULL) {
DEBUG_ERROR("no valid VirtualTap could be found"); DEBUG_ERROR("no valid VirtualTap could be found");
handle_general_failure(); handle_general_failure();
return; return;
@@ -559,7 +553,7 @@ namespace ZeroTier {
{ {
//_picostack_driver_lock.lock(); //_picostack_driver_lock.lock();
VirtualTap *tap = static_cast<VirtualTap*>(dev->tap); VirtualTap *tap = static_cast<VirtualTap*>(dev->tap);
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("invalid dev->tap"); DEBUG_ERROR("invalid dev->tap");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -632,7 +626,7 @@ namespace ZeroTier {
const void *data,unsigned int len) const void *data,unsigned int len)
{ {
//_picostack_driver_lock.lock(); //_picostack_driver_lock.lock();
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("invalid tap"); DEBUG_ERROR("invalid tap");
handle_general_failure(); handle_general_failure();
return; return;
@@ -707,7 +701,7 @@ namespace ZeroTier {
int pico_eth_poll(struct pico_device *dev, int loop_score) int pico_eth_poll(struct pico_device *dev, int loop_score)
{ {
VirtualTap *tap = static_cast<VirtualTap*>(dev->tap); VirtualTap *tap = static_cast<VirtualTap*>(dev->tap);
if(!tap) { if (tap == NULL) {
DEBUG_ERROR("invalid dev->tap"); DEBUG_ERROR("invalid dev->tap");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -743,13 +737,12 @@ namespace ZeroTier {
int picoTCP::pico_Socket(struct pico_socket **p, int socket_family, int socket_type, int protocol) int picoTCP::pico_Socket(struct pico_socket **p, int socket_family, int socket_type, int protocol)
{ {
int err = 0; int err = 0;
if(!can_provision_new_socket(socket_type)) { if (can_provision_new_socket(socket_type) == false) {
DEBUG_ERROR("cannot create additional socket, see PICO_MAX_TIMERS. current=%d", pico_ntimers()); DEBUG_ERROR("cannot create additional socket, see PICO_MAX_TIMERS. current=%d", pico_ntimers());
errno = EMFILE; errno = EMFILE;
err = -1; err = -1;
} }
else else {
{
int protocol_version = 0; int protocol_version = 0;
struct pico_socket *psock; struct pico_socket *psock;
if (socket_family == AF_INET) if (socket_family == AF_INET)
@@ -799,7 +792,7 @@ namespace ZeroTier {
int picoTCP::pico_Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen) int picoTCP::pico_Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
{ {
if(!vs || !vs->picosock) { if (vs == NULL || vs->picosock == NULL) {
DEBUG_ERROR("invalid vs or vs->picosock"); DEBUG_ERROR("invalid vs or vs->picosock");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -841,7 +834,7 @@ namespace ZeroTier {
int picoTCP::pico_Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen) int picoTCP::pico_Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen)
{ {
if(!vs || !vs->picosock) { if (vs == NULL || vs->picosock == NULL) {
DEBUG_ERROR("invalid vs or vs->picosock"); DEBUG_ERROR("invalid vs or vs->picosock");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -879,7 +872,7 @@ namespace ZeroTier {
int picoTCP::pico_Listen(VirtualSocket *vs, int backlog) int picoTCP::pico_Listen(VirtualSocket *vs, int backlog)
{ {
if(!vs || !vs->picosock) { if (vs == NULL || vs->picosock == NULL) {
DEBUG_ERROR("invalid vs or vs->picosock"); DEBUG_ERROR("invalid vs or vs->picosock");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -896,7 +889,7 @@ namespace ZeroTier {
VirtualSocket* picoTCP::pico_Accept(VirtualSocket *vs) VirtualSocket* picoTCP::pico_Accept(VirtualSocket *vs)
{ {
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return NULL; return NULL;
@@ -922,7 +915,7 @@ namespace ZeroTier {
int err = 0; int err = 0;
// TODO: Add RingBuffer overflow checks // TODO: Add RingBuffer overflow checks
// DEBUG_INFO("vs=%p, len=%d", vs, len); // DEBUG_INFO("vs=%p, len=%d", vs, len);
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
@@ -937,7 +930,7 @@ namespace ZeroTier {
DEBUG_ERROR("socket is CLOSED, this wrpico_cb_tcp_writeite() will fail"); DEBUG_ERROR("socket is CLOSED, this wrpico_cb_tcp_writeite() will fail");
return -1; return -1;
} }
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid VirtualSocket (len=%d)", len); DEBUG_ERROR("invalid VirtualSocket (len=%d)", len);
handle_general_failure(); handle_general_failure();
return -1; return -1;
@@ -993,13 +986,13 @@ namespace ZeroTier {
int picoTCP::pico_Close(VirtualSocket *vs) int picoTCP::pico_Close(VirtualSocket *vs)
{ {
if(!vs) { if (vs == NULL) {
DEBUG_ERROR("invalid vs"); DEBUG_ERROR("invalid vs");
handle_general_failure(); handle_general_failure();
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
} }
DEBUG_EXTRA("vs=%p, picosock=%p, fd=%d", vs, vs->picosock, vs->app_fd); DEBUG_EXTRA("vs=%p, picosock=%p, fd=%d", vs, vs->picosock, vs->app_fd);
if(!vs || !vs->picosock) if (vs == NULL || vs->picosock == NULL)
return ZT_ERR_GENERAL_FAILURE; return ZT_ERR_GENERAL_FAILURE;
int err = 0; int err = 0;
Mutex::Lock _l(vs->tap->_tcpconns_m); Mutex::Lock _l(vs->tap->_tcpconns_m);