4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * rtime - get time from remote machine
45 * gets time, obtaining value from host
46 * on the udp/time socket. Since timeserver returns
47 * with time of day in seconds since Jan 1, 1900, must
48 * subtract seconds before Jan 1, 1970 to get
52 #include <sys/types.h>
55 #include <sys/socket.h>
58 #include <netinet/in.h>
61 #define NYEARS (1970 - 1900)
62 #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
64 extern int _socket(int, int, int);
65 extern int _sendto(int, const char *, int, int,
66 const struct sockaddr
*, int);
67 extern int _recvfrom(int, char *, int, int,
68 struct sockaddr
*, int *);
69 extern int _connect(int, struct sockaddr
*, int);
70 extern int __rpc_dtbsize();
71 extern ssize_t
read(int, void *, size_t);
72 extern int close(int);
73 static void do_close();
76 rtime(addrp
, timep
, timeout
)
77 struct sockaddr_in
*addrp
;
78 struct timeval
*timep
;
79 struct timeval
*timeout
;
85 struct sockaddr_in from
;
89 if (timeout
== NULL
) {
94 s
= _socket(AF_INET
, type
, 0);
98 addrp
->sin_family
= AF_INET
;
99 addrp
->sin_port
= htons(IPPORT_TIMESERVER
);
100 if (type
== SOCK_DGRAM
) {
101 res
= _sendto(s
, (char *)&thetime
, sizeof (thetime
), 0,
102 (struct sockaddr
*)addrp
, sizeof (*addrp
));
110 res
= select(__rpc_dtbsize(), &readfds
, NULL
,
112 } while (res
< 0 && errno
== EINTR
);
120 fromlen
= sizeof (from
);
121 res
= _recvfrom(s
, (char *)&thetime
, sizeof (thetime
), 0,
122 (struct sockaddr
*)&from
, &fromlen
);
128 if (_connect(s
, (struct sockaddr
*)addrp
,
129 sizeof (*addrp
)) < 0) {
133 res
= read(s
, (char *)&thetime
, sizeof (thetime
));
139 if (res
!= sizeof (thetime
)) {
143 thetime
= ntohl(thetime
);
145 thetime
= thetime
- TOFFSET
;
147 if (thetime
> INT32_MAX
)
150 timep
->tv_sec
= thetime
;