diff --git a/netapi.h b/netapi.h index 2bb455e..9a33cdf 100644 --- a/netapi.h +++ b/netapi.h @@ -18,10 +18,6 @@ #pragma warning (disable: 4200) -uint64 OsGetMilliseconds(); -void OsGetTimestampTAI64N(uint8 dst[12]); -void OsInterruptibleSleep(int millis); - union IpAddr { sockaddr_in sin; sockaddr_in6 sin6; diff --git a/network_bsd_common.cpp b/network_bsd_common.cpp index f0a59fa..0d4346f 100644 --- a/network_bsd_common.cpp +++ b/network_bsd_common.cpp @@ -249,45 +249,6 @@ done: #endif // defined(OS_LINUX) -#if defined(OS_MACOSX) -static mach_timebase_info_data_t timebase = { 0, 0 }; -static uint64_t initclock; - -void InitOsxGetMilliseconds() { - if (mach_timebase_info(&timebase) != 0) - abort(); - initclock = mach_absolute_time(); - - timebase.denom *= 1000000; -} - -uint64 OsGetMilliseconds() -{ - uint64_t clock = mach_absolute_time() - initclock; - return clock * (uint64_t)timebase.numer / (uint64_t)timebase.denom; -} - -#else // defined(OS_MACOSX) -uint64 OsGetMilliseconds() { - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { - //error - fprintf(stderr, "clock_gettime failed\n"); - exit(1); - } - return (uint64)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000); -} -#endif - -void OsGetTimestampTAI64N(uint8 dst[12]) { - struct timeval tv; - gettimeofday(&tv, NULL); - uint64 secs_since_epoch = tv.tv_sec + 0x400000000000000a; - uint32 nanos = tv.tv_usec * 1000; - WriteBE64(dst, secs_since_epoch); - WriteBE32(dst + 8, nanos); -} - void OsInterruptibleSleep(int millis) { usleep((useconds_t)millis * 1000); } diff --git a/network_win32.cpp b/network_win32.cpp index a8c0469..c1fc397 100644 --- a/network_win32.cpp +++ b/network_win32.cpp @@ -60,26 +60,6 @@ static bool IsIpv6AddressSet(const void *p) { return (ReadLE64(p) | ReadLE64((char*)p + 8)) != 0; } -void OsInterruptibleSleep(int millis) { - SleepEx(millis, TRUE); -} - -uint64 OsGetMilliseconds() { - return GetTickCount64(); -} - -void OsGetTimestampTAI64N(uint8 dst[12]) { - SYSTEMTIME systime; - uint64 file_time_uint64 = 0; - GetSystemTime(&systime); - SystemTimeToFileTime(&systime, (FILETIME*)&file_time_uint64); - uint64 time_since_epoch_100ns = (file_time_uint64 - 116444736000000000); - uint64 secs_since_epoch = time_since_epoch_100ns / 10000000 + 0x400000000000000a; - uint32 nanos = (uint32)(time_since_epoch_100ns % 10000000) * 100; - WriteBE64(dst, secs_since_epoch); - WriteBE32(dst + 8, nanos); -} - extern "C" PSLIST_ENTRY __fastcall InterlockedPushListSList( IN PSLIST_HEADER ListHead, diff --git a/util.cpp b/util.cpp index 2d7fe8e..8c45163 100644 --- a/util.cpp +++ b/util.cpp @@ -20,6 +20,7 @@ #include #include #include "tunsafe_types.h" +#include "tunsafe_endian.h" static const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -410,3 +411,73 @@ bool ParseBase64Key(const char *s, uint8 key[32]) { return base64_decode((uint8*)s, strlen(s), key, &size) && size == 32; } + +#if defined(OS_WIN) +uint64 OsGetMilliseconds() { + return GetTickCount64(); +} + +void OsGetTimestampTAI64N(uint8 dst[12]) { + SYSTEMTIME systime; + uint64 file_time_uint64 = 0; + GetSystemTime(&systime); + SystemTimeToFileTime(&systime, (FILETIME*)&file_time_uint64); + uint64 time_since_epoch_100ns = (file_time_uint64 - 116444736000000000); + uint64 secs_since_epoch = time_since_epoch_100ns / 10000000 + 0x400000000000000a; + uint32 nanos = (uint32)(time_since_epoch_100ns % 10000000) * 100; + WriteBE64(dst, secs_since_epoch); + WriteBE32(dst + 8, nanos); +} + +void OsInterruptibleSleep(int millis) { + SleepEx(millis, TRUE); +} + +#endif // defined(OS_WIN) + + +#if defined(OS_POSIX) + +#if defined(OS_MACOSX) +static mach_timebase_info_data_t timebase = { 0, 0 }; +static uint64_t initclock; + +void InitOsxGetMilliseconds() { + if (mach_timebase_info(&timebase) != 0) + abort(); + initclock = mach_absolute_time(); + + timebase.denom *= 1000000; +} + +uint64 OsGetMilliseconds() { + uint64_t clock = mach_absolute_time() - initclock; + return clock * (uint64_t)timebase.numer / (uint64_t)timebase.denom; +} + +#else // defined(OS_MACOSX) +uint64 OsGetMilliseconds() { + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + //error + fprintf(stderr, "clock_gettime failed\n"); + exit(1); + } + return (uint64)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000); +} +#endif + +void OsGetTimestampTAI64N(uint8 dst[12]) { + struct timeval tv; + gettimeofday(&tv, NULL); + uint64 secs_since_epoch = tv.tv_sec + 0x400000000000000a; + uint32 nanos = tv.tv_usec * 1000; + WriteBE64(dst, secs_since_epoch); + WriteBE32(dst + 8, nanos); +} + +void OsInterruptibleSleep(int millis) { + usleep((useconds_t)millis * 1000); + } + +#endif // defined(OS_POSIX) \ No newline at end of file diff --git a/util.h b/util.h index 128d376..7eae298 100644 --- a/util.h +++ b/util.h @@ -41,4 +41,10 @@ bool ParseConfigKeyValue(char *m, std::vector> *result) bool ParseHexString(const char *text, void *data, size_t data_size); void PrintHexString(const void *data, size_t data_size, char *result); void SplitString(char *s, int separator, std::vector *components); -bool ParseBase64Key(const char *s, uint8 key[32]); \ No newline at end of file +bool ParseBase64Key(const char *s, uint8 key[32]); + + +uint64 OsGetMilliseconds(); +void InitOsxGetMilliseconds(); +void OsInterruptibleSleep(int millis); +void OsGetTimestampTAI64N(uint8 dst[12]);