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