aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-06-06 02:26:49 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-06-06 02:26:49 +0200
commite4caf1dd9ce8569371a0eeb77ccf02a572dc0f11 (patch)
tree9bb0c538b7bac20b94272c39576b740436a08c6c
parentb684d1b1864ba9d7968de5565823f8e42f1dc448 (diff)
downloadbusybox-w32-e4caf1dd9ce8569371a0eeb77ccf02a572dc0f11.tar.gz
busybox-w32-e4caf1dd9ce8569371a0eeb77ccf02a572dc0f11.tar.bz2
busybox-w32-e4caf1dd9ce8569371a0eeb77ccf02a572dc0f11.zip
ntpd: retry initial DNS resolution (forever, no timeout for now).
Some users start ntpd on boot, and don't babysit it. If it dies because DNS is not yet up and therefore NTP servers can't be found, users are not happy. Example behavior with a peer name which can't be resolved: ntpd: bad address 'qwe.rty.ghj.kl' ...5 sec... ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ntpd: bad address 'qwe.rty.ghj.kl' ... Based on the patch by Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> function old new delta resolve_peer_hostname - 81 +81 ntpd_main 1130 1061 -69 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 81/-69) Total: 12 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/ntpd.c47
1 files changed, 30 insertions, 17 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 410318979..98158a304 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -267,7 +267,6 @@ typedef struct {
267 267
268typedef struct { 268typedef struct {
269 len_and_sockaddr *p_lsa; 269 len_and_sockaddr *p_lsa;
270 char *p_hostname;
271 char *p_dotted; 270 char *p_dotted;
272 int p_fd; 271 int p_fd;
273 int datapoint_idx; 272 int datapoint_idx;
@@ -293,6 +292,7 @@ typedef struct {
293 datapoint_t filter_datapoint[NUM_DATAPOINTS]; 292 datapoint_t filter_datapoint[NUM_DATAPOINTS];
294 /* last sent packet: */ 293 /* last sent packet: */
295 msg_t p_xmt_msg; 294 msg_t p_xmt_msg;
295 char p_hostname[1];
296} peer_t; 296} peer_t;
297 297
298 298
@@ -765,14 +765,38 @@ reset_peer_stats(peer_t *p, double offset)
765} 765}
766 766
767static void 767static void
768resolve_peer_hostname(peer_t *p, int loop_on_fail)
769{
770 len_and_sockaddr *lsa;
771
772 again:
773 lsa = host2sockaddr(p->p_hostname, 123);
774 if (!lsa) {
775 /* error message already emitted by host2sockaddr() */
776 if (!loop_on_fail)
777 return;
778//FIXME: do this to avoid infinite looping on typo in a hostname?
779//well... in which case, what is a good value for loop_on_fail?
780 //if (--loop_on_fail == 0)
781 // xfunc_die();
782 sleep(5);
783 goto again;
784 }
785 free(p->p_lsa);
786 free(p->p_dotted);
787 p->p_lsa = lsa;
788 p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
789}
790
791static void
768add_peers(const char *s) 792add_peers(const char *s)
769{ 793{
770 llist_t *item; 794 llist_t *item;
771 peer_t *p; 795 peer_t *p;
772 796
773 p = xzalloc(sizeof(*p)); 797 p = xzalloc(sizeof(*p) + strlen(s));
774 p->p_lsa = xhost2sockaddr(s, 123); 798 strcpy(p->p_hostname, s);
775 p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa); 799 resolve_peer_hostname(p, /*loop_on_fail=*/ 1);
776 800
777 /* Names like N.<country2chars>.pool.ntp.org are randomly resolved 801 /* Names like N.<country2chars>.pool.ntp.org are randomly resolved
778 * to a pool of machines. Sometimes different N's resolve to the same IP. 802 * to a pool of machines. Sometimes different N's resolve to the same IP.
@@ -789,7 +813,6 @@ add_peers(const char *s)
789 } 813 }
790 } 814 }
791 815
792 p->p_hostname = xstrdup(s);
793 p->p_fd = -1; 816 p->p_fd = -1;
794 p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); 817 p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
795 p->next_action_time = G.cur_time; /* = set_next(p, 0); */ 818 p->next_action_time = G.cur_time; /* = set_next(p, 0); */
@@ -2338,18 +2361,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
2338 p->p_dotted, p->reachable_bits, timeout); 2361 p->p_dotted, p->reachable_bits, timeout);
2339 2362
2340 /* What if don't see it because it changed its IP? */ 2363 /* What if don't see it because it changed its IP? */
2341 if (p->reachable_bits == 0) { 2364 if (p->reachable_bits == 0)
2342 len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123); 2365 resolve_peer_hostname(p, /*loop_on_fail=*/ 0);
2343 if (lsa) {
2344 char *dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
2345 //if (strcmp(dotted, p->p_dotted) != 0)
2346 // bb_error_msg("peer IP changed");
2347 free(p->p_lsa);
2348 free(p->p_dotted);
2349 p->p_lsa = lsa;
2350 p->p_dotted = dotted;
2351 }
2352 }
2353 2366
2354 set_next(p, timeout); 2367 set_next(p, timeout);
2355 } 2368 }