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