diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index d0e1b45..51b059b 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -249,17 +249,17 @@ namespace ZeroTier { Mutex::Lock _l(_multicastGroups_m); // TODO: get multicast subscriptions std::vector allIps(ips()); - for(std::vector::iterator ip(allIps.begin());ip!=allIps.end();++ip) + for (std::vector::iterator ip(allIps.begin());ip!=allIps.end();++ip) newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip)); std::sort(newGroups.begin(),newGroups.end()); std::unique(newGroups.begin(),newGroups.end()); - for(std::vector::iterator m(newGroups.begin());m!=newGroups.end();++m) { + for (std::vector::iterator m(newGroups.begin());m!=newGroups.end();++m) { if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m)) added.push_back(*m); } - for(std::vector::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) { + for (std::vector::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) { if (!std::binary_search(newGroups.begin(),newGroups.end(),*m)) removed.push_back(*m); } @@ -388,7 +388,7 @@ namespace ZeroTier { void VirtualTap::removeVirtualSocket(VirtualSocket *vs) { Mutex::Lock _l(_tcpconns_m); - for(int i=0; i<_VirtualSockets.size(); i++) { + for (int i=0; i<_VirtualSockets.size(); i++) { if (vs == _VirtualSockets[i]) { _VirtualSockets.erase(_VirtualSockets.begin() + i); break; @@ -733,12 +733,12 @@ namespace ZeroTier { char ipbuf[INET6_ADDRSTRLEN], ipbuf2[INET6_ADDRSTRLEN], ipbuf3[INET6_ADDRSTRLEN]; // TODO: Rework this when we have time // check if pushed route exists in tap (add) - for(int i=0; iat(i).target; via_addr = managed_routes->at(i).via; nm = target_addr.netmask(); - for(int j=0; jat(j).target; via_addr = managed_routes->at(j).via; nm = target_addr.netmask(); diff --git a/src/libzt.cpp b/src/libzt.cpp index 84afd7d..8ea7491 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -141,10 +141,10 @@ void zts_start(const char *path) void zts_simple_start(const char *path, const char *nwid) { zts_start(path); - while(zts_running() == false) { + while (zts_running() == false) { nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); } - while(true) { + while (true) { try { zts_join(nwid); break; @@ -154,7 +154,7 @@ void zts_simple_start(const char *path, const char *nwid) handle_general_failure(); } } - while(zts_has_address(nwid) == false) { + while (zts_has_address(nwid) == false) { nanosleep((const struct timespec[]) {{0, (ZT_API_CHECK_INTERVAL * 1000000)}}, NULL); } } @@ -181,7 +181,7 @@ void zts_join(const char * nwid) { } // provide ZTO service reference to virtual taps // TODO: This might prove to be unreliable, but it works for now - for(int i=0;izt1ServiceRef=(void*)ZeroTier::zt1Service; } @@ -283,7 +283,7 @@ void zts_get_ipv4_address(const char *nwid, char *addrstr, const int addrlen) uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int); if (tap && tap->_ips.size()) { - for(int i=0; i_ips.size(); i++) { + for (int i=0; i_ips.size(); i++) { if (tap->_ips[i].isV4()) { char ipbuf[INET_ADDRSTRLEN]; std::string addr = tap->_ips[i].toString(ipbuf); @@ -305,7 +305,7 @@ void zts_get_ipv6_address(const char *nwid, char *addrstr, const int addrlen) uint64_t nwid_int = strtoull(nwid, NULL, 16); ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int); if (tap && tap->_ips.size()) { - for(int i=0; i_ips.size(); i++) { + for (int i=0; i_ips.size(); i++) { if (tap->_ips[i].isV6()) { char ipbuf[INET6_ADDRSTRLEN]; std::string addr = tap->_ips[i].toString(ipbuf); @@ -350,7 +350,7 @@ int zts_get_peer_address(char *peer, const char *devID) { if (ZeroTier::zt1Service) { ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers(); // uint64_t addr; - for(int i=0; ipeerCount; i++) { + for (int i=0; ipeerCount; i++) { // ZT_Peer *p = &(pl->peers[i]); // DEBUG_INFO("peer[%d] = %lx", i, p->address); } @@ -395,6 +395,7 @@ Darwin: // int socket_family, int socket_type, int protocol int zts_socket(ZT_SOCKET_SIG) { + DEBUG_EXTRA(); int err = errno = 0; if (socket_family < 0 || socket_type < 0 || protocol < 0) { errno = EINVAL; @@ -599,12 +600,12 @@ int zts_connect(ZT_CONNECT_SIG) { } 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); 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) { errno = ECONNRESET; @@ -821,7 +822,7 @@ int zts_accept(ZT_ACCEPT_SIG) { accepted_vs = tap->Accept(vs); } else { // blocking - while(true) { + while (true) { nanosleep((const struct timespec[]) {{0, (ZT_ACCEPT_RECHECK_DELAY * 1000000)}}, NULL); accepted_vs = tap->Accept(vs); if (accepted_vs) @@ -1097,7 +1098,7 @@ Linux / Darwin: int zts_close(ZT_CLOSE_SIG) { - //DEBUG_EXTRA("fd=%d", fd); + DEBUG_EXTRA("fd=%d", fd); int err = errno = 0; if (fd < 0 || fd >= ZT_MAX_SOCKETS) { errno = EBADF; @@ -1667,7 +1668,7 @@ int zts_read(ZT_READ_SIG) { } int zts_write(ZT_WRITE_SIG) { - //DEBUG_TRANS("fd=%d", fd); + DEBUG_TRANS("fd=%d", fd); return write(fd, buf, len); } @@ -2085,7 +2086,7 @@ ZeroTier::VirtualTap *getTapByNWID(uint64_t nwid) { ZeroTier::_vtaps_lock.lock(); ZeroTier::VirtualTap *s, *tap = nullptr; - for(int i=0; i_nwid == nwid) { tap = s; } } @@ -2098,19 +2099,19 @@ ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr) ZeroTier::_vtaps_lock.lock(); ZeroTier::VirtualTap *s, *tap = nullptr; //char ipbuf[64], ipbuf2[64], ipbuf3[64]; - for(int i=0; i_ips.size(); j++) { + for (int j=0; j_ips.size(); j++) { 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)); + //DEBUG_EXTRA("looking at tap %s, --- for <%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2)); if (s->_ips[j].isEqualPrefix(addr) || s->_ips[j].ipsEqual(addr) || s->_ips[j].containsAddress(addr) || (addr->isV6() && ipv6_in_subnet(&s->_ips[j], addr)) ) { - //DEBUG_INFO("selected tap %s, ", s->_dev.c_str(), s->_ips[j].toString(ipbuf)); + //DEBUG_EXTRA("selected tap %s, ", s->_dev.c_str(), s->_ips[j].toString(ipbuf)); ZeroTier::_vtaps_lock.unlock(); return s; } @@ -2120,12 +2121,12 @@ ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr) 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)) { - // DEBUG_INFO("chose tap with route ", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3)); + //DEBUG_EXTRA("chose tap with route ", target.toString(ipbuf), nm.toString(ipbuf2), via.toString(ipbuf3)); ZeroTier::_vtaps_lock.unlock(); return s; } @@ -2140,7 +2141,7 @@ ZeroTier::VirtualTap *getTapByName(char *ifname) { ZeroTier::_vtaps_lock.lock(); ZeroTier::VirtualTap *s, *tap = nullptr; - for(int i=0; i_dev.c_str(), ifname) == false) { tap = s; @@ -2154,7 +2155,7 @@ ZeroTier::VirtualTap *getTapByIndex(int index) { ZeroTier::_vtaps_lock.lock(); ZeroTier::VirtualTap *s, *tap = nullptr; - for(int i=0; iifindex == index) { tap = s; @@ -2318,7 +2319,7 @@ std::pair *get_assigned_virtual void disableTaps() { ZeroTier::_vtaps_lock.lock(); - for(int i=0; i_enabled = false; } @@ -2351,7 +2352,7 @@ void *zts_start_service(void *thread_id) if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) { ptmp.push_back(ZT_PATH_SEPARATOR); } - for(std::vector::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) { + for (std::vector::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) { if (ptmp.length() > 0) { ptmp.push_back(ZT_PATH_SEPARATOR); } @@ -2376,7 +2377,7 @@ void *zts_start_service(void *thread_id) ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp)); // TODO: Better port random range selection int servicePort = 9000 + (randp % 1000); - for(;;) { + for (;;) { ZeroTier::zt1Service = ZeroTier::OneService::newInstance(ZeroTier::homeDir.c_str(),servicePort); switch(ZeroTier::zt1Service->run()) { case ZeroTier::OneService::ONE_STILL_RUNNING: diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 35c4f7c..89edc06 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -70,7 +70,7 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p) ZeroTier::VirtualTap *tap = (ZeroTier::VirtualTap*)netif->state; bufptr = buf; - for(q = p; q != NULL; q = q->next) { + for (q = p; q != NULL; q = q->next) { memcpy(bufptr, q->payload, q->len); bufptr += q->len; totalLength += q->len; @@ -196,24 +196,24 @@ namespace ZeroTier // TODO: These will likely need some sort of locking protection int count = 0; struct tcp_pcb *pcb_ptr = tcp_active_pcbs; // PCBs that can RX/TX data - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; } pcb_ptr = tcp_tw_pcbs; // PCBs in TIME-WAIT state - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; } /* TODO pcb_ptr = tcp_listen_pcbs; - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; DEBUG_ERROR("FOUND --- tcp_listen_pcbs PCB COUNT = %d", count); }*/ pcb_ptr = tcp_bound_pcbs; // PCBs in a bound state - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; } @@ -225,7 +225,7 @@ namespace ZeroTier // TODO: These will likely need some sort of locking protection int count = 0; struct udp_pcb *pcb_ptr = udp_pcbs; - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; } @@ -238,7 +238,7 @@ namespace ZeroTier /* int count = 0; struct raw_pcb *pcb_ptr = raw_pcbs; - while(pcb_ptr) { + while (pcb_ptr) { pcb_ptr = pcb_ptr->next; count++; DEBUG_ERROR("FOUND --- raw_pcbs PCB COUNT = %d", count); @@ -266,7 +266,7 @@ namespace ZeroTier void lwIP::lwip_loop(VirtualTap *tap) { uint64_t prev_tcp_time = 0, prev_discovery_time = 0; - while(tap->_run) + while (tap->_run) { uint64_t now = OSUtils::now(); uint64_t since_tcp = now - prev_tcp_time; @@ -745,7 +745,7 @@ namespace ZeroTier vs->tap->_tcpconns_m.lock(); vs->_rx_m.lock(); // cycle through pbufs and write them to the RX buffer - while(p != NULL) { + while (p != NULL) { if (p->len <= 0) { break; } @@ -853,7 +853,7 @@ namespace ZeroTier memset(udp_payload_buf, 0, sizeof(ZT_SOCKET_MSG_BUF_SZ)); char *msg_ptr = udp_payload_buf; int tot_len = 0; - while(p != NULL) { + while (p != NULL) { if (p->len <= 0) { break; } diff --git a/src/picoTCP.cpp b/src/picoTCP.cpp index 89ed0e2..9724159 100644 --- a/src/picoTCP.cpp +++ b/src/picoTCP.cpp @@ -233,7 +233,7 @@ namespace ZeroTier { void picoTCP::pico_loop(VirtualTap *tap) { - while(tap->_run) + while (tap->_run) { tap->_phy.poll(ZT_PHY_POLL_INTERVAL); //_picostack_driver_lock.lock(); @@ -297,7 +297,7 @@ namespace ZeroTier { handle_general_failure(); } } - while(r > 0); + while (r > 0); } // from stack socket to app socket diff --git a/test/selftest.cpp b/test/selftest.cpp index 9bc3a31..2e95dc3 100644 --- a/test/selftest.cpp +++ b/test/selftest.cpp @@ -24,8 +24,6 @@ * of your own application. */ - // Comprehensive stress test for socket-like API - #include #include #include @@ -51,14 +49,15 @@ #include #include "libzt.h" +#include "Utils.hpp" #define EXIT_ON_FAIL false #define PASSED 1 #define FAILED 0 -#define ECHO_INTERVAL 1000000 // us -#define SLAM_INTERVAL 500000 +#define ECHO_INTERVAL 1000000 // microseconds +#define SLAM_INTERVAL 500000 // microseconds #define WAIT_FOR_TEST_TO_CONCLUDE 0 #define WAIT_FOR_TRANSMISSION_TO_COMPLETE 5 @@ -80,12 +79,15 @@ #define MIN_PORT 5000 #define MAX_PORT 50000 -#define TCP_UNIT_TEST_SIG_4 struct sockaddr_in *addr, int op, int cnt, char *details, bool *passed -#define UDP_UNIT_TEST_SIG_4 struct sockaddr_in *local_addr, struct sockaddr_in *remote_addr, int op, int cnt, char *details, bool *passed - -#define TCP_UNIT_TEST_SIG_6 struct sockaddr_in6 *addr, int op, int cnt, char *details, bool *passed -#define UDP_UNIT_TEST_SIG_6 struct sockaddr_in6 *local_addr, struct sockaddr_in6 *remote_addr, int op, int cnt, char *details, bool *passed +#define TCP_UNIT_TEST_SIG_4 struct sockaddr_in *addr, int op, int cnt, char *details, \ + bool *passed +#define UDP_UNIT_TEST_SIG_4 struct sockaddr_in *local_addr, struct sockaddr_in *remote_addr, \ + int op, int cnt, char *details, bool *passed +#define TCP_UNIT_TEST_SIG_6 struct sockaddr_in6 *addr, int op, int cnt, char *details, \ + bool *passed +#define UDP_UNIT_TEST_SIG_6 struct sockaddr_in6 *local_addr, struct sockaddr_in6 *remote_addr, \ + int op, int cnt, char *details, bool *passed #define ECHOTEST_MODE_RX 333 #define ECHOTEST_MODE_TX 666 @@ -192,9 +194,10 @@ std::map testConf; /* Helper Functions */ /****************************************************************************/ -void displayResults(int *results, int size) { +void displayResults(int *results, int size) +{ int success = 0, failure = 0; - for(int i=0; i tplus_s) { @@ -249,15 +256,24 @@ void wait_until_tplus_s(long int original_time, int tplus_s) { wait_until_tplus(original_time, tplus_s * 1000); } -void generate_random_data(void *buf, size_t n, int min, int max) { +int rand_in_range(int min, int max) +{ + unsigned int seed; + ZeroTier::Utils::getSecureRandom((void*)&seed,sizeof(seed)); + srand(seed); + return min + rand() % static_cast(max - min + 1); +} + +void generate_random_data(void *buf, size_t n, int min, int max) +{ char *b = (char*)buf; - srand((unsigned)time(0)); - for(int i=0; i(max - min + 1)); + for (int i=0; isin_port = htons(port); @@ -556,7 +572,7 @@ void udp_client_4(UDP_UNIT_TEST_SIG_4) return; } struct sockaddr_storage saddr; - while(true) { + while (true) { sleep(1); // tx if ((w = SENDTO(fd, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { @@ -622,7 +638,7 @@ void udp_server_4(UDP_UNIT_TEST_SIG_4) DEBUG_TEST("sending DGRAM(s) to %s : %d", inet_ntoa(remote_addr->sin_addr), ntohs(remote_addr->sin_port)); // tx long int tx_ti = get_now_ts(); - while(true) { + while (true) { sleep(1); if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { DEBUG_ERROR("error sending packet, err=%d", errno); @@ -676,7 +692,7 @@ void udp_client_6(UDP_UNIT_TEST_SIG_6) // start sending UDP packets in the hopes that at least one will be picked up by the server struct sockaddr_storage saddr; - while(true) { + while (true) { // tx if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { DEBUG_ERROR("error sending packet, err=%d", errno); @@ -745,7 +761,7 @@ void udp_server_6(UDP_UNIT_TEST_SIG_6) // once we receive a UDP packet, spend 10 seconds sending responses in the hopes that the client will see // tx long int tx_ti = get_now_ts(); - while(true) { + while (true) { usleep(100000); //DEBUG_TEST("sending UDP packet"); if ((w = SENDTO(fd, msg.c_str(), len, 0, (struct sockaddr *)remote_addr, sizeof(*remote_addr))) < 0) { @@ -805,7 +821,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4) int wrem = cnt, rrem = cnt; // TX long int tx_ti = get_now_ts(); - while(wrem) { + while (wrem) { int next_write = std::min(4096, wrem); signal(SIGPIPE, SIG_IGN); n = WRITE(fd, &txbuf[w], next_write); @@ -820,7 +836,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4) DEBUG_TEST("wrote=%d, reading next...", w); // RX long int rx_ti = 0; - while(rrem) { + while (rrem) { n = READ(fd, &rxbuf[r], rrem); if (rx_ti == 0) { // wait for first message rx_ti = get_now_ts(); @@ -837,7 +853,7 @@ void tcp_client_sustained_4(TCP_UNIT_TEST_SIG_4) err = CLOSE(fd); // Compare RX and TX buffer and detect mismatches bool match = true; - for(int i=0; i 0) { @@ -900,7 +916,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6) DEBUG_TEST("wrote=%d", w); // RX long int rx_ti = 0; - while(rrem) { + while (rrem) { n = READ(fd, &rxbuf[r], rrem); if (rx_ti == 0) { // wait for first message rx_ti = get_now_ts(); @@ -917,7 +933,7 @@ void tcp_client_sustained_6(TCP_UNIT_TEST_SIG_6) err = CLOSE(fd); // Compare RX and TX buffer and detect mismatches bool match = true; - for(int i=0; i 0) { if (rx_ti == 0) { // wait for first message @@ -994,7 +1010,7 @@ void tcp_server_sustained_4(TCP_UNIT_TEST_SIG_4) long int rx_tf = get_now_ts(); DEBUG_TEST("read=%d, writing next...", r); long int tx_ti = get_now_ts(); - while(wrem) { + while (wrem) { int next_write = std::min(1024, wrem); n = WRITE(client_fd, &rxbuf[w], next_write); if (n > 0) { @@ -1067,7 +1083,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6) if (op == TEST_OP_N_BYTES) { int wrem = cnt, rrem = cnt; long int rx_ti = 0; - while(rrem) { + while (rrem) { n = READ(client_fd, &rxbuf[r], rrem); if (n > 0) { if (rx_ti == 0) { // wait for first message @@ -1081,7 +1097,7 @@ void tcp_server_sustained_6(TCP_UNIT_TEST_SIG_6) long int rx_tf = get_now_ts(); DEBUG_TEST("read=%d", r); long int tx_ti = get_now_ts(); - while(wrem) { + while (wrem) { int next_write = std::min(1024, wrem); n = WRITE(client_fd, &rxbuf[w], next_write); if (n > 0) { @@ -1140,7 +1156,7 @@ void udp_client_sustained_4(UDP_UNIT_TEST_SIG_4) return; } int num_to_send = 10; - for(int i=0; i 0) { // TODO: what should be expected for each platform? Should this mirror them? optval = 0; DEBUG_TEST("setting level=%d, optname=%d, optval=%d...", level, optname, optval); - if ((err = SETSOCKOPT(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &optval, (socklen_t)sizeof(int))) < 0) { + if ((err = SETSOCKOPT(fd, level, optname, (char *) &optval, (socklen_t)sizeof(int))) < 0) { DEBUG_ERROR("error while setting on socket"); *passed = false; err = -1; @@ -1661,12 +1678,12 @@ int slam_api_test() if (false) { // open and close SLAM_NUMBER*SLAM_REPEAT sockets - for(int j=0; j used_ports; - for(int j=0; j *routes = zts_get_network_routes(nwid); - for(int i=0; isize(); i++) { + for (int i=0; isize(); i++) { struct sockaddr_in *target = (struct sockaddr_in*)&(routes->at(i).target); struct sockaddr_in *via = (struct sockaddr_in*)&(routes->at(i).via); char target_str[INET6_ADDRSTRLEN]; @@ -1882,7 +1899,7 @@ int random_api_test() int num_operations = 100; char *opbuf = (char*)malloc(num_operations*sizeof(char)); generate_random_data(opbuf, num_operations, 0, 9); - for(int i=0; ifd; + struct sockaddr_in *remote_addr = fdp->remote_addr; + //fprintf(stderr, "fd=%d\n", fd); + int w = 0; + for (int i=0; i