From a1f439795900d42d2fa0c5a4fb87e501d045102c Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Sun, 30 Aug 2020 14:19:56 -0700 Subject: [PATCH 1/5] linux implementaiton of zerotier-cli dump --- one.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/one.cpp b/one.cpp index f582f94f9..d8a53dcd4 100644 --- a/one.cpp +++ b/one.cpp @@ -47,6 +47,11 @@ #include #include #include +#include +#include +#include +#include +#include #ifndef ZT_NO_CAPABILITIES #include #include @@ -1044,9 +1049,81 @@ static int cli(int argc,char **argv) free(addresses); addresses = NULL; } +#elif defined(__LINUX__) + struct ifreq ifr; + struct ifconf ifc; + char buf[1024]; + char stringBuffer[128]; + int success = 0; + + int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + + ifc.ifc_len = sizeof(buf); + ifc.ifc_buf = buf; + ioctl(sock, SIOCGIFCONF, &ifc); + + struct ifreq *it = ifc.ifc_req; + const struct ifreq * const end = it + (ifc.ifc_len / sizeof(struct ifreq)); + int count = 0; + for(; it != end; ++it) { + strcpy(ifr.ifr_name, it->ifr_name); + if(ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) { + if (!(ifr.ifr_flags & IFF_LOOPBACK)) { // skip loopback + dump << "Interface " << count++ << ZT_EOL_S << "-----------" << ZT_EOL_S; + dump << "Name: " << ifr.ifr_name << ZT_EOL_S; + if (ioctl(sock, SIOCGIFMTU, &ifr) == 0) { + dump << "MTU: " << ifr.ifr_mtu << ZT_EOL_S; + } + if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) { + unsigned char mac_addr[6]; + memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 6); + char macStr[16]; + sprintf(macStr, "%02x:%02x:%02x:%02x:%02x:%02x", + mac_addr[0], + mac_addr[1], + mac_addr[2], + mac_addr[3], + mac_addr[4], + mac_addr[5]); + dump << "MAC: " << macStr << ZT_EOL_S; + } + + dump << "Addresses: " << ZT_EOL_S; + struct ifaddrs *ifap, *ifa; + void *addr; + getifaddrs(&ifap); + for(ifa = ifap; ifa; ifa = ifa->ifa_next) { + if(strcmp(ifr.ifr_name, ifa->ifa_name) == 0) { + if(ifa->ifa_addr->sa_family == AF_INET) { + struct sockaddr_in *ipv4 = (struct sockaddr_in*)ifa->ifa_addr; + addr = &ipv4->sin_addr; + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)ifa->ifa_addr; + addr = &ipv6->sin6_addr; + } else { + continue; + } + inet_ntop(ifa->ifa_addr->sa_family, addr, stringBuffer, sizeof(stringBuffer)); + dump << stringBuffer << ZT_EOL_S; + } + } + } + } + } + close(sock); + char cwd[PATH_MAX]; + getcwd(cwd, sizeof(cwd)); + sprintf(cwd, "%s%szerotier_dump.txt", cwd, ZT_PATH_SEPARATOR_S); + fprintf(stdout, "Writing dump to: %s\n", cwd); + int fd = open(cwd, O_CREAT|O_RDWR,0664); + if (fd == -1) { + fprintf(stderr, "Error creating file.\n"); + } + write(fd, dump.str().c_str(), dump.str().size()); + close(fd); #endif - fprintf(stderr, "%s\n", dump.str().c_str()); + // fprintf(stderr, "%s\n", dump.str().c_str()); } else { cliPrintHelp(argv[0],stderr); From 361ca1e8b458e783d322f1904335cc2ef357d328 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 15 Sep 2020 16:49:19 -0700 Subject: [PATCH 2/5] add link to CoreServices framework --- make-mac.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make-mac.mk b/make-mac.mk index c608e037d..02765d24e 100644 --- a/make-mac.mk +++ b/make-mac.mk @@ -32,7 +32,7 @@ ifeq ($(ZT_CONTROLLER),1) INCLUDES+=-I/usr/local/opt/libpq/include -Iext/hiredis-0.14.1/include/ -Iext/redis-plus-plus-1.1.1/install/macos/include/sw/ endif -LIBS+=-framework SystemConfiguration -framework CoreFoundation +LIBS+=-framework CoreServices -framework SystemConfiguration -framework CoreFoundation # Official releases are signed with our Apple cert and apply software updates by default ifeq ($(ZT_OFFICIAL_RELEASE),1) From 927aeb15f6aee1b4cf1769d59fce6b3e762384f2 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 15 Sep 2020 16:49:49 -0700 Subject: [PATCH 3/5] macos output dump to file if possible if not, write to stdout --- one.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/one.cpp b/one.cpp index d8a53dcd4..112ae7a94 100644 --- a/one.cpp +++ b/one.cpp @@ -87,6 +87,7 @@ #ifdef __APPLE__ #include +#include #include #include #include @@ -879,6 +880,8 @@ static int cli(int argc,char **argv) dump << "macOS" << ZT_EOL_S; #elif defined(_WIN32) dump << "Windows" << ZT_EOL_S; +#elif defined(__LINUX__) + dump << "Linux" << ZT_EOL_S; #else dump << "other unix based OS" << ZT_EOL_S; #endif @@ -985,6 +988,30 @@ static int cli(int argc,char **argv) dump << ZT_EOL_S; } + + + FSRef fsref; + UInt8 path[PATH_MAX]; + if (FSFindFolder(kUserDomain, kDesktopFolderType, kDontCreateFolder, &fsref) == noErr && + FSRefMakePath(&fsref, path, sizeof(path)) == noErr) { + + } else if (getenv("SUDO_USER")) { + sprintf((char*)path, "/Users/%s/Desktop/", getenv("SUDO_USER")); + } else { + fprintf(stdout, "%s", dump.str().c_str()); + return 0; + } + + sprintf((char*)path, "%s%szerotier_dump.txt", (char*)path, ZT_PATH_SEPARATOR_S); + + fprintf(stdout, "Writing dump to: %s\n", path); + int fd = open((char*)path, O_CREAT|O_RDWR,0664); + if (fd == -1) { + fprintf(stderr, "Error creating file.\n"); + return 1; + } + write(fd, dump.str().c_str(), dump.str().size()); + close(fd); #elif defined(_WIN32) ULONG buffLen = 16384; PIP_ADAPTER_ADDRESSES addresses; @@ -1118,6 +1145,7 @@ static int cli(int argc,char **argv) int fd = open(cwd, O_CREAT|O_RDWR,0664); if (fd == -1) { fprintf(stderr, "Error creating file.\n"); + return 1; } write(fd, dump.str().c_str(), dump.str().size()); close(fd); From 5090e950038102e6e658e94a207099fe12034e78 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 15 Sep 2020 16:50:55 -0700 Subject: [PATCH 4/5] dump basics to stdout on other platforms --- one.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/one.cpp b/one.cpp index 112ae7a94..500553a8d 100644 --- a/one.cpp +++ b/one.cpp @@ -1149,6 +1149,8 @@ static int cli(int argc,char **argv) } write(fd, dump.str().c_str(), dump.str().size()); close(fd); +#else + fprintf(stderr, "%s", dump.str().c_str()); #endif // fprintf(stderr, "%s\n", dump.str().c_str()); From cff315298595f9ef3c3e5975a21e8c45090969c3 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Wed, 16 Sep 2020 10:03:03 -0700 Subject: [PATCH 5/5] windows create dump file on desktop --- one.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/one.cpp b/one.cpp index 500553a8d..9005596ff 100644 --- a/one.cpp +++ b/one.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "osdep/WindowsEthernetTap.hpp" #include "windows/ZeroTierOne/ServiceInstaller.h" #include "windows/ZeroTierOne/ServiceBase.h" @@ -1076,6 +1077,41 @@ static int cli(int argc,char **argv) free(addresses); addresses = NULL; } + + char path[MAX_PATH + 1] = {}; + if (SHGetFolderPathA(NULL, CSIDL_DESKTOP, NULL, 0, path) == S_OK) { + sprintf(path, "%s%szerotier_dump.txt", path, ZT_PATH_SEPARATOR_S); + fprintf(stdout, "Writing dump to: %s\n", path); + HANDLE file = CreateFileA( + path, + GENERIC_WRITE, + 0, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL + ); + if (file == INVALID_HANDLE_VALUE) { + fprintf(stdout, "%s", dump.str().c_str()); + return 0; + } + + BOOL err = WriteFile( + file, + dump.str().c_str(), + dump.str().size(), + NULL, + NULL + ); + if (err = FALSE) { + fprintf(stderr, "Error writing file"); + return 1; + } + CloseHandle(file); + } + else { + fprintf(stdout, "%s", dump.str().c_str()); + } #elif defined(__LINUX__) struct ifreq ifr; struct ifconf ifc;