diff --git a/src/SDK_Intercept.c b/src/SDK_Intercept.c index a2a7584..fcd843f 100644 --- a/src/SDK_Intercept.c +++ b/src/SDK_Intercept.c @@ -121,7 +121,6 @@ pthread_key_t thr_id_key; // Return whether 'intercept' API is enabled for this thread bool check_intercept_enabled() { - // DEBUG_EXTRA(""); if(!realconnect){ load_symbols(); } @@ -306,14 +305,18 @@ pthread_key_t thr_id_key; DEBUG_INFO("addr=%s", s_un->sun_path); } - int port = connaddr->sin_port; - int ip = connaddr->sin_addr.s_addr; - unsigned char d[4]; - d[0] = ip & 0xFF; - d[1] = (ip >> 8) & 0xFF; - d[2] = (ip >> 16) & 0xFF; - d[3] = (ip >> 24) & 0xFF; - DEBUG_INFO("addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], ntohs(port)); + char addrstr[INET6_ADDRSTRLEN]; + if(addr->sa_family == AF_INET) { + struct sockaddr_in *connaddr = (struct sockaddr_in *)addr; + inet_ntop(AF_INET, &(connaddr->sin_addr), addrstr, INET_ADDRSTRLEN); + sprintf(addrstr, "%s:%d", addrstr, ntohs(connaddr->sin_port)); + } + if(addr->sa_family == AF_INET6) { + struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr; + inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN); + sprintf(addrstr, "%s:%d", addrstr, ntohs(connaddr6->sin6_port)); + } + DEBUG_INFO("addr=%s", addrstr); if(!check_intercept_enabled()) return realconnect(fd, addr, addrlen); @@ -363,26 +366,32 @@ pthread_key_t thr_id_key; // make sure we don't touch any standard outputs if(fd == 0 || fd == 1 || fd == 2) return(realbind(fd, addr, addrlen)); - struct sockaddr_in *connaddr; - connaddr = (struct sockaddr_in *)addr; - if(connaddr->sin_family == AF_LOCAL + struct sockaddr_in *connaddr; + struct sockaddr_in6 *connaddr6; + + char addrstr[INET6_ADDRSTRLEN]; + if(addr->sa_family == AF_INET) { + struct sockaddr_in *connaddr = (struct sockaddr_in *)addr; + inet_ntop(AF_INET, &(connaddr->sin_addr), addrstr, INET_ADDRSTRLEN); + sprintf(addrstr, "%s:%d", addrstr, ntohs(connaddr->sin_port)); + } + if(addr->sa_family == AF_INET6) { + struct sockaddr_in6 *connaddr6 = (struct sockaddr_in6 *)addr; + inet_ntop(AF_INET6, &(connaddr6->sin6_addr), addrstr, INET6_ADDRSTRLEN); + sprintf(addrstr, "%s:%d", addrstr, ntohs(connaddr6->sin6_port)); + } + DEBUG_INFO("addr=%s", addrstr); + + if(addr->sa_family == AF_LOCAL #if defined(__linux__) - || connaddr->sin_family == AF_NETLINK + || addr->sa_family == AF_NETLINK #endif - || connaddr->sin_family == AF_UNIX) { + || addr->sa_family == AF_UNIX) { int err = realbind(fd, addr, addrlen); DEBUG_BLANK("realbind(): err=%d", err); return err; } - int port = connaddr->sin_port; - int ip = connaddr->sin_addr.s_addr; - unsigned char d[4]; - d[0] = ip & 0xFF; - d[1] = (ip >> 8) & 0xFF; - d[2] = (ip >> 16) & 0xFF; - d[3] = (ip >> 24) & 0xFF; - DEBUG_INFO("addr=%d.%d.%d.%d:%d", d[0],d[1],d[2],d[3], ntohs(port)); int sock_type; socklen_t sock_type_len = sizeof(sock_type); diff --git a/src/SDK_LWIPStack.hpp b/src/SDK_LWIPStack.hpp index 3691e9d..0a86402 100644 --- a/src/SDK_LWIPStack.hpp +++ b/src/SDK_LWIPStack.hpp @@ -35,9 +35,13 @@ #include "lwip/init.h" #include "lwip/udp.h" +#include "Mutex.hpp" +#include "OSUtils.hpp" +#include "SDK_Debug.h" + #if defined(LWIP_VERSION_2) // 2.0.0 #else // 1.4.1 - #include "lwip/tcp_impl.h" + // #include "lwip/tcp_impl.h" #endif #include @@ -242,7 +246,7 @@ namespace ZeroTier { #ifdef __DYNAMIC_LWIP__ // Use dynamically-loaded symbols (for use in normal desktop applications) if(_libref == NULL) - printf("dlerror(): %s\n", dlerror()); + DEBUG_ERROR("dlerror(): %s", dlerror()); _ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(_libref, "ethernet_input"); _etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(_libref, "etharp_output"); diff --git a/src/SDK_Proxy.cpp b/src/SDK_Proxy.cpp index bcfa678..4a06fc1 100644 --- a/src/SDK_Proxy.cpp +++ b/src/SDK_Proxy.cpp @@ -80,12 +80,12 @@ namespace ZeroTier int NetconEthernetTap::stopProxyServer() { - DEBUG_INFO("stopProxyServer()"); + DEBUG_INFO(); if(proxyListenPhySocket) { _phy.close(proxyListenPhySocket); return 0; } - DEBUG_ERROR("stopProxyServer(): Invalid proxyListenPhySocket"); + DEBUG_ERROR("invalid proxyListenPhySocket"); return -1; } @@ -96,25 +96,25 @@ namespace ZeroTier // - If no address, assume 127.0.0.1: // - If no port assignment file, 127.0.0.1:RANDOM_PORT - DEBUG_INFO("startProxyServer()\n"); + DEBUG_INFO(); int portno = -1; if(addr) { - DEBUG_INFO("startProxyServer(): Using provided address"); + DEBUG_INFO("using provided address"); // This address pointer may come from a different memory space and might be de-allocated, so we keep a copy memcpy(&proxyServerAddress, addr, sizeof(struct sockaddr_storage)); struct sockaddr_in *in4 = (struct sockaddr_in *)&addr; proxyListenPhySocket = _phy.tcpListen((const struct sockaddr*)&in4,(void *)this); sockstate = SOCKS_OPEN; - DEBUG_INFO("SOCKS5 proxy server address for <%.16lx> is: <%s> (sock=%p)\n", nwid, inet_ntoa(in4->sin_addr), /*ntohs(in4->sin_port), */(void*)&proxyListenPhySocket); + DEBUG_INFO("SOCKS5 proxy server address for <%.16lx> is: <%s> (sock=%p)", nwid, inet_ntoa(in4->sin_addr), /*ntohs(in4->sin_port), */(void*)&proxyListenPhySocket); return 0; } else { - DEBUG_INFO("startProxyServer(): No address provided. Checking port file."); + DEBUG_INFO("no address provided. Checking port file."); // Look for a port file for this network's proxy server instance char portFile[4096]; Utils::snprintf(portFile,sizeof(portFile),"%s/networks.d/%.16llx.port",homepath,nwid); std::string portStr; - DEBUG_INFO("Proxy(): Reading port from: %s\n", portFile); + DEBUG_INFO("reading port from: %s\n", portFile); if(ZeroTier::OSUtils::fileExists(portFile,true)) { if(ZeroTier::OSUtils::readFile(portFile, portStr)) { @@ -125,12 +125,12 @@ namespace ZeroTier unsigned int randp = 0; Utils::getSecureRandom(&randp,sizeof(randp)); portno = 1000 + (randp % 1000); - DEBUG_INFO("Proxy(): No port specified in networks.d/%.16lx.port, randomly picking port\n", nwid); + DEBUG_INFO("no port specified in networks.d/%.16lx.port, randomly picking port", nwid); std::stringstream ss; ss << portno; portStr = ss.str(); if(!ZeroTier::OSUtils::writeFile(portFile, portStr)) { - DEBUG_ERROR("unable to write proxy port file: %s\n", portFile); + DEBUG_ERROR("unable to write proxy port file: %s", portFile); } } struct sockaddr_in in4; @@ -168,14 +168,14 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) { - DEBUG_INFO("phyOnTcpData(): sock=%p, len=%lu\n", (void*)&sock, len); + DEBUG_INFO("sock=%p, len=%lu", (void*)&sock, len); unsigned char *buf; buf = (unsigned char *)data; // Get connection for this PhySocket Connection *conn = getConnection(sock); if(!conn) { - DEBUG_INFO("phyOnTcpData(): Unable to locate Connection for sock=%p\n", (void*)&sock); + DEBUG_INFO("unable to locate Connection for sock=%p", (void*)&sock); return; } @@ -222,7 +222,7 @@ namespace ZeroTier if(firstSupportedMethod == 2) { supportedMethod = firstSupportedMethod; } - DEBUG_INFO(" INFO \n", version, methodsLength, supportedMethod); + DEBUG_INFO(" INFO ", version, methodsLength, supportedMethod); // Send METHOD selection msg // +----+--------+ @@ -255,16 +255,16 @@ namespace ZeroTier int cmd = buf[IDX_COMMAND]; int addr_type = buf[IDX_ATYP]; - DEBUG_INFO("SOCKS REQUEST = \n", version, cmd, addr_type); + DEBUG_INFO("SOCKS REQUEST = ", version, cmd, addr_type); // CONNECT request if(cmd == 1) { - DEBUG_INFO("CONNECT request\n"); + DEBUG_INFO("CONNECT request"); // Ipv4 /* if(addr_type == 144) { - //printf("IPv4\n"); + //DEBUG_INFO("IPv4\n"); int raw_addr; memcpy(&raw_addr, &buf[4], 4); char newaddr[16]; @@ -302,7 +302,7 @@ namespace ZeroTier ExtractAddress(addr_type,buf,&addr); PhySocket * new_sock = handleSocketProxy(sock, SOCK_STREAM); if(!new_sock) - DEBUG_ERROR("Error while creating proxied-socket\n"); + DEBUG_ERROR("error while creating proxied-socket"); handleConnectProxy(sock, &addr); // Convert connection err code into SOCKS-err-code @@ -324,7 +324,7 @@ namespace ZeroTier // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ - DEBUG_INFO("REPLY = %d\n", addr.sin_port); + DEBUG_INFO("REPLY = %d", addr.sin_port); char reply[len]; // TODO: determine proper length int addr_len = domain_len; memset(reply, 0, len); // Create reply buffer at least as big as incoming SOCKS request data @@ -346,7 +346,7 @@ namespace ZeroTier // BIND Request if(cmd == 2) { - DEBUG_INFO("BIND request\n"); + DEBUG_INFO("BIND request"); //char raw_addr[15]; //int bind_port; } @@ -355,7 +355,7 @@ namespace ZeroTier if(cmd == 3) { // PORT supplied should be port assigned by server in previous msg - DEBUG_INFO("UDP association request\n"); + DEBUG_INFO("UDP association request"); // SOCKS_CONNECT (Cont.) // +----+-----+-------+------+----------+----------+ @@ -381,10 +381,10 @@ namespace ZeroTier // Create new lwIP PCB PhySocket * new_sock = handleSocketProxy(sock, SOCK_DGRAM); - DEBUG_INFO("new_sock = %p\n", (void*)&sock); - DEBUG_INFO("new_sock = %p\n", (void*)&new_sock); + DEBUG_INFO("sock = %p", (void*)&sock); + DEBUG_INFO("new_sock = %p", (void*)&new_sock); if(!new_sock) - DEBUG_ERROR("Error while creating proxied-socket\n"); + DEBUG_ERROR("error while creating proxied-socket"); // Form address struct sockaddr_in addr; @@ -407,7 +407,7 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) { - DEBUG_INFO("phyOnTcpAccept(): sock=%p\n", (void*)&sockN); + DEBUG_INFO("sock=%p", (void*)&sockN); Connection *newConn = new Connection(); newConn->sock = sockN; _phy.setNotifyWritable(sockN, false); @@ -416,17 +416,17 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) { - DEBUG_INFO("phyOnTcpConnect(): sock=%p\n", (void*)&sock); + DEBUG_INFO("sock=%p", (void*)&sock); } // Unused -- no UDP or TCP from this thread/Phy<> void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address, const struct sockaddr *from,void *data,unsigned long len) { - DEBUG_INFO("phyOnDatagram(): len = %lu\n", len); + DEBUG_INFO("len = %lu", len); if(len) { Connection *conn = getConnection(sock); if(!conn){ - DEBUG_ERROR("unable to locate Connection: sock=%p\n", (void*)sock); + DEBUG_ERROR("unable to locate Connection: sock=%p", (void*)sock); return; } unsigned char *buf = (unsigned char*)data; @@ -438,20 +438,20 @@ namespace ZeroTier void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) { - DEBUG_INFO("phyOnTcpClose(): sock=%p\n", (void*)&sock); + DEBUG_INFO("sock=%p", (void*)&sock); Mutex::Lock _l(_tcpconns_m); closeConnection(sock); } void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr, bool lwip_invoked) { - DEBUG_INFO(" phyOnTcpWritable(): sock=%p\n", (void*)&sock); + DEBUG_INFO("sock=%p", (void*)&sock); processReceivedData(sock,uptr,lwip_invoked); } // RX data on stream socks and send back over client sock's underlying fd void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) { - DEBUG_INFO("phyOnFileDescriptorActivity(): sock=%p\n", (void*&)sock); + DEBUG_INFO("sock=%p", (void*&)sock); } } \ No newline at end of file diff --git a/src/SDK_RPC.c b/src/SDK_RPC.c index 60bd80e..27cecce 100644 --- a/src/SDK_RPC.c +++ b/src/SDK_RPC.c @@ -69,7 +69,6 @@ static int rpc_count; static pthread_mutex_t lock; void rpc_mutex_init() { if(pthread_mutex_init(&lock, NULL) != 0) { - // fprintf(stderr, "error while initializing service call mutex\n"); } } void rpc_mutex_destroy() { @@ -123,7 +122,7 @@ int load_symbols_rpc() int rpc_join(char * sockname) { if(sockname == NULL) { - fprintf(stderr,"Warning, rpc netpath is NULL\n"); + DEBUG_ERROR("warning, rpc netpath is NULL"); } if(!load_symbols_rpc()) return -1; @@ -139,7 +138,7 @@ int rpc_join(char * sockname) #else if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){ #endif - fprintf(stderr,"Error while creating RPC socket\n"); + DEBUG_ERROR("error while creating RPC socket"); return -1; } while((conn_err != 0) /* && (attempts < SERVICE_CONNECT_ATTEMPTS) */){ @@ -148,7 +147,7 @@ int rpc_join(char * sockname) #else if((conn_err = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) { #endif - fprintf(stderr,"Error while connecting to RPC socket. Re-attempting...\n"); + DEBUG_ERROR("error while connecting to RPC socket. Re-attempting..."); usleep(100000); } else @@ -174,8 +173,8 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len) // Generate token int fdrand = open("/dev/urandom", O_RDONLY); if(read(fdrand, &CANARY, CANARY_SZ) < 0) { - fprintf(stderr,"unable to read from /dev/urandom for RPC canary data\n"); - return -1; + DEBUG_ERROR("unable to read from /dev/urandom for RPC canary data"); + return -1; } close(fdrand); memcpy(&canary_num, CANARY, CANARY_SZ); @@ -211,20 +210,20 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len) // Write RPC long n_write = write(rpc_sock, &metabuf, BUF_SZ); if(n_write < 0) { - fprintf(stderr,"Error writing command to service (CMD = %d)\n", cmdbuf[CMD_ID_IDX]); + DEBUG_ERROR("error writing command to service (CMD = %d)", cmdbuf[CMD_ID_IDX]); errno = 0; } // Write token to corresponding data stream if(read(rpc_sock, &c, 1) < 0) { - fprintf(stderr,"unable to read RPC ACK byte from service.\n"); - close(rpc_sock); + DEBUG_ERROR("unable to read RPC ACK byte from service."); + close(rpc_sock); return -1; } if(c == 'z' && n_write > 0 && forfd > -1){ if(send(forfd, &CANARY, CANARY_SZ+PADDING_SZ, 0) < 0) { - perror("send: \n"); - fprintf(stderr,"unable to write canary to stream (fd=%d)\n", forfd); - close(rpc_sock); + perror("send: \n"); + DEBUG_ERROR("unable to write canary to stream (fd=%d)", forfd); + close(rpc_sock); return -1; } } @@ -321,11 +320,11 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd) cmsg = CMSG_FIRSTHDR(&msg); if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { if (cmsg->cmsg_level != SOL_SOCKET) { - fprintf(stderr,"invalid cmsg_level %d\n",cmsg->cmsg_level); + DEBUG_ERROR("invalid cmsg_level %d",cmsg->cmsg_level); return -1; } if (cmsg->cmsg_type != SCM_RIGHTS) { - fprintf(stderr,"invalid cmsg_type %d\n",cmsg->cmsg_type); + DEBUG_ERROR("invalid cmsg_type %d",cmsg->cmsg_type); return -1; } *fd = *((int *) CMSG_DATA(cmsg)); @@ -334,7 +333,7 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd) } else { size = read (sock, buf, bufsize); if (size < 0) { - fprintf(stderr,"sock_fd_read(): read: Error\n"); + DEBUG_ERROR("sock_fd_read(): read: Error"); return -1; } } diff --git a/src/SDK_Service.cpp b/src/SDK_Service.cpp index 08386d4..e2963f2 100644 --- a/src/SDK_Service.cpp +++ b/src/SDK_Service.cpp @@ -472,7 +472,7 @@ void *zts_start_core_service(void *thread_id) { } } else { - fprintf(stderr, "homeDir is empty, could not construct path"); + DEBUG_ERROR(stderr, "homeDir is empty, could not construct path"); return NULL; } diff --git a/tests/api_test/client6.c b/tests/api_test/client6.c new file mode 100644 index 0000000..ee08d4b --- /dev/null +++ b/tests/api_test/client6.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +void error(char *msg) { + perror(msg); + exit(0); +} + +int main(int argc, char *argv[]) { + int sockfd, portno, n; + struct sockaddr_in6 serv_addr; + struct hostent *server; + char buffer[256] = "This is a string from client!"; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(0); + } + portno = atoi(argv[2]); + + printf("\nIPv6 TCP Client Started...\n"); + + //Sockets Layer Call: socket() + sockfd = socket(AF_INET6, SOCK_STREAM, 0); + if (sockfd < 0) + error("ERROR opening socket"); + + //Sockets Layer Call: gethostbyname2() + server = gethostbyname2(argv[1],AF_INET6); + if (server == NULL) { + fprintf(stderr, "ERROR, no such host\n"); + exit(0); + } + + memset((char *) &serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin6_flowinfo = 0; + serv_addr.sin6_family = AF_INET6; + memmove((char *) &serv_addr.sin6_addr.s6_addr, (char *) server->h_addr, server->h_length); + serv_addr.sin6_port = htons(portno); + + //Sockets Layer Call: connect() + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + error("ERROR connecting"); + + + //Sockets Layer Call: send() + n = send(sockfd,buffer, strlen(buffer)+1, 0); + if (n < 0) + error("ERROR writing to socket"); + + memset(buffer, 0, 256); + + //Sockets Layer Call: recv() + n = recv(sockfd, buffer, 255, 0); + if (n < 0) + error("ERROR reading from socket"); + printf("Message from server: %s\n", buffer); + + //Sockets Layer Call: close() + close(sockfd); + + return 0; +} \ No newline at end of file