1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 04/28/95";
10 #endif /* not lint */
11
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/socket.h>
15
16 #include <netinet/in.h>
17 #include <netdb.h>
18 #define TSPTYPES
19 #include <protocols/timed.h>
20
21 #include <err.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "extern.h"
28
29 #define WAITACK 2 /* seconds */
30 #define WAITDATEACK 5 /* seconds */
31
32 extern int retval;
33
34 /*
35 * Set the date in the machines controlled by timedaemons by communicating the
36 * new date to the local timedaemon. If the timedaemon is in the master state,
37 * it performs the correction on all slaves. If it is in the slave state, it
38 * notifies the master that a correction is needed.
39 * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
40 */
41 int
netsettime(tval)42 netsettime(tval)
43 time_t tval;
44 {
45 struct timeval tout;
46 struct servent *sp;
47 struct tsp msg;
48 struct sockaddr_in sin, dest, from;
49 fd_set ready;
50 long waittime;
51 int s, length, port, timed_ack, found, err;
52 char hostname[MAXHOSTNAMELEN];
53
54 if ((sp = getservbyname("timed", "udp")) == NULL) {
55 warnx("udp/timed: unknown service");
56 return (retval = 2);
57 }
58
59 memset(&dest, 0, sizeof(dest));
60 dest.sin_port = sp->s_port;
61 dest.sin_family = AF_INET;
62 dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
63 s = socket(AF_INET, SOCK_DGRAM, 0);
64 if (s < 0) {
65 if (errno != EPROTONOSUPPORT)
66 warn("timed");
67 return (retval = 2);
68 }
69
70 memset(&sin, 0, sizeof(sin));
71 sin.sin_family = AF_INET;
72 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
73 sin.sin_port = htons((u_short)port);
74 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
75 break;
76 if (errno == EADDRINUSE)
77 continue;
78 if (errno != EADDRNOTAVAIL)
79 warn("bind");
80 goto bad;
81 }
82 if (port == IPPORT_RESERVED / 2) {
83 warnx("all ports in use");
84 goto bad;
85 }
86 msg.tsp_type = TSP_SETDATE;
87 msg.tsp_vers = TSPVERSION;
88 if (gethostname(hostname, sizeof(hostname))) {
89 warn("gethostname");
90 goto bad;
91 }
92 (void)strncpy(msg.tsp_name, hostname, sizeof(hostname));
93 msg.tsp_seq = htons((u_short)0);
94 msg.tsp_time.tv_sec = htonl((u_long)tval);
95 msg.tsp_time.tv_usec = htonl((u_long)0);
96 length = sizeof(struct sockaddr_in);
97 if (connect(s, (struct sockaddr *)&dest, length) < 0) {
98 warn("connect");
99 goto bad;
100 }
101 if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
102 if (errno != ECONNREFUSED)
103 warn("send");
104 goto bad;
105 }
106
107 timed_ack = -1;
108 waittime = WAITACK;
109 loop:
110 tout.tv_sec = waittime;
111 tout.tv_usec = 0;
112
113 FD_ZERO(&ready);
114 FD_SET(s, &ready);
115 found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
116
117 length = sizeof(err);
118 if (!getsockopt(s,
119 SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) {
120 if (err != ECONNREFUSED)
121 warn("send (delayed error)");
122 goto bad;
123 }
124
125 if (found > 0 && FD_ISSET(s, &ready)) {
126 length = sizeof(struct sockaddr_in);
127 if (recvfrom(s, &msg, sizeof(struct tsp), 0,
128 (struct sockaddr *)&from, &length) < 0) {
129 if (errno != ECONNREFUSED)
130 warn("recvfrom");
131 goto bad;
132 }
133 msg.tsp_seq = ntohs(msg.tsp_seq);
134 msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
135 msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
136 switch (msg.tsp_type) {
137 case TSP_ACK:
138 timed_ack = TSP_ACK;
139 waittime = WAITDATEACK;
140 goto loop;
141 case TSP_DATEACK:
142 (void)close(s);
143 return (0);
144 default:
145 warnx("wrong ack received from timed: %s",
146 tsptype[msg.tsp_type]);
147 timed_ack = -1;
148 break;
149 }
150 }
151 if (timed_ack == -1)
152 warnx("can't reach time daemon, time set locally");
153
154 bad:
155 (void)close(s);
156 return (retval = 2);
157 }
158