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