X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fntpd.c;h=bfd5705fcd30a8ab2293eec6baa2c7a02ec6c119;hb=2b1559056cf32c42675ecd937796e1455bcb5c2c;hp=410318979008a101ebcdfa7e6003ad52bb215c02;hpb=c8641962e4cbde48108ddfc1c105e3320778190d;p=oweals%2Fbusybox.git diff --git a/networking/ntpd.c b/networking/ntpd.c index 410318979..bfd5705fc 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -40,6 +40,32 @@ * purpose. It is provided "as is" without express or implied warranty. *********************************************************************** */ +//config:config NTPD +//config: bool "ntpd" +//config: default y +//config: select PLATFORM_LINUX +//config: help +//config: The NTP client/server daemon. +//config: +//config:config FEATURE_NTPD_SERVER +//config: bool "Make ntpd usable as a NTP server" +//config: default y +//config: depends on NTPD +//config: help +//config: Make ntpd usable as a NTP server. If you disable this option +//config: ntpd will be usable only as a NTP client. +//config: +//config:config FEATURE_NTPD_CONF +//config: bool "Make ntpd understand /etc/ntp.conf" +//config: default y +//config: depends on NTPD +//config: help +//config: Make ntpd look in /etc/ntp.conf for peers. Only "server address" +//config: is supported. + +//applet:IF_NTPD(APPLET(ntpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_NTPD) += ntpd.o //usage:#define ntpd_trivial_usage //usage: "[-dnqNw"IF_FEATURE_NTPD_SERVER("l -I IFACE")"] [-S PROG] [-p PEER]..." @@ -129,6 +155,7 @@ #define RETRY_INTERVAL 32 /* on send/recv error, retry in N secs (need to be power of 2) */ #define NOREPLY_INTERVAL 512 /* sent, but got no reply: cap next query by this many seconds */ #define RESPONSE_INTERVAL 16 /* wait for reply up to N secs */ +#define HOSTNAME_INTERVAL 5 /* hostname lookup failed. Wait N secs for next try */ /* Step threshold (sec). std ntpd uses 0.128. */ @@ -267,7 +294,6 @@ typedef struct { typedef struct { len_and_sockaddr *p_lsa; - char *p_hostname; char *p_dotted; int p_fd; int datapoint_idx; @@ -293,6 +319,7 @@ typedef struct { datapoint_t filter_datapoint[NUM_DATAPOINTS]; /* last sent packet: */ msg_t p_xmt_msg; + char p_hostname[1]; } peer_t; @@ -764,37 +791,52 @@ reset_peer_stats(peer_t *p, double offset) VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time); } +static len_and_sockaddr* +resolve_peer_hostname(peer_t *p) +{ + len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123); + if (lsa) { + free(p->p_lsa); + free(p->p_dotted); + p->p_lsa = lsa; + p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); + } else { + /* error message is emitted by host2sockaddr() */ + set_next(p, HOSTNAME_INTERVAL); + } + return lsa; +} + static void add_peers(const char *s) { llist_t *item; peer_t *p; - p = xzalloc(sizeof(*p)); - p->p_lsa = xhost2sockaddr(s, 123); - p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa); + p = xzalloc(sizeof(*p) + strlen(s)); + strcpy(p->p_hostname, s); + p->p_fd = -1; + p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); + p->next_action_time = G.cur_time; /* = set_next(p, 0); */ + reset_peer_stats(p, STEP_THRESHOLD); /* Names like N..pool.ntp.org are randomly resolved * to a pool of machines. Sometimes different N's resolve to the same IP. * It is not useful to have two peers with same IP. We skip duplicates. */ - for (item = G.ntp_peers; item != NULL; item = item->link) { - peer_t *pp = (peer_t *) item->data; - if (strcmp(p->p_dotted, pp->p_dotted) == 0) { - bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted); - free(p->p_lsa); - free(p->p_dotted); - free(p); - return; + if (resolve_peer_hostname(p)) { + for (item = G.ntp_peers; item != NULL; item = item->link) { + peer_t *pp = (peer_t *) item->data; + if (pp->p_dotted && strcmp(p->p_dotted, pp->p_dotted) == 0) { + bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted); + free(p->p_lsa); + free(p->p_dotted); + free(p); + return; + } } } - p->p_hostname = xstrdup(s); - p->p_fd = -1; - p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); - p->next_action_time = G.cur_time; /* = set_next(p, 0); */ - reset_peer_stats(p, STEP_THRESHOLD); - llist_add_to(&G.ntp_peers, p); G.peer_cnt++; } @@ -822,6 +864,11 @@ do_sendto(int fd, static void send_query_to_peer(peer_t *p) { + if (!p->p_lsa) { + if (!resolve_peer_hostname(p)) + return; + } + /* Why do we need to bind()? * See what happens when we don't bind: * @@ -2028,6 +2075,13 @@ recv_and_process_client_pkt(void /*int fd*/) goto bail; } + /* Respond only to client and symmetric active packets */ + if ((msg.m_status & MODE_MASK) != MODE_CLIENT + && (msg.m_status & MODE_MASK) != MODE_SYM_ACT + ) { + goto bail; + } + query_status = msg.m_status; query_xmttime = msg.m_xmttime; @@ -2174,15 +2228,15 @@ static NOINLINE void ntp_init(char **argv) /* Parse options */ peers = NULL; - opt_complementary = "dd:p::wn" /* -d: counter; -p: list; -w implies -n */ + opt_complementary = "dd:wn" /* -d: counter; -p: list; -w implies -n */ IF_FEATURE_NTPD_SERVER(":Il"); /* -I implies -l */ opts = getopt32(argv, "nqNx" /* compat */ - "wp:S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ + "wp:*S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ IF_FEATURE_NTPD_SERVER("I:") /* compat */ "d" /* compat */ "46aAbgL", /* compat, ignored */ - &peers,&G.script_name, + &peers, &G.script_name, #if ENABLE_FEATURE_NTPD_SERVER &G.if_name, #endif @@ -2190,6 +2244,28 @@ static NOINLINE void ntp_init(char **argv) // if (opts & OPT_x) /* disable stepping, only slew is allowed */ // G.time_was_stepped = 1; + +#if ENABLE_FEATURE_NTPD_SERVER + G_listen_fd = -1; + if (opts & OPT_l) { + G_listen_fd = create_and_bind_dgram_or_die(NULL, 123); + if (G.if_name) { + if (setsockopt_bindtodevice(G_listen_fd, G.if_name)) + xfunc_die(); + } + socket_want_pktinfo(G_listen_fd); + setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY); + } +#endif + /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */ + if (opts & OPT_N) + setpriority(PRIO_PROCESS, 0, -15); + + if (!(opts & OPT_n)) { + bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv); + logmode = LOGMODE_NONE; + } + if (peers) { while (peers) add_peers(llist_pop(&peers)); @@ -2218,26 +2294,6 @@ static NOINLINE void ntp_init(char **argv) /* -l but no peers: "stratum 1 server" mode */ G.stratum = 1; } -#if ENABLE_FEATURE_NTPD_SERVER - G_listen_fd = -1; - if (opts & OPT_l) { - G_listen_fd = create_and_bind_dgram_or_die(NULL, 123); - if (opts & OPT_I) { - if (setsockopt_bindtodevice(G_listen_fd, G.if_name)) - xfunc_die(); - } - socket_want_pktinfo(G_listen_fd); - setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY); - } -#endif - if (!(opts & OPT_n)) { - bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv); - logmode = LOGMODE_NONE; - } - /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */ - if (opts & OPT_N) - setpriority(PRIO_PROCESS, 0, -15); - /* If network is up, syncronization occurs in ~10 seconds. * We give "ntpd -q" 10 seconds to get first reply, * then another 50 seconds to finish syncing. @@ -2338,18 +2394,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) p->p_dotted, p->reachable_bits, timeout); /* What if don't see it because it changed its IP? */ - if (p->reachable_bits == 0) { - len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123); - if (lsa) { - char *dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); - //if (strcmp(dotted, p->p_dotted) != 0) - // bb_error_msg("peer IP changed"); - free(p->p_lsa); - free(p->p_dotted); - p->p_lsa = lsa; - p->p_dotted = dotted; - } - } + if (p->reachable_bits == 0) + resolve_peer_hostname(p); set_next(p, timeout); }