diff options
-rw-r--r-- | usr.sbin/rdate/ntp.c | 72 | ||||
-rw-r--r-- | usr.sbin/rdate/rfc868time.c | 51 |
2 files changed, 57 insertions, 66 deletions
diff --git a/usr.sbin/rdate/ntp.c b/usr.sbin/rdate/ntp.c index 041afa6d66b..8878eefc298 100644 --- a/usr.sbin/rdate/ntp.c +++ b/usr.sbin/rdate/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.4 2002/05/16 15:01:32 jakob Exp $ */ +/* $OpenBSD: ntp.c,v 1.5 2002/05/16 21:05:24 jakob Exp $ */ /* * Copyright (c) 1996, 1997 by N.M. Maclaren. All rights reserved. @@ -112,51 +112,41 @@ void create_timeval(double, struct timeval *, struct timeval *); void ntp_client(const char *hostname, struct timeval *new, struct timeval *adjust) { - struct sockaddr_in server, peer; - struct protoent *pp, ppp; - struct servent *sp, ssp; - struct hostent *hp; + struct addrinfo hints, *res0, *res; double offset, error; int packets = 0, s; - if ((hp = gethostbyname(hostname)) == NULL) - errx(1, "%s: %s", hostname, hstrerror(h_errno)); - - if ((sp = getservbyname("ntp", "udp")) == NULL) { - sp = &ssp; - sp->s_port = 123; - sp->s_proto = "udp"; - } - - if ((pp = getprotobyname(sp->s_proto)) == NULL) { - pp = &ppp; - pp->p_proto = 17; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; + error = getaddrinfo(hostname, "ntp", &hints, &res0); + if (error) { + errx(1, "%s: %s", hostname, gai_strerror(error)); + /*NOTREACHED*/ } - if ((s = socket(AF_INET, SOCK_DGRAM, pp->p_proto)) == -1) - err(1, "Could not create socket"); - - bzero(&peer, sizeof(peer)); - peer.sin_family = AF_INET; - peer.sin_port = sp->s_port; - (void) memcpy(&(peer.sin_addr.s_addr), hp->h_addr, hp->h_length); - - bzero(&server, sizeof(server)); - server.sin_family = AF_INET; - server.sin_addr.s_addr = INADDR_ANY; - - if (bind(s, (struct sockaddr *) &server, sizeof(server))) - err(1, "Could not bind to socket"); - - packets = sync_ntp(s, (struct sockaddr *) &peer, &offset, &error); + s = -1; + for (res = res0; res; res = res->ai_next) { + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (s < 0) + continue; - close(s); - - if (packets == 0) - errx(1, "No acceptable packets received"); - - if (error > NTP_INSANITY) - errx(1, "Unable to get a reasonable time estimate"); + packets = sync_ntp(s, res->ai_addr, &offset, &error); + if (packets == 0) { +#ifdef DEBUG + fprintf(stderr, "try the next address\n"); +#endif + close(s); + s = -1; + continue; + } + if (error > NTP_INSANITY) { + /* should we try the next address instead? */ + errx(1, "Unable to get a reasonable time estimate"); + } + break; + } + freeaddrinfo(res0); #ifdef DEBUG fprintf(stderr,"Correction: %.6f +/- %.6f\n", offset,error); @@ -246,7 +236,7 @@ write_packet(int fd, const struct sockaddr *peer, struct ntp_data *data) int length; pack_ntp(transmit, NTP_PACKET_MIN, data); - length = sendto(fd, transmit, NTP_PACKET_MIN, 0, peer, sizeof(*peer)); + length = sendto(fd, transmit, NTP_PACKET_MIN, 0, peer, peer->sa_len); if (length <= 0) { warnx("Unable to send NTP packet to server"); return 1; diff --git a/usr.sbin/rdate/rfc868time.c b/usr.sbin/rdate/rfc868time.c index ce316f6cbe0..dabf97ec784 100644 --- a/usr.sbin/rdate/rfc868time.c +++ b/usr.sbin/rdate/rfc868time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rfc868time.c,v 1.2 2002/05/16 11:00:53 deraadt Exp $ */ +/* $OpenBSD: rfc868time.c,v 1.3 2002/05/16 21:05:24 jakob Exp $ */ /* $NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $ */ /* @@ -42,7 +42,7 @@ #if 0 from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $"; #else -static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.2 2002/05/16 11:00:53 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.3 2002/05/16 21:05:24 jakob Exp $"; #endif #endif /* lint */ @@ -68,37 +68,38 @@ void rfc868time_client (const char *hostname, struct timeval *new, struct timeval *adjust) { - struct protoent *pp, ppp; - struct servent *sp, ssp; - struct sockaddr_in sin; - struct hostent *hp; + struct addrinfo hints, *res0, *res; struct timeval old; time_t tim; int s; - - if ((hp = gethostbyname(hostname)) == NULL) - errx(1, "%s: %s", hostname, hstrerror(h_errno)); - - if ((sp = getservbyname("time", "tcp")) == NULL) { - sp = &ssp; - sp->s_port = 37; - sp->s_proto = "tcp"; + int error; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + error = getaddrinfo(hostname, "time", &hints, &res0); + if (error) { + errx(1, "%s: %s", hostname, gai_strerror(error)); + /*NOTREACHED*/ } - if ((pp = getprotobyname(sp->s_proto)) == NULL) { - pp = &ppp; - pp->p_proto = 6; - } - if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1) - err(1, "Could not create socket"); - bzero(&sin, sizeof sin); - sin.sin_family = AF_INET; - sin.sin_port = sp->s_port; + s = -1; + for (res = res0; res; res = res->ai_next) { + s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (s < 0) + continue; - (void) memcpy(&(sin.sin_addr.s_addr), hp->h_addr, hp->h_length); + if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { + close(s); + s = -1; + continue; + } - if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) + break; + } + if (s < 0) err(1, "Could not connect socket"); + freeaddrinfo(res0); if (read(s, &tim, sizeof(time_t)) != sizeof(time_t)) err(1, "Could not read data"); |