netdate.c revision 1.13 1 /* $NetBSD: netdate.c,v 1.13 1998/01/10 00:27:34 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: netdate.c,v 1.13 1998/01/10 00:27:34 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
48
49 #include <netinet/in.h>
50 #include <netdb.h>
51 #define TSPTYPES
52 #include <protocols/timed.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include "extern.h"
61
62 #define WAITACK 2 /* seconds */
63 #define WAITDATEACK 5 /* seconds */
64
65 extern int retval;
66
67 /*
68 * Set the date in the machines controlled by timedaemons by communicating the
69 * new date to the local timedaemon. If the timedaemon is in the master state,
70 * it performs the correction on all slaves. If it is in the slave state, it
71 * notifies the master that a correction is needed.
72 * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
73 */
74 int
75 netsettime(tval)
76 time_t tval;
77 {
78 struct timeval tout;
79 struct servent *sp;
80 struct tsp msg;
81 struct sockaddr_in sin, dest, from;
82 fd_set ready;
83 long waittime;
84 int s, length, on, timed_ack, found, err;
85 char hostname[MAXHOSTNAMELEN];
86
87 if ((sp = getservbyname("timed", "udp")) == NULL) {
88 warnx("udp/timed: unknown service");
89 return (retval = 2);
90 }
91
92 (void)memset(&dest, 0, sizeof(dest));
93 dest.sin_len = sizeof(struct sockaddr_in);
94 dest.sin_family = AF_INET;
95 dest.sin_port = sp->s_port;
96 dest.sin_addr.s_addr = htonl(INADDR_ANY);
97 s = socket(AF_INET, SOCK_DGRAM, 0);
98 if (s < 0) {
99 if (errno != EPROTONOSUPPORT)
100 warn("timed");
101 return (retval = 2);
102 }
103
104 on = IP_PORTRANGE_LOW;
105 if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on)) < 0) {
106 warn("setsockopt");
107 goto bad;
108 }
109
110 (void)memset(&sin, 0, sizeof(sin));
111 sin.sin_len = sizeof(struct sockaddr_in);
112 sin.sin_family = AF_INET;
113 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
114 warn("bind");
115 goto bad;
116 }
117
118 msg.tsp_type = TSP_SETDATE;
119 msg.tsp_vers = TSPVERSION;
120 if (gethostname(hostname, sizeof(hostname))) {
121 warn("gethostname");
122 goto bad;
123 }
124 (void)strncpy(msg.tsp_name, hostname, sizeof(hostname));
125 msg.tsp_seq = htons((u_short)0);
126 msg.tsp_time.tv_sec = htonl((u_long)tval);
127 msg.tsp_time.tv_usec = htonl((u_long)0);
128 length = sizeof(struct sockaddr_in);
129 if (connect(s, (struct sockaddr *)&dest, length) < 0) {
130 warn("connect");
131 goto bad;
132 }
133 if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
134 if (errno != ECONNREFUSED)
135 warn("send");
136 goto bad;
137 }
138
139 timed_ack = -1;
140 waittime = WAITACK;
141 loop:
142 tout.tv_sec = waittime;
143 tout.tv_usec = 0;
144
145 FD_ZERO(&ready);
146 FD_SET(s, &ready);
147 found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
148
149 length = sizeof(err);
150 if (!getsockopt(s,
151 SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) {
152 if (err != ECONNREFUSED)
153 warn("send (delayed error)");
154 goto bad;
155 }
156
157 if (found > 0 && FD_ISSET(s, &ready)) {
158 length = sizeof(struct sockaddr_in);
159 if (recvfrom(s, &msg, sizeof(struct tsp), 0,
160 (struct sockaddr *)&from, &length) < 0) {
161 if (errno != ECONNREFUSED)
162 warn("recvfrom");
163 goto bad;
164 }
165 msg.tsp_seq = ntohs(msg.tsp_seq);
166 msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
167 msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
168 switch (msg.tsp_type) {
169 case TSP_ACK:
170 timed_ack = TSP_ACK;
171 waittime = WAITDATEACK;
172 goto loop;
173 case TSP_DATEACK:
174 (void)close(s);
175 return (0);
176 default:
177 warnx("wrong ack received from timed: %s",
178 tsptype[msg.tsp_type]);
179 timed_ack = -1;
180 break;
181 }
182 }
183 if (timed_ack == -1)
184 warnx("can't reach time daemon, time set locally");
185
186 bad:
187 (void)close(s);
188 return (retval = 2);
189 }
190