From: Harlan Stenn Date: Tue, 11 Jan 2011 01:32:44 +0000 (+0000) Subject: cleanup X-Git-Tag: NTP_4_2_7P131~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=87c32f140de0e58bb5f07e5f872bac43a6ef7ec8;p=thirdparty%2Fntp.git cleanup bk: 4d2bb33cvg1U7fvacKPejET1AIoj5w --- diff --git a/sntp/main.c b/sntp/main.c index 058621fc7..a3d59e002 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -11,13 +11,6 @@ #include #include -#if 0 -// Might need: -#include -#include -#include -#endif - int n_pending_dns = 0; int n_pending_ntp = 0; int ai_fam_pref = AF_UNSPEC; @@ -25,6 +18,7 @@ SOCKET sock4 = -1; /* Socket for IPv4 */ SOCKET sock6 = -1; /* Socket for IPv6 */ char *progname; struct event_base *base = NULL; +struct evdns_base *dnsbase = NULL; struct dns_ctx { const char * name; @@ -56,6 +50,7 @@ static union { #define r_pkt rbuf.pkt +void handle_lookup( char *name, int flags ); void dns_cb (int errcode, struct evutil_addrinfo *addr, void *ptr); void ntp_cb (evutil_socket_t, short, void *); void set_li_vn_mode (struct pkt *spkt, char leap, char version, char mode); @@ -87,9 +82,6 @@ sntp_main ( { int i; int optct; - /* boolean, u_int quiets gcc4 signed overflow warning */ - // struct addrinfo *ai; - struct evdns_base *dnsbase; long l; progname = argv[0]; @@ -122,9 +114,7 @@ sntp_main ( /* IPv6 available? */ if (isc_net_probeipv6() != ISC_R_SUCCESS) { ai_fam_pref = AF_INET; -#ifdef DEBUG - printf("No ipv6 support available, forcing ipv4\n"); -#endif + DPRINTF(1, ("No ipv6 support available, forcing ipv4\n")); } else { /* Check for options -4 and -6 */ if (HAVE_OPT(IPV4)) @@ -133,36 +123,19 @@ sntp_main ( ai_fam_pref = AF_INET6; } - /* Parse config file if declared TODO */ + /* TODO: Parse config file if declared */ /* - ** If there's a specified KOD file init KOD system. If not use - ** default file. For embedded systems with no writable filesystem, + ** Init the KOD system. + ** 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"); + kod_init_kod_db(OPT_ARG(KOD)); + // HMS: Should we use arg-defalt for this too? 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 @@ -182,43 +155,14 @@ sntp_main ( return -1; } - if (ENABLED_OPT(BROADCAST)) { - struct evutil_addrinfo hints; - struct dns_ctx *dns_ctx; - - ZERO(hints); - hints.ai_family = ai_fam_pref; - hints.ai_flags |= EVUTIL_AI_CANONNAME; - hints.ai_flags |= EVUTIL_AI_NUMERICSERV; - /* - ** Unless we specify a socktype, we'll get at least two - ** entries for each address: one for TCP and one for - ** UDP. That's not what we want. - */ - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - dns_ctx = emalloc(sizeof(*dns_ctx)); - memset(dns_ctx, 0, sizeof(*dns_ctx)); - - dns_ctx->name = OPT_ARG(BROADCAST); - dns_ctx->flags = CTX_BCST; - dns_ctx->timeout = timeout_tv; - - if (ENABLED_OPT(AUTHENTICATION) && - atoint(OPT_ARG(AUTHENTICATION), &l)) { - dns_ctx->key_id = l; - get_key(dns_ctx->key_id, &dns_ctx->key); - } else { - dns_ctx->key_id = -1; - dns_ctx->key = NULL; - } + if (HAVE_OPT(BROADCAST)) { + int cn = STACKCT_OPT( BROADCAST ); + const char ** cp = STACKLST_OPT( BROADCAST ); - printf("broadcast-before: <%s> n_pending_dns = %d\n", OPT_ARG(BROADCAST), n_pending_dns); - ++n_pending_dns; - evdns_getaddrinfo(dnsbase, OPT_ARG(BROADCAST), "123", &hints, - dns_cb, dns_ctx); - printf("broadcast-after: <%s> n_pending_dns = %d\n", OPT_ARG(BROADCAST), n_pending_dns); + while (cn-- > 0) { + handle_lookup(*cp, CTX_BCST); + cp++; + } } if (HAVE_OPT(CONCURRENT)) { @@ -226,52 +170,15 @@ sntp_main ( const char ** cp = STACKLST_OPT( CONCURRENT ); while (cn-- > 0) { - /* Do something with *cp++ */ - printf("Concurrent: <%s>\n", *cp++); + handle_lookup(*cp, CTX_UCST|CTX_CONC); + cp++; } } for (i = 0; i < argc; ++i) { - struct evutil_addrinfo hints; /* local copy is OK */ - struct dns_ctx *dns_ctx; - - ZERO(hints); - hints.ai_family = ai_fam_pref; - hints.ai_flags |= EVUTIL_AI_CANONNAME; - hints.ai_flags |= EVUTIL_AI_NUMERICSERV; - /* - ** Unless we specify a socktype, we'll get at least two - ** entries for each address: one for TCP and one for - ** UDP. That's not what we want. - */ - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - dns_ctx = emalloc(sizeof(*dns_ctx)); - memset(dns_ctx, 0, sizeof(*dns_ctx)); - - dns_ctx->name = argv[i]; - dns_ctx->flags = CTX_UCST; - dns_ctx->timeout = ucst_timeout_tv; - - if (ENABLED_OPT(AUTHENTICATION) && - atoint(OPT_ARG(AUTHENTICATION), &l)) { - dns_ctx->key_id = l; - get_key(dns_ctx->key_id, &dns_ctx->key); - } else { - dns_ctx->key_id = -1; - dns_ctx->key = NULL; - } - - // printf("unicast-before: <%s> n_pending_dns = %d\n", argv[i], n_pending_dns); - ++n_pending_dns; - evdns_getaddrinfo(dnsbase, argv[i], "123", &hints, - dns_cb, dns_ctx); - // printf("unicast-after: <%s> n_pending_dns = %d\n", argv[i], n_pending_dns); + handle_lookup(argv[i], CTX_UCST); } - // printf("unicast: n_pending_dns = %d\n", n_pending_dns); - event_base_dispatch(base); evdns_base_free(dnsbase, 0); @@ -281,6 +188,59 @@ sntp_main ( } +/* +** handle_lookup +*/ +void +handle_lookup( + char *name, + int flags + ) +{ + struct evutil_addrinfo hints; /* Local copy is OK */ + struct dns_ctx *dns_ctx; + long l; + + DPRINTF(1, ("handle_lookup(%s,%#x)\n", name, flags)); + + ZERO(hints); + hints.ai_family = ai_fam_pref; + hints.ai_flags |= EVUTIL_AI_CANONNAME; + hints.ai_flags |= EVUTIL_AI_NUMERICSERV; + /* + ** Unless we specify a socktype, we'll get at least two + ** entries for each address: one for TCP and one for + ** UDP. That's not what we want. + */ + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + dns_ctx = emalloc(sizeof(*dns_ctx)); + memset(dns_ctx, 0, sizeof(*dns_ctx)); + + dns_ctx->name = OPT_ARG(BROADCAST); + dns_ctx->flags = flags; + dns_ctx->timeout = + (flags & CTX_BCST) + ? timeout_tv + : ucst_timeout_tv + ; + + // The following should arguably be passed in... + if (ENABLED_OPT(AUTHENTICATION) && + atoint(OPT_ARG(AUTHENTICATION), &l)) { + dns_ctx->key_id = l; + get_key(dns_ctx->key_id, &dns_ctx->key); + } else { + dns_ctx->key_id = -1; + dns_ctx->key = NULL; + } + + ++n_pending_dns; + evdns_getaddrinfo(dnsbase, name, "123", &hints, dns_cb, dns_ctx); +} + + /* ** DNS Callback: ** - For each IP: diff --git a/sntp/sntp-opts.c b/sntp/sntp-opts.c index aaad1e7b5..aa455ec39 100644 --- a/sntp/sntp-opts.c +++ b/sntp/sntp-opts.c @@ -1,7 +1,7 @@ /* * EDIT THIS FILE WITH CAUTION (sntp-opts.c) * - * It has been AutoGen-ed January 10, 2011 at 11:23:51 PM by AutoGen 5.11.6pre7 + * It has been AutoGen-ed January 11, 2011 at 01:30:03 AM by AutoGen 5.11.6pre7 * From the definitions sntp-opts.def * and the template file options * @@ -116,6 +116,7 @@ static char const zKodText[] = "KoD history filename"; static char const zKod_NAME[] = "KOD"; static char const zKod_Name[] = "kod"; +static char const zKodDefaultArg[] = "/var/db/ntp-kod"; #define KOD_FLAGS (OPTST_DISABLED \ | OPTST_SET_ARGTYPE(OPARG_TYPE_FILE)) @@ -206,7 +207,7 @@ static char const zBroadcastText[] = "Use broadcasts to the address specified for synchronisation"; static char const zBroadcast_NAME[] = "BROADCAST"; static char const zBroadcast_Name[] = "broadcast"; -#define BROADCAST_FLAGS (OPTST_DISABLED \ +#define BROADCAST_FLAGS (OPTST_DISABLED | OPTST_STACKED \ | OPTST_SET_ARGTYPE(OPARG_TYPE_STRING)) /* @@ -370,7 +371,7 @@ static tOptDesc optDesc[ OPTION_CT ] = { /* equivalenced to */ NO_EQUIVALENT, /* min, max, act ct */ 0, 1, 0, /* opt state flags */ KOD_FLAGS, 0, - /* last opt argumnt */ { NULL }, + /* last opt argumnt */ { zKodDefaultArg }, /* arg list/cookie */ NULL, /* must/cannot opts */ NULL, NULL, /* option proc */ doOptKod, @@ -452,12 +453,12 @@ static tOptDesc optDesc[ OPTION_CT ] = { { /* entry idx, value */ 11, VALUE_OPT_BROADCAST, /* equiv idx, value */ 11, VALUE_OPT_BROADCAST, /* equivalenced to */ NO_EQUIVALENT, - /* min, max, act ct */ 0, 1, 0, + /* min, max, act ct */ 0, NOLIMIT, 0, /* opt state flags */ BROADCAST_FLAGS, 0, /* last opt argumnt */ { NULL }, /* arg list/cookie */ NULL, /* must/cannot opts */ NULL, NULL, - /* option proc */ NULL, + /* option proc */ optionStackArg, /* desc, NAME, name */ zBroadcastText, zBroadcast_NAME, zBroadcast_Name, /* disablement strs */ NULL, NULL }, diff --git a/sntp/sntp-opts.def b/sntp/sntp-opts.def index 0c724ee8f..c58792234 100644 --- a/sntp/sntp-opts.def +++ b/sntp/sntp-opts.def @@ -55,6 +55,7 @@ flag = { value = K; arg-type = file; arg-name = "file-name"; + arg-default = "/var/db/ntp-kod"; descrip = "KoD history filename"; doc = <<- _EndOfDoc_ Specifies the filename to be used for the persistent history of KoD @@ -143,6 +144,8 @@ flag = { descrip = "Use broadcasts to the address specified for synchronisation"; arg-type = string; arg-name = "broadcast-address"; + max = NOLIMIT; + stack-arg; doc = <<- _EndOfDoc_ If specified SNTP will listen to the specified broadcast address for NTP broadcasts. The default maximum wait time, diff --git a/sntp/sntp-opts.h b/sntp/sntp-opts.h index 7d9431732..06f9a04a0 100644 --- a/sntp/sntp-opts.h +++ b/sntp/sntp-opts.h @@ -1,7 +1,7 @@ /* * EDIT THIS FILE WITH CAUTION (sntp-opts.h) * - * It has been AutoGen-ed January 10, 2011 at 11:23:51 PM by AutoGen 5.11.6pre7 + * It has been AutoGen-ed January 11, 2011 at 01:30:02 AM by AutoGen 5.11.6pre7 * From the definitions sntp-opts.def * and the template file options * diff --git a/sntp/sntp-opts.texi b/sntp/sntp-opts.texi index aa0439201..2a4946cd4 100644 --- a/sntp/sntp-opts.texi +++ b/sntp/sntp-opts.texi @@ -6,7 +6,7 @@ # # EDIT THIS FILE WITH CAUTION (sntp-opts.texi) # -# It has been AutoGen-ed January 10, 2011 at 11:23:56 PM by AutoGen 5.11.6pre7 +# It has been AutoGen-ed January 11, 2011 at 01:30:08 AM by AutoGen 5.11.6pre7 # From the definitions sntp-opts.def # and the template file aginfo.tpl @end ignore @@ -108,6 +108,7 @@ USAGE: sntp [ - [] | --[@{=| @}] ]... \ -c Str concurrent Send concurrent queries to all IPs returned for host-name - may appear multiple times -b Str broadcast Use broadcasts to the address specified for synchronisation + - may appear multiple times -t Num timeout Specify the number of seconds to wait for broadcasts -a Num authentication Enable authentication with the key auth-keynumber -k Fil keyfile Specify a keyfile. SNTP will look in this file for the key specified with -a @@ -171,6 +172,13 @@ key's identifier. See the keyfile option (-k) for more details. @cindex sntp-broadcast This is the ``use broadcasts to the address specified for synchronisation'' option. + +This option has some usage constraints. It: +@itemize @bullet +@item +may appear an unlimited number of times. +@end itemize + If specified SNTP will listen to the specified broadcast address for NTP broadcasts. The default maximum wait time, 68 seconds, can be modified with -t. diff --git a/sntp/sntp.1 b/sntp/sntp.1 index bda755c02..82858a3f3 100644 --- a/sntp/sntp.1 +++ b/sntp/sntp.1 @@ -1,7 +1,7 @@ -.TH SNTP 1 2011-01-10 "( 4.2.7p114)" "Programmer's Manual" +.TH SNTP 1 2011-01-11 "( 4.2.7p114)" "Programmer's Manual" .\" EDIT THIS FILE WITH CAUTION (sntp.1) .\" -.\" It has been AutoGen-ed January 10, 2011 at 11:23:55 PM by AutoGen 5.11.6pre7 +.\" It has been AutoGen-ed January 11, 2011 at 01:30:07 AM by AutoGen 5.11.6pre7 .\" From the definitions sntp-opts.def .\" and the template file agman1.tpl .\" @@ -78,6 +78,9 @@ to the IPv6 namespace. .TP .BR \-K " \fIfile-name\fP, " \--kod "=" \fIfile-name\fP KoD history filename. +The default \fIfile-name\fP for this option is: +.ti +4 + /var/db/ntp-kod .sp Specifies the filename to be used for the persistent history of KoD responses received from servers. The default is @@ -136,6 +139,7 @@ so we can send concurrent queries. .TP .BR \-b " \fIbroadcast-address\fP, " \--broadcast "=" \fIbroadcast-address\fP Use broadcasts to the address specified for synchronisation. +This option may appear an unlimited number of times. .sp If specified SNTP will listen to the specified broadcast address for NTP broadcasts. The default maximum wait time, diff --git a/sntp/sntp.html b/sntp/sntp.html index 83a59243e..b7435aca3 100644 --- a/sntp/sntp.html +++ b/sntp/sntp.html @@ -183,6 +183,7 @@ USAGE: sntp [ -<flag> [<val>] | --<name>[{=| }<val>] ]. -c Str concurrent Send concurrent queries to all IPs returned for host-name - may appear multiple times -b Str broadcast Use broadcasts to the address specified for synchronisation + - may appear multiple times -t Num timeout Specify the number of seconds to wait for broadcasts -a Num authentication Enable authentication with the key auth-keynumber -k Fil keyfile Specify a keyfile. SNTP will look in this file for the key specified with -a @@ -264,8 +265,14 @@ Up: sntp Invocation

broadcast option (-b)

-This is the “use broadcasts to the address specified for synchronisation” option. -If specified SNTP will listen to the specified broadcast address +This is the “use broadcasts to the address specified for synchronisation” option. + +

This option has some usage constraints. It: +

    +
  • may appear an unlimited number of times. +
+ +

If specified SNTP will listen to the specified broadcast address for NTP broadcasts. The default maximum wait time, 68 seconds, can be modified with -t.