From abc9700cfbe39d8aa75f93c3c33661d9d9be4a04 Mon Sep 17 00:00:00 2001 From: henning <> Date: Tue, 5 Oct 2004 11:23:28 +0000 Subject: [PATCH] in client_addr_init() and client_nextaddr(), do not set up the socket and connect it, instead leave it at -1. in client_query, set up and connect the socket if it is -1. and, the real reason for this change: handle connect failures gracefully ok otto --- src/usr.sbin/ntpd/client.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index 4b0e9c2e..ca81029b 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.36 2004/09/30 10:19:43 henning Exp $ */ +/* $OpenBSD: client.c,v 1.37 2004/10/05 11:23:28 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -83,15 +83,7 @@ client_addr_init(struct ntp_peer *p) } } - if (p->addr != NULL) { - if ((p->query->fd = socket(p->addr->ss.ss_family, - SOCK_DGRAM, 0)) == -1) - fatal("client_query socket"); - if (connect(p->query->fd, (struct sockaddr *)&p->addr->ss, - p->addr->ss.ss_len) == -1) - fatal("client_query connect"); - } - + p->query->fd = -1; set_next(p, 0); return (0); @@ -111,12 +103,6 @@ client_nextaddr(struct ntp_peer *p) if ((p->addr = p->addr->next) == NULL) p->addr = p->addr_head.a; - if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) - fatal("client_query socket"); - if (connect(p->query->fd, (struct sockaddr *)&p->addr->ss, - p->addr->ss.ss_len) == -1) - fatal("client_query connect"); - p->shift = 0; p->trustlevel = TRUSTLEVEL_PATHETIC; @@ -131,6 +117,22 @@ client_query(struct ntp_peer *p) return (-1); } + if (p->query->fd == -1) { + if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, + 0)) == -1) + fatal("client_query socket"); + if (connect(p->query->fd, (struct sockaddr *)&p->addr->ss, + p->addr->ss.ss_len) == -1) { + if (errno == ECONNREFUSED || errno == ENETUNREACH || + errno == EHOSTUNREACH) { + client_nextaddr(p); + set_next(p, INTERVAL_QUERY_PATHETIC); + return (-1); + } else + fatal("client_query connect"); + } + } + /* * Send out a random 64-bit number as our transmit time. The NTP * server will copy said number into the originate field on the