From b2ccf8dd31d1457ae9f0ae270054117179220370 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 7 Apr 2015 08:29:34 +0000 Subject: Imported from /home/lorry/working-area/delta_ntp/ntp-4.2.8p2.tar.gz. --- ports/winnt/include/config.h | 8 ++- ports/winnt/include/termios.h | 2 +- ports/winnt/libntp/arc4wrap.c | 84 ++++++++++++++++++++++++ ports/winnt/libntp/getclock.c | 2 + ports/winnt/libntp/termios.c | 3 +- ports/winnt/ntpd/nt_clockstuff.c | 41 +++++++----- ports/winnt/vs2005/libntp.vcproj | 4 ++ ports/winnt/vs2008/libntp/libntp.vcproj | 12 +++- ports/winnt/vs2013/libntp/libntp.vcxproj | 2 + ports/winnt/vs2013/libntp/libntp.vcxproj.filters | 6 ++ 10 files changed, 143 insertions(+), 21 deletions(-) create mode 100644 ports/winnt/libntp/arc4wrap.c (limited to 'ports') diff --git a/ports/winnt/include/config.h b/ports/winnt/include/config.h index b99aaa7..f56df9d 100644 --- a/ports/winnt/include/config.h +++ b/ports/winnt/include/config.h @@ -258,6 +258,7 @@ typedef int socklen_t; /* Enable OpenSSL */ #define OPENSSL 1 +#define USE_OPENSSL_CRYPTO_RAND 1 /* * Keywords and functions that Microsoft maps @@ -299,9 +300,13 @@ typedef int socklen_t; #define getpid _getpid #define timegm _mkgmtime #define errno_to_str isc__strerror +/* + * symbol returning the name of the current function + */ +#define __func__ __FUNCTION__ typedef int pid_t; /* PID is an int */ -typedef int ssize_t; /* ssize is an int */ +typedef int ssize_t; /* ssize is an int */ /* * Map the stream to the file number @@ -452,6 +457,7 @@ typedef unsigned long uintptr_t; #if defined(_MSC_VER) && _MSC_VER<1800 # define MISSING_INTTYPES_H 1 /* not provided by VS2012 and earlier */ # define MISSING_STDBOOL_H 1 /* not provided by VS2012 and earlier */ +# define MISSING_C99_STRUCT_INIT 1 /* see [Bug 2728] */ #else #if defined(_MSC_VER) && _MSC_VER>=1800 /* VS2013 and above support C99 types */ diff --git a/ports/winnt/include/termios.h b/ports/winnt/include/termios.h index 9c1cc62..94331af 100644 --- a/ports/winnt/include/termios.h +++ b/ports/winnt/include/termios.h @@ -205,7 +205,7 @@ struct termios #define cfsetispeed(dcb, spd) (0) extern int closeserial (int); -extern int ioctl (int, int, int *); +extern int ioctl (int, int, void *); extern int tcsetattr (int, int, const struct termios *); extern int tcgetattr (int, struct termios *); extern int tcflush (int, int); diff --git a/ports/winnt/libntp/arc4wrap.c b/ports/winnt/libntp/arc4wrap.c new file mode 100644 index 0000000..9513d15 --- /dev/null +++ b/ports/winnt/libntp/arc4wrap.c @@ -0,0 +1,84 @@ +/* + * arc4wrap.c - wrapper for libevent's ARCFOUR random number generator + * + * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. + * The contents of 'html/copyright.html' apply. + * -------------------------------------------------------------------- + * This is an inclusion wrapper for the ARCFOUR implementation in + * libevent. It's main usage is to enable a openSSL-free build on Win32 + * without a full integration of libevent. This provides Win32 specific + * glue to make the PRNG working. Porting to POSIX should be easy, but + * on most POSIX systems using openSSL is no problem and falling back to + * using ARCFOUR instead of the openSSL PRNG is not necessary. And even + * if it is, there's a good chance that ARCFOUR is a system library. + */ +#include +#ifdef _WIN32 +# include +# include +#else +# error this is currently a pure windows port +#endif + +#include +#include +#include +#include "ntp_types.h" +#include "ntp_stdlib.h" + +/* ARCFOUR implementation glue */ +/* export type is empty, since this goes into a static library*/ +#define ARC4RANDOM_EXPORT +/* we use default uint32_t as UINT32 */ +#define ARC4RANDOM_UINT32 uint32_t +/* do not use ARCFOUR's default includes - we gobble it all up here. */ +#define ARC4RANDOM_NO_INCLUDES +/* And the locking. Could probably be left empty. */ +#define ARC4_LOCK_() private_lock_() +#define ARC4_UNLOCK_() private_unlock_() + +/* support code */ + +static void +evutil_memclear_( + void *buf, + size_t len) +{ + memset(buf, 0, len); +} + +/* locking uses a manual thread-safe ONCE pattern. There's no static + * initialiser pattern that can be used for critical sections, and + * we must make sure we do the creation exactly once on the first call. + */ + +static long once_ = 0; +static CRITICAL_SECTION csec_; + +static void +private_lock_(void) +{ +again: + switch (InterlockedCompareExchange(&once_, 1, 0)) { + case 0: + InitializeCriticalSection(&csec_); + InterlockedExchange(&once_, 2); + case 2: + EnterCriticalSection(&csec_); + break; + + default: + YieldProcessor(); + goto again; + } +} + +static void +private_unlock_(void) +{ + if (InterlockedExchangeAdd(&once_, 0) == 2) + LeaveCriticalSection(&csec_); +} + +#pragma warning(disable : 4244) +#include "../../../sntp/libevent/arc4random.c" diff --git a/ports/winnt/libntp/getclock.c b/ports/winnt/libntp/getclock.c index 68496bd..0fdcb69 100644 --- a/ports/winnt/libntp/getclock.c +++ b/ports/winnt/libntp/getclock.c @@ -33,6 +33,8 @@ getclock( return -1; } + if (NULL == get_sys_time_as_filetime) + init_win_precise_time(); (*get_sys_time_as_filetime)(&uNow.ft); /* diff --git a/ports/winnt/libntp/termios.c b/ports/winnt/libntp/termios.c index 3fe4e48..5ac017d 100644 --- a/ports/winnt/libntp/termios.c +++ b/ports/winnt/libntp/termios.c @@ -468,12 +468,13 @@ int ioctl( int fd, int op, - int *pi + void *pv ) { HANDLE h; int result; int modctl; + int *pi = (int *) pv; h = (HANDLE)_get_osfhandle(fd); diff --git a/ports/winnt/ntpd/nt_clockstuff.c b/ports/winnt/ntpd/nt_clockstuff.c index 052bfcd..dc2f150 100644 --- a/ports/winnt/ntpd/nt_clockstuff.c +++ b/ports/winnt/ntpd/nt_clockstuff.c @@ -68,8 +68,8 @@ BOOL init_randfile(); static long last_Adj = 0; #define LS_CORR_INTV_SECS 2 /* seconds to apply leap second correction */ -#define LS_CORR_INTV ( (LONGLONG) HECTONANOSECONDS * LS_CORR_INTV_SECS ) -#define LS_CORR_LIMIT ( (LONGLONG) HECTONANOSECONDS / 2 ) // half a second +#define LS_CORR_INTV ( 1000ul * LS_CORR_INTV_SECS ) +#define LS_CORR_LIMIT ( 250ul ) // quarter second typedef union ft_ull { FILETIME ft; @@ -81,8 +81,6 @@ typedef union ft_ull { /* leap second stuff */ static FT_ULL ls_ft; static DWORD ls_time_adjustment; -static ULONGLONG ls_ref_perf_cnt; -static LONGLONG ls_elapsed; static BOOL winnt_time_initialized = FALSE; static BOOL winnt_use_interpolation = FALSE; @@ -469,13 +467,15 @@ adj_systime( /* ntp time scale origin as ticks since 1601-01-01 */ static const ULONGLONG HNS_JAN_1900 = 94354848000000000ull; + static DWORD ls_start_tick; /* start of slew in 1ms ticks */ + static double adjtime_carry; double dtemp; u_char isneg; BOOL rc; long TimeAdjustment; SYSTEMTIME st; - ULONGLONG this_perf_count; + DWORD ls_elapsed; FT_ULL curr_ft; leap_result_t lsi; @@ -542,7 +542,8 @@ adj_systime( /* A leap second insert is scheduled at the end * of the day. Since we have not yet computed the * time stamp, do it now. Signal electric mode - * for this insert. + * for this insert. We start processing 1 second early + * because we want to slew over 2 seconds. */ ls_ft.ull = lsi.ttime.Q_s * HECTONANOSECONDS + HNS_JAN_1900; @@ -552,6 +553,10 @@ adj_systime( "for %04d-%02d-%02d %02d:%02d:%02d UTC", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); + /* slew starts with last second before insertion! + * And we have to tell the core that we deal with it. + */ + ls_ft.ull -= (HECTONANOSECONDS + HECTONANOSECONDS/2); leapsec_electric(TRUE); } else if (lsi.tai_diff < 0) { /* Do not handle negative leap seconds here. If this @@ -574,29 +579,29 @@ adj_systime( /* * If the time stamp for the next leap second has been set - * then check if the leap second must be handled + * then check if the leap second must be handled. We use + * free-running milliseconds from 'GetTickCount()', which + * is documented as not affected by clock and/or speed + * adjustments. */ if (ls_ft.ull != 0) { - this_perf_count = perf_ctr(); - if (0 == ls_time_adjustment) { /* has not yet been scheduled */ - GetSystemTimeAsFileTime(&curr_ft.ft); if (curr_ft.ull >= ls_ft.ull) { + ls_ft.ull = _UI64_MAX; /* guard against second schedule */ ls_time_adjustment = clockperiod / LS_CORR_INTV_SECS; - ls_ref_perf_cnt = this_perf_count; - ls_elapsed = 0; - msyslog(LOG_NOTICE, "Inserting positive leap second."); + ls_start_tick = GetTickCount(); + msyslog(LOG_NOTICE, "Started leap second insertion."); } + ls_elapsed = 0; } else { /* leap sec adjustment has been scheduled previously */ - ls_elapsed = (this_perf_count - ls_ref_perf_cnt) - * HECTONANOSECONDS / PerfCtrFreq; + ls_elapsed = GetTickCount() - ls_start_tick; } if (ls_time_adjustment != 0) { /* leap second adjustment is currently active */ if (ls_elapsed > (LS_CORR_INTV - LS_CORR_LIMIT)) { ls_time_adjustment = 0; /* leap second adjustment done */ - ls_ft.ull = 0; + msyslog(LOG_NOTICE, "Finished leap second insertion."); } /* @@ -819,6 +824,10 @@ init_winnt_time(void) msyslog(LOG_INFO, "MM timer resolution: %u..%u msec, set to %u msec", tc.wPeriodMin, tc.wPeriodMax, wTimerRes ); + + /* Pause briefly before measuring the clock precision, see [Bug 2790] */ + Sleep( 33 ); + } else { msyslog(LOG_ERR, "Multimedia timer unavailable"); } diff --git a/ports/winnt/vs2005/libntp.vcproj b/ports/winnt/vs2005/libntp.vcproj index f2d0b9e..fdc3374 100644 --- a/ports/winnt/vs2005/libntp.vcproj +++ b/ports/winnt/vs2005/libntp.vcproj @@ -180,6 +180,10 @@ RelativePath="..\..\..\libntp\adjtime.c" > + + diff --git a/ports/winnt/vs2008/libntp/libntp.vcproj b/ports/winnt/vs2008/libntp/libntp.vcproj index 6bf25cb..b87d178 100644 --- a/ports/winnt/vs2008/libntp/libntp.vcproj +++ b/ports/winnt/vs2008/libntp/libntp.vcproj @@ -192,6 +192,10 @@ RelativePath="..\..\..\..\libntp\adjtime.c" > + + @@ -400,6 +404,10 @@ RelativePath="..\..\..\..\libntp\ntp_calendar.c" > + + @@ -874,11 +882,11 @@ > + @@ -571,6 +572,7 @@ + diff --git a/ports/winnt/vs2013/libntp/libntp.vcxproj.filters b/ports/winnt/vs2013/libntp/libntp.vcxproj.filters index 4eefad7..10596c6 100644 --- a/ports/winnt/vs2013/libntp/libntp.vcxproj.filters +++ b/ports/winnt/vs2013/libntp/libntp.vcxproj.filters @@ -317,6 +317,12 @@ Source Files + + Source Files + + + Source Files + -- cgit v1.2.1