diff --git a/core/CallContext.hpp b/core/CallContext.hpp index 4ef9d8143..41f841456 100644 --- a/core/CallContext.hpp +++ b/core/CallContext.hpp @@ -19,10 +19,11 @@ namespace ZeroTier { /** - * A per-API-call equivalent to the runtime + * A per-API-call equivalent to the general context. * - * This captures several things that are passed into API calls and follow - * the call chain. Some such as tPtr may be supplied to callbacks. + * This is created when external C API calls are made and follows the call + * graph around from function to function as needed. It's cleaner and probably + * faster than passing clock, ticks, and tPtr around everywhere. */ class CallContext { diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 88790c340..127c92225 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -18,8 +18,10 @@ #include #ifndef __WINDOWS__ + #include #include + #endif #include @@ -32,31 +34,35 @@ namespace ZeroTier { #ifdef __APPLE__ + static clock_serv_t _machGetRealtimeClock() noexcept { clock_serv_t c; - host_get_clock_service(mach_host_self(),CALENDAR_CLOCK,&c); + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &c); return c; } + static clock_serv_t _machGetMonotonicClock() noexcept { clock_serv_t c; - host_get_clock_service(mach_host_self(),REALTIME_CLOCK,&c); + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &c); return c; } + clock_serv_t OSUtils::s_machRealtimeClock = _machGetRealtimeClock(); clock_serv_t OSUtils::s_machMonotonicClock = _machGetMonotonicClock(); + #endif -unsigned int OSUtils::ztsnprintf(char *buf,unsigned int len,const char *fmt,...) +unsigned int OSUtils::ztsnprintf(char *buf, unsigned int len, const char *fmt, ...) { va_list ap; - va_start(ap,fmt); - int n = (int)vsnprintf(buf,len,fmt,ap); + va_start(ap, fmt); + int n = (int)vsnprintf(buf, len, fmt, ap); va_end(ap); - if ((n >= (int)len)||(n < 0)) { + if ((n >= (int)len) || (n < 0)) { if (len) buf[len - 1] = (char)0; throw std::length_error("buf[] overflow"); @@ -66,13 +72,14 @@ unsigned int OSUtils::ztsnprintf(char *buf,unsigned int len,const char *fmt,...) } #ifdef __UNIX_LIKE__ -bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath) + +bool OSUtils::redirectUnixOutputs(const char *stdoutPath, const char *stderrPath) { - int fdout = open(stdoutPath,O_WRONLY|O_CREAT,0600); + int fdout = open(stdoutPath, O_WRONLY | O_CREAT, 0600); if (fdout > 0) { int fderr; if (stderrPath) { - fderr = open(stderrPath,O_WRONLY|O_CREAT,0600); + fderr = open(stderrPath, O_WRONLY | O_CREAT, 0600); if (fderr <= 0) { ::close(fdout); return false; @@ -80,17 +87,18 @@ bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath) } else fderr = fdout; ::close(STDOUT_FILENO); ::close(STDERR_FILENO); - ::dup2(fdout,STDOUT_FILENO); - ::dup2(fderr,STDERR_FILENO); + ::dup2(fdout, STDOUT_FILENO); + ::dup2(fderr, STDERR_FILENO); return true; } return false; } + #endif // __UNIX_LIKE__ -Vector OSUtils::listDirectory(const char *path,bool includeDirectories) +Vector< String > OSUtils::listDirectory(const char *path, bool includeDirectories) { - Vector r; + Vector< String > r; #ifdef __WINDOWS__ HANDLE hFind; @@ -109,11 +117,11 @@ Vector OSUtils::listDirectory(const char *path,bool includeDirectories) if (!d) return r; dptr = (struct dirent *)0; - for(;;) { - if (readdir_r(d,&de,&dptr)) + for (;;) { + if (readdir_r(d, &de, &dptr)) break; if (dptr) { - if ((strcmp(dptr->d_name,".") != 0)&&(strcmp(dptr->d_name,"..") != 0)&&((dptr->d_type != DT_DIR)||(includeDirectories))) + if ((strcmp(dptr->d_name, ".") != 0) && (strcmp(dptr->d_name, "..") != 0) && ((dptr->d_type != DT_DIR) || (includeDirectories))) r.push_back(String(dptr->d_name)); } else break; } @@ -150,12 +158,12 @@ bool OSUtils::rmDashRf(const char *path) if (!d) return true; dptr = (struct dirent *)0; - for(;;) { - if (readdir_r(d,&de,&dptr) != 0) + for (;;) { + if (readdir_r(d, &de, &dptr) != 0) break; if (!dptr) break; - if ((strcmp(dptr->d_name,".") != 0)&&(strcmp(dptr->d_name,"..") != 0)&&(strlen(dptr->d_name) > 0)) { + if ((strcmp(dptr->d_name, ".") != 0) && (strcmp(dptr->d_name, "..") != 0) && (strlen(dptr->d_name) > 0)) { String p(path); p.push_back(ZT_PATH_SEPARATOR); p.append(dptr->d_name); @@ -170,10 +178,10 @@ bool OSUtils::rmDashRf(const char *path) #endif } -void OSUtils::lockDownFile(const char *path,bool isDir) +void OSUtils::lockDownFile(const char *path, bool isDir) { #ifdef __UNIX_LIKE__ - chmod(path,isDir ? 0700 : 0600); + chmod(path, isDir ? 0700 : 0600); #else #ifdef __WINDOWS__ { @@ -202,25 +210,25 @@ void OSUtils::lockDownFile(const char *path,bool isDir) #endif } -bool OSUtils::fileExists(const char *path,bool followLinks) +bool OSUtils::fileExists(const char *path, bool followLinks) { struct stat s; #ifdef __UNIX_LIKE__ if (!followLinks) - return (lstat(path,&s) == 0); + return (lstat(path, &s) == 0); #endif - return (stat(path,&s) == 0); + return (stat(path, &s) == 0); } -bool OSUtils::readFile(const char *path,String &buf) +bool OSUtils::readFile(const char *path, String &buf) { char tmp[16384]; - FILE *f = fopen(path,"rb"); + FILE *f = fopen(path, "rb"); if (f) { - for(;;) { - long n = (long)fread(tmp,1,sizeof(tmp),f); + for (;;) { + long n = (long)fread(tmp, 1, sizeof(tmp), f); if (n > 0) - buf.append(tmp,n); + buf.append(tmp, n); else break; } fclose(f); @@ -229,11 +237,11 @@ bool OSUtils::readFile(const char *path,String &buf) return false; } -bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len) +bool OSUtils::writeFile(const char *path, const void *buf, unsigned int len) { - FILE *f = fopen(path,"wb"); + FILE *f = fopen(path, "wb"); if (f) { - if ((long)fwrite(buf,1,len,f) != (long)len) { + if ((long)fwrite(buf, 1, len, f) != (long)len) { fclose(f); return false; } else { @@ -244,9 +252,9 @@ bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len) return false; } -Vector OSUtils::split(const char *s,const char *const sep,const char *esc,const char *quot) +Vector< String > OSUtils::split(const char *s, const char *const sep, const char *esc, const char *quot) { - Vector fields; + Vector< String > fields; String buf; if (!esc) @@ -268,11 +276,11 @@ Vector OSUtils::split(const char *s,const char *const sep,const char *es } else buf.push_back(*s); } else { const char *quotTmp; - if (strchr(esc,*s)) + if (strchr(esc, *s)) escapeState = true; - else if ((buf.size() <= 0)&&((quotTmp = strchr(quot,*s)))) + else if ((buf.size() <= 0) && ((quotTmp = strchr(quot, *s)))) quoteState = *quotTmp; - else if (strchr(sep,*s)) { + else if (strchr(sep, *s)) { if (buf.size() > 0) { fields.push_back(buf); buf.clear(); @@ -292,23 +300,23 @@ ZeroTier::String OSUtils::platformDefaultHomePath() { #ifdef __QNAP__ char *cmd = "/sbin/getcfg zerotier Install_Path -f /etc/config/qpkg.conf"; - char buf[128]; - FILE *fp; - if ((fp = popen(cmd, "r")) == NULL) { - printf("Error opening pipe!\n"); - return NULL; - } - while (fgets(buf, 128, fp) != NULL) { } - if(pclose(fp)) { - printf("Command not found or exited with error status\n"); - return NULL; - } - String homeDir = String(buf); - homeDir.erase(std::remove(homeDir.begin(), homeDir.end(), '\n'), homeDir.end()); - return homeDir; + char buf[128]; + FILE *fp; + if ((fp = popen(cmd, "r")) == NULL) { + printf("Error opening pipe!\n"); + return NULL; + } + while (fgets(buf, 128, fp) != NULL) { } + if(pclose(fp)) { + printf("Command not found or exited with error status\n"); + return NULL; + } + String homeDir = String(buf); + homeDir.erase(std::remove(homeDir.begin(), homeDir.end(), '\n'), homeDir.end()); + return homeDir; #endif - // Check for user-defined environment variable before using defaults + // Check for user-defined environment variable before using defaults #ifdef __WINDOWS__ DWORD bufferSize = 65535; ZeroTier::String userDefinedPath; @@ -316,7 +324,7 @@ ZeroTier::String OSUtils::platformDefaultHomePath() if (bufferSize) return userDefinedPath; #else - if(const char* userDefinedPath = getenv("ZEROTIER_HOME")) + if (const char *userDefinedPath = getenv("ZEROTIER_HOME")) return String(userDefinedPath); #endif diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index 2470fed66..df3491d5e 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -196,11 +196,7 @@ public: #else #ifdef __LINUX__ timespec ts; -#ifdef CLOCK_MONOTONIC_COARSE - clock_gettime(CLOCK_MONOTONIC_COARSE,&ts); -#else - clock_gettime(CLOCK_MONOTONIC,&ts); -#endif + clock_gettime(CLOCK_BOOTTIME,&ts); return ( (1000LL * (int64_t)ts.tv_sec) + ((int64_t)(ts.tv_nsec / 1000000)) ); #else #ifdef __APPLE__ diff --git a/rust-zerotier-core/src/certificate.rs b/rust-zerotier-core/src/certificate.rs index 139e91719..857460cc6 100644 --- a/rust-zerotier-core/src/certificate.rs +++ b/rust-zerotier-core/src/certificate.rs @@ -807,6 +807,6 @@ mod tests { let cert_signed_decoded = cert_signed_decoded.ok().unwrap(); assert!(cert_signed_decoded.signature.len() > 0); - assert!(cert_signed_decoded.verify() == CertificateError::None); + assert!(cert_signed_decoded.verify(-1) == CertificateError::None); } } diff --git a/service/src/commands/cert.rs b/service/src/commands/cert.rs index 6f31d3f5e..c2a678676 100644 --- a/service/src/commands/cert.rs +++ b/service/src/commands/cert.rs @@ -274,7 +274,7 @@ fn delete<'a>(store: &Arc, cli_args: &ArgMatches<'a>) -> i32 { 0 } -pub(crate) fn run<'a>(store: Arc, cli_args: &ArgMatches<'a>) -> i32 { +pub(crate) fn run(store: Arc, cli_args: &ArgMatches) -> i32 { match cli_args.subcommand() { ("list", None) => list(&store), ("show", Some(sub_cli_args)) => show(&store, sub_cli_args), diff --git a/service/src/fastudpsocket.rs b/service/src/fastudpsocket.rs index 6f4ebcd74..f1d54dcde 100644 --- a/service/src/fastudpsocket.rs +++ b/service/src/fastudpsocket.rs @@ -35,17 +35,9 @@ use crate::osdep as osdep; #[cfg(unix)] fn bind_udp_socket(_device_name: &str, address: &InetAddress) -> Result { unsafe { - let af; - let sa_len; - match address.family() { - InetAddressFamily::IPv4 => { - af = osdep::AF_INET; - sa_len = std::mem::size_of::() as osdep::socklen_t; - } - InetAddressFamily::IPv6 => { - af = osdep::AF_INET6; - sa_len = std::mem::size_of::() as osdep::socklen_t; - } + let (af, sa_len) = match address.family() { + InetAddressFamily::IPv4 => (osdep::AF_INET, std::mem::size_of::() as osdep::socklen_t), + InetAddressFamily::IPv6 => (osdep::AF_INET6, std::mem::size_of::() as osdep::socklen_t), _ => { return Err("unrecognized address family"); } diff --git a/service/src/getifaddrs.rs b/service/src/getifaddrs.rs index b4d7086c5..a4e65b63f 100644 --- a/service/src/getifaddrs.rs +++ b/service/src/getifaddrs.rs @@ -18,7 +18,6 @@ use zerotier_core::InetAddress; use crate::osdep as osdep; -#[inline(always)] fn s6_addr_as_ptr(a: &A) -> *const A { a as *const A } diff --git a/service/src/httpclient.rs b/service/src/httpclient.rs index d63543b21..b731c5108 100644 --- a/service/src/httpclient.rs +++ b/service/src/httpclient.rs @@ -47,9 +47,9 @@ impl Error for UnexpectedStatusCodeError {} impl std::fmt::Display for UnexpectedStatusCodeError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { if self.1.is_empty() { - write!(f, "unexpected status code: {} {}", self.0.as_str(), self.0.canonical_reason().unwrap_or("???")) + write!(f, "{} {} (???)", self.0.as_str(), self.0.canonical_reason().unwrap_or("???")) } else { - write!(f, "unexpected status code: {} {} ({})", self.0.as_str(), self.0.canonical_reason().unwrap_or("???"), self.1) + write!(f, "{} {} ({})", self.0.as_str(), self.0.canonical_reason().unwrap_or("???"), self.1) } } }