From 5e320fb950f99ac7591eba5f5b10174cc58a4fbd Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Tue, 19 Sep 2017 10:52:59 -0700 Subject: [PATCH] Fixed lwIP driver thread model violation (tcp_close() was being called by application thread) --- include/libzt.h | 7 +- src/VirtualSocket.hpp | 28 +- src/VirtualTap.cpp | 2 +- src/libzt.cpp | 3610 ++++++++++++++++++++--------------------- src/lwIP.cpp | 124 +- src/lwIP.hpp | 2 +- src/picoTCP.cpp | 8 +- src/picoTCP.hpp | 2 +- 8 files changed, 1927 insertions(+), 1856 deletions(-) diff --git a/include/libzt.h b/include/libzt.h index 2274b0d..90e74cb 100644 --- a/include/libzt.h +++ b/include/libzt.h @@ -213,11 +213,6 @@ struct zts_ifreq { #define ZT_HOME_PATH_MAX_LEN 128 #define ZT_MAC_ADDRSTRLEN 18 -#define ZT_SOCK_STATE_NONE 100 -#define ZT_SOCK_STATE_UNHANDLED_CONNECTED 101 -#define ZT_SOCK_STATE_CONNECTED 102 -#define ZT_SOCK_STATE_LISTENING 103 - #define ZT_ERR_OK 0 #define ZT_ERR_GENERAL_FAILURE -88 @@ -594,7 +589,7 @@ namespace ZeroTier extern ZeroTier::lwIP *lwipstack; class VirtualTap; - struct VirtualSocket; + class VirtualSocket; struct InetAddress; } diff --git a/src/VirtualSocket.hpp b/src/VirtualSocket.hpp index b5c378b..7e28295 100644 --- a/src/VirtualSocket.hpp +++ b/src/VirtualSocket.hpp @@ -45,6 +45,13 @@ #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 + namespace ZeroTier { class VirtualTap; @@ -56,13 +63,29 @@ namespace ZeroTier { * function of this object, however I'd like to discourage this since this * object also handles non-connection-based traffic as well. */ - struct VirtualSocket + class VirtualSocket { + private: + int _state = VS_OK; + public: RingBuffer *TXbuf; RingBuffer *RXbuf; - Mutex _tx_m, _rx_m; + Mutex _tx_m, _rx_m, _op_m; PhySocket *sock = NULL; + // State control + void set_state(int state) { + // 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); + _op_m.unlock(); + } + int get_state() { + //DEBUG_EXTRA("GET STATE = %d (vs=%p)", _state, this); + return _state; + } + #if defined(STACK_PICO) struct pico_socket *picosock = NULL; #endif @@ -88,7 +111,6 @@ namespace ZeroTier { int socket_family = 0; int socket_type = 0; int protocol = 0; - int state = 0; // See libzt.h for (ZT_SOCK_STATE_*) int app_fd = 0; // used by app for I/O int sdk_fd = 0; // used by lib for I/O diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index 51b059b..a7690b0 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -719,7 +719,7 @@ namespace ZeroTier { Mutex::Lock _l(_tcpconns_m); std::time_t current_ts = std::time(nullptr); if (current_ts > last_housekeeping_ts + ZT_HOUSEKEEPING_INTERVAL) { - DEBUG_EXTRA(); + // DEBUG_EXTRA(); // update managed routes (add/del from network stacks) ZeroTier::OneService *service = ((ZeroTier::OneService *)zt1ServiceRef); if (service) { diff --git a/src/libzt.cpp b/src/libzt.cpp index 8ea7491..9a3092e 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -80,35 +80,35 @@ extern "C" { namespace ZeroTier { - static ZeroTier::OneService *zt1Service; + 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; + picoTCP *picostack = NULL; #endif #if defined(STACK_LWIP) - lwIP *lwipstack = NULL; + lwIP *lwipstack = NULL; #endif - /* - * VirtualSockets that have been created but not bound to a VirtualTap interface yet - */ - std::map unmap; + /* + * VirtualSockets that have been created but not bound to a VirtualTap interface yet + */ + std::map unmap; - /* - * For fast lookup of VirtualSockets and VirtualTaps via given file descriptor - */ - std::map*> fdmap; + /* + * For fast lookup of VirtualSockets and VirtualTaps via given file descriptor + */ + std::map*> fdmap; - /* - * Virtual tap interfaces, one per virtual network - */ - std::vector vtaps; + /* + * Virtual tap interfaces, 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 +117,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 } /****************************************************************************/ @@ -383,80 +383,80 @@ void zts_disable_http_control_plane() 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. + [ ] [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; - } + 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; + return err; } @@ -464,385 +464,385 @@ int zts_socket(ZT_SOCKET_SIG) { 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. + [ ] [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. + [ ] [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_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 ZT_SOCK_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]->state == PICO_ERR_ECONNRESET) { - errno = ECONNRESET; - DEBUG_ERROR("ECONNRESET"); - err = -1; - } + 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]->state == ZT_SOCK_STATE_UNHANDLED_CONNECTED) { - tap->_VirtualSockets[i]->state = ZT_SOCK_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; } /* 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. + [--] [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 - } - } - } + 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(). + [--] [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. + [ ] [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->state = ZT_SOCK_STATE_LISTENING; - ZeroTier::_multiplexer_lock.unlock(); - 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("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. + [--] [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 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; } @@ -850,19 +850,19 @@ int zts_accept(ZT_ACCEPT_SIG) { 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. + [ ] [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: @@ -870,336 +870,336 @@ 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_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. + 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 + int fd, struct sockaddr *addr, socklen_t *addrlen, int flags */ #if defined(__linux__) int zts_accept4(ZT_ACCEPT4_SIG) { - 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. + [--] [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 fd, int level, int optname, const void *optval, socklen_t optlen */ int zts_setsockopt(ZT_SETSOCKOPT_SIG) { - 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. + [--] [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) { - 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. + [--] [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 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. + [--] [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) { - 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)). + [ ] [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) { - 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)). + [ ] [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) { - errno = 0; - return sethostname(name, len); + errno = 0; + return sethostname(name, len); } /* Linux: - See: http://yarchive.net/comp/linux/close_return_value.html + 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. + [--] [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) { - 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) { - errno = 0; - return poll(fds, nfds, timeout); + errno = 0; + return poll(fds, nfds, timeout); } int zts_select(ZT_SELECT_SIG) { - 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 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; + } + else { + 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. + [ ] [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 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; } @@ -1207,469 +1207,469 @@ int zts_ioctl(ZT_IOCTL_SIG) 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. + [ ] [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 + 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) { - //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: + 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).) + [ ] 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. + (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 + ZT_SEND_SIG int fd, const void *buf, size_t len, int flags */ ssize_t zts_send(ZT_SEND_SIG) { - // 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) { - 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: + 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. + 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. + [ ] 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 + ZT_RECV_SIG int fd, void *buf, size_t len, int flags */ ssize_t zts_recv(ZT_RECV_SIG) { - 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->state != ZT_SOCK_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: + 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. + [ ] [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 + 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) { - //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) { - //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); + //DEBUG_TRANS("fd=%d", fd); + return read(fd, buf, len); } int zts_write(ZT_WRITE_SIG) { - DEBUG_TRANS("fd=%d", fd); - return write(fd, buf, len); + DEBUG_TRANS("fd=%d", fd); + return write(fd, buf, len); } @@ -1677,61 +1677,61 @@ int zts_write(ZT_WRITE_SIG) { 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. + [--] [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 + ZT_SHUTDOWN_SIG int fd, int how */ int zts_shutdown(ZT_SHUTDOWN_SIG) { - 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->state != ZT_SOCK_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 +1744,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 +2013,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 +2078,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,242 +2182,242 @@ 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; + 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; + 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; + 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; + 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; + 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; + 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 } #ifdef __cplusplus } -#endif +#endif \ No newline at end of file diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 89edc06..5f3c365 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -643,51 +643,53 @@ namespace ZeroTier int lwIP::lwip_Close(VirtualSocket *vs) { + // requests to close non-LISTEN PCBs are handled lwip_cb_poll() + int err = -1; if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return -1; } - DEBUG_EXTRA("fd=%d, vs=%p", vs->app_fd, vs); - int err = 0; - errno = 0; - if (vs->socket_type == SOCK_DGRAM) { - udp_remove((struct udp_pcb*)vs->pcb); - } if (vs->socket_type == SOCK_STREAM) { - if (vs->pcb) { - struct tcp_pcb* tpcb = (struct tcp_pcb*)vs->pcb; - if (tpcb->state == CLOSED) { - DEBUG_EXTRA("pcb is in CLOSED state"); - // calling tcp_close() here would be redundant - return 0; - } - if (tpcb->state == CLOSE_WAIT) { - DEBUG_EXTRA("pcb is in CLOSE_WAIT state"); - // calling tcp_close() here would be redundant - } - if (tpcb->state > TIME_WAIT) { - DEBUG_ERROR("warning, pcb=%p is in an invalid state=%d", vs->pcb, tpcb->state); - handle_general_failure(); - err = -1; - } - // unregister callbacks for this PCB - tcp_arg(tpcb, NULL); - if (tpcb->state == LISTEN) { - tcp_accept(tpcb, NULL); - } - else { - tcp_recv(tpcb, NULL); - tcp_sent(tpcb, NULL); - tcp_poll(tpcb, NULL, 0); - tcp_err(tpcb, NULL); - } + struct tcp_pcb *tpcb = (struct tcp_pcb*)(vs->pcb); + if (tpcb == NULL) { + DEBUG_ERROR("invalid pcb"); + handle_general_failure(); + return -1; + } + // should be safe to tcp_close() from application thread IF PCB is in LISTENING state (I think) + if (tpcb->state == LISTEN) { + DEBUG_EXTRA("PCB is in LISTEN, calling tcp_close() from application thread."); + tcp_accept(tpcb, NULL); if ((err = tcp_close(tpcb)) < 0) { DEBUG_ERROR("error while calling tcp_close, fd=%d, vs=%p, pcb=%p", vs->app_fd, vs, vs->pcb); errno = lwip_err_to_errno(err); err = -1; } + return ERR_OK; } + // handle junk state values + if (tpcb->state > TIME_WAIT) { + DEBUG_EXTRA("invalid TCP pcb state, already closed, report ERR_OK"); + return ERR_OK; + } + else { + // place a request for the stack to close this VirtualSocket's PCB + vs->set_state(VS_SHOULD_STOP); + // 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) { + return ERR_OK; + } + } + } + } + if (vs->socket_type == SOCK_DGRAM) { + // place a request for the stack to close this VirtualSocket's PCB + vs->set_state(VS_SHOULD_STOP); } return err; } @@ -740,6 +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); return ERR_ABRT; } vs->tap->_tcpconns_m.lock(); @@ -876,7 +879,6 @@ namespace ZeroTier // payload memcpy(udp_msg_buf + sizeof(int32_t) + sizeof(struct sockaddr_storage), &udp_payload_buf, tot_len); if ((w = write(vs->sdk_fd, udp_msg_buf, msg_tot_len)) < 0) { - perror("write"); DEBUG_ERROR("write(fd=%d)=%d, errno=%d", vs->sdk_fd, w, errno); } } @@ -933,7 +935,7 @@ namespace ZeroTier } // add to unhandled connection set for zts_connect to pick up on vs->tap->_tcpconns_m.lock(); - vs->state = ZT_SOCK_STATE_UNHANDLED_CONNECTED; + vs->set_state(VS_STATE_UNHANDLED_CONNECTED); vs->tap->_VirtualSockets.push_back(vs); vs->tap->_tcpconns_m.unlock(); return ERR_OK; @@ -950,6 +952,58 @@ namespace ZeroTier if (vs->socket_type == SOCK_DGRAM) { 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) { + DEBUG_EXTRA("closing pcb=%p, fd=%d, vs=%p", PCB, vs->app_fd, vs); + int err = 0; + errno = 0; + if (vs->socket_type == SOCK_DGRAM) { + udp_remove((struct udp_pcb*)vs->pcb); + } + if (vs->socket_type == SOCK_STREAM) { + if (vs->pcb) { + struct tcp_pcb* tpcb = (struct tcp_pcb*)vs->pcb; + if (tpcb->state == CLOSED) { + DEBUG_EXTRA("pcb is in CLOSED state"); + // calling tcp_close() here would be redundant + return 0; + } + //if (tpcb->state == CLOSE_WAIT) { + // DEBUG_EXTRA("pcb is in CLOSE_WAIT state"); + // // calling tcp_close() here would be redundant + //} + if (tpcb->state > TIME_WAIT) { + DEBUG_ERROR("warning, pcb=%p is in an invalid state=%d", vs->pcb, tpcb->state); + handle_general_failure(); + err = -1; + } + // unregister callbacks for this PCB + tcp_arg(tpcb, NULL); + if (tpcb->state == LISTEN) { + tcp_accept(tpcb, NULL); + } + else { + tcp_recv(tpcb, NULL); + tcp_sent(tpcb, NULL); + tcp_poll(tpcb, NULL, 0); + tcp_err(tpcb, NULL); + } + if ((err = tcp_close(tpcb)) < 0) { + DEBUG_ERROR("error while calling tcp_close, fd=%d, vs=%p, pcb=%p", vs->app_fd, vs, vs->pcb); + errno = lwip_err_to_errno(err); + err = -1; + } + else { + vs->set_state(VS_STOPPED); // success + } + } + } + } + + + // Handle transmission and reception of data if (vs->socket_type == SOCK_STREAM) { DEBUG_INFO("fd=%d, vs=%p, PCB=%p, vs->pcb=%p, vs->pcb->state=%d", vs->app_fd, vs, PCB, (struct tcp_pcb*)(vs->pcb), ((struct tcp_pcb*)(vs->pcb))->state); if (((struct tcp_pcb*)(vs->pcb))->state == CLOSE_WAIT) { diff --git a/src/lwIP.hpp b/src/lwIP.hpp index 703d2ff..ae4d468 100644 --- a/src/lwIP.hpp +++ b/src/lwIP.hpp @@ -191,7 +191,7 @@ extern "C" err_t tcp_shutdown(LWIP_TCP_SHUTDOWN_SIG); namespace ZeroTier { class VirtualTap; - struct VirtualSocket; + class VirtualSocket; class lwIP { diff --git a/src/picoTCP.cpp b/src/picoTCP.cpp index 9724159..f775ac3 100644 --- a/src/picoTCP.cpp +++ b/src/picoTCP.cpp @@ -470,7 +470,7 @@ namespace ZeroTier { // may now be issued in order to accept the incoming VirtualSocket from a remote host. if (ev & PICO_SOCK_EV_CONN) { DEBUG_EXTRA("PICO_SOCK_EV_CONN"); - if (vs->state == ZT_SOCK_STATE_LISTENING) { + if (vs->state == VS_STATE_LISTENING) { uint16_t port; struct pico_socket *client_psock = nullptr; struct pico_ip4 orig4; @@ -529,9 +529,9 @@ namespace ZeroTier { vs->_AcceptedConnections.push(new_vs); new_vs->sock = new_vs->tap->_phy.wrapSocket(new_vs->sdk_fd, new_vs); } - if (vs->state != ZT_SOCK_STATE_LISTENING) { + if (vs->state != VS_STATE_LISTENING) { // set state so socket multiplexer logic will pick this up - vs->state = ZT_SOCK_STATE_UNHANDLED_CONNECTED; + vs->state = VS_STATE_UNHANDLED_CONNECTED; } } // PICO_SOCK_EV_RD - triggered when new data arrives on the socket. A new receive action @@ -882,7 +882,7 @@ namespace ZeroTier { vs->picosock, err, pico_err, beautify_pico_error(pico_err)); return map_pico_err_to_errno(pico_err); } - vs->state = ZT_SOCK_STATE_LISTENING; + vs->state = VS_STATE_LISTENING; return ZT_ERR_OK; } diff --git a/src/picoTCP.hpp b/src/picoTCP.hpp index 00b6c23..8ff908d 100644 --- a/src/picoTCP.hpp +++ b/src/picoTCP.hpp @@ -86,7 +86,7 @@ namespace ZeroTier int pico_eth_poll(struct pico_device *dev, int loop_score); class VirtualTap; - struct VirtualSocket; + class VirtualSocket; class picoTCP {