[Bug 1832] ntpdate doesn't allow timeout > 2s.
[Bug 1933] WWVB/Spectracom driver timestamps LFs, not CRs.
Backport utility routines from ntp-dev: mprintf(), emalloc_zero().
bk: 4ddc7b20q46RhHddOdauhS9oGZZKbw
+---
+
+* [Bug 1695] ntpdate takes longer than necessary.
+* [Bug 1832] ntpdate doesn't allow timeout > 2s.
+* [Bug 1933] WWVB/Spectracom driver timestamps LFs, not CRs.
+* Backport utility routines from ntp-dev: mprintf(), emalloc_zero().
+
---
(4.2.6p4-beta1) 2011/05/16 Released by Harlan Stenn <stenn@ntp.org>
* int result;
* int value;
*
- * NTP_REQUIRE(a != NULL);
+ * REQUIRE(a != NULL);
* ...
* bar(&value);
- * NTP_INSIST(value > 2);
+ * INSIST(value > 2);
* ...
*
- * NTP_ENSURE(result != 12);
+ * ENSURE(result != 12);
* return result;
* }
*
- * open question: when would we use NTP_INVARIANT()?
+ * open question: when would we use INVARIANT()?
+ *
+ * For cases where the overhead for non-debug builds is deemed too high,
+ * use DEBUG_REQUIRE(), DEBUG_INSIST(), DEBUG_ENSURE(), and/or
+ * DEBUG_INVARIANT().
*/
#ifndef NTP_ASSERT_H
extern void calysto_assume(unsigned char cnd); /* assume this always holds */
extern void calysto_assert(unsigned char cnd); /* check whether this holds */
-#define NTP_REQUIRE(x) calysto_assert(x)
-#define NTP_INSIST(x) calysto_assume(x) /* DLH calysto_assert()? */
-#define NTP_INVARIANT(x) calysto_assume(x)
-#define NTP_ENSURE(x) calysto_assert(x)
+#define ALWAYS_REQUIRE(x) calysto_assert(x)
+#define ALWAYS_INSIST(x) calysto_assume(x) /* DLH calysto_assert()? */
+#define ALWAYS_INVARIANT(x) calysto_assume(x)
+#define ALWAYS_ENSURE(x) calysto_assert(x)
-# elif defined(__COVERITY__)
+/* # elif defined(__COVERITY__) */
+/*
+ * DH: try letting coverity scan our actual assertion macros, now that
+ * isc_assertioncallback_t is marked __attribute__ __noreturn__.
+ */
/*
* Coverity has special knowledge that assert(x) terminates the process
* that seems to be a reasonable trade-off.
*/
-#define NTP_REQUIRE(x) assert(x)
-#define NTP_INSIST(x) assert(x)
-#define NTP_INVARIANT(x) assert(x)
-#define NTP_ENSURE(x) assert(x)
+/*
+#define ALWAYS_REQUIRE(x) assert(x)
+#define ALWAYS_INSIST(x) assert(x)
+#define ALWAYS_INVARIANT(x) assert(x)
+#define ALWAYS_ENSURE(x) assert(x)
+*/
# else /* neither Coverity nor Calysto */
#include "isc/assertions.h"
-#define NTP_REQUIRE(x) ISC_REQUIRE(x)
-#define NTP_INSIST(x) ISC_INSIST(x)
-#define NTP_INVARIANT(x) ISC_INVARIANT(x)
-#define NTP_ENSURE(x) ISC_ENSURE(x)
+#define ALWAYS_REQUIRE(x) ISC_REQUIRE(x)
+#define ALWAYS_INSIST(x) ISC_INSIST(x)
+#define ALWAYS_INVARIANT(x) ISC_INVARIANT(x)
+#define ALWAYS_ENSURE(x) ISC_ENSURE(x)
# endif /* neither Coverity nor Calysto */
+
+#define REQUIRE(x) ALWAYS_REQUIRE(x)
+#define INSIST(x) ALWAYS_INSIST(x)
+#define INVARIANT(x) ALWAYS_INVARIANT(x)
+#define ENSURE(x) ALWAYS_ENSURE(x)
+
+/*
+ * We initially used NTP_REQUIRE() instead of REQUIRE() etc, but that
+ * is unneccesarily verbose, as libisc use of REQUIRE() etc shows.
+ */
+#define NTP_REQUIRE(x) REQUIRE(x)
+#define NTP_INSIST(x) INSIST(x)
+#define NTP_INVARIANT(x) INVARIANT(x)
+#define NTP_ENSURE(x) ENSURE(x)
+
+# ifdef DEBUG
+#define DEBUG_REQUIRE(x) REQUIRE(x)
+#define DEBUG_INSIST(x) INSIST(x)
+#define DEBUG_INVARIANT(x) INVARIANT(x)
+#define DEBUG_ENSURE(x) ENSURE(x)
+# else
+#define DEBUG_REQUIRE(x) (void)(x)
+#define DEBUG_INSIST(x) (void)(x)
+#define DEBUG_INVARIANT(x) (void)(x)
+#define DEBUG_ENSURE(x) (void)(x)
+# endif
+
#endif /* NTP_ASSERT_H */
#ifndef NTP_MALLOC_H
#define NTP_MALLOC_H
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#else
# endif
#endif
+/*
+ * Deal with platform differences declaring alloca()
+ * This comes nearly verbatim from:
+ *
+ * http://www.gnu.org/software/autoconf/manual/autoconf.html#Particular-Functions
+ *
+ * The only modifications were to remove C++ support and guard against
+ * redefining alloca.
+ */
+#ifdef HAVE_ALLOCA_H
+# include <alloca.h>
+#elif defined __GNUC__
+# ifndef alloca
+# define alloca __builtin_alloca
+# endif
+#elif defined _AIX
+# ifndef alloca
+# define alloca __alloca
+# endif
+#elif defined _MSC_VER
+# include <malloc.h>
+# ifndef alloca
+# define alloca _alloca
+# endif
+#else
+# include <stddef.h>
+void * alloca(size_t);
+#endif
+
+#ifdef EREALLOC_IMPL
+# define EREALLOC_CALLSITE /* preserve __FILE__ and __LINE__ */
+#else
+# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \
+ realloc(ptr, (newsz))
+#endif
+
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+# define zero_mem(p, s) bzero(p, s)
+#endif
+
+#ifndef zero_mem
+# define zero_mem(p, s) memset(p, 0, s)
+#endif
+#define ZERO(var) zero_mem(&(var), sizeof(var))
+
#endif /* NTP_MALLOC_H */
struct refclockproc {
struct refclockio io; /* I/O handler structure */
- caddr_t unitptr; /* pointer to unit structure */
+ void * unitptr; /* pointer to unit structure */
u_char leap; /* leap/synchronization code */
u_char currentstatus; /* clock status */
u_char lastevent; /* last exception event */
#include "l_stdlib.h"
#include "ntp_rfc2553.h"
#include "ntp_types.h"
+#include "ntp_malloc.h"
#include "ntp_string.h"
#include "ntp_net.h"
#include "ntp_syslog.h"
# endif
#endif
-extern size_t mvsnprintf(char *, size_t, const char *, va_list);
-extern size_t msnprintf(char *, size_t, const char *, ...)
- __attribute__((__format__(__printf__, 3, 4)));
+extern int mprintf(const char *, ...)
+ __attribute__((__format__(__printf__, 1, 2)));
+extern int mfprintf(FILE *, const char *, ...)
+ __attribute__((__format__(__printf__, 2, 3)));
+extern int mvfprintf(FILE *, const char *, va_list)
+ __attribute__((__format__(__printf__, 2, 0)));
+extern int mvsnprintf(char *, size_t, const char *, va_list)
+ __attribute__((__format__(__printf__, 3, 0)));
+extern int msnprintf(char *, size_t, const char *, ...)
+ __attribute__((__format__(__printf__, 3, 4)));
extern void msyslog(int, const char *, ...)
__attribute__((__format__(__printf__, 2, 3)));
extern u_long calyearstart (u_long);
extern const char *clockname (int);
extern int clocktime (int, int, int, int, int, u_long, u_long *, u_int32 *);
-#if !defined(_MSC_VER) || !defined(_DEBUG)
-extern void * emalloc (size_t);
-extern void * erealloc (void *, size_t);
-extern char * estrdup (const char *);
-#else
-extern void * debug_erealloc (void *, size_t, const char *, int);
-#define emalloc(c) debug_erealloc(NULL, (c), __FILE__, __LINE__)
-#define erealloc(p, c) debug_erealloc((p), (c), __FILE__, __LINE__)
-extern char * debug_estrdup (const char *, const char *, int);
-#define estrdup(s) debug_estrdup((s), __FILE__, __LINE__)
-#endif
extern int ntp_getopt (int, char **, const char *);
extern void init_auth (void);
extern void init_lib (void);
extern void MD5auth_setkey (keyid_t, int, const u_char *, const int);
extern u_int32 addr2refid (sockaddr_u *);
+/* emalloc.c */
+#ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */
+extern void * ereallocz (void *, size_t, size_t, int);
+#define erealloczsite(p, n, o, z, f, l) ereallocz(p, n, o, (z))
+extern void * emalloc (size_t);
+#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE)
+#define erealloc(p, c) ereallocz(p, (c), 0, FALSE)
+#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE)
+extern char * estrdup_impl (const char *);
+#define estrdup(s) estrdup_impl(s)
+#else
+extern void * ereallocz (void *, size_t, size_t, int,
+ const char *, int);
+#define erealloczsite ereallocz
+#define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \
+ __FILE__, __LINE__)
+#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \
+ __FILE__, __LINE__)
+#define erealloc(p, c) ereallocz(p, (c), 0, FALSE, \
+ __FILE__, __LINE__)
+#define erealloc_zero(p, n, o) ereallocz(p, n, (o), TRUE, \
+ __FILE__, __LINE__)
+extern char * estrdup_impl (const char *, const char *, int);
+#define estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
+#endif
+
extern int atoint (const char *, long *);
extern int atouint (const char *, u_long *);
#include <sys/types.h>
#include "ntp_machine.h"
+#ifndef TRUE
+# define TRUE 1
+#endif
+#ifndef FALSE
+# define FALSE 0
+#endif
+
/*
* This is another naming conflict.
* On NetBSD for MAC the macro "mac" is defined as 1
/*
* emalloc - return new memory obtained from the system. Belch if none.
*/
+#include <config.h>
#include "ntp_types.h"
#include "ntp_malloc.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
-extern char *progname;
-
-#if !defined(_MSC_VER) || !defined(_DEBUG)
-
-
-void *
-erealloc(
- void * prev,
- size_t size
- )
-{
- void * mem;
-
- mem = realloc(prev, size ? size : 1);
-
- if (NULL == mem) {
- msyslog(LOG_ERR,
- "fatal out of memory (%u bytes)", (u_int)size);
- fprintf(stderr,
- "%s: fatal out of memory (%u bytes)", progname,
- (u_int)size);
- exit(1);
- }
-
- return mem;
-}
-
-
-void *
-emalloc(
- size_t size
- )
-{
- return erealloc(NULL, size);
-}
-
-
-char *
-estrdup(
- const char * str
- )
-{
- char * copy;
-
- copy = strdup(str);
-
- if (NULL == copy) {
- msyslog(LOG_ERR,
- "fatal out of memory duplicating %u bytes",
- (u_int)strlen(str) + 1);
- fprintf(stderr,
- "%s: fatal out of memory duplicating %u bytes",
- progname, (u_int)strlen(str) + 1);
- exit(1);
- }
-
- return copy;
-}
-
-#else /* below is _MSC_VER && _DEBUG */
/*
* When using the debug MS CRT allocator, each allocation stores the
* allocations to show up as coming from emalloc.c, so we preserve the
* original callsite's source file and line using macros which pass
* __FILE__ and __LINE__ as parameters to these routines.
+ * Other debug malloc implementations can be used by defining
+ * EREALLOC_IMPL() as ports/winnt/include/config.h does.
*/
void *
-debug_erealloc(
- void * prev,
- size_t size,
- const char * file, /* __FILE__ */
- int line /* __LINE__ */
+ereallocz(
+ void * ptr,
+ size_t newsz,
+ size_t priorsz,
+ int zero_init
+#ifdef EREALLOC_CALLSITE /* ntp_malloc.h */
+ ,
+ const char * file,
+ int line
+#endif
)
{
- void * mem;
+ char * mem;
+ size_t allocsz;
- mem = _realloc_dbg(prev, size ? size : 1,
- _NORMAL_BLOCK, file, line);
+ if (0 == newsz)
+ allocsz = 1;
+ else
+ allocsz = newsz;
+ mem = EREALLOC_IMPL(ptr, allocsz, file, line);
if (NULL == mem) {
+ msyslog_term = TRUE;
+#ifndef EREALLOC_CALLSITE
+ msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
+ (u_long)newsz);
+#else
msyslog(LOG_ERR,
- "fatal: out of memory in %s line %d size %u",
- file, line, (u_int)size);
- fprintf(stderr,
- "%s: fatal: out of memory in %s line %d size %u",
- progname, file, line, (u_int)size);
+ "fatal out of memory %s line %d (%lu bytes)",
+ file, line, (u_long)newsz);
+#endif
exit(1);
}
+ if (zero_init && newsz > priorsz)
+ zero_mem(mem + priorsz, newsz - priorsz);
+
return mem;
}
+
char *
-debug_estrdup(
- const char * str,
- const char * file, /* __FILE__ */
- int line /* __LINE__ */
+estrdup_impl(
+ const char * str
+#ifdef EREALLOC_CALLSITE
+ ,
+ const char * file,
+ int line
+#endif
)
{
char * copy;
size_t bytes;
bytes = strlen(str) + 1;
- copy = debug_erealloc(NULL, bytes, file, line);
+ copy = ereallocz(NULL, bytes, 0, FALSE
+#ifdef EREALLOC_CALLSITE
+ , file, line
+#endif
+ );
memcpy(copy, str, bytes);
return copy;
}
-#endif /* _MSC_VER && _DEBUG */
+
+#ifndef EREALLOC_CALLSITE
+void *
+emalloc(size_t newsz)
+{
+ return ereallocz(NULL, newsz, 0, FALSE);
+}
+#endif
+
}
-size_t
+int
mvsnprintf(
char * buf,
size_t bufsiz,
va_list ap
)
{
- char nfmt[256];
- int errval;
+#ifndef VSNPRINTF_PERCENT_M
+ char nfmt[256];
+#else
+ const char * nfmt = fmt;
+#endif
+ int errval;
/*
* Save the error value as soon as possible
*/
- errval = errno;
-
#ifdef SYS_WINNT
errval = GetLastError();
if (NO_ERROR == errval)
+#endif /* SYS_WINNT */
errval = errno;
+
+#ifndef VSNPRINTF_PERCENT_M
+ format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
+#else
+ errno = errval;
+#endif
+ return vsnprintf(buf, bufsiz, nfmt, ap);
+}
+
+
+int
+mvfprintf(
+ FILE * fp,
+ const char * fmt,
+ va_list ap
+ )
+{
+#ifndef VSNPRINTF_PERCENT_M
+ char nfmt[256];
+#else
+ const char * nfmt = fmt;
+#endif
+ int errval;
+
+ /*
+ * Save the error value as soon as possible
+ */
+#ifdef SYS_WINNT
+ errval = GetLastError();
+ if (NO_ERROR == errval)
#endif /* SYS_WINNT */
+ errval = errno;
+#ifndef VSNPRINTF_PERCENT_M
format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
+#else
+ errno = errval;
+#endif
+ return vfprintf(fp, nfmt, ap);
+}
- return vsnprintf(buf, bufsiz, nfmt, ap);
+
+int
+mfprintf(
+ FILE * fp,
+ const char * fmt,
+ ...
+ )
+{
+ va_list ap;
+ int rc;
+
+ va_start(ap, fmt);
+ rc = mvfprintf(fp, fmt, ap);
+ va_end(ap);
+
+ return rc;
+}
+
+
+int
+mprintf(
+ const char * fmt,
+ ...
+ )
+{
+ va_list ap;
+ int rc;
+
+ va_start(ap, fmt);
+ rc = mvfprintf(stdout, fmt, ap);
+ va_end(ap);
+
+ return rc;
}
-size_t
+
+int
msnprintf(
char * buf,
size_t bufsiz,
-#include "ntp_malloc.h"
+#include <config.h>
-#if !HAVE_STRDUP
+#include <string.h>
+#include "ntp_malloc.h"
-#define NULL 0
+#ifndef HAVE_STRDUP
char *strdup(const char *s);
const char *s
)
{
- char *cp;
+ size_t octets;
+ char * cp;
+
+ if (s) {
+ octets = 1 + strlen(s);
+ cp = malloc(octets);
+ if (NULL != cp)
+ memcpy(cp, s, octets);
+ else
+ cp = NULL;
- if (s) {
- cp = (char *) malloc((unsigned) (strlen(s)+1));
- if (cp) {
- (void) strcpy(cp, s);
- }
- } else {
- cp = (char *) NULL;
- }
- return(cp);
+ return(cp);
}
#else
-int strdup_bs;
+int strdup_c_nonempty_compilation_unit;
#endif
* hh:mm:ss = hours, minutes, seconds
* i = synchronization flag (' ' = in synch, '?' = out of synch)
*
- * The alarm condition is indicated by other than ' ' at a, which occurs
+ * The alarm condition is indicated by other than ' ' at i, which occurs
* during initial synchronization and when received signal is lost for
* about ten hours.
*
* ddd = day of year
* hh:mm:ss.fff = hours, minutes, seconds, milliseconds
*
- * The alarm condition is indicated by other than ' ' at a, which occurs
+ * The alarm condition is indicated by other than ' ' at i, which occurs
* during initial synchronization and when received signal is lost for
* about ten hours. The unlock condition is indicated by other than ' '
* at q.
#define DESCRIPTION "Spectracom WWVB/GPS Receiver" /* WRU */
#define LENWWVB0 22 /* format 0 timecode length */
-#define LENWWVB1 22 /* format 1 timecode length */
#define LENWWVB2 24 /* format 2 timecode length */
-#define LENWWVB3 29 /* format 3 timecode length */
+#define LENWWVB3 29 /* format 3 timecode length */
#define MONLIN 15 /* number of monitoring lines */
/*
int tcount; /* timecode sample counter */
int pcount; /* PPS sample counter */
#endif /* HAVE_PPSAPI */
- l_fp laststamp; /* last receive timestamp */
+ l_fp laststamp; /* last <CR> timestamp */
+ int prev_eol_cr; /* was last EOL <CR> (not <LF>)? */
u_char lasthour; /* last hour (for monitor) */
u_char linect; /* count ignored lines (for monitor */
};
static void wwvb_poll (int, struct peer *);
static void wwvb_timer (int, struct peer *);
#ifdef HAVE_PPSAPI
-static void wwvb_control (int, struct refclockstat *,
+static void wwvb_control (int, const struct refclockstat *,
struct refclockstat *, struct peer *);
#define WWVB_CONTROL wwvb_control
#else
* Open serial port. Use CLK line discipline, if available.
*/
snprintf(device, sizeof(device), DEVICE, unit);
- if (0 == (fd = refclock_open(device, SPEED232, LDISC_CLK)))
+ fd = refclock_open(device, SPEED232, LDISC_CLK);
+ if (fd <= 0)
return (0);
/*
* Allocate and initialize unit structure
*/
- up = emalloc(sizeof(*up));
- memset(up, 0, sizeof(*up));
+ up = emalloc_zero(sizeof(*up));
pp = peer->procptr;
- pp->unitptr = (caddr_t)up;
pp->io.clock_recv = wwvb_receive;
- pp->io.srcclock = (caddr_t)peer;
+ pp->io.srcclock = (void *)peer;
pp->io.datalen = 0;
pp->io.fd = fd;
if (!io_addclock(&pp->io)) {
close(fd);
pp->io.fd = -1;
free(up);
- pp->unitptr = NULL;
return (0);
}
+ pp->unitptr = up;
/*
* Initialize miscellaneous variables
struct refclockproc *pp;
pp = peer->procptr;
- up = (struct wwvbunit *)pp->unitptr;
+ up = pp->unitptr;
if (-1 != pp->io.fd)
io_closeclock(&pp->io);
if (NULL != up)
/*
* Initialize pointers and read the timecode and timestamp
*/
- peer = (struct peer *)rbufp->recv_srcclock;
+ peer = rbufp->recv_peer;
pp = peer->procptr;
- up = (struct wwvbunit *)pp->unitptr;
+ up = pp->unitptr;
temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
/*
* reading precision is only to the millisecond. Thus, unless
* you have a PPS gadget and don't have to have the year, format
* 0 provides the lowest jitter.
+ * Save the timestamp of each <CR> in up->laststamp. Lines with
+ * no characters occur for every <LF>, and for some <CR>s when
+ * format 0 is used. Format 0 starts and ends each cycle with a
+ * <CR><LF> pair, format 2 starts each cycle with its only pair.
+ * The preceding <CR> is the on-time character for both formats.
+ * The timestamp provided with non-empty lines corresponds to
+ * the <CR> following the timecode, which is ultimately not used
+ * with format 0 and is used for the following timecode for
+ * format 2.
*/
if (temp == 0) {
- up->laststamp = trtmp;
+ if (up->prev_eol_cr) {
+ DPRINTF(2, ("wwvb: <LF> @ %s\n",
+ prettydate(&trtmp)));
+ } else {
+ up->laststamp = trtmp;
+ DPRINTF(2, ("wwvb: <CR> @ %s\n",
+ prettydate(&trtmp)));
+ }
+ up->prev_eol_cr = !up->prev_eol_cr;
return;
}
pp->lencode = temp;
pp->lastrec = up->laststamp;
+ up->laststamp = trtmp;
+ up->prev_eol_cr = TRUE;
+ DPRINTF(2, ("wwvb: code @ %s\n"
+ " using %s minus one char\n",
+ prettydate(&trtmp), prettydate(&pp->lastrec)));
+ if (L_ISZERO(&pp->lastrec))
+ return;
/*
* We get down to business, check the timecode format and decode
if (sscanf(pp->a_lastcode,
"%c %3d %2d:%2d:%2d%c%cTZ=%2d",
&syncchar, &pp->day, &pp->hour, &pp->minute,
- &pp->second, &tmpchar, &dstchar, &tz) == 8)
+ &pp->second, &tmpchar, &dstchar, &tz) == 8) {
pp->nsec = 0;
break;
+ }
+ goto bad_format;
case LENWWVB2:
"%c%c %2d %3d %2d:%2d:%2d.%3ld %c",
&syncchar, &qualchar, &pp->year, &pp->day,
&pp->hour, &pp->minute, &pp->second, &pp->nsec,
- &leapchar) == 9)
+ &leapchar) == 9) {
pp->nsec *= 1000000;
break;
+ }
+ goto bad_format;
case LENWWVB3:
- /*
+ /*
* Timecode format 3: "0003I yyyymmdd hhmmss+0000SL#"
+ * WARNING: Undocumented, and the on-time character # is
+ * not yet handled correctly by this driver. It may be
+ * as simple as compensating for an additional 1/960 s.
*/
if (sscanf(pp->a_lastcode,
"0003%c %4d%2d%2d %2d%2d%2d+0000%c%c",
pp->nsec = 0;
break;
}
+ goto bad_format;
default:
+ bad_format:
/*
* Unknown format: If dumping internal table, record
register struct wwvbunit *up;
struct refclockproc *pp;
char pollchar; /* character sent to clock */
+ l_fp now;
/*
* Time to poll the clock. The Spectracom clock responds to a
* the clock; all others just listen in.
*/
pp = peer->procptr;
- up = (struct wwvbunit *)pp->unitptr;
+ up = pp->unitptr;
if (up->linect > 0)
pollchar = 'R';
else
pollchar = 'T';
if (write(pp->io.fd, &pollchar, 1) != 1)
refclock_report(peer, CEVNT_FAULT);
+#ifdef DEBUG
+ get_systime(&now);
+ if (debug)
+ printf("%c poll at %s\n", pollchar, prettydate(&now));
+#endif
#ifdef HAVE_PPSAPI
if (up->ppsapi_lit &&
refclock_pps(peer, &up->atom, pp->sloppyclockflag) > 0) {
* are received, declare a timeout and keep going.
*/
pp = peer->procptr;
- up = (struct wwvbunit *)pp->unitptr;
+ up = pp->unitptr;
pp->polls++;
/*
static void
wwvb_control(
int unit,
- struct refclockstat *in_st,
+ const struct refclockstat *in_st,
struct refclockstat *out_st,
struct peer *peer
)
struct refclockproc *pp;
pp = peer->procptr;
- up = (struct wwvbunit *)pp->unitptr;
+ up = pp->unitptr;
if (!(pp->sloppyclockflag & CLK_FLAG1)) {
if (!up->ppsapi_tried)
#include "ntp_syslog.h"
#include "ntp_select.h"
#include "ntp_stdlib.h"
+#include "ntp_assert.h"
#include <ssl_applink.c>
#include "isc/net.h"
*/
int ai_fam_templ;
-int nbsock; /* the number of sockets used */
+int nbsock; /* the number of sockets used */
SOCKET fd[MAX_AF];
-int fd_family[MAX_AF]; /* to remember the socket family */
+int fd_family[MAX_AF]; /* to remember the socket family */
#ifdef HAVE_POLL_H
struct pollfd fdmask[MAX_AF];
#else
} else {
sys_timeout = ((LFPTOFP(&tmp) * TIMER_HZ)
+ 0x8000) >> 16;
- /*
- * No less than 1s between requests to
- * a server to stay within ntpd's
- * default "discard minimum 1" (and 1s
- * enforcement slop). That is enforced
- * only if the nondefault limited
- * restriction is in place, such as with
- * "restrict ... limited" and "restrict
- * ... kod limited".
- */
- if (MINTIMEOUT < sys_timeout)
- sys_timeout = MINTIMEOUT;
+ sys_timeout = max(sys_timeout, MINTIMEOUT);
}
break;
case 'v':
* Shift this data in, then schedule another transmit.
*/
server_data(server, (s_fp) di, &ci, 0);
+
+ if ((int)server->filter_nextpt >= sys_samples) {
+ /*
+ * Got all the data we need. Mark this guy
+ * completed and return.
+ */
+ server->event_time = 0;
+ complete_servers++;
+ return;
+ }
+
server->event_time = current_time + sys_timeout;
}
int error;
/* Service name */
char service[5];
- strcpy(service, "ntp");
+ sockaddr_u addr;
+
+ strncpy(service, "ntp", sizeof(service));
/* Get host address. Looking for UDP datagram connection. */
- memset(&hints, 0, sizeof(hints));
+ ZERO(hints);
hints.ai_family = ai_fam_templ;
hints.ai_socktype = SOCK_DGRAM;
return;
}
#ifdef DEBUG
- else if (debug) {
- fprintf(stderr, "host found : %s\n", stohost((sockaddr_u *)addrResult->ai_addr));
+ if (debug) {
+ ZERO(addr);
+ INSIST(addrResult->ai_addrlen <= sizeof(addr));
+ memcpy(&addr, addrResult->ai_addr, addrResult->ai_addrlen);
+ fprintf(stderr, "host found : %s\n", stohost(&addr));
}
#endif
/* We must get all returned server in case the first one fails */
for (ptr = addrResult; ptr != NULL; ptr = ptr->ai_next) {
- if (is_reachable ((sockaddr_u *)ptr->ai_addr)) {
- server = emalloc(sizeof(*server));
- memset(server, 0, sizeof(*server));
-
+ ZERO(addr);
+ INSIST(ptr->ai_addrlen <= sizeof(addr));
+ memcpy(&addr, ptr->ai_addr, ptr->ai_addrlen);
+ if (is_reachable(&addr)) {
+ server = emalloc_zero(sizeof(*server));
memcpy(&server->srcadr, ptr->ai_addr, ptr->ai_addrlen);
server->event_time = ++sys_numservers;
if (sys_servers == NULL)
struct server *sp;
for (sp = sys_servers; sp->next_server != NULL;
- sp = sp->next_server) ;
+ sp = sp->next_server)
+ /* empty */;
sp->next_server = server;
}
}
complete_servers++;
}
- server = emalloc(sizeof(*server));
- memset(server, 0, sizeof(*server));
+ server = emalloc_zero(sizeof(*server));
server->srcadr = *addr;
server->event_time = ++sys_numservers;
for (sp = sys_servers; sp->next_server != NULL;
- sp = sp->next_server) ;
+ sp = sp->next_server)
+ /* empty */;
sp->next_server = server;
transmit(server);
}
{
struct addrinfo *res, *ressave;
struct addrinfo hints;
+ sockaddr_u addr;
char service[5];
+ int rc;
int optval = 1;
int check_ntp_port_in_use = !debug && !simple_query && !unpriv_port;
* Open the socket
*/
- strcpy(service, "ntp");
+ strncpy(service, "ntp", sizeof(service));
/*
* Init hints addrinfo structure
*/
- memset(&hints, 0, sizeof(hints));
+ ZERO(hints);
hints.ai_family = ai_fam_templ;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_DGRAM;
- if(getaddrinfo(NULL, service, &hints, &res) != 0) {
- msyslog(LOG_ERR, "getaddrinfo() failed: %m");
- exit(1);
- /*NOTREACHED*/
+ if (getaddrinfo(NULL, service, &hints, &res) != 0) {
+ msyslog(LOG_ERR, "getaddrinfo() failed: %m");
+ exit(1);
+ /*NOTREACHED*/
}
#ifdef SYS_WINNT
* bind the socket to the NTP port
*/
if (check_ntp_port_in_use) {
- if (bind(fd[nbsock], res->ai_addr,
- SOCKLEN((sockaddr_u *)res->ai_addr)) < 0) {
-#ifndef SYS_WINNT
- if (errno == EADDRINUSE)
-#else
- if (WSAGetLastError() == WSAEADDRINUSE)
-#endif /* SYS_WINNT */
+ ZERO(addr);
+ INSIST(res->ai_addrlen <= sizeof(addr));
+ memcpy(&addr, res->ai_addr, res->ai_addrlen);
+ rc = bind(fd[nbsock], &addr.sa, SOCKLEN(&addr));
+ if (rc < 0) {
+ if (EADDRINUSE == socket_errno())
msyslog(LOG_ERR, "the NTP socket is in use, exiting");
else
msyslog(LOG_ERR, "bind() fails: %m");
register int n;
register struct recvbuf *rb;
struct sock_timeval tvzero;
- int fromlen;
+ GETSOCKNAME_SOCKLEN_TYPE fromlen;
l_fp ts;
int i;
#ifdef HAVE_POLL_H
{
register int i;
char junk[5];
- char *str;
+ const char *str;
if (!debug) {
(void) fprintf(fp, "server %s, stratum %d, offset %s, delay %s\n",
lfptoa(&pp->offset, 6));
}
-#if !defined(HAVE_VSPRINTF)
-int
-vsprintf(
- char *str,
- const char *fmt,
- va_list ap
- )
-{
- FILE f;
- int len;
-
- f._flag = _IOWRT+_IOSTRG;
- f._ptr = str;
- f._cnt = 32767;
- len = _doprnt(fmt, ap, &f);
- *f._ptr = 0;
- return (len);
-}
-#endif
-
-#if 0
-/* override function in library since SA_RESTART makes ALL syscalls restart */
-#ifdef SA_RESTART
-void
-signal_no_reset(
- int sig,
- void (*func)()
- )
-{
- int n;
- struct sigaction vec;
-
- vec.sa_handler = func;
- sigemptyset(&vec.sa_mask);
- vec.sa_flags = 0;
-
- while (1)
- {
- n = sigaction(sig, &vec, NULL);
- if (n == -1 && errno == EINTR)
- continue;
- break;
- }
- if (n == -1)
- {
- perror("sigaction");
- exit(1);
- }
-}
-#endif
-#endif
#ifdef HAVE_NETINFO
static ni_namelist *
#include <malloc.h>
#include <crtdbg.h>
/* #define MALLOC_LINT */ /* defers free() */
+# define EREALLOC_IMPL(ptr, newsz, filenm, loc) \
+ _realloc_dbg(ptr, newsz, _NORMAL_BLOCK, filenm, loc)
#endif
/*