2015-04-09 00:10:21 +00:00
|
|
|
/*
|
2020-05-12 08:35:48 +00:00
|
|
|
* Copyright (c)2013-2020 ZeroTier, Inc.
|
2015-04-09 00:10:21 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2015-04-09 00:10:21 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2015-04-09 00:10:21 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2015-04-09 00:10:21 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2015-04-09 00:10:21 +00:00
|
|
|
|
|
|
|
#ifndef ZT_OSUTILS_HPP
|
|
|
|
#define ZT_OSUTILS_HPP
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "../node/Constants.hpp"
|
2015-04-20 23:07:38 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
2015-04-09 00:10:21 +00:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-12-27 19:07:35 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlwapi.h>
|
2015-04-09 00:10:21 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2015-04-10 03:54:00 +00:00
|
|
|
#include <errno.h>
|
2015-04-09 00:10:21 +00:00
|
|
|
#include <sys/time.h>
|
2015-04-10 03:54:00 +00:00
|
|
|
#include <sys/stat.h>
|
2015-04-09 00:10:21 +00:00
|
|
|
#include <arpa/inet.h>
|
2017-05-10 04:21:56 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
2015-04-09 00:10:21 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-13 19:24:55 +00:00
|
|
|
#ifndef OMIT_JSON_SUPPORT
|
2022-05-13 16:34:15 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2019-05-13 19:24:55 +00:00
|
|
|
#endif
|
2017-01-10 21:51:10 +00:00
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Miscellaneous utility functions and global constants
|
|
|
|
*/
|
|
|
|
class OSUtils
|
|
|
|
{
|
|
|
|
public:
|
2017-07-06 23:11:11 +00:00
|
|
|
/**
|
|
|
|
* Variant of snprintf that is portable and throws an exception
|
|
|
|
*
|
|
|
|
* This just wraps the local implementation whatever it's called, while
|
|
|
|
* performing a few other checks and adding exceptions for overflow.
|
|
|
|
*
|
|
|
|
* @param buf Buffer to write to
|
|
|
|
* @param len Length of buffer in bytes
|
|
|
|
* @param fmt Format string
|
|
|
|
* @param ... Format arguments
|
|
|
|
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
|
|
|
*/
|
|
|
|
static unsigned int ztsnprintf(char *buf,unsigned int len,const char *fmt,...);
|
|
|
|
|
2023-05-03 20:43:45 +00:00
|
|
|
/**
|
|
|
|
* Converts a uint64_t network ID into a string
|
|
|
|
*
|
|
|
|
* @param nwid network ID
|
|
|
|
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
|
|
|
*/
|
|
|
|
static std::string networkIDStr(const uint64_t nwid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a uint64_t node ID into a string
|
|
|
|
*
|
|
|
|
* @param nid node ID
|
|
|
|
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
|
|
|
*/
|
|
|
|
static std::string nodeIDStr(const uint64_t nid);
|
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
|
|
|
/**
|
|
|
|
* Close STDOUT_FILENO and STDERR_FILENO and replace them with output to given path
|
|
|
|
*
|
|
|
|
* This can be called after fork() and prior to exec() to suppress output
|
|
|
|
* from a subprocess, such as auto-update.
|
|
|
|
*
|
|
|
|
* @param stdoutPath Path to file to use for stdout
|
|
|
|
* @param stderrPath Path to file to use for stderr, or NULL for same as stdout (default)
|
|
|
|
* @return True on success
|
|
|
|
*/
|
|
|
|
static bool redirectUnixOutputs(const char *stdoutPath,const char *stderrPath = (const char *)0)
|
|
|
|
throw();
|
|
|
|
#endif // __UNIX_LIKE__
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a file
|
|
|
|
*
|
|
|
|
* @param path Path to delete
|
|
|
|
* @return True if delete was successful
|
|
|
|
*/
|
|
|
|
static inline bool rm(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return (DeleteFileA(path) != FALSE);
|
|
|
|
#else
|
|
|
|
return (unlink(path) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
static inline bool rm(const std::string &path) { return rm(path.c_str()); }
|
2015-04-09 00:10:21 +00:00
|
|
|
|
2015-04-10 03:54:00 +00:00
|
|
|
static inline bool mkdir(const char *path)
|
|
|
|
{
|
2015-04-24 22:05:28 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if (::PathIsDirectoryA(path))
|
|
|
|
return true;
|
|
|
|
return (::CreateDirectoryA(path,NULL) == TRUE);
|
|
|
|
#else
|
2015-04-10 03:54:00 +00:00
|
|
|
if (::mkdir(path,0755) != 0)
|
|
|
|
return (errno == EEXIST);
|
|
|
|
return true;
|
2015-04-24 22:05:28 +00:00
|
|
|
#endif
|
2015-04-10 03:54:00 +00:00
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
static inline bool mkdir(const std::string &path) { return OSUtils::mkdir(path.c_str()); }
|
|
|
|
|
|
|
|
static inline bool rename(const char *o,const char *n)
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
DeleteFileA(n);
|
|
|
|
return (::rename(o,n) == 0);
|
|
|
|
#else
|
|
|
|
return (::rename(o,n) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2015-04-10 03:54:00 +00:00
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
/**
|
|
|
|
* List a directory's contents
|
2015-04-14 22:32:05 +00:00
|
|
|
*
|
2015-04-09 00:10:21 +00:00
|
|
|
* @param path Path to list
|
2017-04-18 20:48:19 +00:00
|
|
|
* @param includeDirectories If true, include directories as well as files
|
2016-08-16 21:05:17 +00:00
|
|
|
* @return Names of files in directory (without path prepended)
|
2015-04-09 00:10:21 +00:00
|
|
|
*/
|
2017-04-18 20:48:19 +00:00
|
|
|
static std::vector<std::string> listDirectory(const char *path,bool includeDirectories = false);
|
2015-04-09 00:10:21 +00:00
|
|
|
|
2017-03-08 18:18:12 +00:00
|
|
|
/**
|
|
|
|
* Clean a directory of files whose last modified time is older than this
|
|
|
|
*
|
|
|
|
* This ignores directories, symbolic links, and other special files.
|
|
|
|
*
|
|
|
|
* @param olderThan Last modified older than timestamp (ms since epoch)
|
|
|
|
* @return Number of cleaned files or negative on fatal error
|
|
|
|
*/
|
2017-10-25 23:08:14 +00:00
|
|
|
static long cleanDirectory(const char *path,const int64_t olderThan);
|
2017-03-08 18:18:12 +00:00
|
|
|
|
2016-08-16 21:05:17 +00:00
|
|
|
/**
|
|
|
|
* Delete a directory and all its files and subdirectories recursively
|
|
|
|
*
|
|
|
|
* @param path Path to delete
|
|
|
|
* @return True on success
|
|
|
|
*/
|
|
|
|
static bool rmDashRf(const char *path);
|
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
/**
|
|
|
|
* Set modes on a file to something secure
|
2015-07-24 21:50:44 +00:00
|
|
|
*
|
2015-04-09 00:10:21 +00:00
|
|
|
* This locks a file so that only the owner can access it. What it actually
|
|
|
|
* does varies by platform.
|
2015-07-24 21:50:44 +00:00
|
|
|
*
|
2015-04-09 00:10:21 +00:00
|
|
|
* @param path Path to lock
|
|
|
|
* @param isDir True if this is a directory
|
|
|
|
*/
|
|
|
|
static void lockDownFile(const char *path,bool isDir);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file last modification time
|
|
|
|
*
|
|
|
|
* Resolution is often only second, not millisecond, but the return is
|
|
|
|
* always in ms for comparison against now().
|
|
|
|
*
|
|
|
|
* @param path Path to file to get time
|
|
|
|
* @return Last modification time in ms since epoch or 0 if not found
|
|
|
|
*/
|
|
|
|
static uint64_t getLastModified(const char *path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param path Path to check
|
|
|
|
* @param followLinks Follow links (on platforms with that concept)
|
|
|
|
* @return True if file or directory exists at path location
|
|
|
|
*/
|
|
|
|
static bool fileExists(const char *path,bool followLinks = true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param path Path to file
|
|
|
|
* @return File size or -1 if nonexistent or other failure
|
|
|
|
*/
|
|
|
|
static int64_t getFileSize(const char *path);
|
|
|
|
|
2015-04-20 23:07:38 +00:00
|
|
|
/**
|
|
|
|
* Get IP (v4 and/or v6) addresses for a given host
|
|
|
|
*
|
|
|
|
* This is a blocking resolver.
|
|
|
|
*
|
|
|
|
* @param name Host name
|
|
|
|
* @return IP addresses in InetAddress sort order or empty vector if not found
|
|
|
|
*/
|
|
|
|
static std::vector<InetAddress> resolve(const char *name);
|
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
/**
|
|
|
|
* @return Current time in milliseconds since epoch
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
static inline int64_t now()
|
2015-04-09 00:10:21 +00:00
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
FILETIME ft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
ULARGE_INTEGER tmp;
|
|
|
|
GetSystemTime(&st);
|
|
|
|
SystemTimeToFileTime(&st,&ft);
|
|
|
|
tmp.LowPart = ft.dwLowDateTime;
|
|
|
|
tmp.HighPart = ft.dwHighDateTime;
|
2017-10-02 22:52:57 +00:00
|
|
|
return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds );
|
2015-04-09 00:10:21 +00:00
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,(struct timezone *)0);
|
2017-10-02 22:52:57 +00:00
|
|
|
return ( (1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000) );
|
2015-04-09 00:10:21 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the full contents of a file into a string buffer
|
|
|
|
*
|
|
|
|
* The buffer isn't cleared, so if it already contains data the file's data will
|
|
|
|
* be appended.
|
|
|
|
*
|
|
|
|
* @param path Path of file to read
|
|
|
|
* @param buf Buffer to fill
|
|
|
|
* @return True if open and read successful
|
|
|
|
*/
|
|
|
|
static bool readFile(const char *path,std::string &buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a block of data to disk, replacing any current file contents
|
|
|
|
*
|
|
|
|
* @param path Path to write
|
|
|
|
* @param buf Buffer containing data
|
|
|
|
* @param len Length of buffer
|
|
|
|
* @return True if entire file was successfully written
|
|
|
|
*/
|
|
|
|
static bool writeFile(const char *path,const void *buf,unsigned int len);
|
|
|
|
|
2016-11-18 23:49:28 +00:00
|
|
|
/**
|
|
|
|
* Split a string by delimiter, with optional escape and quote characters
|
|
|
|
*
|
|
|
|
* @param s String to split
|
|
|
|
* @param sep One or more separators
|
|
|
|
* @param esc Zero or more escape characters
|
|
|
|
* @param quot Zero or more quote characters
|
|
|
|
* @return Vector of tokens
|
|
|
|
*/
|
|
|
|
static std::vector<std::string> split(const char *s,const char *const sep,const char *esc,const char *quot);
|
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
/**
|
|
|
|
* Write a block of data to disk, replacing any current file contents
|
|
|
|
*
|
|
|
|
* @param path Path to write
|
|
|
|
* @param s Data to write
|
|
|
|
* @return True if entire file was successfully written
|
|
|
|
*/
|
2015-04-16 00:00:26 +00:00
|
|
|
static inline bool writeFile(const char *path,const std::string &s) { return writeFile(path,s.data(),(unsigned int)s.length()); }
|
2015-04-16 21:13:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param c ASCII character to convert
|
|
|
|
* @return Lower case ASCII character or unchanged if not a letter
|
|
|
|
*/
|
|
|
|
static inline char toLower(char c) throw() { return (char)OSUtils::TOLOWER_TABLE[(unsigned long)c]; }
|
|
|
|
|
2016-06-23 19:37:15 +00:00
|
|
|
/**
|
|
|
|
* @return Platform default ZeroTier One home path
|
|
|
|
*/
|
|
|
|
static std::string platformDefaultHomePath();
|
|
|
|
|
2019-05-13 19:24:55 +00:00
|
|
|
#ifndef OMIT_JSON_SUPPORT
|
2017-01-10 21:51:10 +00:00
|
|
|
static nlohmann::json jsonParse(const std::string &buf);
|
2017-07-19 21:13:17 +00:00
|
|
|
static std::string jsonDump(const nlohmann::json &j,int indentation = 1);
|
2017-01-10 21:51:10 +00:00
|
|
|
static uint64_t jsonInt(const nlohmann::json &jv,const uint64_t dfl);
|
2020-05-12 08:35:48 +00:00
|
|
|
static double jsonDouble(const nlohmann::json &jv,const double dfl);
|
2017-11-02 14:05:11 +00:00
|
|
|
static uint64_t jsonIntHex(const nlohmann::json &jv,const uint64_t dfl);
|
2017-01-10 21:51:10 +00:00
|
|
|
static bool jsonBool(const nlohmann::json &jv,const bool dfl);
|
|
|
|
static std::string jsonString(const nlohmann::json &jv,const char *dfl);
|
2017-01-11 22:37:31 +00:00
|
|
|
static std::string jsonBinFromHex(const nlohmann::json &jv);
|
2019-05-13 19:24:55 +00:00
|
|
|
#endif // OMIT_JSON_SUPPORT
|
2017-01-10 21:51:10 +00:00
|
|
|
|
2015-04-16 21:13:44 +00:00
|
|
|
private:
|
|
|
|
static const unsigned char TOLOWER_TABLE[256];
|
2015-04-09 00:10:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|