diff --git a/include/libzt.h b/include/libzt.h index 07e055c..c471b86 100644 --- a/include/libzt.h +++ b/include/libzt.h @@ -108,6 +108,7 @@ struct zts_ifreq { #define LWIP_APPLICATION_POLL_FREQ 2 #define LWIP_TCP_TIMER_INTERVAL 50 #define LWIP_STATUS_TMR_INTERVAL 500 // How often we check VirtualSocket statuses (in ms) +// #define LWIP_CHKSUM , See: RFC1071 for inspiration #endif /****************************************************************************/ @@ -551,13 +552,6 @@ namespace ZeroTier struct InetAddress; } -/* - * Gets a pointer to a pico_socket given a file descriptor - */ -#if defined(STACK_PICO) -int zts_get_pico_socket(int fd, struct pico_socket **s); -#endif - /* * Whether we can add a new socket or not. Depends on stack in use */ @@ -618,6 +612,11 @@ void add_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSoc */ void del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSocket *vs, int fd); +/* + * Gets a pair of associated virtual objects (VirtualSocket bound to a VirtualTap) + */ +std::pair *get_assigned_virtual_pair(int fd); + /* * Destroys all virtual tap devices */ diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index 21bf84a..87e5231 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -527,6 +527,9 @@ namespace ZeroTier { // Send data to a specified host int VirtualTap::SendTo(VirtualSocket *vs, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen) { + /* FIXME: There is a call to *_Connect for each send, we should probably figure out a better way to do this, + possibly consult the stack for "connection" state */ + // TODO: flags int err = 0; #if defined(STACK_PICO) diff --git a/src/libzt.cpp b/src/libzt.cpp index 57bfb32..91b5a75 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -719,6 +719,7 @@ Linux: [ ] [EOPNOTSUPP] The socket is not of a type that supports the listen() operation. */ int zts_listen(ZT_LISTEN_SIG) { + DEBUG_EXTRA("fd=%d", fd); int err = errno = 0; if(fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; @@ -729,8 +730,8 @@ int zts_listen(ZT_LISTEN_SIG) { errno = EACCES; return -1; } + std::pair *p = get_assigned_virtual_pair(fd); ZeroTier::_multiplexer_lock.lock(); - std::pair *p = ZeroTier::fdmap[fd]; if(!p) { DEBUG_ERROR("unable to locate VirtualSocket pair. did you bind?"); errno = EDESTADDRREQ; @@ -766,7 +767,7 @@ Darwin: */ int zts_accept(ZT_ACCEPT_SIG) { int err = errno = 0; - //DEBUG_EXTRA("fd=%d", fd); + DEBUG_EXTRA("fd=%d", fd); if(fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; return -1; @@ -781,8 +782,7 @@ int zts_accept(ZT_ACCEPT_SIG) { errno = EMFILE; return -1; } - ZeroTier::_multiplexer_lock.lock(); - std::pair *p = ZeroTier::fdmap[fd]; + std::pair *p = get_assigned_virtual_pair(fd); if(!p) { DEBUG_ERROR("unable to locate VirtualSocket pair (did you zts_bind())?"); errno = EBADF; @@ -818,7 +818,7 @@ int zts_accept(ZT_ACCEPT_SIG) { } } if(accepted_vs) { - ZeroTier::fdmap[accepted_vs->app_fd] = new std::pair(accepted_vs, tap); + add_assigned_virtual_socket(tap, accepted_vs, accepted_vs->app_fd); err = accepted_vs->app_fd; } } @@ -830,7 +830,6 @@ int zts_accept(ZT_ACCEPT_SIG) { } } } - ZeroTier::_multiplexer_lock.unlock(); return err; } @@ -911,10 +910,15 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG) return -1; } #if defined(STACK_PICO) - // Disable Nagle's algorithm - struct pico_socket *p = NULL; - err = zts_get_pico_socket(fd, &p); + ZeroTier::VirtualSocket *vs = get_virtual_socket(fd); + if(!vs) { + DEBUG_ERROR("invalid fd=%d", fd); + errno = EBADF; + return -1; + } + struct pico_socket *p = vs->picosock; 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) { @@ -927,6 +931,9 @@ int zts_setsockopt(ZT_SETSOCKOPT_SIG) } err = setsockopt(fd, level, optname, optval, optlen); return err; +#endif +#if defined(STACK_LWIP) + #endif return 0; } @@ -986,7 +993,7 @@ Linux: */ int zts_getpeername(ZT_GETPEERNAME_SIG) { - DEBUG_INFO("fd=%d"); + DEBUG_INFO("fd=%d", fd); int err = errno = 0; if(fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; @@ -1977,45 +1984,6 @@ namespace ZeroTier { /* SDK Socket API Helper functions --- DON'T CALL THESE DIRECTLY */ /****************************************************************************/ -#if defined(STACK_PICO) -int zts_get_pico_socket(int fd, struct pico_socket **s) -{ - int err = 0; - if(!ZeroTier::zt1Service) { - DEBUG_ERROR("cannot locate socket. service not started. call zts_start(path) first"); - errno = EBADF; - err = -1; - } - else { - ZeroTier::_multiplexer_lock.lock(); - // First, look for for unassigned VirtualSockets - ZeroTier::VirtualSocket *vs = ZeroTier::unmap[fd]; - if(vs) - { - *s = vs->picosock; - err = 1; // unassigned - } - else - { - std::pair *p = ZeroTier::fdmap[fd]; - if(!p) - { - DEBUG_ERROR("unable to locate VirtualSocket pair."); - errno = EBADF; - err = -1; - } - else - { - *s = p->first->picosock; - err = 0; // assigned - } - } - ZeroTier::_multiplexer_lock.unlock(); - } - return err; -} -#endif - bool can_provision_new_socket(int socket_type) { #if defined(STACK_PICO) @@ -2247,6 +2215,14 @@ void del_assigned_virtual_socket(ZeroTier::VirtualTap *tap, ZeroTier::VirtualSoc ZeroTier::_multiplexer_lock.unlock(); } +std::pair *get_assigned_virtual_pair(int fd) +{ + ZeroTier::_multiplexer_lock.lock(); + std::pair *p = ZeroTier::fdmap[fd]; + ZeroTier::_multiplexer_lock.unlock(); + return p; +} + void dismantleTaps() { ZeroTier::_vtaps_lock.lock();