Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 69103d6

Browse files
bwardenbryteise
authored andcommitted
Wait up to 100 seconds for hostname lookup
In case networking takes as long as a minute to come up, give hostname lookup up to 100 seconds to complete. Give initial HTTP request up to 120 seconds _from when we started_ to succeed, so even if we spent all our time waiting on DNS, we still give 20 more seconds for the HTTP server.
1 parent 1ac420e commit 69103d6

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

src/ucd-data-fetch.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,40 @@ int main(int argc, char *argv[]) {
266266
server.sin_addr.s_addr = inet_addr(config[conf].ip);
267267
server.sin_port = htons(config[conf].port);
268268

269+
struct timespec ts;
270+
ts.tv_sec = 0;
271+
ts.tv_nsec = 50000000;
272+
269273
/* Do we need to look up a hostname? */
270274
if ((int) server.sin_addr.s_addr == -1) {
271-
struct hostent *hp = gethostbyname(config[conf].ip);
272-
if (!hp || hp->h_length <= 0) {
273-
FAIL("gethostbyname()");
274-
}
275+
n = 0;
276+
for (;;) {
277+
struct hostent *hp = gethostbyname(config[conf].ip);
278+
if (hp != NULL) {
279+
if (hp->h_length > 0) {
280+
/* Got it; use the resulting IP address */
281+
server.sin_family = (short unsigned int) (hp->h_addrtype & 0xFFFF);
282+
memcpy(&(server.sin_addr.s_addr), hp->h_addr, (size_t) hp->h_length);
283+
break;
284+
}
285+
else {
286+
fprintf(stderr, "gethostbyname(): empty response");
287+
exit(EXIT_FAILURE);
288+
}
289+
}
275290

276-
/* Got it; use the resulting IP address */
277-
server.sin_family = (short unsigned int) (hp->h_addrtype & 0xFFFF);
278-
memcpy(&(server.sin_addr.s_addr), hp->h_addr, (size_t) hp->h_length);
291+
if ((h_errno != TRY_AGAIN) && (h_errno != NO_RECOVERY)) {
292+
herror("gethostbyname()");
293+
exit(EXIT_FAILURE);
294+
}
295+
nanosleep(&ts, NULL);
296+
if (++n > 2000) { /* 100 secs */
297+
herror("gethostbyname()");
298+
exit(EXIT_FAILURE);
299+
}
300+
}
279301
}
280302

281-
struct timespec ts;
282-
ts.tv_sec = 0;
283-
ts.tv_nsec = 50000000;
284-
285303
for (;;) {
286304
int r = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
287305
if (r == 0) {
@@ -291,7 +309,7 @@ int main(int argc, char *argv[]) {
291309
FAIL("connect()");
292310
}
293311
nanosleep(&ts, NULL);
294-
if (++n > 200) { /* 10 secs */
312+
if (++n > 2400) { /* 120 secs - any used up in gethostbyname */
295313
FAIL("timeout in connect()");
296314
}
297315
}

0 commit comments

Comments
 (0)