+* [Bug 1283] default to remembering KoD in sntp.
+* clean up numerous sntp/kod_management.c bugs.
+* use all addresses resolved from each DNS name in sntp.
(4.2.5p205) 2009/08/18 Released by Harlan Stenn <stenn@ntp.org>
* accopt.html typo fixes from Dave Mills.
* [Bug 1285] Log ntpq :config/config-from-file events.
AC_DEFINE_UNQUOTED(DSTMINUTES, $ans, [The number of minutes in a DST adjustment])
AC_MSG_RESULT([$ans])
-AC_MSG_CHECKING([[if ntpd will retry on permanent DNS errors]])
+AC_MSG_CHECKING([[if ntpd will retry permanent DNS failures]])
AC_ARG_ENABLE(
[ignore-dns-errors],
AS_HELP_STRING(
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
+extern char *progname;
+
#if !defined(_MSC_VER) || !defined(_DEBUG)
mem = realloc(prev, size);
if (NULL == mem) {
- msyslog(LOG_ERR, "fatal out of memory (%u bytes)",
- (u_int)size);
+ 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);
}
if (NULL == copy) {
msyslog(LOG_ERR,
- "fatal out of memory duplicating %u byte "
- "string '%s'",
- (u_int)strlen(str) + 1, str);
+ "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);
}
mem = _realloc_dbg(prev, size, _NORMAL_BLOCK, file, line);
if (NULL == mem) {
- msyslog(LOG_ERR, "fatal: out of memory in %s line %d size %u",
- file, line, (u_int)size);
+ 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);
exit(1);
}
)
{
char * copy;
+ size_t bytes;
- copy = debug_erealloc(NULL, strlen(str) + 1, file, line);
- strcpy(copy, str);
+ bytes = strlen(str) + 1;
+ copy = debug_erealloc(NULL, bytes, file, line);
+ memcpy(copy, str, bytes);
return copy;
}
static void config_peers(struct config_tree *);
static void config_unpeers(struct config_tree *);
-static void config_ntpd(struct config_tree *);
#ifdef SIM
static void config_sim(struct config_tree *);
static void config_ntpdsim(struct config_tree *);
+#else
+static void config_ntpd(struct config_tree *);
#endif
enum gnn_type {
)
{
struct attr_val *my_qosconfig;
- char *s;
-#ifdef HAVE_IPTOS_SUPPORT
- unsigned int qtos = 0;
-#endif
while (NULL != (my_qosconfig = dequeue(ptree->qos))) {
- s = my_qosconfig->value.s;
- free(s);
+ free(my_qosconfig->value.s);
free_node(my_qosconfig);
}
}
struct config_tree *ptree
)
{
- server_info *serv_info;
- struct attr_val *init_stmt;
-
if (NULL == ptree->sim_details)
return;
* the simulator. The simulator ignores a lot of the standard ntpd configuration
* options
*/
-
+#ifndef SIM
static void
config_ntpd(
struct config_tree *ptree
config_fudge(ptree);
config_qos(ptree);
}
+#endif /* !SIM */
+
#ifdef SIM
static void
m4_defun([_LT_AC_LANG_F77_CONFIG], [:])
AC_PROG_LIBTOOL
+NTP_DIR_SEP
# Checks for libraries.
MD5Init(&ctx);
- char *digest_data = (char *) malloc(sizeof(char) * (LEN_PKT_NOMAC + cmp_key->key_len));
+ char *digest_data = (char *) emalloc(sizeof(char) * (LEN_PKT_NOMAC + cmp_key->key_len));
for(a=0; a<LEN_PKT_NOMAC; a++)
digest_data[a] = pkt_data[a];
while(!feof(keyf)) {
- struct key *act = (struct key *) malloc(sizeof(struct key));
+ struct key *act = (struct key *) emalloc(sizeof(struct key));
line_limit = 0;
fgets(kbuf, sizeof(kbuf), keyf);
/* TODO check for memory leaks */
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include "kod_management.h"
#include "log.h"
#include "sntp-opts.h"
+#include "ntp_stdlib.h"
#define DEBUG
-int kod_init = 0, entryc = 0;
+int kod_init = 0, kod_db_cnt = 0;
const char *kod_db_file;
-struct kod_entry *kod_db;
-FILE *db_s;
+struct kod_entry **kod_db; /* array of pointers to kod_entry */
/*
)
{
register int a, b, resc = 0;
- struct kod_entry *sptr;
- sptr = kod_db;
- for (a = 0; a < entryc && sptr; a++) {
- if (!strcmp(sptr->hostname, hostname))
+ for (a = 0; a < kod_db_cnt; a++)
+ if (!strcmp(kod_db[a]->hostname, hostname))
resc++;
- sptr = sptr->next;
- }
-
if (!resc)
return 0;
- *dst = malloc(sizeof(struct kod_entry) * resc);
- if (NULL == *dst)
- return 0;
+ *dst = emalloc(resc * sizeof(**dst));
- sptr = kod_db;
b = 0;
- for (a = 0; a < entryc && sptr; a++) {
- if (!strcmp(sptr->hostname, hostname)) {
- (*dst)[b] = *sptr;
- (*dst)[b].next = &((*dst)[b + 1]);
+ for (a = 0; a < kod_db_cnt; a++)
+ if (!strcmp(kod_db[a]->hostname, hostname)) {
+ (*dst)[b] = *kod_db[a];
b++;
}
- sptr = sptr->next;
- }
- if (b)
- (*dst)[b - 1].next = NULL;
-
return resc;
}
-#if 0 /* presently useless */
-int
-kod_entry_exists (
- char *search_str
- )
+
+void
+add_entry(
+ char *hostname,
+ char *type /* 4 bytes not \0 terminated */
+ )
{
- struct kod_entry **dummy = NULL;
+ int n;
- if(dummy == NULL)
- return 0;
+ n = kod_db_cnt++;
+ kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0]));
+ kod_db[n] = emalloc(sizeof(*kod_db[n]));
- else
- return 1;
-}
-#endif
+ kod_db[n]->timestamp = time(NULL);
-void
-add_entry (
- char *hostname,
- char *type
- )
-{
- if(kod_init) {
- struct kod_entry *new_entry = (struct kod_entry *) malloc(sizeof(struct kod_entry));
- strcpy(new_entry->hostname, hostname);
- strncpy(new_entry->type, type, 4);
- new_entry->next = NULL;
-
- kod_db[entryc-1].next = new_entry;
- entryc++;
- }
-}
+ memcpy(kod_db[n]->type, type, 4);
+ kod_db[n]->type[sizeof(kod_db[n]->type) - 1] = '\0';
-void
-kod_atexit (
- )
-{
- if(kod_init)
- write_kod_db();
+ strncpy(kod_db[n]->hostname, hostname,
+ sizeof(kod_db[n]->hostname));
+ kod_db[n]->hostname[sizeof(kod_db[n]->hostname) - 1] = '\0';
}
+
void
-delete_entry (
- char *hostname,
- char *type
- )
+delete_entry(
+ char *hostname,
+ char *type
+ )
{
register int a;
- struct kod_entry *nptr = kod_db;
+ for (a = 0; a < kod_db_cnt; a++)
+ if (!strcmp(kod_db[a]->hostname, hostname)
+ && !strcmp(kod_db[a]->type, type))
+ break;
- for(a=0; a<entryc && nptr != NULL; a++) {
- if(!strcmp(nptr->hostname, hostname) && !strncmp(nptr->type, type, 4)) {
- struct kod_entry *cptr = nptr;
- struct kod_entry *nnptr = nptr;
+ if (a == kod_db_cnt)
+ return;
- nptr--;
- nnptr++;
+ free(kod_db[a]);
+ kod_db_cnt--;
- nptr->next = nnptr;
- free(cptr);
- nptr = NULL;
- }
- else {
- nptr = nptr->next;
- }
- }
+ if (a < kod_db_cnt)
+ memmove(&kod_db[a], &kod_db[a + 1],
+ (kod_db_cnt - a) * sizeof(kod_db[0]));
}
+
void
-write_kod_db (
- )
+write_kod_db(void)
{
- if(!kod_init)
- return;
-
+ FILE *db_s;
+ char *pch;
+ int dirmode;
register int a;
- struct kod_entry *nptr = kod_db;
- if(fopen(kod_db_file, "w") == NULL) {
- char msg[80];
- snprintf(msg, 80, "Can't open KOD db file %s for writing!", kod_db_file);
+ db_s = fopen(kod_db_file, "w");
+
+ /*
+ * If opening fails, blindly attempt to create each directory
+ * in the path first, then retry the open.
+ */
+ if (NULL == db_s && strlen(kod_db_file)) {
+ dirmode = S_IRUSR | S_IWUSR | S_IXUSR
+ | S_IRGRP | S_IXGRP
+ | S_IROTH | S_IXOTH;
+ pch = strchr(kod_db_file + 1, DIR_SEP);
+ while (NULL != pch) {
+ *pch = '\0';
+ mkdir(kod_db_file, dirmode);
+ *pch = DIR_SEP;
+ pch = strchr(pch + 1, DIR_SEP);
+ }
+ }
+
+ db_s = fopen(kod_db_file, "w");
+ if (NULL == db_s) {
+ char msg[80];
+ snprintf(msg, sizeof(msg),
+ "Can't open KOD db file %s for writing!",
+ kod_db_file);
#ifdef DEBUG
debug_msg(msg);
#endif
-
log_msg(msg, 2);
return;
}
- for(a=0; a<entryc && nptr != NULL; a++) {
- fprintf(db_s, "%s:%i:%s\n", nptr->hostname, nptr->timestamp, nptr->type);
- nptr = nptr->next;
+ for (a = 0; a < kod_db_cnt; a++) {
+ fprintf(db_s, "%16.16llx %s %s\n", (unsigned long long)
+ kod_db[a]->timestamp, kod_db[a]->type,
+ kod_db[a]->hostname);
}
fflush(db_s);
fclose(db_s);
}
-
+
+
void
-kod_init_kod_db (
- const char *db_file
- )
+kod_init_kod_db(
+ const char *db_file
+ )
{
- register int a, b;
-
- /* Max. of 255 characters for hostname, 10 for timestamp, 4 for kisscode, 2 for format : and 1 for \n */
+ /*
+ * Max. of 254 characters for hostname, 10 for timestamp, 4 for
+ * kisscode, 2 for spaces, 1 for \n, and 1 for \0
+ */
char fbuf[272];
+ FILE *db_s;
+ int a, b, sepc;
+ unsigned long long ull;
+ char *str_ptr;
char error = 0;
- if (kod_init)
- return;
+ atexit(write_kod_db);
#ifdef DEBUG
printf("Initializing KOD DB...\n");
#endif
+ kod_db_file = estrdup(db_file);
+
+
db_s = fopen(db_file, "r");
- if(db_s == NULL) {
+ if (NULL == db_s) {
char msg[80];
- snprintf(msg, 80, "kod_init_kod_db(): Cannot open KOD db file %s", db_file);
-
+ snprintf(msg, sizeof(msg), "kod_init_kod_db(): Cannot open KoD db file %s", db_file);
#ifdef DEBUG
debug_msg(msg);
printf("%s\n", msg);
#endif
-
log_msg(msg, 2);
+
+ return;
}
if(ENABLED_OPT(NORMALVERBOSE))
- printf("Starting to read KOD file %s...\n", db_file);
+ printf("Starting to read KoD file %s...\n", db_file);
/* First let's see how many entries there are and check for right syntax */
while(!feof(db_s)) {
- int sepc = 0;
-
fgets(fbuf, sizeof(fbuf), db_s);
+
+ /* ignore blank lines */
+ if ('\n' == fbuf[0])
+ continue;
+ sepc = 0;
for(a=0; a<strlen(fbuf); a++) {
- if(fbuf[a] == ':')
+ if (' ' == fbuf[a])
sepc++;
- if(fbuf[a] == '\n') {
- if(sepc != 2) {
- char msg[80];
- snprintf(msg, sizeof(msg), "Syntax error in KOD db file %s in line %i (missing :)", db_file, (entryc + 1));
+ if ('\n' == fbuf[a]) {
+ if (sepc != 2) {
+ if (strcmp(db_file, "/dev/null")) {
+ char msg[80];
+ snprintf(msg, sizeof(msg), "Syntax error in KoD db file %s in line %i (missing space)", db_file, (kod_db_cnt + 1));
-#ifdef DEBUG
- debug_msg(msg);
- printf("%s\n", msg);
-#endif
+ #ifdef DEBUG
+ debug_msg(msg);
+ printf("%s\n", msg);
+ #endif
- log_msg(msg, 1);
+ log_msg(msg, 1);
+ }
return;
}
sepc = 0;
- entryc++;
+ kod_db_cnt++;
}
}
}
- entryc--;
+ kod_db_cnt--;
#ifdef DEBUG
- printf("KOD DB %s contains %i entries, reading...\n", db_file, entryc);
+ printf("KoD DB %s contains %d entries, reading...\n", db_file, kod_db_cnt);
#endif
rewind(db_s);
- kod_db = (struct kod_entry *) malloc(sizeof(struct kod_entry) * entryc);
+ kod_db = emalloc(sizeof(kod_db[0]) * kod_db_cnt);
- /* Read contents of file and make a linked list */
- for(b=0; !feof(db_s) && !ferror(db_s) && b < entryc; b++) {
- char *str_ptr;
- int j;
+ /* Read contents of file */
+ for(b=0; !feof(db_s) && !ferror(db_s) && b < kod_db_cnt; b++) {
str_ptr = fgets(fbuf, sizeof(fbuf), db_s);
if (NULL == str_ptr) {
break;
}
- j = sscanf(fbuf, "%255[^:]", (char *) &(kod_db[b].hostname));
- /* sscanf returns count of fields, not characters, so this looks iffy */
- /* also why not a single call to sscanf combining both of these? */
- j += sscanf(fbuf + j, "%*[^:]:%i:%4s", &kod_db[b].timestamp, (char *) &(kod_db[b].type));
+ /* ignore blank lines */
+ if ('\n' == fbuf[0]) {
+ b--;
+ continue;
+ }
+
+ kod_db[b] = emalloc(sizeof(*kod_db[b]));
+
+ if (3 != sscanf(fbuf, "%llx %4s %254s", &ull,
+ kod_db[b]->type, kod_db[b]->hostname)) {
- kod_db[b].next = NULL;
- if (b > 0)
- kod_db[b-1].next = &kod_db[b];
+ free(kod_db[b]);
+ kod_db[b] = NULL;
+ error = 1;
+ break;
+ }
+
+ kod_db[b]->timestamp = (time_t)ull;
}
if (ferror(db_s) || error) {
char msg[80];
- snprintf(msg, sizeof(msg), "An error occured while parsing the KOD db file %s", db_file);
+ snprintf(msg, sizeof(msg), "An error occured while parsing the KoD db file %s", db_file);
#ifdef DEBUG
debug_msg(msg);
#endif
}
#ifdef DEBUG
- for(a=0; a<entryc; a++)
- printf("KOD entry %i: %s at %i type %s\n", a, kod_db[a].hostname,
- kod_db[a].timestamp, kod_db[a].type);
+ for (a = 0; a < kod_db_cnt; a++)
+ printf("KoD entry %d: %s at %llx type %s\n", a,
+ kod_db[a]->hostname,
+ (unsigned long long)kod_db[a]->timestamp,
+ kod_db[a]->type);
printf("\n");
#endif
fclose(db_s);
-
- kod_db_file = db_file;
-
- kod_init = 1;
- atexit(kod_atexit);
}
#ifndef KOD_MANAGEMENT_H
#define KOD_MANAGEMENT_H
+#include <time.h>
+
struct kod_entry {
char hostname[255];
- unsigned int timestamp;
+ time_t timestamp;
char type[5];
- struct kod_entry *next;
};
-int search_entry (char *hostname, struct kod_entry **dst);
-int kod_entry_exists (char *search_str);
+int search_entry(char *hostname, struct kod_entry **dst);
-void add_entry (char *hostname, char *type);
-void delete_entry (char *hostname, char *type);
-void kod_init_kod_db (const char *db_file);
-void write_kod_db (void);
-void kod_atexit (void);
+void add_entry(char *hostname, char *type);
+void delete_entry(char *hostname, char *type);
+void kod_init_kod_db(const char *db_file);
+void write_kod_db(void);
#endif
#include "utilities.h"
#include "log.h"
+char *progname = "sntp"; /* for msyslog */
int ai_fam_pref;
volatile int debug;
int set_time (double offset);
-#if !HAVE_MALLOC
-void *
-rpl_malloc (size_t n)
-{
- if (n == 0)
- n = 1;
- return malloc (n);
-}
-#endif /* !HAVE_MALLOC */
-
int
main (
int argc,
int optct;
int sync_data_suc = 0;
struct addrinfo **resh = NULL;
+ struct addrinfo *ai;
int resc;
+ int kodc;
+ int ow_ret;
+ char *hostname;
/* IPv6 available? */
if (isc_net_probeipv6() != ISC_R_SUCCESS) {
if (HAVE_OPT(FILELOG))
init_log(OPT_ARG(FILELOG));
- /* If there's a specified KOD file init KOD system.
- * If not and system may save to HD use default file.
+ /*
+ * If there's a specified KOD file init KOD system. If not use
+ * default file. For embedded systems with no writable
+ * filesystem, -K /dev/null can be used to disable KoD storage.
*/
if (HAVE_OPT(KOD))
kod_init_kod_db(OPT_ARG(KOD));
+ else
+ kod_init_kod_db("/var/db/ntp-kod");
if (HAVE_OPT(KEYFILE))
auth_init(OPT_ARG(KEYFILE), &keys);
+#ifdef EXERCISE_KOD_DB
+ add_entry("192.168.169.170", "DENY");
+ add_entry("192.168.169.171", "DENY");
+ add_entry("192.168.169.172", "DENY");
+ add_entry("192.168.169.173", "DENY");
+ add_entry("192.168.169.174", "DENY");
+ delete_entry("192.168.169.174", "DENY");
+ delete_entry("192.168.169.172", "DENY");
+ delete_entry("192.168.169.170", "DENY");
+ if ((kodc = search_entry("192.168.169.173", &reason)) == 0)
+ printf("entry for 192.168.169.173 not found but should have been!\n");
+ else
+ free(reason);
+#endif
/* Considering employing a variable that prevents functions of doing anything until
* everything is initialized properly
* let's just pay attention to previous KoDs.
*/
for (c = 0; c < resc && !sync_data_suc; c++) {
- getnameinfo(resh[c]->ai_addr, resh[c]->ai_addrlen, adr_buf,
- sizeof(adr_buf), NULL, 0, NI_NUMERICHOST);
-
- int kodc;
- char *hostname = addrinfo_to_str(resh[c]);
-
- if ((kodc = search_entry(hostname, &reason)) == 0) {
- if (is_reachable(resh[c])) {
- int ow_ret = on_wire(resh[c]);
-
- if (ow_ret < 0)
- printf("on_wire failed for server %s!\n", hostname);
- else
- sync_data_suc = 1;
+ ai = resh[c];
+ do {
+ hostname = addrinfo_to_str(ai);
+
+ if ((kodc = search_entry(hostname, &reason)) == 0) {
+ if (is_reachable(ai)) {
+ ow_ret = on_wire(ai);
+ if (ow_ret < 0)
+ printf("on_wire failed for server %s!\n", hostname);
+ else
+ sync_data_suc = 1;
+ }
+ } else {
+ printf("%d prior KoD%s for %s, skipping.\n",
+ kodc, (kodc > 1) ? "s" : "", hostname);
+ free(reason);
}
- } else {
- printf("KoD %i packages exists for %s, stopping any further communication.\n",
- kodc, adr_buf);
- free(reason);
- }
-
+ free(hostname);
+ ai = ai->ai_next;
+ } while (NULL != ai);
freeaddrinfo(resh[c]);
- free(hostname);
}
free(resh);
{
register int try;
SOCKET sock;
- struct pkt *x_pkt = (struct pkt *) alloca(sizeof(struct pkt));
- struct pkt *r_pkt = (struct pkt *) alloca(sizeof(struct pkt));
+ struct pkt x_pkt;
+ struct pkt r_pkt;
+ char *ref;
for(try=0; try<5; try++) {
struct timeval tv_xmt, tv_dst;
char *hostname = NULL, *ts_str = NULL;
l_fp p_rec, p_xmt, p_ref, p_org, xmt, tmp, dst;
- memset(r_pkt, 0, sizeof(*r_pkt));
- memset(x_pkt, 0, sizeof(*x_pkt));
+ memset(&r_pkt, 0, sizeof(r_pkt));
+ memset(&x_pkt, 0, sizeof(x_pkt));
error = GETTIMEOFDAY(&tv_xmt, (struct timezone *)NULL);
#endif
TVTOTS(&tv_xmt, &xmt);
- HTONL_FP(&xmt, &(x_pkt->xmt));
+ HTONL_FP(&xmt, &(x_pkt.xmt));
- x_pkt->stratum = STRATUM_TO_PKT(STRATUM_UNSPEC);
- x_pkt->ppoll = 8;
+ x_pkt.stratum = STRATUM_TO_PKT(STRATUM_UNSPEC);
+ x_pkt.ppoll = 8;
/* FIXME! Modus broadcast + adr. check -> bdr. pkt */
- set_li_vn_mode(x_pkt, LEAP_NOTINSYNC, 4, 3);
+ set_li_vn_mode(&x_pkt, LEAP_NOTINSYNC, 4, 3);
create_socket(&sock, (sockaddr_u *)host->ai_addr);
- sendpkt(sock, (sockaddr_u *)host->ai_addr, x_pkt, LEN_PKT_NOMAC);
- rpktl = recvpkt(sock, r_pkt, x_pkt);
+ sendpkt(sock, (sockaddr_u *)host->ai_addr, &x_pkt, LEN_PKT_NOMAC);
+ rpktl = recvpkt(sock, &r_pkt, &x_pkt);
closesocket(sock);
case KOD_DEMOBILIZE:
/* Received a DENY or RESTR KOD packet */
hostname = addrinfo_to_str(host);
- add_entry(hostname, (char *) &r_pkt->refid);
+ ref = (char *)&r_pkt.refid;
+ add_entry(hostname, ref);
if(ENABLED_OPT(NORMALVERBOSE))
- printf("sntp on_wire: Received KOD packet with code: %s from %s, demobilizing all connections\n",
- (char *) r_pkt->refid, hostname);
+ printf("sntp on_wire: Received KOD packet with code: %c%c%c%c from %s, demobilizing all connections\n",
+ ref[0], ref[1], ref[2], ref[3],
+ hostname);
- char *log_str = (char *) malloc(sizeof(char) * (INET6_ADDRSTRLEN + 72));
- snprintf(log_str, sizeof(log_str),
- "Received a KOD packet with code %s from %s, demobilizing all connections",
- (char *) &r_pkt->refid, hostname);
+ char *log_str = (char *) emalloc(sizeof(char) * (INET6_ADDRSTRLEN + 72));
+ snprintf(log_str, INET6_ADDRSTRLEN + 72,
+ "Received a KOD packet with code %c%c%c%c from %s, demobilizing all connections",
+ ref[0], ref[1], ref[2], ref[3],
+ hostname);
log_msg(log_str, 2);
case 1:
/* Convert timestamps from network to host byte order */
- NTOHL_FP(&r_pkt->reftime, &p_ref);
- NTOHL_FP(&r_pkt->org, &p_org);
- NTOHL_FP(&r_pkt->rec, &p_rec);
- NTOHL_FP(&r_pkt->xmt, &p_xmt);
+ NTOHL_FP(&r_pkt.reftime, &p_ref);
+ NTOHL_FP(&r_pkt.org, &p_org);
+ NTOHL_FP(&r_pkt.rec, &p_rec);
+ NTOHL_FP(&r_pkt.xmt, &p_xmt);
if(ENABLED_OPT(NORMALVERBOSE)) {
getnameinfo(host->ai_addr, host->ai_addrlen, adr_buf,
}
#ifdef DEBUG
- pkt_output(r_pkt, rpktl, stdout);
+ pkt_output(&r_pkt, rpktl, stdout);
- printf("sntp on_wire: rpkt->reftime:\n");
- l_fp_output(&(r_pkt->reftime), stdout);
- printf("sntp on_wire: rpkt->org:\n");
- l_fp_output(&(r_pkt->org), stdout);
- printf("sntp on_wire: rpkt->rec:\n");
- l_fp_output(&(r_pkt->rec), stdout);
- printf("sntp on_wire: rpkt->rec:\n");
- l_fp_output_bin(&(r_pkt->rec), stdout);
- printf("sntp on_wire: rpkt->rec:\n");
- l_fp_output_dec(&(r_pkt->rec), stdout);
- printf("sntp on_wire: rpkt->xmt:\n");
- l_fp_output(&(r_pkt->xmt), stdout);
+ printf("sntp on_wire: r_pkt.reftime:\n");
+ l_fp_output(&(r_pkt.reftime), stdout);
+ printf("sntp on_wire: r_pkt.org:\n");
+ l_fp_output(&(r_pkt.org), stdout);
+ printf("sntp on_wire: r_pkt.rec:\n");
+ l_fp_output(&(r_pkt.rec), stdout);
+ printf("sntp on_wire: r_pkt.rec:\n");
+ l_fp_output_bin(&(r_pkt.rec), stdout);
+ printf("sntp on_wire: r_pkt.rec:\n");
+ l_fp_output_dec(&(r_pkt.rec), stdout);
+ printf("sntp on_wire: r_pkt.xmt:\n");
+ l_fp_output(&(r_pkt.xmt), stdout);
#endif
/* Compute offset etc. */
char **dst
)
{
- unsigned char *comph = (unsigned char *) malloc(sizeof(char) * 12);
+ unsigned char *comph = (unsigned char *) emalloc(sizeof(char) * 12);
/* When declaring id_high unsigned char, will the char cast on the right side
* convert to unsigned char, too?
length += 4;
- char *res = (char *) malloc(sizeof(char) * length);
+ char *res = (char *) emalloc(sizeof(char) * length);
for(a=0; a<dlength; a++) {
res[a] = *qname[a];
size_t length = (dlength + 10 + rdlength);
- char *res = (char *) malloc(sizeof(char) * length);
+ char *res = (char *) emalloc(sizeof(char) * length);
for(a=0; a<dlength; a++) {
res[a] = *qname[a];
dotc++;
}
- char *res = (char *) malloc(sizeof(char) * strlen(hostname) + dotc + 1);
- char *len = (char *) malloc(sizeof(char) * dotc);
+ char *res = (char *) emalloc(sizeof(char) * strlen(hostname) + dotc + 1);
+ char *len = (char *) emalloc(sizeof(char) * dotc);
char prevd = 0;
for(a=0, b=0; a<strlen(hostname); a++) {
if (hostc < 1 || NULL == res)
return 0;
- tres = malloc(sizeof(struct addrinfo *) * hostc);
+ tres = emalloc(sizeof(struct addrinfo *) * hostc);
for (a = 0, resc = 0; a < hostc; a++) {
struct addrinfo hints;
if (error) {
size_t msg_length = strlen(hosts[a]) + 21;
- char *logmsg = (char *) malloc(sizeof(char) * msg_length);
+ char *logmsg = (char *) emalloc(sizeof(char) * msg_length);
snprintf(logmsg, msg_length, "Error looking up %s", hosts[a]);
#ifdef DEBUG
if(ENABLED_OPT(TIMEOUT))
timeout_tv.tv_sec = (int) OPT_ARG(TIMEOUT);
else
- timeout_tv.tv_sec = 60;
+ timeout_tv.tv_sec = 68; /* ntpd broadcasts every 64s */
switch(select(rsock + 1, &bcst_fd, 0, 0, &timeout_tv)) {
FD_CLR(rsock, &bcst_fd);
register int a;
int is_authentic, has_mac = 0, orig_pkt_len;
- char *rdata = malloc(sizeof(char) * 256);
+ char *rdata = emalloc(sizeof(char) * 256);
int pkt_len = recv_bcst_data(rsock, rdata, 256, sas, &sender);
/* Much space, just to be sure */
- rdata = malloc(sizeof(char) * 256);
+ rdata = emalloc(sizeof(char) * 256);
int pkt_len = recvdata(rsock, &sender, rdata, 256);
autogen definitions options;
#include autogen-version.def
+#include copyright.def
prog-name = "sntp";
prog-title = "standard SNTP program";
homerc = $HOME, ".";
argument = '...';
-copyright = {
- date = "2008";
- owner = "ntp.org";
- eaddr = "http://bugs.ntp.org, bugs@ntp.org";
- type = note;
- text = `cat COPYRIGHT`;
-};
-
-
long-opts;
config-header = "config.h";
name = kod;
value = K;
arg-type = string;
- descrip = "Specify a file for the KOD packet storage";
+ descrip = "KoD history filename";
doc = <<- _EndOfDoc_
- Specifies the file to be used to store KOD packaet information.
+ Modifies the filename to be used to persist the history of KoD
+ responses received from servers. The default is
+ /var/db/ntp-kod.
_EndOfDoc_;
};
flag = {
name = broadcast;
value = b;
- descrip = "Use broadcast packages from the broadcast address specified for synchronisation";
+ descrip = "Use broadcasts to the address specified for synchronisation";
arg-type = string;
doc = <<- _EndOfDoc_
- If specified SNTP will wait 1 minute for broadcast packets
- from the broadcast address specified for synchronisation.
- The amount of time SNTP waits can be specified with -t.
+ If specified SNTP will listen for NTP broadcasts to the
+ specified broadcast address. The default maximum wait time,
+ 68 seconds, can be modified with -t.
_EndOfDoc_;
};
flag = {
name = timeout;
value = t;
- descrip = "Specify the number of seconds until SNTP times out when waiting for broadcast packets";
+ descrip = "Specify the number of seconds to wait for broadcasts";
arg-type = number;
doc = <<- _EndOfDoc_
When waiting for a broadcast packet SNTP will wait the number
- of seconds specified and times out.
+ of seconds specified before giving up. Default 68 seconds.
_EndOfDoc_;
};
)
{
register int a;
+ u_char *pkt;
- unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * pkt_length);
-
- if (NULL == cpy)
- return;
-
- for(a=0; a<pkt_length; a++)
- cpy[a] = ((unsigned char *) dpkg)[a];
-
+ pkt = (u_char *)dpkg;
fprintf(output, HLINE);
- for(a=0; a<pkt_length; a++) {
- if(a > 0 && a%8 == 0)
+ for (a = 0; a < pkt_length; a++) {
+ if (a > 0 && a % 8 == 0)
fprintf(output, "\n");
- fprintf(output, "%i: %x \t", a, cpy[a]);
+ fprintf(output, "%d: %x \t", a, pkt[a]);
}
fprintf(output, "\n");
fprintf(output, HLINE);
-
- free(cpy);
}
/* Output a long floating point value in hex in the style described above
struct addrinfo *addr
)
{
- char *buf = (char *) malloc(sizeof(char) * INET6_ADDRSTRLEN);
+ char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN);
getnameinfo(addr->ai_addr, addr->ai_addrlen, buf,
INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
sockaddr_u *saddr
)
{
- char *buf = (char *) malloc(sizeof(char) * INET6_ADDRSTRLEN);
+ char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN);
getnameinfo(&saddr->sa, SOCKLEN(saddr), buf,
INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
- char *buf = (char *) malloc(sizeof(char) * 48);
+ char *buf = (char *) emalloc(sizeof(char) * 48);
time_t cur_time = time(NULL);
struct tm *tm_ptr;
- if (NULL == buf)
- return "tv_to_str() malloc(48) failed";
-
tm_ptr = localtime(&cur_time);
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
+#include <ntp_stdlib.h>
#include <ntp_fp.h>
#include <ntp.h>