145694Sbostic /*-
260659Sbostic * Copyright (c) 1990, 1993
360659Sbostic * The Regents of the University of California. All rights reserved.
445694Sbostic *
545694Sbostic * %sccs.include.redist.c%
645694Sbostic */
745694Sbostic
845694Sbostic #ifndef lint
9*68979Sbostic static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 04/28/95";
1045694Sbostic #endif /* not lint */
1145694Sbostic
1245694Sbostic #include <sys/param.h>
1345694Sbostic #include <sys/time.h>
1445694Sbostic #include <sys/socket.h>
1559459Sbostic
1645694Sbostic #include <netinet/in.h>
1745694Sbostic #include <netdb.h>
1845694Sbostic #define TSPTYPES
1945694Sbostic #include <protocols/timed.h>
2059459Sbostic
2159459Sbostic #include <err.h>
2259459Sbostic #include <errno.h>
2345694Sbostic #include <stdio.h>
2446650Sbostic #include <string.h>
2559459Sbostic #include <unistd.h>
2645694Sbostic
2759459Sbostic #include "extern.h"
2859459Sbostic
2945694Sbostic #define WAITACK 2 /* seconds */
3045694Sbostic #define WAITDATEACK 5 /* seconds */
3145694Sbostic
3245694Sbostic extern int retval;
3345694Sbostic
3445694Sbostic /*
3545694Sbostic * Set the date in the machines controlled by timedaemons by communicating the
3645694Sbostic * new date to the local timedaemon. If the timedaemon is in the master state,
3745694Sbostic * it performs the correction on all slaves. If it is in the slave state, it
3845694Sbostic * notifies the master that a correction is needed.
3945694Sbostic * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
4045694Sbostic */
4159459Sbostic int
netsettime(tval)4245694Sbostic netsettime(tval)
4345694Sbostic time_t tval;
4445694Sbostic {
4545694Sbostic struct timeval tout;
4645694Sbostic struct servent *sp;
4745694Sbostic struct tsp msg;
4845694Sbostic struct sockaddr_in sin, dest, from;
4945694Sbostic fd_set ready;
5045694Sbostic long waittime;
5145694Sbostic int s, length, port, timed_ack, found, err;
5245694Sbostic char hostname[MAXHOSTNAMELEN];
5345694Sbostic
5445694Sbostic if ((sp = getservbyname("timed", "udp")) == NULL) {
5559459Sbostic warnx("udp/timed: unknown service");
5645694Sbostic return (retval = 2);
5745694Sbostic }
5845694Sbostic
59*68979Sbostic memset(&dest, 0, sizeof(dest));
6045694Sbostic dest.sin_port = sp->s_port;
6145694Sbostic dest.sin_family = AF_INET;
6245694Sbostic dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
6345694Sbostic s = socket(AF_INET, SOCK_DGRAM, 0);
6445694Sbostic if (s < 0) {
6545694Sbostic if (errno != EPROTONOSUPPORT)
6659459Sbostic warn("timed");
6759459Sbostic return (retval = 2);
6845694Sbostic }
6945694Sbostic
7059459Sbostic memset(&sin, 0, sizeof(sin));
7145694Sbostic sin.sin_family = AF_INET;
7245694Sbostic for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
7345694Sbostic sin.sin_port = htons((u_short)port);
7445694Sbostic if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
7545694Sbostic break;
7645694Sbostic if (errno == EADDRINUSE)
7745694Sbostic continue;
7845694Sbostic if (errno != EADDRNOTAVAIL)
7959459Sbostic warn("bind");
8045694Sbostic goto bad;
8145694Sbostic }
8245694Sbostic if (port == IPPORT_RESERVED / 2) {
8359459Sbostic warnx("all ports in use");
8445694Sbostic goto bad;
8545694Sbostic }
8645694Sbostic msg.tsp_type = TSP_SETDATE;
8745694Sbostic msg.tsp_vers = TSPVERSION;
8845694Sbostic if (gethostname(hostname, sizeof(hostname))) {
8959459Sbostic warn("gethostname");
9045694Sbostic goto bad;
9145694Sbostic }
9245694Sbostic (void)strncpy(msg.tsp_name, hostname, sizeof(hostname));
9345694Sbostic msg.tsp_seq = htons((u_short)0);
9445694Sbostic msg.tsp_time.tv_sec = htonl((u_long)tval);
9545694Sbostic msg.tsp_time.tv_usec = htonl((u_long)0);
9645694Sbostic length = sizeof(struct sockaddr_in);
9746650Sbostic if (connect(s, (struct sockaddr *)&dest, length) < 0) {
9859459Sbostic warn("connect");
9945694Sbostic goto bad;
10045694Sbostic }
10145694Sbostic if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
10245694Sbostic if (errno != ECONNREFUSED)
10359459Sbostic warn("send");
10445694Sbostic goto bad;
10545694Sbostic }
10645694Sbostic
10745694Sbostic timed_ack = -1;
10845694Sbostic waittime = WAITACK;
10945694Sbostic loop:
11045694Sbostic tout.tv_sec = waittime;
11145694Sbostic tout.tv_usec = 0;
11245694Sbostic
11345694Sbostic FD_ZERO(&ready);
11445694Sbostic FD_SET(s, &ready);
11545694Sbostic found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
11645694Sbostic
11745694Sbostic length = sizeof(err);
11859459Sbostic if (!getsockopt(s,
11959459Sbostic SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) {
12045694Sbostic if (err != ECONNREFUSED)
12159459Sbostic warn("send (delayed error)");
12245694Sbostic goto bad;
12345694Sbostic }
12445694Sbostic
12545694Sbostic if (found > 0 && FD_ISSET(s, &ready)) {
12645694Sbostic length = sizeof(struct sockaddr_in);
12746650Sbostic if (recvfrom(s, &msg, sizeof(struct tsp), 0,
12846650Sbostic (struct sockaddr *)&from, &length) < 0) {
12945694Sbostic if (errno != ECONNREFUSED)
13059459Sbostic warn("recvfrom");
13145694Sbostic goto bad;
13245694Sbostic }
13345694Sbostic msg.tsp_seq = ntohs(msg.tsp_seq);
13445694Sbostic msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
13545694Sbostic msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
13645694Sbostic switch (msg.tsp_type) {
13745694Sbostic case TSP_ACK:
13845694Sbostic timed_ack = TSP_ACK;
13945694Sbostic waittime = WAITDATEACK;
14045694Sbostic goto loop;
14145694Sbostic case TSP_DATEACK:
14245694Sbostic (void)close(s);
14345694Sbostic return (0);
14445694Sbostic default:
14559459Sbostic warnx("wrong ack received from timed: %s",
14645694Sbostic tsptype[msg.tsp_type]);
14745694Sbostic timed_ack = -1;
14845694Sbostic break;
14945694Sbostic }
15045694Sbostic }
15145694Sbostic if (timed_ack == -1)
15259459Sbostic warnx("can't reach time daemon, time set locally");
15345694Sbostic
15445694Sbostic bad:
15545694Sbostic (void)close(s);
15659459Sbostic return (retval = 2);
15745694Sbostic }
158