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