aboutsummaryrefslogtreecommitdiffstats
path: root/main/busybox/4007-ntpd-postpone-hostname-resolution-if-fails-on-startu.patch
blob: 81f4ef8c92543012f211783a286315a68dea8ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
From 52278b8df9433b2e32d2d842637024b42fcf1a94 Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
Date: Fri, 13 May 2016 00:00:11 +0300
Subject: [PATCH 4007/4007] ntpd: postpone hostname resolution if fails on
 startup

Signed-off-by: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
---
 networking/ntpd.c | 116 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 63 insertions(+), 53 deletions(-)

diff --git a/networking/ntpd.c b/networking/ntpd.c
index 4103189..7f7d69e 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -765,34 +765,48 @@ reset_peer_stats(peer_t *p, double offset)
 }
 
 static void
+resolve_peer_hostname(peer_t *p) {
+	len_and_sockaddr *lsa = host2sockaddr(p->p_hostname, 123);
+	if (lsa) {
+		if (p->p_lsa) {
+			free(p->p_lsa);
+			free(p->p_dotted);
+		}
+		p->p_lsa = lsa;
+		p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
+	}
+	set_next(p, lsa ? 0 : RETRY_INTERVAL);
+}
+
+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->p_hostname = xstrdup(s);
+	resolve_peer_hostname(p);
 
 	/* Names like N.<country2chars>.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 (p->p_lsa)
+		for (item = G.ntp_peers; item != NULL; item = item->link) {
+			peer_t *pp = (peer_t *) item->data;
+			if (pp->p_lsa && strcmp(p->p_dotted, pp->p_dotted) == 0) {
+				bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted);
+				free(p->p_hostname);
+				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);
@@ -2317,54 +2331,50 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 		for (item = G.ntp_peers; item != NULL; item = item->link) {
 			peer_t *p = (peer_t *) item->data;
 
-			if (p->next_action_time <= G.cur_time) {
-				if (p->p_fd == -1) {
-					/* Time to send new req */
-					if (--cnt == 0) {
-						VERB4 bb_error_msg("disabling burst mode");
-						G.polladj_count = 0;
-						G.poll_exp = MINPOLL;
-					}
-					send_query_to_peer(p);
-				} else {
-					/* Timed out waiting for reply */
-					close(p->p_fd);
-					p->p_fd = -1;
-					/* If poll interval is small, increase it */
-					if (G.poll_exp < BIGPOLL)
-						adjust_poll(MINPOLL);
-					timeout = poll_interval(NOREPLY_INTERVAL);
-					bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us",
-							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->p_lsa) {
+
+				if (p->next_action_time <= G.cur_time) {
+					if (p->p_fd == -1) {
+						/* Time to send new req */
+						if (--cnt == 0) {
+							VERB4 bb_error_msg("disabling burst mode");
+							G.polladj_count = 0;
+							G.poll_exp = MINPOLL;
 						}
+						send_query_to_peer(p);
+					} else {
+						/* Timed out waiting for reply */
+						close(p->p_fd);
+						p->p_fd = -1;
+						/* If poll interval is small, increase it */
+						if (G.poll_exp < BIGPOLL)
+							adjust_poll(MINPOLL);
+						timeout = poll_interval(NOREPLY_INTERVAL);
+						bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us",
+								p->p_dotted, p->reachable_bits, timeout);
+
+						/* What if don't see it because it changed its IP? */
+						if (p->reachable_bits == 0)
+							resolve_peer_hostname(p);
+
+						set_next(p, timeout);
 					}
+				}
 
-					set_next(p, timeout);
+				if (p->p_fd >= 0) {
+					/* Wait for reply from this peer */
+					pfd[i].fd = p->p_fd;
+					pfd[i].events = POLLIN;
+					idx2peer[i] = p;
+					i++;
 				}
-			}
+
+			} else
+				resolve_peer_hostname(p);
 
 			if (p->next_action_time < nextaction)
 				nextaction = p->next_action_time;
 
-			if (p->p_fd >= 0) {
-				/* Wait for reply from this peer */
-				pfd[i].fd = p->p_fd;
-				pfd[i].events = POLLIN;
-				idx2peer[i] = p;
-				i++;
-			}
 		}
 
 		timeout = nextaction - G.cur_time;
-- 
2.8.3