1 /* $OpenBSD: rfc868time.c,v 1.13 2023/01/04 13:00:11 jsg 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * rdate.c: Set the date from the specified host 31 * 32 * Uses the rfc868 time protocol at socket 37 (tcp). 33 * Time is returned as the number of seconds since 34 * midnight January 1st 1900. 35 */ 36 37 #include <sys/socket.h> 38 #include <sys/time.h> 39 #include <netinet/in.h> 40 41 #include <stdio.h> 42 #include <ctype.h> 43 #include <err.h> 44 #include <string.h> 45 #include <netdb.h> 46 #include <unistd.h> 47 #include <time.h> 48 49 /* Obviously it is not just for SNTP clients... */ 50 #include "ntpleaps.h" 51 52 /* seconds from midnight Jan 1900 - 1970 */ 53 #define DIFFERENCE 2208988800UL 54 55 void 56 rfc868time_client(const char *hostname, int family, struct timeval *new, 57 struct timeval *adjust, int leapflag); 58 59 60 void 61 rfc868time_client(const char *hostname, int family, struct timeval *new, 62 struct timeval *adjust, int leapflag) 63 { 64 struct addrinfo hints, *res0, *res; 65 struct timeval old; 66 u_int32_t tim; /* RFC 868 states clearly this is an uint32 */ 67 int s; 68 int error; 69 u_int64_t td; 70 71 memset(&hints, 0, sizeof(hints)); 72 hints.ai_family = family; 73 hints.ai_socktype = SOCK_STREAM; 74 error = getaddrinfo(hostname, "time", &hints, &res0); 75 if (error) { 76 errx(1, "%s: %s", hostname, gai_strerror(error)); 77 /*NOTREACHED*/ 78 } 79 80 if (pledge("stdio inet", NULL) == -1) 81 err(1, "pledge"); 82 83 s = -1; 84 for (res = res0; res; res = res->ai_next) { 85 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 86 if (s == -1) 87 continue; 88 89 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) { 90 close(s); 91 s = -1; 92 continue; 93 } 94 95 break; 96 } 97 if (s == -1) 98 err(1, "Could not connect socket"); 99 freeaddrinfo(res0); 100 101 if (read(s, &tim, sizeof(tim)) != sizeof(tim)) 102 err(1, "Could not read data"); 103 104 (void) close(s); 105 tim = ntohl(tim) - DIFFERENCE; 106 107 if (gettimeofday(&old, NULL) == -1) 108 err(1, "Could not get local time of day"); 109 110 td = SEC_TO_TAI64(old.tv_sec); 111 if (leapflag) 112 ntpleaps_sub(&td); 113 114 adjust->tv_sec = tim - TAI64_TO_SEC(td); 115 adjust->tv_usec = 0; 116 117 new->tv_sec = old.tv_sec + adjust->tv_sec; 118 new->tv_usec = 0; 119 } 120