]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1283] default to remembering KoD in sntp
authorDave Hart <hart@ntp.org>
Wed, 26 Aug 2009 00:48:46 +0000 (00:48 +0000)
committerDave Hart <hart@ntp.org>
Wed, 26 Aug 2009 00:48:46 +0000 (00:48 +0000)
clean up numerous sntp/kod_management.c bugs
use all addresses resolved from each DNS name

bk: 4a94866eyG-z9Y-DJ7BKTr6NmYwMew

14 files changed:
ChangeLog
configure.ac
libntp/emalloc.c
ntpd/ntp_config.c
sntp/configure.ac
sntp/crypto.c
sntp/kod_management.c
sntp/kod_management.h
sntp/main.c
sntp/netutils.c
sntp/networking.c
sntp/sntp-opts.def
sntp/utilities.c
sntp/utilities.h

index 805b28efd09f7fd7d6605e7982a62702b813f011..54da7b6bbda688593ce30d03dda433370b68b392 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+* [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.
index f0c067cfeb89449f1b482b5276157059f1bbace9..a201292f5cec70b37eb8a24a8129fb17eae1e972 100644 (file)
@@ -2017,7 +2017,7 @@ AC_ARG_ENABLE(
 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(
index 0bdf102a8aa437edff3e51a1fcdb66842841e7b7..8798a31934f062f902e416b4d3e60004f2932ab9 100644 (file)
@@ -6,6 +6,8 @@
 #include "ntp_syslog.h"
 #include "ntp_stdlib.h"
 
+extern char *progname;
+
 #if !defined(_MSC_VER) || !defined(_DEBUG)
 
 
@@ -20,8 +22,11 @@ erealloc(
        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);
        }
 
@@ -49,9 +54,11 @@ estrdup(
 
        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);
        }
 
@@ -82,8 +89,12 @@ debug_erealloc(
        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);
        }
 
@@ -98,9 +109,11 @@ debug_estrdup(
        )
 {
        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;
 }
index b02bddc7e3c2987c9a6428ca2a6782393ba9243a..f9fb0de717dada4c0657481b5225cd7330e8c1f4 100644 (file)
@@ -260,10 +260,11 @@ static void config_vars(struct config_tree *);
 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 {
@@ -2883,14 +2884,9 @@ free_config_qos(
        )
 {
        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);
        }
 }
