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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the Regents of the University of
36 #pragma ident "%Z%%M% %I% %E% SMI"
39 * get time from remote machine
41 * gets time, obtaining value from host
42 * on the (udp, tcp)/time tli connection. Since timeserver returns
43 * with time of day in seconds since Jan 1, 1900, must
44 * subtract seconds before Jan 1, 1970 to get
51 #include <rpc/nettype.h>
55 extern int __rpc_timeval_to_msec();
58 #define debug(msg) t_error(msg)
63 #define NYEARS (1970 - 1900)
64 #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
67 * This is based upon the internet time server, but it contacts it by
68 * using TLI instead of socket.
71 rtime_tli(char *host
, struct timeval
*timep
, struct timeval
*timeout
)
75 struct nd_addrlist
*nlist
= NULL
;
76 struct nd_hostserv rpcbind_hs
;
77 struct netconfig
*nconf
= NULL
;
81 nconf
= __rpc_getconfip(timeout
== NULL
? "tcp" : "udp");
85 if ((fd
= t_open(nconf
->nc_device
, O_RDWR
, NULL
)) == -1) {
89 if (t_bind(fd
, NULL
, NULL
) < 0) {
94 /* Get the address of the rpcbind */
95 rpcbind_hs
.h_host
= host
;
96 rpcbind_hs
.h_serv
= "time";
97 /* Basically get the address of the remote machine on IP */
98 if (netdir_getbyname(nconf
, &rpcbind_hs
, &nlist
))
101 if (nconf
->nc_semantics
== NC_TPI_CLTS
) {
102 struct t_unitdata tu_data
;
107 tu_data
.addr
= *nlist
->n_addrs
;
108 tu_data
.udata
.buf
= (char *)&thetime
;
109 tu_data
.udata
.len
= (uint_t
)sizeof (thetime
);
110 tu_data
.udata
.maxlen
= tu_data
.udata
.len
;
112 tu_data
.opt
.maxlen
= 0;
113 if (t_sndudata(fd
, &tu_data
) == -1) {
118 pfd
.events
= POLLIN
| POLLPRI
| POLLRDNORM
| POLLRDBAND
;
120 msec
= __rpc_timeval_to_msec(timeout
);
122 res
= poll(&pfd
, 1, msec
);
124 if ((res
<= 0) || (pfd
.revents
& POLLNVAL
))
126 if (t_rcvudata(fd
, &tu_data
, &flag
) < 0) {
132 struct t_call sndcall
;
134 sndcall
.addr
= *nlist
->n_addrs
;
135 sndcall
.opt
.len
= sndcall
.opt
.maxlen
= 0;
136 sndcall
.udata
.len
= sndcall
.udata
.maxlen
= 0;
138 if (t_connect(fd
, &sndcall
, NULL
) == -1) {
142 if (t_rcv(fd
, (char *)&thetime
, (uint_t
)sizeof (thetime
), &flag
)
143 != (uint_t
)sizeof (thetime
)) {
150 thetime
= ntohl(thetime
);
151 timep
->tv_sec
= thetime
- TOFFSET
;
156 (void) freenetconfigent(nconf
);
160 netdir_free((char *)nlist
, ND_ADDRLIST
);