diff --git a/examples/ztproxy/ztproxy.cpp b/examples/ztproxy/ztproxy.cpp index 129ebc7..7b90a8d 100644 --- a/examples/ztproxy/ztproxy.cpp +++ b/examples/ztproxy/ztproxy.cpp @@ -24,27 +24,16 @@ * of your own application. */ -#include -#include -#include #include -#include -#include #include -#include #include #include #include #include #include -#include -#include -#include -#include #include #include -#include #include #include "RingBuffer.hpp" @@ -55,7 +44,8 @@ namespace ZeroTier { typedef void PhySocket; - ZTProxy::ZTProxy(int proxy_listen_port, std::string nwid, std::string path, std::string internal_addr, int internal_port) + ZTProxy::ZTProxy(int proxy_listen_port, std::string nwid, std::string path, std::string internal_addr, + int internal_port) : _enabled(true), _run(true), @@ -74,7 +64,7 @@ namespace ZeroTier { struct sockaddr_in in4; memset(&in4,0,sizeof(in4)); in4.sin_family = AF_INET; - in4.sin_addr.s_addr = Utils::hton((uint32_t)(0x7f000001)); // right now we just listen for TCP @127.0.0.1 + in4.sin_addr.s_addr = Utils::hton((uint32_t)(0x7f000001)); // listen for TCP @127.0.0.1 in4.sin_port = Utils::hton((uint16_t)proxy_listen_port); _tcpListenSocket = _phy.tcpListen((const struct sockaddr *)&in4,this); // IPv6 @@ -86,11 +76,12 @@ namespace ZeroTier { in6.sin6_port = Utils::hton((uint16_t)proxy_listen_port); _tcpListenSocket6 = _phy.tcpListen((const struct sockaddr *)&in6,this); - if(!_tcpListenSocket) + if (!_tcpListenSocket) { DEBUG_ERROR("Error binding on port %d for IPv4 HTTP listen socket", proxy_listen_port); - if(!_tcpListenSocket6) + } + if (!_tcpListenSocket6) { DEBUG_ERROR("Error binding on port %d for IPv6 HTTP listen socket", proxy_listen_port); - + } _thread = Thread::start(this); } @@ -107,13 +98,11 @@ namespace ZeroTier { throw() { TcpConnection *conn = NULL; - uint32_t msecs = 1; struct timeval tv; tv.tv_sec = msecs / 1000; tv.tv_usec = (msecs % 1000) * 1000; int ret = 0; - // Main I/O loop // Moves data between client application socket and libzt VirtualSocket while(_run) { @@ -131,26 +120,26 @@ namespace ZeroTier { nfds = clist[i]->zfd > nfds ? clist[i]->zfd : nfds; } - ret = zts_select(nfds + 1, &read_set, &write_set, NULL, &tv); - + ret = zts_select(nfds + 1, &read_set, &write_set, NULL, &tv); if (ret > 0) { - for (int fd_i=0; fd_irx_m.lock(); if (conn->RXbuf->count() > 0) { - DEBUG_INFO("libzt has incoming data on fd=%d, we will receive it via conn=%p, sock=%p", conn->zfd, conn, conn->client_sock); + //DEBUG_INFO("libzt has incoming data on fd=%d, RXing via conn=%p, sock=%p", + // conn->zfd, conn, conn->client_sock); } if ((rd = zts_read(conn->zfd, conn->RXbuf->get_buf(),ZT_MAX_MTU)) < 0) { - DEBUG_ERROR("there was an error while reading data from libzt, err=%d", rd); + DEBUG_ERROR("error while reading data from libzt, err=%d", rd); } else { //DEBUG_INFO("LIBZT -> RXBUFFER = %d bytes", rd); @@ -158,7 +147,7 @@ namespace ZeroTier { } // attempt to write data to client from buffer if ((wr = _phy.streamSend(conn->client_sock, conn->RXbuf->get_buf(), conn->RXbuf->count())) < 0) { - DEBUG_ERROR("there was an error while writing the data from the RXbuf to the client PhySocket, err=%d", wr); + DEBUG_ERROR("error while writing the data from the RXbuf to the client PhySocket, err=%d", wr); } else { //DEBUG_INFO("RXBUFFER -> CLIENT = %d bytes", wr); @@ -169,21 +158,19 @@ namespace ZeroTier { // TX, Handle data outgoing from client to libzt if (FD_ISSET(fd_i, &write_set)) { - int rd, wr = 0; + int wr = 0; conn = zmap[fd_i]; if (conn == NULL) { - DEBUG_ERROR("invalid conn=%p", conn); + DEBUG_ERROR("invalid conn"); exit(0); } // read data from client and place it on ring buffer - - // conn->tx_m.lock(); if (conn->TXbuf->count() > 0) { - DEBUG_INFO("client has outgoing data of len=%d on fd=%d, we will send it via conn=%p, sock=%p", conn->TXbuf->count(), conn->zfd, conn, conn->client_sock); - wr = zts_write(conn->zfd, conn->TXbuf->get_buf(), conn->TXbuf->count()); - if (wr < 0) { - DEBUG_ERROR("there was an error while sending the data over libzt, err=%d", wr); + // DEBUG_INFO("client has outgoing data of len=%d on fd=%d, TXing via conn=%p, sock=%p", + // conn->TXbuf->count(), conn->zfd, conn, conn->client_sock); + if ((wr = zts_write(conn->zfd, conn->TXbuf->get_buf(), conn->TXbuf->count())) < 0) { + DEBUG_ERROR("error while sending the data over libzt, err=%d", wr); } else { //DEBUG_INFO("TXBUFFER -> LIBZT = %d bytes", wr); @@ -191,31 +178,27 @@ namespace ZeroTier { } } conn->tx_m.unlock(); - } } } - conn_m.unlock(); + conn_m.unlock(); } } - void ZTProxy::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) + void ZTProxy::phyOnTcpData(PhySocket *sock, void **uptr, void *data, unsigned long len) { int wr = 0, zfd = -1, err = 0; - DEBUG_INFO("sock=%p, len=%lu", sock, len); - unsigned char *buf = (unsigned char*)data; + DEBUG_EXTRA("sock=%p, len=%lu", sock, len); std::string host = _internal_addr; - // Get the TcpConnection object TcpConnection *conn = cmap[sock]; - if(conn == NULL) { - DEBUG_ERROR("no connection object"); - return; + if (conn == NULL) { + DEBUG_ERROR("invalid conn"); + exit(0); } - - if(conn->zfd < 0) { // no connection yet - DEBUG_INFO("no connection yet, proxying..."); - if(host != "") + if (conn->zfd < 0) { // no connection yet + DEBUG_INFO("establishing proxy connection..."); + if (host != "") { uint16_t dest_port, ipv; dest_port = _internal_port; @@ -223,7 +206,7 @@ namespace ZeroTier { ipv = host.find(":") != std::string::npos ? 6 : 4; - if(ipv == 4) { + if (ipv == 4) { // Connect to proxied host via libzt DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port); struct sockaddr_in in4; @@ -234,7 +217,7 @@ namespace ZeroTier { zfd = zts_socket(AF_INET, SOCK_STREAM, 0); err = zts_connect(zfd, (const struct sockaddr *)&in4, sizeof(in4)); } - if(ipv == 6) { + if (ipv == 6) { // Connect to proxied host via libzt //DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port); struct sockaddr_in6 in6; @@ -251,7 +234,6 @@ namespace ZeroTier { // now release TX buffer contents we previously saved, since we can't connect DEBUG_ERROR("error while connecting to remote host (zfd=%d, err=%d)", zfd, err); conn->tx_m.lock(); - DEBUG_INFO("resetting TX buffer"); conn->TXbuf->reset(); conn->tx_m.unlock(); return; @@ -260,7 +242,6 @@ namespace ZeroTier { DEBUG_INFO("successfully connected to remote host"); } } - conn_m.lock(); // on success, add connection entry to map, set physock for later clist.push_back(conn); @@ -276,43 +257,13 @@ namespace ZeroTier { DEBUG_ERROR("there was an error while writing data from client to tx buffer, err=%d", wr); } else { - DEBUG_INFO("CLIENT -> TXBUFFER = %d bytes", wr); + // DEBUG_INFO("CLIENT -> TXBUFFER = %d bytes", wr); } conn->tx_m.unlock(); } - void ZTProxy::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) - { - // Not used, connections are handled via user space network stack and VirtualTap - DEBUG_INFO("not used. exiting..."); - exit(0); - } - void ZTProxy::phyOnTcpWritable(PhySocket *sock,void **uptr) - { - // Not used, connections are handled via user space network stack and VirtualTap - DEBUG_INFO(); - //exit(0); - } - void ZTProxy::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) - { - // Not used, connections are handled via user space network stack and VirtualTap - DEBUG_INFO("sock=%p", sock); - //exit(0); - } - void ZTProxy::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) - { - // Not used, connections are handled via user space network stack and VirtualTap - DEBUG_INFO("sock=%p", sock); - //exit(0); - } - void ZTProxy::phyOnUnixClose(PhySocket *sock,void **uptr) - { - // Not used, connections are handled via user space network stack and VirtualTap - DEBUG_INFO("sock=%p", sock); - //exit(0); - } - - void ZTProxy::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) + void ZTProxy::phyOnTcpAccept(PhySocket *sockL, PhySocket *sockN, void **uptrL, void **uptrN, + const struct sockaddr *from) { DEBUG_INFO("sockL=%p, sockN=%p", sockL, sockN); TcpConnection *conn = new TcpConnection(); @@ -320,32 +271,12 @@ namespace ZeroTier { cmap[sockN]=conn; } - void ZTProxy::phyOnUnixData(PhySocket *sock,void **uptr,void *data,ssize_t len) - { - DEBUG_INFO("sock=%p, len=%lu", sock, len); - unsigned char *buf = (unsigned char*)data; - TcpConnection *conn = cmap[sock]; - if(conn == NULL) { - DEBUG_ERROR("no connection object"); - return; - } - else // If connection to host already established, just forward the data in the correct direction - { - - } - } - void ZTProxy::phyOnUnixWritable(PhySocket *sock,void **uptr,bool lwip_invoked) - { - DEBUG_INFO("sock=%p", sock); - exit(0); - } - - void ZTProxy::phyOnTcpClose(PhySocket *sock,void **uptr) + void ZTProxy::phyOnTcpClose(PhySocket *sock, void **uptr) { DEBUG_INFO("sock=%p", sock); conn_m.lock(); TcpConnection *conn = cmap[sock]; - if(conn) { + if (conn) { conn->client_sock=NULL; cmap.erase(sock); for (int i=0; i(BUF_SZ); RXbuf = new RingBuffer(BUF_SZ); } ~TcpConnection() { - printf("~TcpConnection()\n"); delete TXbuf; delete RXbuf; + client_sock = NULL; TXbuf = NULL; RXbuf = NULL; } @@ -106,7 +105,7 @@ namespace ZeroTier { Mutex conn_m; fd_set read_set, write_set; - int nfds = 0; + int nfds; int _proxy_listen_port; int _internal_port;