diff --git a/include/Debug.hpp b/include/Debug.hpp index 1fdee1a..d3d7b2d 100644 --- a/include/Debug.hpp +++ b/include/Debug.hpp @@ -77,7 +77,6 @@ #if defined(NS_DEBUG) #define DEBUG_LWIP(fmt, args...) fprintf(stderr, ZT_YEL "LWIP : %17s:%5d:%25s: " fmt \ ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) - #define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL "STACK: %17s:%5d:%25s: " fmt \ ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else @@ -116,8 +115,6 @@ "INFO : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #define DEBUG_ATTN(fmt, args...) fprintf(stderr, ZT_CYN \ "ATTN : %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) - #define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL \ - "STACK: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #define DEBUG_BLANK(fmt, args...) fprintf(stderr, \ "INFO : %17s:%5d:" fmt "\n", ZT_FILENAME, __LINE__, ##args) #endif diff --git a/include/lwipopts.h b/include/lwipopts.h index 0a0297a..d122af9 100644 --- a/include/lwipopts.h +++ b/include/lwipopts.h @@ -43,7 +43,6 @@ * will be set to standard values. Override anything you dont like! */ #include "lwip/debug.h" -#include "Debug.hpp" // IP Protocol version #if defined(LIBZT_IPV6) diff --git a/src/Utilities.cpp b/src/Utilities.cpp index c969008..73a8fc6 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -29,56 +29,56 @@ char *beautify_eth_proto_nums(int proto) { - if(proto == 0x0800) return (char*)"IPv4"; - if(proto == 0x0806) return (char*)"ARP"; - if(proto == 0x0842) return (char*)"Wake-on-LAN"; - if(proto == 0x22F3) return (char*)"IETF TRILL Protocol"; - if(proto == 0x22EA) return (char*)"Stream Reservation Protocol"; - if(proto == 0x6003) return (char*)"DECnet Phase IV"; - if(proto == 0x8035) return (char*)"Reverse Address Resolution Protocol"; - if(proto == 0x809B) return (char*)"AppleTalk (Ethertalk)"; - if(proto == 0x80F3) return (char*)"AppleTalk Address Resolution Protocol (AARP)"; - if(proto == 0x8100) return (char*)"VLAN-tagged frame (IEEE 802.1Q) and Shortest Path Bridging IEEE 802.1aq with NNI compatibility"; - if(proto == 0x8137) return (char*)"IPX"; - if(proto == 0x8204) return (char*)"QNX Qnet"; - if(proto == 0x86DD) return (char*)"IPv6"; - if(proto == 0x8808) return (char*)"Ethernet flow control"; - if(proto == 0x8809) return (char*)"Ethernet Slow Protocols"; - if(proto == 0x8819) return (char*)"CobraNet"; - if(proto == 0x8847) return (char*)"MPLS unicast"; - if(proto == 0x8848) return (char*)"MPLS multicast"; - if(proto == 0x8863) return (char*)"PPPoE Discovery Stage"; - if(proto == 0x8864) return (char*)"PPPoE Session Stage"; - if(proto == 0x886D) return (char*)"Intel Advanced Networking Services"; - if(proto == 0x8870) return (char*)"Jumbo Frames (Obsoleted draft-ietf-isis-ext-eth-01)"; - if(proto == 0x887B) return (char*)"HomePlug 1.0 MME"; - if(proto == 0x888E) return (char*)"EAP over LAN (IEEE 802.1X)"; - if(proto == 0x8892) return (char*)"PROFINET Protocol"; - if(proto == 0x889A) return (char*)"HyperSCSI (SCSI over Ethernet)"; - if(proto == 0x88A2) return (char*)"ATA over Ethernet"; - if(proto == 0x88A4) return (char*)"EtherCAT Protocol"; - if(proto == 0x88A8) return (char*)"Provider Bridging (IEEE 802.1ad) & Shortest Path Bridging IEEE 802.1aq"; - if(proto == 0x88AB) return (char*)"Ethernet Powerlink[citation needed]"; - if(proto == 0x88B8) return (char*)"GOOSE (Generic Object Oriented Substation event)"; - if(proto == 0x88B9) return (char*)"GSE (Generic Substation Events) Management Services"; - if(proto == 0x88BA) return (char*)"SV (Sampled Value Transmission)"; - if(proto == 0x88CC) return (char*)"Link Layer Discovery Protocol (LLDP)"; - if(proto == 0x88CD) return (char*)"SERCOS III"; - if(proto == 0x88DC) return (char*)"WSMP, WAVE Short Message Protocol"; - if(proto == 0x88E1) return (char*)"HomePlug AV MME[citation needed]"; - if(proto == 0x88E3) return (char*)"Media Redundancy Protocol (IEC62439-2)"; - if(proto == 0x88E5) return (char*)"MAC security (IEEE 802.1AE)"; - if(proto == 0x88E7) return (char*)"Provider Backbone Bridges (PBB) (IEEE 802.1ah)"; - if(proto == 0x88F7) return (char*)"Precision Time Protocol (PTP) over Ethernet (IEEE 1588)"; - if(proto == 0x88FB) return (char*)"Parallel Redundancy Protocol (PRP)"; - if(proto == 0x8902) return (char*)"IEEE 802.1ag Connectivity Fault Management (CFM) Protocol / ITU-T Recommendation Y.1731 (OAM)"; - if(proto == 0x8906) return (char*)"Fibre Channel over Ethernet (FCoE)"; - if(proto == 0x8914) return (char*)"FCoE Initialization Protocol"; - if(proto == 0x8915) return (char*)"RDMA over Converged Ethernet (RoCE)"; - if(proto == 0x891D) return (char*)"TTEthernet Protocol Control Frame (TTE)"; - if(proto == 0x892F) return (char*)"High-availability Seamless Redundancy (HSR)"; - if(proto == 0x9000) return (char*)"Ethernet Configuration Testing Protocol"; - if(proto == 0x9100) return (char*)"VLAN-tagged (IEEE 802.1Q) frame with double tagging"; + if (proto == 0x0800) return (char*)"IPv4"; + if (proto == 0x0806) return (char*)"ARP"; + if (proto == 0x0842) return (char*)"Wake-on-LAN"; + if (proto == 0x22F3) return (char*)"IETF TRILL Protocol"; + if (proto == 0x22EA) return (char*)"Stream Reservation Protocol"; + if (proto == 0x6003) return (char*)"DECnet Phase IV"; + if (proto == 0x8035) return (char*)"Reverse Address Resolution Protocol"; + if (proto == 0x809B) return (char*)"AppleTalk (Ethertalk)"; + if (proto == 0x80F3) return (char*)"AppleTalk Address Resolution Protocol (AARP)"; + if (proto == 0x8100) return (char*)"VLAN-tagged frame (IEEE 802.1Q) and Shortest Path Bridging IEEE 802.1aq with NNI compatibility"; + if (proto == 0x8137) return (char*)"IPX"; + if (proto == 0x8204) return (char*)"QNX Qnet"; + if (proto == 0x86DD) return (char*)"IPv6"; + if (proto == 0x8808) return (char*)"Ethernet flow control"; + if (proto == 0x8809) return (char*)"Ethernet Slow Protocols"; + if (proto == 0x8819) return (char*)"CobraNet"; + if (proto == 0x8847) return (char*)"MPLS unicast"; + if (proto == 0x8848) return (char*)"MPLS multicast"; + if (proto == 0x8863) return (char*)"PPPoE Discovery Stage"; + if (proto == 0x8864) return (char*)"PPPoE Session Stage"; + if (proto == 0x886D) return (char*)"Intel Advanced Networking Services"; + if (proto == 0x8870) return (char*)"Jumbo Frames (Obsoleted draft-ietf-isis-ext-eth-01)"; + if (proto == 0x887B) return (char*)"HomePlug 1.0 MME"; + if (proto == 0x888E) return (char*)"EAP over LAN (IEEE 802.1X)"; + if (proto == 0x8892) return (char*)"PROFINET Protocol"; + if (proto == 0x889A) return (char*)"HyperSCSI (SCSI over Ethernet)"; + if (proto == 0x88A2) return (char*)"ATA over Ethernet"; + if (proto == 0x88A4) return (char*)"EtherCAT Protocol"; + if (proto == 0x88A8) return (char*)"Provider Bridging (IEEE 802.1ad) & Shortest Path Bridging IEEE 802.1aq"; + if (proto == 0x88AB) return (char*)"Ethernet Powerlink[citation needed]"; + if (proto == 0x88B8) return (char*)"GOOSE (Generic Object Oriented Substation event)"; + if (proto == 0x88B9) return (char*)"GSE (Generic Substation Events) Management Services"; + if (proto == 0x88BA) return (char*)"SV (Sampled Value Transmission)"; + if (proto == 0x88CC) return (char*)"Link Layer Discovery Protocol (LLDP)"; + if (proto == 0x88CD) return (char*)"SERCOS III"; + if (proto == 0x88DC) return (char*)"WSMP, WAVE Short Message Protocol"; + if (proto == 0x88E1) return (char*)"HomePlug AV MME[citation needed]"; + if (proto == 0x88E3) return (char*)"Media Redundancy Protocol (IEC62439-2)"; + if (proto == 0x88E5) return (char*)"MAC security (IEEE 802.1AE)"; + if (proto == 0x88E7) return (char*)"Provider Backbone Bridges (PBB) (IEEE 802.1ah)"; + if (proto == 0x88F7) return (char*)"Precision Time Protocol (PTP) over Ethernet (IEEE 1588)"; + if (proto == 0x88FB) return (char*)"Parallel Redundancy Protocol (PRP)"; + if (proto == 0x8902) return (char*)"IEEE 802.1ag Connectivity Fault Management (CFM) Protocol / ITU-T Recommendation Y.1731 (OAM)"; + if (proto == 0x8906) return (char*)"Fibre Channel over Ethernet (FCoE)"; + if (proto == 0x8914) return (char*)"FCoE Initialization Protocol"; + if (proto == 0x8915) return (char*)"RDMA over Converged Ethernet (RoCE)"; + if (proto == 0x891D) return (char*)"TTEthernet Protocol Control Frame (TTE)"; + if (proto == 0x892F) return (char*)"High-availability Seamless Redundancy (HSR)"; + if (proto == 0x9000) return (char*)"Ethernet Configuration Testing Protocol"; + if (proto == 0x9100) return (char*)"VLAN-tagged (IEEE 802.1Q) frame with double tagging"; return (char*)"UNKNOWN"; } @@ -139,12 +139,12 @@ void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::Ine { char ipstr[INET6_ADDRSTRLEN]; memset(ipstr, 0, INET6_ADDRSTRLEN); - if(socket_family == AF_INET) { + if (socket_family == AF_INET) { inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN); inet->fromString(ipstr); } - if(socket_family == AF_INET6) { + if (socket_family == AF_INET6) { inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, ipstr, INET6_ADDRSTRLEN); char addrstr[64]; diff --git a/src/VirtualSocket.hpp b/src/VirtualSocket.hpp index ec549c0..b5c378b 100644 --- a/src/VirtualSocket.hpp +++ b/src/VirtualSocket.hpp @@ -108,8 +108,8 @@ namespace ZeroTier { // socketpair, I/O channel between app and stack drivers closure_ts = -1; ZT_PHY_SOCKFD_TYPE fdpair[2]; - if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) { - if(errno < 0) { + if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fdpair) < 0) { + if (errno < 0) { DEBUG_ERROR("unable to create socketpair, errno=%d", errno); return; } diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index d8d0b23..827f659 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -27,13 +27,12 @@ #include #include #include +#include -#include #include #include #include #include -#include #include "VirtualTap.hpp" #include "libzt.h" @@ -133,13 +132,13 @@ namespace ZeroTier { bool VirtualTap::registerIpWithStack(const InetAddress &ip) { #if defined(STACK_PICO) - if(picostack){ + if (picostack) { picostack->pico_register_address(this, ip); return true; } #endif #if defined(STACK_LWIP) - if(lwipstack){ + if (lwipstack) { lwipstack->lwip_init_interface(this, ip); return true; } @@ -159,7 +158,7 @@ namespace ZeroTier { #if defined(STACK_PICO) || defined(STACK_LWIP) char ipbuf[INET6_ADDRSTRLEN]; DEBUG_INFO("addIp (%s)", ip.toString(ipbuf)); - if(registerIpWithStack(ip)) { + if (registerIpWithStack(ip)) { if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) { _ips.push_back(ip); std::sort(_ips.begin(),_ips.end()); @@ -196,12 +195,14 @@ namespace ZeroTier { const void *data,unsigned int len) { #if defined(STACK_PICO) - if(picostack) + if (picostack) { picostack->pico_eth_rx(this,from,to,etherType,data,len); + } #endif #if defined(STACK_LWIP) - if(lwipstack) + if (lwipstack) { lwipstack->lwip_eth_rx(this,from,to,etherType,data,len); + } #endif } @@ -213,7 +214,7 @@ namespace ZeroTier { std::string VirtualTap::nodeId() const { // TODO: This is inefficient and awkward, should be replaced with something more elegant - if(zt1ServiceRef) { + if (zt1ServiceRef) { char id[ZT_ID_LEN]; memset(id, 0, sizeof(id)); sprintf(id, "%lx",((ZeroTier::OneService *)zt1ServiceRef)->getNode()->address()); @@ -265,9 +266,9 @@ namespace ZeroTier { throw() { #if defined(STACK_PICO) - if(picostack){ + if (picostack) { picostack->pico_init_interface(this); - if(should_start_stack) { + if (should_start_stack) { // Add link to ipv4_link_add //ZeroTier::InetAddress localhost; //localhost.fromString("127.0.0.1"); @@ -277,17 +278,18 @@ namespace ZeroTier { } #endif #if defined(STACK_LWIP) - if(lwipstack) + if (lwipstack) lwipstack->lwip_loop(this); #endif } void VirtualTap::phyOnUnixClose(PhySocket *sock,void **uptr) { - if(sock) { + if (sock) { VirtualSocket *vs = (VirtualSocket*)uptr; - if(vs) + if (vs) { Close(vs); + } } } @@ -295,9 +297,10 @@ namespace ZeroTier { { //DEBUG_ATTN("sock->fd=%d", _phy.getDescriptor(sock)); VirtualSocket *vs = (VirtualSocket*)*uptr; - if(!vs) + if (vs == NULL) { return; - if(len){ + } + if (len > 0) { Write(vs, data, len); } return; @@ -305,7 +308,7 @@ namespace ZeroTier { void VirtualTap::phyOnUnixWritable(PhySocket *sock, void **uptr, bool stack_invoked) { - if(sock) + if (sock) Read(sock,uptr,stack_invoked); } @@ -316,12 +319,12 @@ namespace ZeroTier { return false; #endif #if defined(STACK_PICO) - if(picostack) { + if (picostack) { return picostack->pico_route_add(this, addr, nm, gw, 0); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { return true; } #endif @@ -335,12 +338,12 @@ namespace ZeroTier { return false; #endif #if defined(STACK_PICO) - if(picostack) { + if (picostack) { return picostack->pico_route_del(this, addr, nm, 0); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { return true; } #endif @@ -357,7 +360,7 @@ namespace ZeroTier { { Mutex::Lock _l(_tcpconns_m); for(int i=0; i<_VirtualSockets.size(); i++) { - if(vs == _VirtualSockets[i]) { + if (vs == _VirtualSockets[i]) { _VirtualSockets.erase(_VirtualSockets.begin() + i); //DEBUG_EXTRA("Removed vs=%p from vt=%p", vs, this); break; @@ -400,13 +403,13 @@ namespace ZeroTier { return -1; #endif #if defined(STACK_PICO) - if(picostack) { + if (picostack) { Mutex::Lock _l(_tcpconns_m); return picostack->pico_Connect(vs, addr, addrlen); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { return lwipstack->lwip_Connect(vs, addr, addrlen); } #endif @@ -419,13 +422,13 @@ namespace ZeroTier { return -1; #endif #if defined(STACK_PICO) - if(picostack) { + if (picostack) { Mutex::Lock _l(_tcpconns_m); return picostack->pico_Bind(vs, addr, addrlen); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { Mutex::Lock _l(_tcpconns_m); return lwipstack->lwip_Bind(this, vs, addr, addrlen); } @@ -439,7 +442,7 @@ namespace ZeroTier { return -1; #endif #if defined(STACK_PICO) - if(picostack) { + if (picostack) { Mutex::Lock _l(_tcpconns_m); return picostack->pico_Listen(vs, backlog); } @@ -448,7 +451,7 @@ namespace ZeroTier { } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { Mutex::Lock _l(_tcpconns_m); return lwipstack->lwip_Listen(vs, backlog); } @@ -465,7 +468,7 @@ namespace ZeroTier { #endif #if defined(STACK_PICO) // TODO: separation of church and state - if(picostack) { + if (picostack) { Mutex::Lock _l(_tcpconns_m); return picostack->pico_Accept(vs); } @@ -474,7 +477,7 @@ namespace ZeroTier { } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { Mutex::Lock _l(_tcpconns_m); return lwipstack->lwip_Accept(vs); } @@ -487,12 +490,12 @@ namespace ZeroTier { // Read from stack/buffers into the app's socket int VirtualTap::Read(PhySocket *sock,void **uptr,bool stack_invoked) { #if defined(STACK_PICO) - if(picostack) { + if (picostack) { return picostack->pico_Read(this, sock, (VirtualSocket*)uptr, stack_invoked); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { return lwipstack->lwip_Read((VirtualSocket*)*(_phy.getuptr(sock)), stack_invoked); } #endif @@ -502,7 +505,7 @@ namespace ZeroTier { // Write data from app socket to the virtual wire, either raw over VL2, or via network stack int VirtualTap::Write(VirtualSocket *vs, void *data, ssize_t len) { // VL2, SOCK_RAW, no network stack - if(vs->socket_type == SOCK_RAW) { + if (vs->socket_type == SOCK_RAW) { struct ether_header *eh = (struct ether_header *) data; MAC src_mac; MAC dest_mac; @@ -512,12 +515,12 @@ namespace ZeroTier { return len; } #if defined(STACK_PICO) - if(picostack) { + if (picostack) { return picostack->pico_Write(vs, data, len); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { return lwipstack->lwip_Write(vs, data, len); } #endif @@ -533,23 +536,23 @@ namespace ZeroTier { // TODO: flags int err = 0; #if defined(STACK_PICO) - if(picostack) { - if((err = picostack->pico_Connect(vs, addr, addrlen)) < 0) { // implicit + if (picostack) { + if ((err = picostack->pico_Connect(vs, addr, addrlen)) < 0) { // implicit errno = ENOTCONN; return err; } - if((err = picostack->pico_Write(vs, (void*)buf, len)) < 0) { + if ((err = picostack->pico_Write(vs, (void*)buf, len)) < 0) { errno = ENOBUFS; // TODO: translate pico err to something more useful return err; } } #endif #if defined(STACK_LWIP) - if(lwipstack) { - if((err = lwipstack->lwip_Connect(vs, addr, addrlen)) < 0) { // implicit + if (lwipstack) { + if ((err = lwipstack->lwip_Connect(vs, addr, addrlen)) < 0) { // implicit return err; } - if((err = lwipstack->lwip_Write(vs, (void*)buf, len)) < 0) { + if ((err = lwipstack->lwip_Write(vs, (void*)buf, len)) < 0) { return err; } } @@ -561,22 +564,22 @@ namespace ZeroTier { // respective protocol control structures int VirtualTap::Close(VirtualSocket *vs) { int err = 0; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid VirtualSocket"); return -1; } removeVirtualSocket(vs); #if defined(STACK_PICO) - if(picostack) { + if (picostack) { err = picostack->pico_Close(vs); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { err = lwipstack->lwip_Close(vs); } #endif - if(vs->sock) { + if (vs->sock) { _phy.close(vs->sock, false); } return err; @@ -587,12 +590,12 @@ namespace ZeroTier { { int err = 0; #if defined(STACK_PICO) - if(picostack) { + if (picostack) { err = picostack->pico_Shutdown(vs, how); } #endif #if defined(STACK_LWIP) - if(lwipstack) { + if (lwipstack) { err = lwipstack->lwip_Shutdown(vs, how); } #endif @@ -603,10 +606,10 @@ namespace ZeroTier { { Mutex::Lock _l(_tcpconns_m); std::time_t current_ts = std::time(nullptr); - if(current_ts > last_housekeeping_ts + ZT_HOUSEKEEPING_INTERVAL) { + if (current_ts > last_housekeeping_ts + ZT_HOUSEKEEPING_INTERVAL) { // update managed routes (add/del from network stacks) ZeroTier::OneService *service = ((ZeroTier::OneService *)zt1ServiceRef); - if(service) { + if (service) { std::vector *managed_routes = service->getRoutes(this->_nwid); ZeroTier::InetAddress target_addr; ZeroTier::InetAddress via_addr; @@ -623,16 +626,16 @@ namespace ZeroTier { via_addr = managed_routes->at(i).via; nm = target_addr.netmask(); for(int j=0; j", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3)); routes.push_back(std::pair(target_addr, nm)); routeAdd(target_addr, nm, via_addr); @@ -646,11 +649,11 @@ namespace ZeroTier { target_addr = managed_routes->at(j).target; via_addr = managed_routes->at(j).via; nm = target_addr.netmask(); - if(routes[i].first.ipsEqual(target_addr) && routes[i].second.ipsEqual(nm)) { + if (routes[i].first.ipsEqual(target_addr) && routes[i].second.ipsEqual(nm)) { found=true; } } - if(!found) { + if (found == false) { DEBUG_INFO("removing route to ", routes[i].first.toString(ipbuf), routes[i].second.toString(ipbuf2)); routes.erase(routes.begin() + i); routeDelete(routes[i].first, routes[i].second); diff --git a/src/VirtualTap.hpp b/src/VirtualTap.hpp index 28f3969..90d2f80 100644 --- a/src/VirtualTap.hpp +++ b/src/VirtualTap.hpp @@ -35,6 +35,7 @@ #include #include #include +#include #include "Constants.hpp" #include "MulticastGroup.hpp" @@ -58,7 +59,8 @@ #include "pico_ipv6.h" #endif #if defined(STACK_LWIP) -#include "lwIP.hpp" +#include "lwip/netif.h" +struct netif; #endif namespace ZeroTier { diff --git a/src/libzt.cpp b/src/libzt.cpp index 6a017d9..bb30906 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -116,11 +116,13 @@ namespace ZeroTier { void zts_start(const char *path) { - if(ZeroTier::zt1Service) + if (ZeroTier::zt1Service) { return; + } #if defined(STACK_PICO) - if(ZeroTier::picostack) + if (ZeroTier::picostack) { return; + } ZeroTier::picostack = new ZeroTier::picoTCP(); pico_stack_init(); #endif @@ -128,8 +130,9 @@ void zts_start(const char *path) ZeroTier::lwipstack = new ZeroTier::lwIP(); lwip_init(); #endif - if(path) + if (path) { ZeroTier::homeDir = path; + } pthread_t service_thread; pthread_create(&service_thread, NULL, zts_start_service, NULL); } @@ -137,10 +140,10 @@ void zts_start(const char *path) void zts_simple_start(const char *path, const char *nwid) { zts_start(path); - while(!zts_running()) { - nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); + while(zts_running() == false) { + nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); } - while(1) { + while(true) { try { zts_join(nwid); break; @@ -150,26 +153,26 @@ void zts_simple_start(const char *path, const char *nwid) handle_general_failure(); } } - while(!zts_has_address(nwid)) { - nanosleep((const struct timespec[]){{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); + while(zts_has_address(nwid) == false) { + nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); } } void zts_stop() { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { ZeroTier::zt1Service->terminate(); dismantleTaps(); } } void zts_join(const char * nwid) { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf"; - if(!ZeroTier::OSUtils::mkdir(ZeroTier::netDir)) { + if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) { DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str()); handle_general_failure(); } - if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) { + if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) { DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str()); handle_general_failure(); } @@ -186,12 +189,12 @@ void zts_join(const char * nwid) { 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)) { + 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)) { - if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) { + 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(); } @@ -199,8 +202,9 @@ void zts_join_soft(const char * filepath, const char * nwid) { } void zts_leave(const char * nwid) { - if(ZeroTier::zt1Service) + if (ZeroTier::zt1Service) { ZeroTier::zt1Service->leave(nwid); + } } void zts_leave_soft(const char * filepath, const char * nwid) { @@ -209,7 +213,7 @@ void zts_leave_soft(const char * filepath, const char * nwid) { } void zts_get_homepath(char *homePath, int len) { - if(ZeroTier::homeDir.length()) { + if (ZeroTier::homeDir.length()) { memset(homePath, 0, len); memcpy(homePath, ZeroTier::homeDir.c_str(), len < ZeroTier::homeDir.length() ? len : ZeroTier::homeDir.length()); } @@ -226,7 +230,7 @@ void zts_lib_version(char *ver) { } int zts_get_device_id(char *devID) { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { char id[ZT_ID_LEN]; sprintf(id, "%lx",ZeroTier::zt1Service->getNode()->address()); memcpy(devID, id, ZT_ID_LEN); @@ -236,8 +240,7 @@ int zts_get_device_id(char *devID) { { std::string fname("identity.public"); std::string fpath(ZeroTier::homeDir); - - if(ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) { + if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) { std::string oldid; 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 @@ -248,7 +251,7 @@ int zts_get_device_id(char *devID) { } int zts_running() { - return !ZeroTier::zt1Service ? false : ZeroTier::zt1Service->isRunning(); + return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning(); } int zts_has_ipv4_address(const char *nwid) @@ -274,12 +277,12 @@ int zts_has_address(const char *nwid) void zts_get_ipv4_address(const char *nwid, char *addrstr, const int addrlen) { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int); - if(tap && tap->_ips.size()){ + if (tap && tap->_ips.size()) { for(int i=0; i_ips.size(); i++) { - if(tap->_ips[i].isV4()) { + 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(); @@ -296,12 +299,12 @@ void zts_get_ipv4_address(const char *nwid, char *addrstr, const int addrlen) void zts_get_ipv6_address(const char *nwid, char *addrstr, const int addrlen) { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int); - if(tap && tap->_ips.size()){ + if (tap && tap->_ips.size()) { for(int i=0; i_ips.size(); i++) { - if(tap->_ips[i].isV6()) { + 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(); @@ -333,14 +336,16 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID) } unsigned long zts_get_peer_count() { - if(ZeroTier::zt1Service) + if (ZeroTier::zt1Service) { return ZeroTier::zt1Service->getNode()->peers()->peerCount; - else + } + else { return 0; + } } int zts_get_peer_address(char *peer, const char *devID) { - if(ZeroTier::zt1Service) { + if (ZeroTier::zt1Service) { ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers(); // uint64_t addr; for(int i=0; ipeerCount; i++) { @@ -389,21 +394,21 @@ Darwin: // int socket_family, int socket_type, int protocol int zts_socket(ZT_SOCKET_SIG) { int err = errno = 0; - if(socket_family < 0 || socket_type < 0 || protocol < 0) { + if (socket_family < 0 || socket_type < 0 || protocol < 0) { errno = EINVAL; return -1; } - if(!ZeroTier::zt1Service) { + 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) { + if (socket_type == SOCK_SEQPACKET) { DEBUG_ERROR("SOCK_SEQPACKET not yet supported."); errno = EPROTONOSUPPORT; // seemingly closest match return -1; } - if(socket_type == SOCK_RAW) { + 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; @@ -415,7 +420,7 @@ int zts_socket(ZT_SOCKET_SIG) { #if defined(STACK_PICO) struct pico_socket *p; err = ZeroTier::picostack->pico_Socket(&p, socket_family, socket_type, protocol); - if(!err && p) { + if (err == false && p) { ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket(); vs->socket_family = socket_family; vs->socket_type = socket_type; @@ -433,7 +438,7 @@ int zts_socket(ZT_SOCKET_SIG) { // TODO: check for max lwIP timers/sockets void *pcb; err = ZeroTier::lwipstack->lwip_Socket(&pcb, socket_family, socket_type, protocol); - if(pcb) { + if (pcb) { ZeroTier::VirtualSocket *vs = new ZeroTier::VirtualSocket(); vs->socket_family = socket_family; vs->socket_type = socket_type; @@ -509,27 +514,27 @@ Linux: int zts_connect(ZT_CONNECT_SIG) { DEBUG_INFO("fd=%d",fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(!ZeroTier::zt1Service) { + if (ZeroTier::zt1Service == NULL) { DEBUG_ERROR("service not started. call zts_start(path) first"); errno = EBADF; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid socket, unable to locate VirtualSocket for fd=%d", fd); errno = EBADF; return -1; } - if(!addr) { + if (addr == NULL) { DEBUG_ERROR("invalid address for fd=%d", fd); errno = EINVAL; return -1; } - if(addrlen <= 0) { + if (addrlen <= 0) { DEBUG_ERROR("invalid address length for fd=%d", fd); errno = EINVAL; return -1; @@ -541,7 +546,7 @@ int zts_connect(ZT_CONNECT_SIG) { ZeroTier::InetAddress inet; sockaddr2inet(vs->socket_family, addr, &inet); ZeroTier::VirtualTap *tap = getTapByAddr(&inet); - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("no route to host, could not find appropriate VirtualTap for fd=%d", fd); errno = ENETUNREACH; return -1; @@ -554,7 +559,7 @@ int zts_connect(ZT_CONNECT_SIG) { #if defined(STACK_LWIP) #endif - if((err = tap->Connect(vs, addr, addrlen)) < 0) { + if ((err = tap->Connect(vs, addr, addrlen)) < 0) { DEBUG_ERROR("error while connecting socket"); // errno will be set by tap->Connect return -1; @@ -586,22 +591,20 @@ int zts_connect(ZT_CONNECT_SIG) { return -1; } blocking = !(f_err & O_NONBLOCK); - if(!blocking) { + if (blocking == false) { errno = EINPROGRESS; // can't connect immediately err = -1; } - if(blocking) { + if (blocking == true) { bool complete = false; - while(true) - { + 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); + nanosleep((const struct timespec[]) {{0, (ZT_CONNECT_RECHECK_DELAY * 1000000)}}, NULL); tap->_tcpconns_m.lock(); - for(int i=0; i_VirtualSockets.size(); i++) - { + for(int i=0; i_VirtualSockets.size(); i++) { #if defined(STACK_PICO) - if(tap->_VirtualSockets[i]->state == PICO_ERR_ECONNRESET) { + if (tap->_VirtualSockets[i]->state == PICO_ERR_ECONNRESET) { errno = ECONNRESET; DEBUG_ERROR("ECONNRESET"); err = -1; @@ -609,13 +612,13 @@ int zts_connect(ZT_CONNECT_SIG) { #endif #if defined(STACK_LWIP) #endif - if(tap->_VirtualSockets[i]->state == ZT_SOCK_STATE_UNHANDLED_CONNECTED) { + 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) { + if (complete) { break; } } @@ -640,46 +643,44 @@ Darwin: */ int zts_bind(ZT_BIND_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(!ZeroTier::zt1Service) { + if (ZeroTier::zt1Service == NULL) { DEBUG_ERROR("service not started. call zts_start(path) first"); errno = EBADF; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + 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) { + if (vs->socket_family == AF_INET) { struct sockaddr_in *in4 = (struct sockaddr_in *)addr; - if(in4->sin_addr.s_addr == INADDR_ANY) { + 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()) { + if (ZeroTier::vtaps.size()) { tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap } } - if(in4->sin_addr.s_addr == 0x7f000001) { + 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()) { + if (ZeroTier::vtaps.size()) { tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap } } } - if(vs->socket_family == AF_INET6) { + 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) { + 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()) { + if (ZeroTier::vtaps.size()) { tap = (ZeroTier::VirtualTap*)(ZeroTier::vtaps[0]); // pick any vtap } } @@ -687,10 +688,10 @@ int zts_bind(ZT_BIND_SIG) { ZeroTier::InetAddress inet; sockaddr2inet(vs->socket_family, addr, &inet); - if(!tap) + if (tap == NULL) { tap = getTapByAddr(&inet); - - if(!tap) { + } + if (tap == NULL) { DEBUG_ERROR("no matching interface to bind to, could not find appropriate VirtualTap for fd=%d", fd); errno = ENETUNREACH; return -1; @@ -701,8 +702,8 @@ int zts_bind(ZT_BIND_SIG) { #endif tap->addVirtualSocket(vs); err = tap->Bind(vs, addr, addrlen); - vs->tap = tap; - if(err == 0) { // success + if (err == 0) { // success + vs->tap = tap; del_unassigned_virtual_socket(fd); add_assigned_virtual_socket(tap, vs, fd); } @@ -730,25 +731,25 @@ Linux: int zts_listen(ZT_LISTEN_SIG) { DEBUG_EXTRA("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(!ZeroTier::zt1Service) { + 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) { + 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 || !vs) { + if (tap == NULL || vs == NULL) { DEBUG_ERROR("unable to locate tap interface for file descriptor"); errno = EBADF; return -1; @@ -777,22 +778,22 @@ Darwin: int zts_accept(ZT_ACCEPT_SIG) { int err = errno = 0; DEBUG_EXTRA("fd=%d", fd); - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(addr && *addrlen <= 0) { + 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)) { + 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) { + if (p == NULL) { DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?"); errno = EBADF; err = -1; @@ -800,7 +801,6 @@ int zts_accept(ZT_ACCEPT_SIG) { 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) { @@ -811,8 +811,8 @@ int zts_accept(ZT_ACCEPT_SIG) { blocking = !(f_err & O_NONBLOCK); } ZeroTier::VirtualSocket *accepted_vs; - if(!err) { - if(!blocking) { // non-blocking + if (err == false) { + if (blocking == false) { // non-blocking DEBUG_EXTRA("EWOULDBLOCK, assuming non-blocking mode"); errno = EWOULDBLOCK; err = -1; @@ -820,19 +820,19 @@ int zts_accept(ZT_ACCEPT_SIG) { } else { // blocking while(true) { - nanosleep((const struct timespec[]){{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL); + nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL); accepted_vs = tap->Accept(vs); - if(accepted_vs) + if (accepted_vs) break; // accepted fd = err } } - if(accepted_vs) { + if (accepted_vs) { add_assigned_virtual_socket(tap, accepted_vs, accepted_vs->app_fd); err = accepted_vs->app_fd; } } - if(err > 0) { - if(addr && *addrlen) { + 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); @@ -879,23 +879,23 @@ EPERM Firewall rules forbid VirtualSocket. 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); +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); +} #endif @@ -914,23 +914,23 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG) // TODO: Move stack-specific logic into stack driver section //DEBUG_INFO("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } #if defined(STACK_PICO) ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid fd=%d", fd); errno = EBADF; return -1; } struct pico_socket *p = vs->picosock; - if(p) { + if (p) { // Disable Nagle's algorithm int value = 1; - if((err = pico_socket_setoption(p, PICO_TCP_NODELAY, &value)) < 0) { - if(err == PICO_ERR_EINVAL) { + if ((err = pico_socket_setoption(p, PICO_TCP_NODELAY, &value)) < 0) { + if (err == PICO_ERR_EINVAL) { DEBUG_ERROR("error while disabling Nagle's algorithm"); errno = ENOPROTOOPT; return -1; @@ -942,7 +942,7 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG) return err; #endif #if defined(STACK_LWIP) - + // TODO #endif return 0; } @@ -961,7 +961,7 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG) int zts_getsockopt(ZT_GETSOCKOPT_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } @@ -980,7 +980,7 @@ int zts_getsockopt(ZT_GETSOCKOPT_SIG) int zts_getsockname(ZT_GETSOCKNAME_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } @@ -1004,12 +1004,12 @@ int zts_getpeername(ZT_GETPEERNAME_SIG) { DEBUG_INFO("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { errno = ENOTCONN; return -1; } @@ -1074,48 +1074,29 @@ int zts_close(ZT_CLOSE_SIG) { //DEBUG_EXTRA("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(!ZeroTier::zt1Service) { + 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_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("no vs found for fd=%d", fd); handle_general_failure(); errno = EBADF; return -1; } del_virtual_socket(fd); - if(vs->tap) { + if (vs->tap) { vs->tap->Close(vs); } delete vs; vs = NULL; return err; - - /* - // check if socket is blocking - int f_err, blocking = 1; - if ((f_err = fcntl(fd, F_GETFL, 0)) < 0) { - DEBUG_ERROR("fcntl error, err = %s, errno = %d", f_err, errno); - err = -1; - } - else { - blocking = !(f_err & O_NONBLOCK); - } - if(blocking) { - for(int i=0; iTXbuf->count() == 0) - break; - usleep(ZT_API_CHECK_INTERVAL * 1000); - } - } - */ } int zts_poll(ZT_POLL_SIG) @@ -1133,7 +1114,7 @@ int zts_select(ZT_SELECT_SIG) int zts_fcntl(ZT_FCNTL_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } @@ -1153,40 +1134,39 @@ int zts_fcntl(ZT_FCNTL_SIG) int zts_ioctl(ZT_IOCTL_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } else { #if defined(__linux__) - if(argp) + if (argp) { struct ifreq *ifr = (struct ifreq *)argp; ZeroTier::VirtualTap *tap = getTapByName(ifr->ifr_name); - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("unable to locate tap interface with that name"); err = -1; errno = EINVAL; } // index of VirtualTap interface - if(request == SIOCGIFINDEX) { + if (request == SIOCGIFINDEX) { ifr->ifr_ifindex = tap->ifindex; err = 0; } // MAC addres or VirtualTap - if(request == SIOCGIFHWADDR) { + 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) { + 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 - { + else { DEBUG_INFO("!argp"); } #else @@ -1234,37 +1214,35 @@ ssize_t zts_sendto(ZT_SENDTO_SIG) { //DEBUG_TRANS("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(len == 0) { + if (len == 0) { return 0; } - if(len > ZT_SOCKET_MSG_BUF_SZ) { + 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) { + if (buf == NULL) { DEBUG_ERROR("msg buf is null"); errno = EINVAL; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + 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) + if (vs->socket_type == SOCK_DGRAM) { + if (vs->socket_family == AF_INET) { char ipstr[INET_ADDRSTRLEN]; memset(ipstr, 0, INET_ADDRSTRLEN); @@ -1272,7 +1250,7 @@ ssize_t zts_sendto(ZT_SENDTO_SIG) (const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN); iaddr.fromString(ipstr); } - if(vs->socket_family == AF_INET6) + if (vs->socket_family == AF_INET6) { char ipstr[INET6_ADDRSTRLEN]; memset(ipstr, 0, INET6_ADDRSTRLEN); @@ -1285,22 +1263,22 @@ ssize_t zts_sendto(ZT_SENDTO_SIG) } // get tap tap = getTapByAddr(&iaddr); - if(!tap) { + 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) { + 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) + 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) { + if (tap) { DEBUG_INFO("found interface of ifindex=%d", tap->ifindex); err = tap->Write(vs, (void*)buf, len); } @@ -1366,62 +1344,61 @@ ssize_t zts_send(ZT_SEND_SIG) { // DEBUG_TRANS("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(len == 0) { + if (len == 0) { return 0; } - if(len > ZT_SOCKET_MSG_BUF_SZ) { + 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_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs for fd=%d", fd); errno = EBADF; return -1; } - if(vs->socket_type != SOCK_STREAM) { + 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) { + if (flags & MSG_DONTROUTE) { DEBUG_INFO("MSG_DONTROUTE not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_DONTWAIT) { + 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) { + if (flags & MSG_EOR) { DEBUG_INFO("MSG_EOR not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_OOB) { + if (flags & MSG_OOB) { DEBUG_INFO("MSG_OOB not implemented yet"); errno = EINVAL; return -1; } #if defined(__linux__) - if(flags & MSG_CONFIRM) { + if (flags & MSG_CONFIRM) { DEBUG_INFO("MSG_CONFIRM not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_MORE) { + if (flags & MSG_MORE) { DEBUG_INFO("MSG_MORE not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_NOSIGNAL) { + if (flags & MSG_NOSIGNAL) { DEBUG_INFO("MSG_NOSIGNAL not implemented yet"); errno = EINVAL; return -1; @@ -1429,12 +1406,11 @@ ssize_t zts_send(ZT_SEND_SIG) #endif err = write(fd, buf, len); - // restore "per-call" flags - if(flags & MSG_DONTWAIT) { + 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 + if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag handle_general_failure(); return -1; } @@ -1447,7 +1423,7 @@ ssize_t zts_sendmsg(ZT_SENDMSG_SIG) { DEBUG_TRANS("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } @@ -1488,55 +1464,55 @@ ssize_t zts_recv(ZT_RECV_SIG) { DEBUG_TRANS("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs for fd=%d", fd); errno = EBADF; return -1; } - if(vs->socket_type != SOCK_STREAM) { + 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) { + 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) { + 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) { + if (flags & MSG_OOB) { DEBUG_INFO("MSG_OOB not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_TRUNC) { + if (flags & MSG_TRUNC) { DEBUG_INFO("MSG_TRUNC not implemented yet"); errno = EINVAL; return -1; } - if(flags & MSG_WAITALL) { + if (flags & MSG_WAITALL) { DEBUG_INFO("MSG_WAITALL not implemented yet"); errno = EINVAL; return -1; } #if defined(__linux__) - if(flags & MSG_ERRQUEUE) { + if (flags & MSG_ERRQUEUE) { DEBUG_INFO("MSG_ERRQUEUE not implemented yet"); errno = EINVAL; return -1; } #endif - if(flags & 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); @@ -1544,9 +1520,9 @@ ssize_t zts_recv(ZT_RECV_SIG) // restore "per-call" flags - if(flags & MSG_DONTWAIT) { + 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 + if (fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK) < 0) { // mask out the blocking flag handle_general_failure(); return -1; } @@ -1584,14 +1560,14 @@ 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) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(len == 0) { + if (len == 0) { return 0; } - if(!buf) { + if (buf == NULL) { DEBUG_ERROR("buf is null"); errno = EINVAL; return -1; @@ -1604,7 +1580,7 @@ ssize_t zts_recvfrom(ZT_RECVFROM_SIG) // 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)){ + if (r != sizeof(int32_t)) { errno = EIO; // TODO: test for this return -1; } @@ -1614,7 +1590,7 @@ ssize_t zts_recvfrom(ZT_RECVFROM_SIG) memcpy(&udp_msg_len, msg_ptr, sizeof(int32_t)); msg_ptr+=sizeof(int32_t); - if(udp_msg_len <= 0) { + if (udp_msg_len <= 0) { DEBUG_ERROR("invalid datagram"); errno = EIO; // TODO: test for this return -1; @@ -1622,14 +1598,14 @@ ssize_t zts_recvfrom(ZT_RECVFROM_SIG) // 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) { + 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)) { + if (addr) { + if (*addrlen < sizeof(struct sockaddr_storage)) { DEBUG_ERROR("invalid address length provided"); errno = EINVAL; return -1; @@ -1650,7 +1626,7 @@ ssize_t zts_recvmsg(ZT_RECVMSG_SIG) { //DEBUG_TRANS("fd=%d", fd); int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } @@ -1687,26 +1663,26 @@ Linux: int zts_shutdown(ZT_SHUTDOWN_SIG) { int err = errno = 0; - if(fd < 0 || fd >= ZT_MAX_SOCKETS) { + if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; } - if(how != SHUT_RD && how != SHUT_WR && how != SHUT_RDWR) { + if (how != SHUT_RD && how != SHUT_WR && how != SHUT_RDWR) { errno = EINVAL; return -1; } ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); - if(!vs) { + 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) { + 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) { + if (vs->tap) { err = vs->tap->Shutdown(vs, how); } return err; @@ -1715,7 +1691,7 @@ int zts_shutdown(ZT_SHUTDOWN_SIG) int zts_add_dns_nameserver(struct sockaddr *addr) { ZeroTier::VirtualTap *vtap = getAnyTap(); - if(vtap){ + if (vtap) { return vtap->add_DNS_Nameserver(addr); } DEBUG_ERROR("unable to locate virtual tap to add DNS nameserver"); @@ -1725,7 +1701,7 @@ int zts_add_dns_nameserver(struct sockaddr *addr) int zts_del_dns_nameserver(struct sockaddr *addr) { ZeroTier::VirtualTap *vtap = getAnyTap(); - if(vtap){ + if (vtap) { return vtap->del_DNS_Nameserver(addr); } DEBUG_ERROR("unable to locate virtual tap to remove DNS nameserver"); @@ -1745,15 +1721,16 @@ namespace ZeroTier { #include JNIEXPORT void JNICALL Java_zerotier_ZeroTier_ztjni_1start(JNIEnv *env, jobject thisObj, jstring path) { - if(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) + if (ZeroTier::zt1Service) { zts_stop(); + } } // Returns whether the ZeroTier service is running @@ -1775,7 +1752,7 @@ namespace ZeroTier { JNIEnv *env, jobject thisObj, jstring nwid) { const char *nwidstr; - if(nwid) { + if (nwid) { nwidstr = env->GetStringUTFChars(nwid, NULL); zts_join(nwidstr); } @@ -1785,7 +1762,7 @@ namespace ZeroTier { JNIEnv *env, jobject thisObj, jstring nwid) { const char *nwidstr; - if(nwid) { + if (nwid) { nwidstr = env->GetStringUTFChars(nwid, NULL); zts_leave(nwidstr); } @@ -1865,7 +1842,7 @@ namespace ZeroTier { 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) + if (rxbytes > 0) memcpy(body, (jbyte*)buffer + payload_offset, rxbytes); (*env).ReleaseByteArrayElements( buf, body, 0); // Update fields of Java ZTAddress object @@ -2000,13 +1977,13 @@ bool can_provision_new_socket(int socket_type) return !(pico_ntimers()+1 > PICO_MAX_TIMERS); #endif #if defined(STACK_LWIP) - if(socket_type == SOCK_STREAM) { + if (socket_type == SOCK_STREAM) { return !(ZeroTier::lwIP::lwip_num_current_tcp_pcbs()+1 > MEMP_NUM_TCP_PCB); } - if(socket_type == SOCK_DGRAM) { + if (socket_type == SOCK_DGRAM) { return !(ZeroTier::lwIP::lwip_num_current_udp_pcbs()+1 > MEMP_NUM_UDP_PCB); } - if(socket_type == SOCK_RAW) { + if (socket_type == SOCK_RAW) { return !(ZeroTier::lwIP::lwip_num_current_raw_pcbs()+1 > MEMP_NUM_RAW_PCB); } return true; @@ -2055,7 +2032,7 @@ ZeroTier::VirtualTap *getTapByNWID(uint64_t nwid) ZeroTier::VirtualTap *s, *tap = nullptr; for(int i=0; i_nwid == nwid) { tap = s; } + if (s->_nwid == nwid) { tap = s; } } ZeroTier::_vtaps_lock.unlock(); return tap; @@ -2070,9 +2047,9 @@ ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr) s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i]; // check address schemes for(int j=0; j_ips.size(); j++) { - if((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) { + if ((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) { //DEBUG_INFO("looking at tap %s, --- for <%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2)); - if(s->_ips[j].isEqualPrefix(addr) + 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)) @@ -2085,14 +2062,14 @@ ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr) } } // check managed routes - if(!tap) { + if (tap == NULL) { std::vector *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid); ZeroTier::InetAddress target, nm, via; - for(int i=0; isize(); i++){ + 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)) { + if (target.containsAddress(addr)) { // DEBUG_INFO("chose tap with route ", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3)); ZeroTier::_vtaps_lock.unlock(); return s; @@ -2110,7 +2087,7 @@ ZeroTier::VirtualTap *getTapByName(char *ifname) ZeroTier::VirtualTap *s, *tap = nullptr; for(int i=0; i_dev.c_str(), ifname)) { + if (strcmp(s->_dev.c_str(), ifname) == false) { tap = s; } } @@ -2124,7 +2101,7 @@ ZeroTier::VirtualTap *getTapByIndex(int index) ZeroTier::VirtualTap *s, *tap = nullptr; for(int i=0; iifindex == index) { + if (s->ifindex == index) { tap = s; } } @@ -2136,7 +2113,7 @@ ZeroTier::VirtualTap *getAnyTap() { ZeroTier::_vtaps_lock.lock(); ZeroTier::VirtualTap *vtap = NULL; - if(ZeroTier::vtaps.size()) { + if (ZeroTier::vtaps.size()) { vtap = (ZeroTier::VirtualTap *)ZeroTier::vtaps[0]; } ZeroTier::_vtaps_lock.unlock(); @@ -2152,10 +2129,10 @@ ZeroTier::VirtualSocket *get_virtual_socket(int fd) ZeroTier::_multiplexer_lock.lock(); // try to locate in unmapped set ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd]; - if(!vs) { + if (vs == NULL) { // if not, try to find in mapped set (bind to vtap has been performed) std::pair *p = ZeroTier::fdmap[fd]; - if(p) { + if (p) { vs = p->first; } else { @@ -2172,11 +2149,11 @@ int del_virtual_socket(int fd) ZeroTier::_multiplexer_lock.lock(); try { std::map::iterator un_iter = ZeroTier::unmap.find(fd); - if(un_iter != ZeroTier::unmap.end()) { + 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()) { + if (fd_iter != ZeroTier::fdmap.end()) { ZeroTier::fdmap.erase(fd_iter); } } @@ -2195,7 +2172,7 @@ int add_unassigned_virtual_socket(int fd, ZeroTier::VirtualSocket *vs) ZeroTier::_multiplexer_lock.lock(); try { std::map::iterator un_iter = ZeroTier::unmap.find(fd); - if(un_iter == ZeroTier::unmap.end()) { + if (un_iter == ZeroTier::unmap.end()) { ZeroTier::unmap[fd] = vs; } else { @@ -2218,7 +2195,7 @@ int del_unassigned_virtual_socket(int fd) ZeroTier::_multiplexer_lock.lock(); try { std::map::iterator un_iter = ZeroTier::unmap.find(fd); - if(un_iter != ZeroTier::unmap.end()) { + if (un_iter != ZeroTier::unmap.end()) { ZeroTier::unmap.erase(un_iter); } } @@ -2237,7 +2214,7 @@ int add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSock ZeroTier::_multiplexer_lock.lock(); try { std::map*>::iterator fd_iter = ZeroTier::fdmap.find(fd); - if(fd_iter == ZeroTier::fdmap.end()) { + if (fd_iter == ZeroTier::fdmap.end()) { ZeroTier::fdmap[fd] = new std::pair(vs, tap); } else { @@ -2260,7 +2237,7 @@ int del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSock ZeroTier::_multiplexer_lock.lock(); try { std::map*>::iterator fd_iter = ZeroTier::fdmap.find(fd); - if(fd_iter != ZeroTier::fdmap.end()) { + if (fd_iter != ZeroTier::fdmap.end()) { ZeroTier::fdmap.erase(fd_iter); } } @@ -2296,8 +2273,7 @@ void dismantleTaps() 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)) { + 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 @@ -2307,26 +2283,26 @@ int zts_get_device_id_from_file(const char *filepath, char *devID) { } // Starts a ZeroTier service in the background -void *zts_start_service(void *thread_id) { - +void *zts_start_service(void *thread_id) +{ DEBUG_INFO("homeDir=%s", ZeroTier::homeDir.c_str()); // 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::vector hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"","")); std::string ptmp; - if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) + 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) + if (ptmp.length() > 0) { ptmp.push_back(ZT_PATH_SEPARATOR); + } ptmp.append(*pi); if ((*pi != ".")&&(*pi != "..")) { - if (!ZeroTier::OSUtils::mkdir(ptmp)) { + if (ZeroTier::OSUtils::mkdir(ptmp) == false) { DEBUG_ERROR("home path does not exist, and could not create"); handle_general_failure(); perror("error\n"); diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 2de3ca9..8ee75bd 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -88,7 +88,7 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p) int proto = ZeroTier::Utils::ntoh((uint16_t)ethhdr->type); tap->_handler(tap->_arg, NULL, tap->_nwid, src_mac, dest_mac, proto, 0, data, len); - if(ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { + if (ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { char flagbuf[32]; memset(&flagbuf, 0, 32); char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZT_ID_LEN]; @@ -143,7 +143,7 @@ namespace ZeroTier } #endif #if defined(LIBZT_IPV6) - if(ip.isV6()) { + if (ip.isV6()) { static ip6_addr_t addr6; struct sockaddr_in6 in6; memcpy(in6.sin6_addr.s6_addr,ip.rawIpData(),16); @@ -307,7 +307,7 @@ namespace ZeroTier const void *data, unsigned int len) { struct pbuf *p,*q; - if (!tap->_enabled){ + if (tap->_enabled == false) { return; } struct eth_hdr ethhdr; @@ -333,7 +333,7 @@ namespace ZeroTier dataptr += q->len; } } - if(ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { + if (ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { char flagbuf[32]; memset(&flagbuf, 0, 32); char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZT_ID_LEN]; @@ -350,12 +350,12 @@ namespace ZeroTier } { #if defined(LIBZT_IPV4) - if(tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) { + if (tap->lwipdev.input(p, &(tap->lwipdev)) != ERR_OK) { DEBUG_ERROR("error while feeding frame into stack interface (ipv4)"); } #endif #if defined(LIBZT_IPV6) - if(tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) { + if (tap->lwipdev6.input(p, &(tap->lwipdev6)) != ERR_OK) { DEBUG_ERROR("error while feeding frame into stack interface (ipv6)"); } #endif @@ -364,18 +364,18 @@ namespace ZeroTier int lwIP::lwip_Socket(void **pcb, int socket_family, int socket_type, int protocol) { - if(!can_provision_new_socket(socket_type)) { + if (can_provision_new_socket(socket_type) == false) { DEBUG_ERROR("unable to create socket due to limitation of network stack, PCBs=%d", lwip_num_total_pcbs()); errno = ENOMEM; return -1; } - if(socket_type == SOCK_STREAM) { + if (socket_type == SOCK_STREAM) { struct tcp_pcb *new_tcp_PCB = tcp_new(); *pcb = new_tcp_PCB; tcp_nagle_disable(new_tcp_PCB); return ERR_OK; } - if(socket_type == SOCK_DGRAM) { + if (socket_type == SOCK_DGRAM) { struct udp_pcb *new_udp_PCB = udp_new(); *pcb = new_udp_PCB; return ERR_OK; @@ -391,7 +391,7 @@ namespace ZeroTier int port = 0, err = 0; #if defined(LIBZT_IPV4) struct sockaddr_in *in4 = (struct sockaddr_in *)addr; - if(addr->sa_family == AF_INET && vs->socket_type == SOCK_STREAM) { + if (addr->sa_family == AF_INET && vs->socket_type == SOCK_STREAM) { inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN); DEBUG_EXTRA("connecting to %s : %d", addrstr, lwip_ntohs(in4->sin_port)); } @@ -401,27 +401,27 @@ namespace ZeroTier #if defined(LIBZT_IPV6) struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&addr; in6_to_ip6((ip6_addr *)&ba, in6); - if(addr->sa_family == AF_INET6 && vs->socket_type == SOCK_STREAM) { + if (addr->sa_family == AF_INET6 && vs->socket_type == SOCK_STREAM) { inet_ntop(AF_INET6, &(in6->sin6_addr), addrstr, INET6_ADDRSTRLEN); DEBUG_EXTRA("connecting to %s : %d", addrstr, lwip_ntohs(in6->sin6_port)); } #endif - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { // generates no network traffic - if((err = udp_connect((struct udp_pcb*)vs->pcb,(ip_addr_t *)&ba,port)) < 0) { + if ((err = udp_connect((struct udp_pcb*)vs->pcb,(ip_addr_t *)&ba,port)) < 0) { DEBUG_ERROR("error while connecting to with UDP"); } udp_recv((struct udp_pcb*)vs->pcb, lwip_cb_udp_recved, vs); return ERR_OK; } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { struct tcp_pcb *tpcb = (struct tcp_pcb*)vs->pcb; tcp_sent(tpcb, lwip_cb_sent); tcp_recv(tpcb, lwip_cb_tcp_recved); tcp_err(tpcb, lwip_cb_err); tcp_poll(tpcb, lwip_cb_poll, LWIP_APPLICATION_POLL_FREQ); tcp_arg(tpcb, vs); - if((err = tcp_connect(tpcb,&ba,port,lwip_cb_connected)) < 0) { + if ((err = tcp_connect(tpcb,&ba,port,lwip_cb_connected)) < 0) { errno = lwip_err_to_errno(err); // We should only return a value if failure happens immediately // Otherwise, we still need to wait for a callback from lwIP. @@ -444,7 +444,7 @@ namespace ZeroTier int port = 0, err = 0; #if defined(LIBZT_IPV4) struct sockaddr_in *in4 = (struct sockaddr_in *)addr; - if(addr->sa_family == AF_INET) { + if (addr->sa_family == AF_INET) { inet_ntop(AF_INET, &(in4->sin_addr), addrstr, INET_ADDRSTRLEN); DEBUG_EXTRA("binding to %s : %d", addrstr, lwip_ntohs(in4->sin_port)); } @@ -454,13 +454,13 @@ namespace ZeroTier #if defined(LIBZT_IPV6) struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; in6_to_ip6((ip6_addr *)&ba, in6); - if(addr->sa_family == AF_INET6) { + if (addr->sa_family == AF_INET6) { inet_ntop(AF_INET6, &(in6->sin6_addr), addrstr, INET6_ADDRSTRLEN); DEBUG_EXTRA("binding to %s : %d", addrstr, lwip_ntohs(in6->sin6_port)); } #endif - if(vs->socket_type == SOCK_DGRAM) { - if((err = udp_bind((struct udp_pcb*)vs->pcb, (const ip_addr_t *)&ba, port)) < 0) { + if (vs->socket_type == SOCK_DGRAM) { + if ((err = udp_bind((struct udp_pcb*)vs->pcb, (const ip_addr_t *)&ba, port)) < 0) { errno = lwip_err_to_errno(err); err = -1; } @@ -471,7 +471,7 @@ namespace ZeroTier } } else if (vs->socket_type == SOCK_STREAM) { - if((err = tcp_bind((struct tcp_pcb*)vs->pcb, (const ip_addr_t *)&ba, port)) < 0) { + if ((err = tcp_bind((struct tcp_pcb*)vs->pcb, (const ip_addr_t *)&ba, port)) < 0) { errno = lwip_err_to_errno(err); err = -1; } @@ -491,7 +491,7 @@ namespace ZeroTier #else listeningPCB = tcp_listen((struct tcp_pcb*)vs->pcb); #endif - if(listeningPCB) { + if (listeningPCB) { vs->pcb = listeningPCB; // set callback tcp_accept(listeningPCB, lwip_cb_accept); @@ -507,7 +507,7 @@ namespace ZeroTier VirtualSocket* lwIP::lwip_Accept(VirtualSocket *vs) { - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid virtual socket"); handle_general_failure(); return NULL; @@ -515,7 +515,7 @@ namespace ZeroTier // Retreive first of queued VirtualSockets from parent VirtualSocket // TODO: check multithreaded behaviour VirtualSocket *new_vs = NULL; - if(vs->_AcceptedConnections.size()) { + if (vs->_AcceptedConnections.size()) { new_vs = vs->_AcceptedConnections.front(); vs->_AcceptedConnections.pop(); } @@ -526,29 +526,29 @@ namespace ZeroTier { DEBUG_EXTRA("vs=%p", vs); int err = 0; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("no virtual socket"); return -1; } - if(!lwip_invoked) { + if (lwip_invoked == false) { DEBUG_INFO("!lwip_invoked"); vs->tap->_tcpconns_m.lock(); vs->_rx_m.lock(); } - if(vs->socket_type == SOCK_STREAM && vs->RXbuf->count()) { + if (vs->socket_type == SOCK_STREAM && vs->RXbuf->count()) { handle_general_failure(); int wr = std::min((ssize_t)ZT_STACK_TCP_SOCKET_RX_SZ, (ssize_t)vs->RXbuf->count()); int n = vs->tap->_phy.streamSend(vs->sock, vs->RXbuf->get_buf(), wr); - if(n > 0) { + if (n > 0) { vs->RXbuf->consume(n); tcp_recved((struct tcp_pcb*)vs->pcb, n); DEBUG_TRANS("TCP RX %d bytes", n); } } - if(vs->RXbuf->count() == 0) { + if (vs->RXbuf->count() == 0) { vs->tap->_phy.setNotifyWritable(vs->sock, false); // nothing else to send to the app } - if(!lwip_invoked) { + if (lwip_invoked == false) { vs->tap->_tcpconns_m.unlock(); vs->_rx_m.unlock(); } @@ -558,39 +558,39 @@ namespace ZeroTier int lwIP::lwip_Write(VirtualSocket *vs, void *data, ssize_t len) { int err = 0; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("no virtual socket"); return -1; } DEBUG_EXTRA("fd=%d, vs=%p, pcb=%p, pcb->state=%d, len=%d", vs->app_fd, vs, (struct tcp_pcb*)(vs->pcb), ((struct tcp_pcb*)(vs->pcb))->state, len); - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { // TODO: Packet re-assembly hasn't yet been tested with lwIP so UDP packets are limited to MTU-sized chunks int udp_trans_len = std::min(len, (ssize_t)ZT_MAX_MTU); struct pbuf * pb = pbuf_alloc(PBUF_TRANSPORT, udp_trans_len, PBUF_POOL); - if(!pb) { + if (pb == NULL) { DEBUG_ERROR("unable to allocate new pbuf of len=%d", udp_trans_len); return -1; } memcpy(pb->payload, data, udp_trans_len); int err = udp_send((struct udp_pcb*)vs->pcb, pb); - if(err == ERR_MEM) { + if (err == ERR_MEM) { DEBUG_ERROR("error sending packet. out of memory"); - } else if(err == ERR_RTE) { + } else if (err == ERR_RTE) { DEBUG_ERROR("could not find route to destinations address"); - } else if(err != ERR_OK) { + } else if (err != ERR_OK) { DEBUG_ERROR("error sending packet - %d", err); } pbuf_free(pb); - if(err == ERR_OK) { + if (err == ERR_OK) { return udp_trans_len; } } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { // How much we are currently allowed to write to the VirtualSocket ssize_t sndbuf = ((struct tcp_pcb*)vs->pcb)->snd_buf; - if(!sndbuf) { + if (sndbuf == 0) { // PCB send buffer is full, turn off readability notifications for the // corresponding PhySocket until lwip_cb_sent() is called and confirms that there is // now space on the buffer @@ -605,24 +605,24 @@ namespace ZeroTier handle_general_failure(); err = ZT_ERR_GENERAL_FAILURE; } - if(vs->TXbuf->count() <= 0) { + if (vs->TXbuf->count() <= 0) { err = -1; // nothing to write } - if(!err) { + if (err == ERR_OK) { int r = std::min((ssize_t)vs->TXbuf->count(), sndbuf); // Writes data pulled from the client's socket buffer to LWIP. This merely sends the // data to LWIP to be enqueued and eventually sent to the network. - if(r > 0) { + if (r > 0) { err = tcp_write((struct tcp_pcb*)vs->pcb, vs->TXbuf->get_buf(), r, vs->copymode); tcp_output((struct tcp_pcb*)vs->pcb); - if(err != ERR_OK) { + if (err != ERR_OK) { DEBUG_ERROR("error while writing to lwIP tcp_pcb, err=%d", err); - if(err == ERR_MEM) { + if (err == ERR_MEM) { DEBUG_ERROR("lwIP out of memory"); } err = -1; } else { - if(vs->copymode & TCP_WRITE_FLAG_COPY) { + if (vs->copymode & TCP_WRITE_FLAG_COPY) { // since we copied the data (allocated pbufs), we can consume the buffer vs->TXbuf->consume(r); // success DEBUG_TRANS("len=%5d tx_buf_len=%10d [VSTXBF --> NSLWIP]", err, vs->TXbuf->count()); @@ -643,7 +643,7 @@ namespace ZeroTier int lwIP::lwip_Close(VirtualSocket *vs) { - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return -1; @@ -651,29 +651,29 @@ namespace ZeroTier DEBUG_EXTRA("fd=%d, vs=%p", vs->app_fd, vs); int err = 0; errno = 0; - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { udp_remove((struct udp_pcb*)vs->pcb); } - if(vs->socket_type == SOCK_STREAM) { - if(vs->pcb) { + if (vs->socket_type == SOCK_STREAM) { + if (vs->pcb) { struct tcp_pcb* tpcb = (struct tcp_pcb*)vs->pcb; - if(tpcb->state == CLOSED) { + if (tpcb->state == CLOSED) { DEBUG_ERROR("pcb is in CLOSED state"); // calling tcp_close() here would be redundant return 0; } - if(tpcb->state == CLOSE_WAIT) { + if (tpcb->state == CLOSE_WAIT) { DEBUG_ERROR("pcb is in CLOSE_WAIT state"); // calling tcp_close() here would be redundant } - if(tpcb->state > TIME_WAIT) { + 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) { + if (tpcb->state == LISTEN) { tcp_accept(tpcb, NULL); } else { @@ -682,7 +682,7 @@ namespace ZeroTier tcp_poll(tpcb, NULL, 0); tcp_err(tpcb, NULL); } - if((err = tcp_close(tpcb)) < 0) { + 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; @@ -695,17 +695,17 @@ namespace ZeroTier int lwIP::lwip_Shutdown(VirtualSocket *vs, int how) { int err=0, shut_rx=0, shut_tx=0; - if(how == SHUT_RD) { + if (how == SHUT_RD) { shut_rx = 1; } - if(how == SHUT_WR) { + if (how == SHUT_WR) { shut_tx = 1; } - if(how == SHUT_RDWR) { + if (how == SHUT_RDWR) { shut_rx = 1; shut_tx = 1; } - if((err = tcp_shutdown((tcp_pcb*)(vs->pcb), shut_rx, shut_tx) < 0)) { + if ((err = tcp_shutdown((tcp_pcb*)(vs->pcb), shut_rx, shut_tx) < 0)) { DEBUG_ERROR("error while shutting down socket, fd=%d", vs->app_fd); } return err; @@ -732,13 +732,13 @@ namespace ZeroTier //DEBUG_INFO(); VirtualSocket *vs = (VirtualSocket *)arg; int tot = 0; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("no virtual socket"); handle_general_failure(); return ERR_OK; } struct pbuf* q = p; - if(p == NULL) { + 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); return ERR_ABRT; } @@ -746,12 +746,12 @@ namespace ZeroTier vs->_rx_m.lock(); // cycle through pbufs and write them to the RX buffer while(p != NULL) { - if(p->len <= 0) { + if (p->len <= 0) { break; } int avail = ZT_TCP_RX_BUF_SZ - vs->RXbuf->count(); int len = p->len; - if(avail < len) { + if (avail < len) { DEBUG_ERROR("not enough room (%d bytes) on RX buffer", avail); } // place new incoming data on ringbuffer before we try to send it to the app @@ -760,16 +760,16 @@ namespace ZeroTier p = p->next; tot += len; } - if(tot) { + if (tot) { tcp_recved(PCB, tot); DEBUG_TRANS("len=%5d rx_buf_len=%10d [NSLWIP --> VSRXBF]", tot, vs->RXbuf->count()); int w, write_attempt_sz = vs->RXbuf->count() < ZT_MAX_MTU ? vs->RXbuf->count() : ZT_MAX_MTU; - if((w = write(vs->sdk_fd, vs->RXbuf->get_buf(), write_attempt_sz)) < 0) { + if ((w = write(vs->sdk_fd, vs->RXbuf->get_buf(), write_attempt_sz)) < 0) { DEBUG_ERROR("write(fd=%d)=%d, errno=%d", vs->sdk_fd, w, errno); } - if(w > 0) { + if (w > 0) { vs->RXbuf->consume(w); - if(w < write_attempt_sz) { + if (w < write_attempt_sz) { DEBUG_TRANS("len=%5d rx_buf_len=%10d [VSRXBF --> APPFDS]", w, vs->RXbuf->count()); DEBUG_EXTRA("intended to write len=%d, only wrote len=%d", write_attempt_sz, w); } @@ -827,12 +827,12 @@ namespace ZeroTier { //DEBUG_EXTRA("arg(vs)=%p, pcb=%p, port=%d)", arg, upcb, port); VirtualSocket *vs = (VirtualSocket *)arg; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid virtual socket"); return; } - if(!p) { - DEBUG_ERROR("!p"); + if (p == NULL) { + DEBUG_ERROR("p == NULL"); return; } struct pbuf* q = p; @@ -851,11 +851,9 @@ namespace ZeroTier char udp_payload_buf[ZT_SOCKET_MSG_BUF_SZ]; char *msg_ptr = udp_payload_buf; - int tot_len = 0; - while(p != NULL) - { - if(p->len <= 0) { + while(p != NULL) { + if (p->len <= 0) { break; } memcpy(msg_ptr, p->payload, p->len); @@ -863,7 +861,7 @@ namespace ZeroTier tot_len += p->len; p = p->next; } - if(tot_len) { + if (tot_len) { int w = 0; //DEBUG_INFO("tot_len=%d", tot_len); char udp_msg_buf[ZT_SOCKET_MSG_BUF_SZ]; // [sz : addr : payload] @@ -872,7 +870,7 @@ namespace ZeroTier memcpy(udp_msg_buf, &len, sizeof(int32_t)); // len: sockaddr+payload memcpy(udp_msg_buf + sizeof(int32_t), &ss, sizeof(struct sockaddr_storage)); // sockaddr memcpy(udp_msg_buf + sizeof(int32_t) + sizeof(struct sockaddr_storage), &udp_payload_buf, tot_len); // payload - if((w = write(vs->sdk_fd, udp_msg_buf, msg_tot_len)) < 0) { + 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); } @@ -887,10 +885,10 @@ namespace ZeroTier { //DEBUG_EXTRA("pcb=%p", PCB); VirtualSocket *vs = (VirtualSocket *)arg; - if(!vs){ + if (vs == NULL) { DEBUG_ERROR("invalid vs for PCB=%p, len=%d", PCB, len); } - if(!(vs->copymode & TCP_WRITE_FLAG_COPY)) { + if ((vs->copymode & TCP_WRITE_FLAG_COPY) == false) { /* From lwIP docs: @@ -926,7 +924,7 @@ namespace ZeroTier { DEBUG_ATTN("pcb=%p", PCB); VirtualSocket *vs = (VirtualSocket *)arg; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid virtual socket"); return -1; } @@ -941,21 +939,21 @@ namespace ZeroTier err_t lwIP::lwip_cb_poll(void* arg, struct tcp_pcb *PCB) { VirtualSocket *vs = (VirtualSocket *)arg; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return ERR_OK; // TODO: determine appropriate error value, if any } - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { DEBUG_INFO("fd=%d, vs=%p, pcb=%p", vs->app_fd, vs, PCB, vs->pcb); } - if(vs->socket_type == SOCK_STREAM) { + 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) { + if (((struct tcp_pcb*)(vs->pcb))->state == CLOSE_WAIT) { DEBUG_EXTRA("pcb->state=CLOSE_WAIT. do nothing"); return ERR_OK; } - if(((struct tcp_pcb*)(vs->pcb))->state == CLOSED) { + if (((struct tcp_pcb*)(vs->pcb))->state == CLOSED) { DEBUG_EXTRA("pcb->state=CLOSED. do nothing"); return ERR_OK; } @@ -966,16 +964,16 @@ namespace ZeroTier // RX vs->_rx_m.lock(); - if(vs->RXbuf->count()) { + if (vs->RXbuf->count()) { // this data has already been acknowledged via tcp_recved(), we merely need to // move it off of the ringbuffer and into the client app int w, write_attempt_sz = vs->RXbuf->count() < ZT_MAX_MTU ? vs->RXbuf->count() : ZT_MAX_MTU; - if((w = write(vs->sdk_fd, vs->RXbuf->get_buf(), write_attempt_sz)) < 0) { + if ((w = write(vs->sdk_fd, vs->RXbuf->get_buf(), write_attempt_sz)) < 0) { DEBUG_ERROR("write(fd=%d)=%d, errno=%d", vs->sdk_fd, w, errno); } - if(w > 0) { + if (w > 0) { vs->RXbuf->consume(w); - if(w < write_attempt_sz) { + if (w < write_attempt_sz) { DEBUG_TRANS("len=%5d rx_buf_len=%10d [VSRXBF --> APPFDS]", w, vs->RXbuf->count()); DEBUG_EXTRA("intended to write len=%d, only wrote len=%d", write_attempt_sz, w); } @@ -987,7 +985,7 @@ namespace ZeroTier vs->_rx_m.unlock(); // No need to lock the TX buffer since lwip_Write() will lock it for us // TX - if(vs->TXbuf->count()) { + if (vs->TXbuf->count()) { // we previously attempted to tcp_write(), but something went wrong, this // is where we retry lwipstack->lwip_Write(vs, vs->TXbuf->get_buf(), vs->TXbuf->count()); @@ -999,14 +997,14 @@ namespace ZeroTier void lwIP::lwip_cb_err(void *arg, err_t err) { VirtualSocket *vs = (VirtualSocket *)arg; - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("err=%d, invalid virtual socket", err); errno = -1; } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { DEBUG_ERROR("vs=%p, pcb=%p, pcb->state=%d, fd=%d, err=%d", vs, vs->pcb, ((struct tcp_pcb*)(vs->pcb))->state, vs->app_fd, err); } - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { DEBUG_ERROR("vs=%p, pcb=%p, fd=%d, err=%d", vs, vs->pcb, vs->app_fd, err); } switch(err) diff --git a/src/lwIP.hpp b/src/lwIP.hpp index 84a7d69..0a17e82 100644 --- a/src/lwIP.hpp +++ b/src/lwIP.hpp @@ -31,6 +31,7 @@ #include #include +#include #include "lwip/mem.h" #include "lwip/pbuf.h" diff --git a/src/picoTCP.cpp b/src/picoTCP.cpp index 8ae0a47..e0379dc 100644 --- a/src/picoTCP.cpp +++ b/src/picoTCP.cpp @@ -101,7 +101,7 @@ namespace ZeroTier { _picostack_driver_lock.lock(); // give right to vtap to start the stack // only one stack loop is permitted - if(!picodev_initialized) { + if (picodev_initialized == false) { tap->should_start_stack = true; picodev.send = pico_eth_tx; // tx picodev.poll = pico_eth_poll; // calls pico_eth_rx @@ -109,7 +109,7 @@ namespace ZeroTier { picodev.tap = tap; uint8_t mac[PICO_SIZE_ETH]; tap->_mac.copyTo(mac, PICO_SIZE_ETH); - if(pico_device_init(&picodev, tap->vtap_abbr_name, mac) != 0) { + if (pico_device_init(&picodev, tap->vtap_abbr_name, mac) != 0) { DEBUG_ERROR("dev init failed"); handle_general_failure(); err = false; @@ -128,7 +128,7 @@ namespace ZeroTier { char ipbuf[INET6_ADDRSTRLEN]; uint8_t hwaddr[6]; // register addresses - if(ip.isV4()) { + if (ip.isV4()) { struct pico_ip4 ipaddr, netmask; ipaddr.addr = *((uint32_t *)ip.rawIpData()); netmask.addr = *((uint32_t *)ip.netmask().rawIpData()); @@ -140,7 +140,7 @@ namespace ZeroTier { DEBUG_INFO("mac=%s", macbuf); err = true; } - if(ip.isV6()) { + if (ip.isV6()) { char ipv6_str[INET6_ADDRSTRLEN], nm_str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, ip.rawIpData(), ipv6_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, ip.netmask().rawIpData(), nm_str, INET6_ADDRSTRLEN); @@ -173,7 +173,7 @@ namespace ZeroTier { struct pico_ip4 gateway; gateway.addr = *((uint32_t *)gw.rawIpData()); int err = pico_ipv4_route_add(address, netmask, gateway, metric, link); - if(err) { + if (err) { DEBUG_ERROR("err=%d, %s", err, beautify_pico_error(pico_err)); } return err; @@ -186,7 +186,7 @@ namespace ZeroTier { struct pico_ip4 netmask; netmask.addr = *((uint32_t *)nm.rawIpData()); int err = pico_ipv4_route_del(address, netmask, metric); - if(err) { + if (err) { DEBUG_ERROR("err=%d, %s", err, beautify_pico_error(pico_err)); } return err; @@ -204,7 +204,7 @@ namespace ZeroTier { uint32_t ipval = 0; pico_string_to_ipv4(ipv4_str, &ipval); ns.addr = ipval; - if((err = pico_dns_client_nameserver(&ns, PICO_DNS_NS_ADD)) < 0) { + if ((err = pico_dns_client_nameserver(&ns, PICO_DNS_NS_ADD)) < 0) { DEBUG_ERROR("error while adding DNS nameserver, err=%d, pico_err=%d, %s", err, pico_err, beautify_pico_error(pico_err)); map_pico_err_to_errno(pico_err); @@ -224,7 +224,7 @@ namespace ZeroTier { uint32_t ipval = 0; pico_string_to_ipv4(ipv4_str, &ipval); ns.addr = ipval; - if((err = pico_dns_client_nameserver(&ns, PICO_DNS_NS_DEL)) < 0) { + if ((err = pico_dns_client_nameserver(&ns, PICO_DNS_NS_DEL)) < 0) { DEBUG_ERROR("error while removing DNS nameserver, err=%d, pico_err=%d, %s", err, pico_err, beautify_pico_error(pico_err)); } @@ -247,19 +247,18 @@ namespace ZeroTier { void picoTCP::pico_cb_tcp_read(ZeroTier::VirtualTap *tap, struct pico_socket *s) { VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("s->priv yielded no valid vs"); handle_general_failure(); return; } Mutex::Lock _l(vs->_rx_m); - - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("invalid tap"); handle_general_failure(); return; } - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return; @@ -274,7 +273,7 @@ namespace ZeroTier { do { int n = 0; int avail = ZT_TCP_RX_BUF_SZ - vs->RXbuf->count(); - if(avail) { + if (avail) { r = pico_socket_recvfrom(s, vs->RXbuf->get_buf(), ZT_STACK_SOCKET_RD_MAX, (void *)&peer.ip4.addr, &port); if (r > 0) @@ -282,10 +281,10 @@ namespace ZeroTier { vs->RXbuf->produce(r); n = tap->_phy.streamSend(vs->sock, vs->RXbuf->get_buf(), r); - if(n>0) + if (n>0) vs->RXbuf->consume(n); } - if(vs->RXbuf->count() == 0) { + if (vs->RXbuf->count() == 0) { tap->_phy.setNotifyWritable(vs->sock, false); } else { @@ -306,19 +305,18 @@ namespace ZeroTier { { // DEBUG_INFO(); VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("s->priv yielded no valid vs"); handle_general_failure(); return; } Mutex::Lock _l(vs->_rx_m); - - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("invalid tap"); handle_general_failure(); return; } - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return; @@ -331,10 +329,10 @@ namespace ZeroTier { } peer; int r = 0, w = 0; // TODO: Consolidate this - if(vs->socket_family == AF_INET) { + if (vs->socket_family == AF_INET) { struct sockaddr_in in4; char udp_payload_buf[ZT_MAX_MTU]; - if((r = pico_socket_recvfrom(s, udp_payload_buf, ZT_SDK_MTU, (void *)&peer.ip4.addr, &port)) < 0) { + if ((r = pico_socket_recvfrom(s, udp_payload_buf, ZT_SDK_MTU, (void *)&peer.ip4.addr, &port)) < 0) { DEBUG_ERROR("err=%d, %s", r, beautify_pico_error(pico_err)); } in4.sin_addr.s_addr = peer.ip4.addr; @@ -347,14 +345,14 @@ namespace ZeroTier { memcpy(udp_msg_buf, &len, sizeof(int32_t)); // len: sockaddr+payload memcpy(udp_msg_buf + sizeof(int32_t), &in4, sizeof(struct sockaddr_storage)); // sockaddr memcpy(udp_msg_buf + sizeof(int32_t) + sizeof(struct sockaddr_storage), &udp_payload_buf, r); // payload - if((w = write(vs->sdk_fd, udp_msg_buf, tot_len)) < 0) { + if ((w = write(vs->sdk_fd, udp_msg_buf, tot_len)) < 0) { DEBUG_ERROR("write()=%d, errno=%d", w, errno); } } - if(vs->socket_family == AF_INET6) { + if (vs->socket_family == AF_INET6) { struct sockaddr_in6 in6; char udp_payload_buf[ZT_MAX_MTU]; - if((r = pico_socket_recvfrom(s, udp_payload_buf, ZT_SDK_MTU, (void *)&peer.ip6.addr, &port)) < 0) { + if ((r = pico_socket_recvfrom(s, udp_payload_buf, ZT_SDK_MTU, (void *)&peer.ip6.addr, &port)) < 0) { DEBUG_ERROR("err=%d, %s", r, beautify_pico_error(pico_err)); } memcpy(&(in6.sin6_addr.s6_addr), &(peer.ip6.addr), sizeof(peer.ip6.addr)); @@ -367,7 +365,7 @@ namespace ZeroTier { memcpy(udp_msg_buf, &len, sizeof(int32_t)); // len: sockaddr+payload memcpy(udp_msg_buf + sizeof(int32_t), &in6, sizeof(struct sockaddr_storage)); // sockaddr memcpy(udp_msg_buf + sizeof(int32_t) + sizeof(struct sockaddr_storage), &udp_payload_buf, r); // payload - if((w = write(vs->sdk_fd, udp_msg_buf, tot_len)) < 0) { + if ((w = write(vs->sdk_fd, udp_msg_buf, tot_len)) < 0) { DEBUG_ERROR("write()=%d, errno=%d", w, errno); } } @@ -376,40 +374,40 @@ namespace ZeroTier { void picoTCP::pico_cb_tcp_write(VirtualTap *tap, struct pico_socket *s) { VirtualSocket *vs = (VirtualSocket*)(((VirtualBindingPair*)s->priv)->vs); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("s->priv yielded no valid vs"); handle_general_failure(); return; } Mutex::Lock _l(vs->_tx_m); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid VirtualSocket"); handle_general_failure(); return; } int txsz = vs->TXbuf->count(); - if(txsz <= 0) + if (txsz <= 0) return; //DEBUG_INFO("TXbuf->count()=%d", vs->TXbuf->count()); int r, max_write_len = std::min(std::min(txsz, ZT_SDK_MTU),ZT_STACK_SOCKET_WR_MAX); - if((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) { + if ((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) { DEBUG_ERROR("unable to write to pico_socket=%p, err=%d, pico_err=%d, %s", vs->picosock, r, pico_err, beautify_pico_error(pico_err)); handle_general_failure(); return; } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { DEBUG_TRANS("len=%5d buf_len=%13d [VSTXBF --> NSPICO] proto=0x%04x (TCP)", r, vs->TXbuf->count(), PICO_PROTO_TCP); } - if(r == 0) { + if (r == 0) { // DEBUG_ERROR("err=%d, pico_err=%d, %s", r, pico_err, beautify_pico_error(pico_err)); // This is a peciliarity of the picoTCP network stack, if we receive no error code, and the size of // the byte stream written is 0, this is an indication that the buffer for this pico_socket is too small // DEBUG_ERROR("pico_socket buffer is too small (adjust ZT_STACK_SOCKET_TX_SZ, ZT_STACK_SOCKET_RX_SZ)"); // handle_general_failure(); } - if(r>0) + if (r>0) vs->TXbuf->consume(r); } @@ -430,7 +428,7 @@ namespace ZeroTier { // PICO_SOCK_EV_ERR - triggered when an error occurs. if (ev & PICO_SOCK_EV_ERR) { - if(pico_err == PICO_ERR_ECONNRESET) { + if (pico_err == PICO_ERR_ECONNRESET) { DEBUG_ERROR("PICO_ERR_ECONNRESET"); } //DEBUG_ERROR("PICO_SOCK_EV_ERR, err=%s, picosock=%p, app_fd=%d, sdk_fd=%d", @@ -442,7 +440,7 @@ namespace ZeroTier { // keep the VirtualSocket half-open (only for sending) after the FIN packet has been received, // allowing new data to be sent in the TCP CLOSE WAIT state. if (ev & PICO_SOCK_EV_CLOSE) { - if((err = pico_socket_close(s)) < 0) { + if ((err = pico_socket_close(s)) < 0) { DEBUG_ERROR("pico_socket_close()=%d, pico_err=%d, %s", err, pico_err, beautify_pico_error(pico_err)); } DEBUG_EXTRA("PICO_SOCK_EV_CLOSE (socket closure) err=%d (%s), picosock=%p", pico_err, beautify_pico_error(pico_err), s); @@ -454,14 +452,14 @@ namespace ZeroTier { // --- handle non-error events --- VirtualBindingPair *vbp = (VirtualBindingPair*)(s->priv); - if(!vbp) { + if (vbp == NULL) { DEBUG_ERROR("s->priv yielded no valid VirtualBindingPair"); handle_general_failure(); return; } VirtualTap *tap = static_cast(vbp->tap); VirtualSocket *vs = static_cast(vbp->vs); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid VirtualSocket"); handle_general_failure(); return; @@ -472,24 +470,21 @@ 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 == ZT_SOCK_STATE_LISTENING) { uint16_t port; struct pico_socket *client_psock = nullptr; struct pico_ip4 orig4; struct pico_ip6 orig6; - - if(vs->socket_family == AF_INET) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 + if (vs->socket_family == AF_INET) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 client_psock = pico_socket_accept(s, &orig4, &port); } - if(vs->socket_family == AF_INET6) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 + if (vs->socket_family == AF_INET6) { // NOTE: p->net->proto_number == PICO_PROTO_IPV4 client_psock = pico_socket_accept(s, &orig6, &port); } - if(!client_psock) { + if (client_psock == NULL) { DEBUG_ERROR("pico_socket_accept(): pico_socket=%p, pico_err=%d, %s", s, pico_err, beautify_pico_error(pico_err)); return; } - // Create a new VirtualSocket and add it to the queue, // some time in the future a call to zts_multiplex_accept() will pick up // this new VirtualSocket, add it to the VirtualSocket list and return its @@ -497,9 +492,8 @@ namespace ZeroTier { VirtualSocket *new_vs = new VirtualSocket(); new_vs->socket_type = SOCK_STREAM; new_vs->picosock = client_psock; - // TODO: Condense this - if(vs->socket_family == AF_INET) { + if (vs->socket_family == AF_INET) { char addrstr[INET_ADDRSTRLEN]; struct sockaddr_storage ss4; struct sockaddr_in *in4 = (struct sockaddr_in *)&ss4; @@ -512,7 +506,7 @@ namespace ZeroTier { inet.fromString(addrstr); new_vs->tap = getTapByAddr(&inet); // assign to tap based on incoming address } - if(vs->socket_family == AF_INET6) { + if (vs->socket_family == AF_INET6) { char addrstr[INET6_ADDRSTRLEN]; struct sockaddr_in6 in6; memcpy(&(in6.sin6_addr.s6_addr), &orig6, sizeof(in6.sin6_addr.s6_addr)); @@ -524,7 +518,7 @@ namespace ZeroTier { inet.fromString(addrstr); new_vs->tap = getTapByAddr(&inet); // assign to tap based on incoming address } - if(!new_vs->tap) { + if (new_vs->tap == NULL) { DEBUG_ERROR("no valid VirtualTap could be found"); handle_general_failure(); return; @@ -535,7 +529,7 @@ 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 != ZT_SOCK_STATE_LISTENING) { // set state so socket multiplexer logic will pick this up vs->state = ZT_SOCK_STATE_UNHANDLED_CONNECTED; } @@ -543,9 +537,9 @@ namespace ZeroTier { // PICO_SOCK_EV_RD - triggered when new data arrives on the socket. A new receive action // can be taken by the socket owner because this event indicates there is new data to receive. if (ev & PICO_SOCK_EV_RD) { - if(vs->socket_type==SOCK_STREAM) + if (vs->socket_type==SOCK_STREAM) pico_cb_tcp_read(tap, s); - if(vs->socket_type==SOCK_DGRAM) + if (vs->socket_type==SOCK_DGRAM) pico_cb_udp_read(tap, s); } // PICO_SOCK_EV_WR - triggered when ready to write to the socket. Issuing a write/send call @@ -559,7 +553,7 @@ namespace ZeroTier { { //_picostack_driver_lock.lock(); VirtualTap *tap = static_cast(dev->tap); - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("invalid dev->tap"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; @@ -570,7 +564,7 @@ namespace ZeroTier { MAC dest_mac; src_mac.setTo(ethhdr->saddr, 6); dest_mac.setTo(ethhdr->daddr, 6); - if(ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { + if (ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZT_ID_LEN]; mac2str(macBuf, ZT_MAC_ADDRSTRLEN, ethhdr->daddr); ZeroTier::MAC mac; @@ -582,16 +576,16 @@ namespace ZeroTier { struct pico_tcp_hdr *hdr; void * tcp_hdr_ptr; - if(Utils::ntoh(ethhdr->proto) == 0x86dd) { // tcp, ipv6 + if (Utils::ntoh(ethhdr->proto) == 0x86dd) { // tcp, ipv6 tcp_hdr_ptr = ðhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR; } - if(Utils::ntoh(ethhdr->proto) == 0x0800) // tcp + if (Utils::ntoh(ethhdr->proto) == 0x0800) // tcp { tcp_hdr_ptr = &buf + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR; hdr = (struct pico_tcp_hdr *)tcp_hdr_ptr; - if(hdr) { + if (hdr) { char *flag_ptr = flagbuf; if (hdr->flags & PICO_TCP_PSH) { @@ -632,7 +626,7 @@ namespace ZeroTier { const void *data,unsigned int len) { //_picostack_driver_lock.lock(); - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("invalid tap"); handle_general_failure(); return; @@ -647,7 +641,7 @@ namespace ZeroTier { ethhdr.proto = Utils::hton((uint16_t)etherType); int32_t msg_len = len + sizeof(int32_t) + sizeof(struct pico_eth_hdr); - if(ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { + if (ZT_DEBUG_LEVEL >= ZT_MSG_TRANSFER) { char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZT_ID_LEN]; mac2str(macBuf, sizeof(macBuf), ethhdr.saddr); ZeroTier::MAC mac; @@ -659,15 +653,15 @@ namespace ZeroTier { struct pico_tcp_hdr *hdr; void * tcp_hdr_ptr; - if(etherType == 0x86dd) { // tcp, ipv6 + if (etherType == 0x86dd) { // tcp, ipv6 tcp_hdr_ptr = ðhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR; } - if(etherType == 0x0800) // tcp, ipv4 + if (etherType == 0x0800) // tcp, ipv4 { tcp_hdr_ptr = ðhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR; hdr = (struct pico_tcp_hdr *)tcp_hdr_ptr; - if(hdr) { + if (hdr) { char *flag_ptr = flagbuf; if (hdr->flags & PICO_TCP_PSH) { @@ -707,7 +701,7 @@ namespace ZeroTier { int pico_eth_poll(struct pico_device *dev, int loop_score) { VirtualTap *tap = static_cast(dev->tap); - if(!tap) { + if (tap == NULL) { DEBUG_ERROR("invalid dev->tap"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; @@ -721,11 +715,11 @@ namespace ZeroTier { len = 0; // get frame len memcpy(&len, tap->pico_frame_rxbuf, sizeof(int32_t)); - if(len > sizeof(int32_t)) { // meaning, since we package the len in the msg, we don't want to recv a 0-(sizeof(int32_t)) sized frame + if (len > sizeof(int32_t)) { // meaning, since we package the len in the msg, we don't want to recv a 0-(sizeof(int32_t)) sized frame memcpy(frame, tap->pico_frame_rxbuf + sizeof(int32_t), len-(sizeof(int32_t)) ); // get frame data memmove(tap->pico_frame_rxbuf, tap->pico_frame_rxbuf + len, MAX_PICO_FRAME_RX_BUF_SZ-len); // shift buffer - if((err = pico_stack_recv(dev, (uint8_t*)frame, (len-sizeof(int32_t)))) < 0) { - if(picostack) { + if ((err = pico_stack_recv(dev, (uint8_t*)frame, (len-sizeof(int32_t)))) < 0) { + if (picostack) { DEBUG_ERROR("pico_stack_recv(), err=%d, pico_err=%d, %s", err, pico_err, picostack->beautify_pico_error(pico_err)); } } @@ -743,32 +737,31 @@ namespace ZeroTier { int picoTCP::pico_Socket(struct pico_socket **p, int socket_family, int socket_type, int protocol) { int err = 0; - if(!can_provision_new_socket(socket_type)) { + if (can_provision_new_socket(socket_type) == false) { DEBUG_ERROR("cannot create additional socket, see PICO_MAX_TIMERS. current=%d", pico_ntimers()); errno = EMFILE; err = -1; } - else - { + else { int protocol_version = 0; struct pico_socket *psock; - if(socket_family == AF_INET) + if (socket_family == AF_INET) protocol_version = PICO_PROTO_IPV4; - if(socket_family == AF_INET6) + if (socket_family == AF_INET6) protocol_version = PICO_PROTO_IPV6; - if(socket_type == SOCK_DGRAM) { + if (socket_type == SOCK_DGRAM) { psock = pico_socket_open( protocol_version, PICO_PROTO_UDP, &ZeroTier::picoTCP::pico_cb_socket_ev); - if(psock) { + if (psock) { // configure size of UDP SND/RCV buffers // TODO } } - if(socket_type == SOCK_STREAM) { + if (socket_type == SOCK_STREAM) { psock = pico_socket_open( protocol_version, PICO_PROTO_TCP, &ZeroTier::picoTCP::pico_cb_socket_ev); - if(psock) { + if (psock) { // configure size of TCP SND/RCV buffers int tx_buf_sz = ZT_STACK_TCP_SOCKET_TX_SZ; int rx_buf_sz = ZT_STACK_TCP_SOCKET_RX_SZ; @@ -777,16 +770,16 @@ namespace ZeroTier { // int value = 1; // pico_socket_setoption(psock, PICO_TCP_NODELAY, &value); - if((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_SNDBUF, &tx_buf_sz)) < 0) + if ((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_SNDBUF, &tx_buf_sz)) < 0) DEBUG_ERROR("unable to set SNDBUF size, err=%d, pico_err=%d, %s", t_err, pico_err, beautify_pico_error(pico_err)); - if((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_RCVBUF, &rx_buf_sz)) < 0) + if ((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_RCVBUF, &rx_buf_sz)) < 0) DEBUG_ERROR("unable to set RCVBUF size, err=%d, pico_err=%d, %s", t_err, pico_err, beautify_pico_error(pico_err)); - if(ZT_SOCK_BEHAVIOR_LINGER) { + if (ZT_SOCK_BEHAVIOR_LINGER) { int linger_time_ms = ZT_SOCK_BEHAVIOR_LINGER_TIME; - if((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_LINGER, &linger_time_ms)) < 0) + if ((t_err = pico_socket_setoption(psock, PICO_SOCKET_OPT_LINGER, &linger_time_ms)) < 0) DEBUG_ERROR("unable to set LINGER, err=%d, pico_err=%d, %s", t_err, pico_err, beautify_pico_error(pico_err)); } @@ -799,13 +792,13 @@ namespace ZeroTier { int picoTCP::pico_Connect(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen) { - if(!vs || !vs->picosock) { + if (vs == NULL || vs->picosock == NULL) { DEBUG_ERROR("invalid vs or vs->picosock"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; } int err = 0; - if(vs->socket_family == AF_INET) { + if (vs->socket_family == AF_INET) { struct pico_ip4 zaddr; memset(&zaddr, 0, sizeof (struct pico_ip4)); struct sockaddr_in *in4 = (struct sockaddr_in*)addr; @@ -814,23 +807,23 @@ namespace ZeroTier { uint32_t ipval = 0; pico_string_to_ipv4(ipv4_str, &ipval); zaddr.addr = ipval; - if(vs->socket_type == SOCK_STREAM) { // connect is an implicit call for non-connection-based VirtualSockets + if (vs->socket_type == SOCK_STREAM) { // connect is an implicit call for non-connection-based VirtualSockets DEBUG_EXTRA("connecting to addr=%s port=%d", ipv4_str, Utils::ntoh(in4->sin_port)); } err = pico_socket_connect(vs->picosock, &zaddr, in4->sin_port); } - if(vs->socket_family == AF_INET6) { + if (vs->socket_family == AF_INET6) { struct pico_ip6 zaddr; struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; char ipv6_str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &(in6->sin6_addr), ipv6_str, INET6_ADDRSTRLEN); pico_string_to_ipv6(ipv6_str, zaddr.addr); - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { DEBUG_EXTRA("connecting to addr=%s port=%d", ipv6_str, Utils::ntoh(in6->sin6_port)); } err = pico_socket_connect(vs->picosock, &zaddr, in6->sin6_port); } - if(err) { + if (err) { DEBUG_ERROR("error connecting pico_socket=%p, err=%d, pico_err=%d, %s", vs->picosock, err, pico_err, beautify_pico_error(pico_err)); return map_pico_err_to_errno(pico_err); @@ -841,13 +834,13 @@ namespace ZeroTier { int picoTCP::pico_Bind(VirtualSocket *vs, const struct sockaddr *addr, socklen_t addrlen) { - if(!vs || !vs->picosock) { + if (vs == NULL || vs->picosock == NULL) { DEBUG_ERROR("invalid vs or vs->picosock"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; } int err = 0; - if(vs->socket_family == AF_INET) { + if (vs->socket_family == AF_INET) { struct pico_ip4 zaddr; uint32_t tempaddr; memset(&zaddr, 0, sizeof (struct pico_ip4)); @@ -859,7 +852,7 @@ namespace ZeroTier { DEBUG_EXTRA("binding to addr=%s port=%d", ipv4_str, Utils::ntoh(in4->sin_port)); err = pico_socket_bind(vs->picosock, &zaddr, (uint16_t *)&(in4->sin_port)); } - if(vs->socket_family == AF_INET6) { + if (vs->socket_family == AF_INET6) { struct pico_ip6 pip6; struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; char ipv6_str[INET6_ADDRSTRLEN]; @@ -869,7 +862,7 @@ namespace ZeroTier { DEBUG_EXTRA("binding to addr=%s port=%d", ipv6_str, Utils::ntoh(in6->sin6_port)); err = pico_socket_bind(vs->picosock, &pip6, (uint16_t *)&(in6->sin6_port)); } - if(err < 0) { + if (err < 0) { DEBUG_ERROR("unable to bind pico_socket=%p, err=%d, pico_err=%d, %s", (vs->picosock), err, pico_err, beautify_pico_error(pico_err)); return map_pico_err_to_errno(pico_err); @@ -879,13 +872,13 @@ namespace ZeroTier { int picoTCP::pico_Listen(VirtualSocket *vs, int backlog) { - if(!vs || !vs->picosock) { + if (vs == NULL || vs->picosock == NULL) { DEBUG_ERROR("invalid vs or vs->picosock"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; } int err = 0; - if((err = pico_socket_listen(vs->picosock, backlog)) < 0) { + if ((err = pico_socket_listen(vs->picosock, backlog)) < 0) { DEBUG_ERROR("error putting pico_socket=%p into listening state. err=%d, pico_err=%d, %s", vs->picosock, err, pico_err, beautify_pico_error(pico_err)); return map_pico_err_to_errno(pico_err); @@ -896,14 +889,14 @@ namespace ZeroTier { VirtualSocket* picoTCP::pico_Accept(VirtualSocket *vs) { - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return NULL; } // Retreive first of queued VirtualSockets from parent VirtualSocket VirtualSocket *new_vs = NULL; - if(vs->_AcceptedConnections.size()) { + if (vs->_AcceptedConnections.size()) { new_vs = vs->_AcceptedConnections.front(); vs->_AcceptedConnections.pop(); } @@ -922,29 +915,29 @@ namespace ZeroTier { int err = 0; // TODO: Add RingBuffer overflow checks // DEBUG_INFO("vs=%p, len=%d", vs, len); - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; } Mutex::Lock _l(vs->_tx_m); - if(len <= 0) { + if (len <= 0) { DEBUG_ERROR("invalid write length (len=%d)", len); handle_general_failure(); return -1; } - if(vs->picosock->state & PICO_SOCKET_STATE_CLOSED){ + if (vs->picosock->state & PICO_SOCKET_STATE_CLOSED) { DEBUG_ERROR("socket is CLOSED, this wrpico_cb_tcp_writeite() will fail"); return -1; } - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid VirtualSocket (len=%d)", len); handle_general_failure(); return -1; } - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { int r; - if((r = pico_socket_write(vs->picosock, data, len)) < 0) { + if ((r = pico_socket_write(vs->picosock, data, len)) < 0) { DEBUG_ERROR("unable to write to picosock=%p, err=%d, pico_err=%d, %s", vs->picosock, r, pico_err, beautify_pico_error(pico_err)); err = -1; @@ -953,9 +946,9 @@ namespace ZeroTier { err = r; // successful write } } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { int original_txsz = vs->TXbuf->count(); - if(original_txsz + len >= ZT_TCP_TX_BUF_SZ) { + if (original_txsz + len >= ZT_TCP_TX_BUF_SZ) { DEBUG_ERROR("txsz=%d, len=%d", original_txsz, len); DEBUG_ERROR("TX buffer is too small, try increasing ZT_TCP_TX_BUF_SZ in libzt.h"); handle_general_failure(); @@ -970,7 +963,7 @@ namespace ZeroTier { } int txsz = vs->TXbuf->count(); int r, max_write_len = std::min(std::min(txsz, ZT_SDK_MTU),ZT_STACK_SOCKET_WR_MAX); - if((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) { + if ((r = pico_socket_write(vs->picosock, vs->TXbuf->get_buf(), max_write_len)) < 0) { DEBUG_ERROR("unable to write to picosock=%p, err=%d, pico_err=%d, %s", vs->picosock, r, pico_err, beautify_pico_error(pico_err)); err = -1; @@ -978,13 +971,13 @@ namespace ZeroTier { else { err = r; // successful write } - if(r>0){ + if (r>0) { vs->TXbuf->consume(r); } - if(vs->socket_type == SOCK_STREAM) { + if (vs->socket_type == SOCK_STREAM) { DEBUG_TRANS("len=%5d buf_len=%13d [VSTXBF --> NSPICO] proto=0x%04x (TCP)", r, vs->TXbuf->count(), PICO_PROTO_TCP); } - if(vs->socket_type == SOCK_DGRAM) { + if (vs->socket_type == SOCK_DGRAM) { DEBUG_TRANS("len=%5d buf_len= [APPFDS --> NSPICO] proto=0x%04x (UDP)", r, PICO_PROTO_TCP); } } @@ -993,19 +986,19 @@ namespace ZeroTier { int picoTCP::pico_Close(VirtualSocket *vs) { - if(!vs) { + if (vs == NULL) { DEBUG_ERROR("invalid vs"); handle_general_failure(); return ZT_ERR_GENERAL_FAILURE; } DEBUG_EXTRA("vs=%p, picosock=%p, fd=%d", vs, vs->picosock, vs->app_fd); - if(!vs || !vs->picosock) + if (vs == NULL || vs->picosock == NULL) return ZT_ERR_GENERAL_FAILURE; int err = 0; Mutex::Lock _l(vs->tap->_tcpconns_m); - if(vs->closure_ts != -1) // it was closed at some point in the past, it'll work itself out + if (vs->closure_ts != -1) // it was closed at some point in the past, it'll work itself out return ZT_ERR_OK; - if((err = pico_socket_close(vs->picosock)) < 0) { + if ((err = pico_socket_close(vs->picosock)) < 0) { errno = pico_err; DEBUG_ERROR("error closing pico_socket, err=%d, pico_err=%s, %s", err, pico_err, beautify_pico_error(pico_err)); @@ -1016,16 +1009,16 @@ namespace ZeroTier { int picoTCP::pico_Shutdown(VirtualSocket *vs, int how) { int err = 0, mode = 0; - if(how == SHUT_RD) { + if (how == SHUT_RD) { mode = PICO_SHUT_RD; } - if(how == SHUT_WR) { + if (how == SHUT_WR) { mode = PICO_SHUT_WR; } - if(how == SHUT_RDWR) { + if (how == SHUT_RDWR) { mode = PICO_SHUT_RDWR; } - if((err = pico_socket_shutdown(vs->picosock, mode)) < 0) { + if ((err = pico_socket_shutdown(vs->picosock, mode)) < 0) { DEBUG_ERROR("error while shutting down socket, fd=%d, pico_err=%d, %s", vs->app_fd, pico_err, beautify_pico_error(pico_err)); } return err; @@ -1033,82 +1026,82 @@ namespace ZeroTier { int picoTCP::map_pico_err_to_errno(int err) { - if(err == PICO_ERR_NOERR) { errno = 0; return 0; } // - if(err == PICO_ERR_EPERM) { errno = ENXIO; } - if(err == PICO_ERR_ENOENT) { errno = ENXIO; } - if(err == PICO_ERR_EINTR) { errno = ENXIO; } - if(err == PICO_ERR_EIO) { errno = ENXIO; } - if(err == PICO_ERR_ENXIO) { errno = ENXIO; } // - if(err == PICO_ERR_EAGAIN) { errno = ENXIO; } - if(err == PICO_ERR_ENOMEM) { errno = ENOMEM; } // - if(err == PICO_ERR_EACCESS) { errno = ENXIO; } - if(err == PICO_ERR_EFAULT) { errno = ENXIO; } - if(err == PICO_ERR_EBUSY) { errno = ENXIO; } - if(err == PICO_ERR_EEXIST) { errno = ENXIO; } - if(err == PICO_ERR_EINVAL) { errno = EINVAL; } // - if(err == PICO_ERR_ENONET) { errno = ENXIO; } - if(err == PICO_ERR_EPROTO) { errno = ENXIO; } - if(err == PICO_ERR_ENOPROTOOPT) { errno = ENXIO; } - if(err == PICO_ERR_EPROTONOSUPPORT) { errno = ENXIO; } - if(err == PICO_ERR_EOPNOTSUPP) { errno = ENXIO; } - if(err == PICO_ERR_EADDRINUSE) { errno = ENXIO; } - if(err == PICO_ERR_EADDRNOTAVAIL) { errno = ENXIO; } - if(err == PICO_ERR_ENETDOWN) { errno = ENXIO; } - if(err == PICO_ERR_ENETUNREACH) { errno = ENXIO; } - if(err == PICO_ERR_ECONNRESET) { errno = ENXIO; } - if(err == PICO_ERR_EISCONN) { errno = ENXIO; } - if(err == PICO_ERR_ENOTCONN) { errno = ENXIO; } - if(err == PICO_ERR_ESHUTDOWN) { errno = ENXIO; } - if(err == PICO_ERR_ETIMEDOUT) { errno = ENXIO; } - if(err == PICO_ERR_ECONNREFUSED) { errno = ENXIO; } - if(err == PICO_ERR_EHOSTDOWN) { errno = ENXIO; } - if(err == PICO_ERR_EHOSTUNREACH) { errno = ENXIO; } + if (err == PICO_ERR_NOERR) { errno = 0; return 0; } // + if (err == PICO_ERR_EPERM) { errno = ENXIO; } + if (err == PICO_ERR_ENOENT) { errno = ENXIO; } + if (err == PICO_ERR_EINTR) { errno = ENXIO; } + if (err == PICO_ERR_EIO) { errno = ENXIO; } + if (err == PICO_ERR_ENXIO) { errno = ENXIO; } // + if (err == PICO_ERR_EAGAIN) { errno = ENXIO; } + if (err == PICO_ERR_ENOMEM) { errno = ENOMEM; } // + if (err == PICO_ERR_EACCESS) { errno = ENXIO; } + if (err == PICO_ERR_EFAULT) { errno = ENXIO; } + if (err == PICO_ERR_EBUSY) { errno = ENXIO; } + if (err == PICO_ERR_EEXIST) { errno = ENXIO; } + if (err == PICO_ERR_EINVAL) { errno = EINVAL; } // + if (err == PICO_ERR_ENONET) { errno = ENXIO; } + if (err == PICO_ERR_EPROTO) { errno = ENXIO; } + if (err == PICO_ERR_ENOPROTOOPT) { errno = ENXIO; } + if (err == PICO_ERR_EPROTONOSUPPORT) { errno = ENXIO; } + if (err == PICO_ERR_EOPNOTSUPP) { errno = ENXIO; } + if (err == PICO_ERR_EADDRINUSE) { errno = ENXIO; } + if (err == PICO_ERR_EADDRNOTAVAIL) { errno = ENXIO; } + if (err == PICO_ERR_ENETDOWN) { errno = ENXIO; } + if (err == PICO_ERR_ENETUNREACH) { errno = ENXIO; } + if (err == PICO_ERR_ECONNRESET) { errno = ENXIO; } + if (err == PICO_ERR_EISCONN) { errno = ENXIO; } + if (err == PICO_ERR_ENOTCONN) { errno = ENXIO; } + if (err == PICO_ERR_ESHUTDOWN) { errno = ENXIO; } + if (err == PICO_ERR_ETIMEDOUT) { errno = ENXIO; } + if (err == PICO_ERR_ECONNREFUSED) { errno = ENXIO; } + if (err == PICO_ERR_EHOSTDOWN) { errno = ENXIO; } + if (err == PICO_ERR_EHOSTUNREACH) { errno = ENXIO; } return -1; } char *picoTCP::beautify_pico_error(int err) { - if(err== 0) return (char*)"PICO_ERR_NOERR"; - if(err== 1) return (char*)"PICO_ERR_EPERM"; - if(err== 2) return (char*)"PICO_ERR_ENOENT"; + if (err== 0) return (char*)"PICO_ERR_NOERR"; + if (err== 1) return (char*)"PICO_ERR_EPERM"; + if (err== 2) return (char*)"PICO_ERR_ENOENT"; // ... - if(err== 4) return (char*)"PICO_ERR_EINTR"; - if(err== 5) return (char*)"PICO_ERR_EIO"; - if(err== 6) return (char*)"PICO_ERR_ENXIO (no such device or address)"; + if (err== 4) return (char*)"PICO_ERR_EINTR"; + if (err== 5) return (char*)"PICO_ERR_EIO"; + if (err== 6) return (char*)"PICO_ERR_ENXIO (no such device or address)"; // ... - if(err== 11) return (char*)"PICO_ERR_EAGAIN"; - if(err== 12) return (char*)"PICO_ERR_ENOMEM (not enough space)"; - if(err== 13) return (char*)"PICO_ERR_EACCESS"; - if(err== 14) return (char*)"PICO_ERR_EFAULT"; + if (err== 11) return (char*)"PICO_ERR_EAGAIN"; + if (err== 12) return (char*)"PICO_ERR_ENOMEM (not enough space)"; + if (err== 13) return (char*)"PICO_ERR_EACCESS"; + if (err== 14) return (char*)"PICO_ERR_EFAULT"; // ... - if(err== 16) return (char*)"PICO_ERR_EBUSY"; - if(err== 17) return (char*)"PICO_ERR_EEXIST"; + if (err== 16) return (char*)"PICO_ERR_EBUSY"; + if (err== 17) return (char*)"PICO_ERR_EEXIST"; // ... - if(err== 22) return (char*)"PICO_ERR_EINVAL (invalid argument)"; + if (err== 22) return (char*)"PICO_ERR_EINVAL (invalid argument)"; // ... - if(err== 64) return (char*)"PICO_ERR_ENONET"; + if (err== 64) return (char*)"PICO_ERR_ENONET"; // ... - if(err== 71) return (char*)"PICO_ERR_EPROTO"; + if (err== 71) return (char*)"PICO_ERR_EPROTO"; // ... - if(err== 92) return (char*)"PICO_ERR_ENOPROTOOPT"; - if(err== 93) return (char*)"PICO_ERR_EPROTONOSUPPORT"; + if (err== 92) return (char*)"PICO_ERR_ENOPROTOOPT"; + if (err== 93) return (char*)"PICO_ERR_EPROTONOSUPPORT"; // ... - if(err== 95) return (char*)"PICO_ERR_EOPNOTSUPP"; - if(err== 98) return (char*)"PICO_ERR_EADDRINUSE"; - if(err== 99) return (char*)"PICO_ERR_EADDRNOTAVAIL"; - if(err==100) return (char*)"PICO_ERR_ENETDOWN"; - if(err==101) return (char*)"PICO_ERR_ENETUNREACH"; + if (err== 95) return (char*)"PICO_ERR_EOPNOTSUPP"; + if (err== 98) return (char*)"PICO_ERR_EADDRINUSE"; + if (err== 99) return (char*)"PICO_ERR_EADDRNOTAVAIL"; + if (err==100) return (char*)"PICO_ERR_ENETDOWN"; + if (err==101) return (char*)"PICO_ERR_ENETUNREACH"; // ... - if(err==104) return (char*)"PICO_ERR_ECONNRESET"; + if (err==104) return (char*)"PICO_ERR_ECONNRESET"; // ... - if(err==106) return (char*)"PICO_ERR_EISCONN"; - if(err==107) return (char*)"PICO_ERR_ENOTCONN"; - if(err==108) return (char*)"PICO_ERR_ESHUTDOWN"; + if (err==106) return (char*)"PICO_ERR_EISCONN"; + if (err==107) return (char*)"PICO_ERR_ENOTCONN"; + if (err==108) return (char*)"PICO_ERR_ESHUTDOWN"; // ... - if(err==110) return (char*)"PICO_ERR_ETIMEDOUT"; - if(err==111) return (char*)"PICO_ERR_ECONNREFUSED"; - if(err==112) return (char*)"PICO_ERR_EHOSTDOWN"; - if(err==113) return (char*)"PICO_ERR_EHOSTUNREACH"; + if (err==110) return (char*)"PICO_ERR_ETIMEDOUT"; + if (err==111) return (char*)"PICO_ERR_ECONNREFUSED"; + if (err==112) return (char*)"PICO_ERR_EHOSTDOWN"; + if (err==113) return (char*)"PICO_ERR_EHOSTUNREACH"; return (char*)"UNKNOWN_ERROR"; } @@ -1143,89 +1136,89 @@ namespace ZeroTier { static char state_str[512]; char *str_ptr = state_str; - if(state & PICO_SOCKET_STATE_UNDEFINED) { + if (state & PICO_SOCKET_STATE_UNDEFINED) { sprintf(str_ptr, "UNDEFINED "); str_ptr += strlen("UNDEFINED "); } - if(state & PICO_SOCKET_STATE_SHUT_LOCAL) { + if (state & PICO_SOCKET_STATE_SHUT_LOCAL) { sprintf(str_ptr, "SHUT_LOCAL "); str_ptr += strlen("SHUT_LOCAL "); } - if(state & PICO_SOCKET_STATE_SHUT_REMOTE) { + if (state & PICO_SOCKET_STATE_SHUT_REMOTE) { sprintf(str_ptr, "SHUT_REMOTE "); str_ptr += strlen("SHUT_REMOTE "); } - if(state & PICO_SOCKET_STATE_BOUND) { + if (state & PICO_SOCKET_STATE_BOUND) { sprintf(str_ptr, "BOUND "); str_ptr += strlen("BOUND "); } - if(state & PICO_SOCKET_STATE_CONNECTED) { + if (state & PICO_SOCKET_STATE_CONNECTED) { sprintf(str_ptr, "CONNECTED "); str_ptr += strlen("CONNECTED "); } - if(state & PICO_SOCKET_STATE_CLOSING) { + if (state & PICO_SOCKET_STATE_CLOSING) { sprintf(str_ptr, "CLOSING "); str_ptr += strlen("CLOSING "); } - if(state & PICO_SOCKET_STATE_CLOSED) { + if (state & PICO_SOCKET_STATE_CLOSED) { sprintf(str_ptr, "CLOSED "); str_ptr += strlen("CLOSED "); } - if(state & PICO_SOCKET_STATE_TCP) { + if (state & PICO_SOCKET_STATE_TCP) { sprintf(str_ptr, "TCP "); str_ptr += strlen("TCP "); } - if(state & PICO_SOCKET_STATE_TCP_UNDEF) { + if (state & PICO_SOCKET_STATE_TCP_UNDEF) { sprintf(str_ptr, "TCP_UNDEF "); str_ptr += strlen("TCP_UNDEF "); } - if(state & PICO_SOCKET_STATE_TCP_CLOSED) { + if (state & PICO_SOCKET_STATE_TCP_CLOSED) { sprintf(str_ptr, "TCP_CLOSED "); str_ptr += strlen("TCP_CLOSED "); } - if(state & PICO_SOCKET_STATE_TCP_LISTEN) { + if (state & PICO_SOCKET_STATE_TCP_LISTEN) { sprintf(str_ptr, "TCP_LISTEN "); str_ptr += strlen("TCP_LISTEN "); } - if(state & PICO_SOCKET_STATE_TCP_SYN_SENT) { + if (state & PICO_SOCKET_STATE_TCP_SYN_SENT) { sprintf(str_ptr, "TCP_SYN_SENT "); str_ptr += strlen("TCP_SYN_SENT "); } - if(state & PICO_SOCKET_STATE_TCP_SYN_RECV) { + if (state & PICO_SOCKET_STATE_TCP_SYN_RECV) { sprintf(str_ptr, "TCP_SYN_RECV "); str_ptr += strlen("TCP_SYN_RECV "); } - if(state & PICO_SOCKET_STATE_TCP_ESTABLISHED) { + if (state & PICO_SOCKET_STATE_TCP_ESTABLISHED) { sprintf(str_ptr, "TCP_ESTABLISHED "); str_ptr += strlen("TCP_ESTABLISHED "); } - if(state & PICO_SOCKET_STATE_TCP_CLOSE_WAIT) { + if (state & PICO_SOCKET_STATE_TCP_CLOSE_WAIT) { sprintf(str_ptr, "TCP_CLOSE_WAIT "); str_ptr += strlen("TCP_CLOSE_WAIT "); } - if(state & PICO_SOCKET_STATE_TCP_LAST_ACK) { + if (state & PICO_SOCKET_STATE_TCP_LAST_ACK) { sprintf(str_ptr, "TCP_LAST_ACK "); str_ptr += strlen("TCP_LAST_ACK "); } - if(state & PICO_SOCKET_STATE_TCP_FIN_WAIT1) { + if (state & PICO_SOCKET_STATE_TCP_FIN_WAIT1) { sprintf(str_ptr, "TCP_FIN_WAIT1 "); str_ptr += strlen("TCP_FIN_WAIT1 "); } - if(state & PICO_SOCKET_STATE_TCP_FIN_WAIT2) { + if (state & PICO_SOCKET_STATE_TCP_FIN_WAIT2) { sprintf(str_ptr, "TCP_FIN_WAIT2 "); str_ptr += strlen("TCP_FIN_WAIT2 "); } - if(state & PICO_SOCKET_STATE_TCP_CLOSING) { + if (state & PICO_SOCKET_STATE_TCP_CLOSING) { sprintf(str_ptr, "TCP_CLOSING "); str_ptr += strlen("TCP_CLOSING "); } - if(state & PICO_SOCKET_STATE_TCP_TIME_WAIT) { + if (state & PICO_SOCKET_STATE_TCP_TIME_WAIT) { sprintf(str_ptr, "TCP_TIME_WAIT "); str_ptr += strlen("TCP_TIME_WAIT "); } - if(state & PICO_SOCKET_STATE_TCP_ARRAYSIZ) { + if (state & PICO_SOCKET_STATE_TCP_ARRAYSIZ) { sprintf(str_ptr, "TCP_ARRAYSIZ "); str_ptr += strlen("TCP_ARRAYSIZ "); }