xref: /openbsd-src/usr.sbin/rdate/rfc868time.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: rfc868time.c,v 1.6 2004/02/16 21:25:41 jakob Exp $	*/
2 /*	$NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $	*/
3 
4 /*
5  * Copyright (c) 1994 Christos Zoulas
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Christos Zoulas.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * rdate.c: Set the date from the specified host
36  *
37  *	Uses the rfc868 time protocol at socket 37 (tcp).
38  *	Time is returned as the number of seconds since
39  *	midnight January 1st 1900.
40  */
41 
42 #ifndef lint
43 #if 0
44 from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $";
45 #else
46 static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.6 2004/02/16 21:25:41 jakob Exp $";
47 #endif
48 #endif				/* lint */
49 
50 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <sys/time.h>
53 #include <netinet/in.h>
54 
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <string.h>
59 #include <netdb.h>
60 #include <unistd.h>
61 #include <time.h>
62 
63 /* Obviously it is not just for SNTP clients... */
64 #include "ntpleaps.h"
65 
66 /* seconds from midnight Jan 1900 - 1970 */
67 #define DIFFERENCE 2208988800UL
68 
69 
70 void
71 rfc868time_client (const char *hostname, int family, struct timeval *new,
72     struct timeval *adjust, int leapflag)
73 {
74 	struct addrinfo hints, *res0, *res;
75 	struct timeval old;
76 	u_int32_t tim;	/* RFC 868 states clearly this is an uint32 */
77 	int s;
78 	int error;
79 	u_int64_t td;
80 
81 	memset(&hints, 0, sizeof(hints));
82 	hints.ai_family = family;
83 	hints.ai_socktype = SOCK_STREAM;
84 	/* XXX what about rfc868 UDP
85 	 * probably not due to the Y2038 issue  -mirabile */
86 	error = getaddrinfo(hostname, "time", &hints, &res0);
87 	if (error) {
88 		errx(1, "%s: %s", hostname, gai_strerror(error));
89 		/*NOTREACHED*/
90 	}
91 
92 	s = -1;
93 	for (res = res0; res; res = res->ai_next) {
94 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
95 		if (s < 0)
96 			continue;
97 
98 		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
99 			close(s);
100 			s = -1;
101 			continue;
102 		}
103 
104 		break;
105 	}
106 	if (s < 0)
107 		err(1, "Could not connect socket");
108 	freeaddrinfo(res0);
109 
110 	if (read(s, &tim, sizeof(tim)) != sizeof(tim))
111 		err(1, "Could not read data");
112 
113 	(void) close(s);
114 	tim = ntohl(tim) - DIFFERENCE;
115 
116 	if (gettimeofday(&old, NULL) == -1)
117 		err(1, "Could not get local time of day");
118 
119 	td = SEC_TO_TAI64(old.tv_sec);
120 	if (leapflag)
121 		ntpleaps_sub(&td);
122 
123 	adjust->tv_sec = tim - TAI64_TO_SEC(td);
124 	adjust->tv_usec = 0;
125 
126 	new->tv_sec = old.tv_sec + adjust->tv_sec;
127 	new->tv_usec = 0;
128 }
129