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

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

View File

@@ -77,7 +77,6 @@
#if defined(NS_DEBUG)
#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

View File

@@ -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)

View File

@@ -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];

View File

@@ -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;
}

View File

@@ -27,13 +27,12 @@
#include <netinet/in.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <algorithm>
#include <utility>
#include <sys/poll.h>
#include <stdint.h>
#include <utility>
#include <string>
#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,27 +278,29 @@ 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);
}
}
}
void VirtualTap::phyOnUnixData(PhySocket *sock, void **uptr, void *data, ssize_t len)
{
//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<ZT_VirtualNetworkRoute> *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<routes.size(); j++) {
if(via_addr.ipsEqual(null_addr) || target_addr.ipsEqual(null_addr)) {
if (via_addr.ipsEqual(null_addr) || target_addr.ipsEqual(null_addr)) {
found=true;
continue;
}
if(routes[j].first.ipsEqual(target_addr) && routes[j].second.ipsEqual(nm)) {
if (routes[j].first.ipsEqual(target_addr) && routes[j].second.ipsEqual(nm)) {
found=true;
}
}
if(!found) {
if(!via_addr.ipsEqual(null_addr)) {
if (found == false) {
if (via_addr.ipsEqual(null_addr) == false) {
DEBUG_INFO("adding route <target=%s, nm=%s, via=%s>", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3));
routes.push_back(std::pair<ZeroTier::InetAddress,ZeroTier::InetAddress>(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 <target=%s>", routes[i].first.toString(ipbuf), routes[i].second.toString(ipbuf2));
routes.erase(routes.begin() + i);
routeDelete(routes[i].first, routes[i].second);

View File

@@ -35,6 +35,7 @@
#include <utility>
#include <stdexcept>
#include <stdint.h>
#include <errno.h>
#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 {

File diff suppressed because it is too large Load Diff

View File

@@ -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)

View File

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

View File

@@ -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<VirtualTap*>(vbp->tap);
VirtualSocket *vs = static_cast<VirtualSocket*>(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<VirtualTap*>(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 = &ethhdr + 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 = &ethhdr + PICO_SIZE_ETHHDR + PICO_SIZE_IP4HDR;
}
if(etherType == 0x0800) // tcp, ipv4
if (etherType == 0x0800) // tcp, ipv4
{
tcp_hdr_ptr = &ethhdr + 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<VirtualTap*>(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 ");
}