]>
git.saurik.com Git - apple/shell_cmds.git/blob - date/netdate.c
1 /* $NetBSD: netdate.c,v 1.16 1998/07/28 03:47:15 mycroft Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95";
41 __RCSID("$NetBSD: netdate.c,v 1.16 1998/07/28 03:47:15 mycroft Exp $");
45 #include <sys/param.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
52 #include <protocols/timed.h>
62 #define WAITACK 2 /* seconds */
63 #define WAITDATEACK 5 /* seconds */
68 * Set the date in the machines controlled by timedaemons by communicating the
69 * new date to the local timedaemon. If the timedaemon is in the master state,
70 * it performs the correction on all slaves. If it is in the slave state, it
71 * notifies the master that a correction is needed.
72 * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
81 struct sockaddr_in sin
, dest
, from
;
84 int s
, length
, timed_ack
, found
, error
;
88 char hostname
[MAXHOSTNAMELEN
+ 1];
90 if ((sp
= getservbyname("timed", "udp")) == NULL
) {
91 warnx("udp/timed: unknown service");
95 (void)memset(&dest
, 0, sizeof(dest
));
96 dest
.sin_len
= sizeof(struct sockaddr_in
);
97 dest
.sin_family
= AF_INET
;
98 dest
.sin_port
= sp
->s_port
;
99 dest
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
100 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
102 if (errno
!= EPROTONOSUPPORT
)
108 on
= IP_PORTRANGE_LOW
;
109 if (setsockopt(s
, IPPROTO_IP
, IP_PORTRANGE
, &on
, sizeof(on
)) < 0) {
115 (void)memset(&sin
, 0, sizeof(sin
));
116 sin
.sin_len
= sizeof(struct sockaddr_in
);
117 sin
.sin_family
= AF_INET
;
118 if (bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0) {
123 msg
.tsp_type
= TSP_SETDATE
;
124 msg
.tsp_vers
= TSPVERSION
;
125 if (gethostname(hostname
, sizeof(hostname
))) {
129 hostname
[sizeof(hostname
) - 1] = '\0';
130 (void)strncpy(msg
.tsp_name
, hostname
, sizeof(hostname
));
131 msg
.tsp_seq
= htons((u_short
)0);
132 msg
.tsp_time
.tv_sec
= htonl((u_long
)tval
);
133 msg
.tsp_time
.tv_usec
= htonl((u_long
)0);
134 length
= sizeof(struct sockaddr_in
);
135 if (connect(s
, (struct sockaddr
*)&dest
, length
) < 0) {
139 if (send(s
, (char *)&msg
, sizeof(struct tsp
), 0) < 0) {
140 if (errno
!= ECONNREFUSED
)
148 tout
.tv_sec
= waittime
;
153 found
= select(FD_SETSIZE
, &ready
, (fd_set
*)0, (fd_set
*)0, &tout
);
155 length
= sizeof(error
);
157 SOL_SOCKET
, SO_ERROR
, (char *)&error
, &length
) && error
) {
158 if (error
!= ECONNREFUSED
)
159 warn("send (delayed error)");
163 if (found
> 0 && FD_ISSET(s
, &ready
)) {
164 length
= sizeof(struct sockaddr_in
);
165 if (recvfrom(s
, &msg
, sizeof(struct tsp
), 0,
166 (struct sockaddr
*)&from
, &length
) < 0) {
167 if (errno
!= ECONNREFUSED
)
171 msg
.tsp_seq
= ntohs(msg
.tsp_seq
);
172 msg
.tsp_time
.tv_sec
= ntohl(msg
.tsp_time
.tv_sec
);
173 msg
.tsp_time
.tv_usec
= ntohl(msg
.tsp_time
.tv_usec
);
174 switch (msg
.tsp_type
) {
177 waittime
= WAITDATEACK
;
183 warnx("wrong ack received from timed: %s",
184 tsptype
[msg
.tsp_type
]);
190 warnx("can't reach time daemon, time set locally");