@@ -3667,9 +3663,6 @@ free_config_sim(
        struct config_tree *ptree
        )
 {
-       server_info *serv_info;
-       struct attr_val *init_stmt;
-
        if (NULL == ptree->sim_details)
                return;
 
@@ -3688,7 +3681,7 @@ free_config_sim(
  * the simulator. The simulator ignores a lot of the standard ntpd configuration
  * options
  */
-
+#ifndef SIM
 static void
 config_ntpd(
        struct config_tree *ptree
@@ -3712,6 +3705,8 @@ config_ntpd(
        config_fudge(ptree);
        config_qos(ptree);
 }
+#endif /* !SIM */
+
 
 #ifdef SIM
 static void
index aba6f52d0d8961a802ca85cc7fe45a591eba8c02..69be79eff0721ef60ecf94507c4698800e124b8b 100644 (file)
@@ -126,6 +126,7 @@ m4_defun([_LT_AC_LANG_CXX_CONFIG], [:])
 m4_defun([_LT_AC_LANG_F77_CONFIG], [:])
 
 AC_PROG_LIBTOOL
+NTP_DIR_SEP
 
 # Checks for libraries.
 
index 5867e0c0639a6dd720073e541c4c22ca36f1b406..9f209684447256f0a39a02e9684c9012a54d1daf 100644 (file)
@@ -41,7 +41,7 @@ auth_md5 (
        
        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];
@@ -97,7 +97,7 @@ auth_init (
 
 
        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);
index a172fb90bc0f4760200c46d24d6e8b93d616b0d5..ca0bc2977830c32c48381faa715b6fadf0835097 100644 (file)
 
 /* 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 */
 
 
 /*
@@ -39,223 +41,220 @@ search_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) {
@@ -263,20 +262,30 @@ kod_init_kod_db (
                        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
@@ -286,18 +295,15 @@ kod_init_kod_db (
        }
 
 #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);
 }
index 3629dbd3763b2d38be4a8492029572f03f7a062a..239c078cd08eb91c746ffe632bad8d2640f20119 100644 (file)
 #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
index 39353c42e544e945e91047915d96ee4844da10db..552f0e55868a4c9ed00f79ad64da951909c9b97c 100644 (file)
@@ -15,6 +15,7 @@
 #include "utilities.h"
 #include "log.h"
 
+char *progname = "sntp";       /* for msyslog */
 
 int ai_fam_pref;
 volatile int debug;
@@ -28,16 +29,6 @@ int on_wire (struct addrinfo *host);
 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,
@@ -61,7 +52,11 @@ sntp_main (
        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) {
@@ -92,15 +87,33 @@ sntp_main (
        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 
@@ -116,29 +129,27 @@ sntp_main (
         * 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);
 
@@ -153,8 +164,9 @@ on_wire (
 {
        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;
@@ -163,8 +175,8 @@ on_wire (
                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);
 
@@ -176,17 +188,17 @@ on_wire (
 #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);
 
@@ -209,16 +221,19 @@ on_wire (
                        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);
 
@@ -232,10 +247,10 @@ on_wire (
                        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, 
@@ -245,20 +260,20 @@ on_wire (
                        }
 
 #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. */
index b6d1d0f17b02e9de09ddbc2018598a09e7650761..185250573e806540da5bd262ec8529b94bfc7925 100644 (file)
@@ -112,7 +112,7 @@ void compile_header (
                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?
@@ -201,7 +201,7 @@ size_t compile_query (
 
        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];
@@ -230,7 +230,7 @@ size_t compile_resource_record (
 
        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];
@@ -272,8 +272,8 @@ size_t domain_to_qname (
                        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++) {
index 35373c1101938bca6d48bda1fa4a8000f65f99dc..93b78f902a2acc6b4010b33345f1be5d7331ad01 100644 (file)
@@ -41,7 +41,7 @@ resolve_hosts (
        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;
@@ -66,7 +66,7 @@ resolve_hosts (
 
                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
@@ -292,7 +292,7 @@ recv_bcst_data (
        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);
@@ -347,7 +347,7 @@ recv_bcst_pkt (
        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);
 
@@ -528,7 +528,7 @@ recvpkt (
 
 
        /* Much space, just to be sure */
-       rdata = malloc(sizeof(char) * 256);
+       rdata = emalloc(sizeof(char) * 256);
 
        int pkt_len = recvdata(rsock, &sender, rdata, 256);
 
index 8145d341aff8531deb8bf4b1e46807efeefbfca9..46a410e28f8381ef0ec86ef4586e6ad095a7157e 100644 (file)
@@ -3,21 +3,13 @@
 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";
@@ -72,9 +64,11 @@ flag = {
     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_;
 };
 
@@ -122,23 +116,23 @@ flag = {
 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_;
 };
 
index f338ec2eac7a8afe6e9ef4cd5c027d87c2725d51..e3a14aed6449ea5a991aab09a5cc42aa9f7d03eb 100644 (file)
@@ -28,29 +28,21 @@ pkt_output (
           )
 {
        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 
@@ -138,7 +130,7 @@ addrinfo_to_str (
                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);
@@ -156,7 +148,7 @@ ss_to_str (
                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);
@@ -177,13 +169,10 @@ tv_to_str (
                "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);
 
 
index 78962f4032ae48190496103a8509e97ac60a778e..604ac69ef81a30dc79244604423a6ee5fff119ef 100644 (file)
@@ -38,6 +38,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <ntp_stdlib.h>
 #include <ntp_fp.h>
 #include <ntp.h>