xref: /onnv-gate/usr/src/lib/libnsl/rpc/rtime_tli.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
27*1219Sraf 
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
330Sstevel@tonic-gate  * California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
39*1219Sraf  * get time from remote machine
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * gets time, obtaining value from host
420Sstevel@tonic-gate  * on the (udp, tcp)/time tli connection. Since timeserver returns
430Sstevel@tonic-gate  * with time of day in seconds since Jan 1, 1900, must
440Sstevel@tonic-gate  * subtract seconds before Jan 1, 1970 to get
450Sstevel@tonic-gate  * what unix uses.
460Sstevel@tonic-gate  */
47*1219Sraf #include "mt.h"
480Sstevel@tonic-gate #include <rpc/rpc.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <sys/poll.h>
510Sstevel@tonic-gate #include <rpc/nettype.h>
520Sstevel@tonic-gate #include <netdir.h>
530Sstevel@tonic-gate #include <stdio.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate extern int __rpc_timeval_to_msec();
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #ifdef DEBUG
580Sstevel@tonic-gate #define	debug(msg)	t_error(msg)
590Sstevel@tonic-gate #else
600Sstevel@tonic-gate #define	debug(msg)
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	NYEARS	(1970 - 1900)
64132Srobinson #define	TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * This is based upon the internet time server, but it contacts it by
680Sstevel@tonic-gate  * using TLI instead of socket.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate int
rtime_tli(char * host,struct timeval * timep,struct timeval * timeout)71132Srobinson rtime_tli(char *host, struct timeval *timep, struct timeval *timeout)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	uint32_t thetime;
740Sstevel@tonic-gate 	int flag;
750Sstevel@tonic-gate 	struct nd_addrlist *nlist = NULL;
760Sstevel@tonic-gate 	struct nd_hostserv rpcbind_hs;
770Sstevel@tonic-gate 	struct netconfig *nconf = NULL;
780Sstevel@tonic-gate 	int foundit = 0;
790Sstevel@tonic-gate 	int fd = -1;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	nconf = __rpc_getconfip(timeout == NULL ? "tcp" : "udp");
82132Srobinson 	if (nconf == NULL)
830Sstevel@tonic-gate 		goto error;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
860Sstevel@tonic-gate 		debug("open");
870Sstevel@tonic-gate 		goto error;
880Sstevel@tonic-gate 	}
89132Srobinson 	if (t_bind(fd, NULL, NULL) < 0) {
900Sstevel@tonic-gate 		debug("bind");
910Sstevel@tonic-gate 		goto error;
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/* Get the address of the rpcbind */
950Sstevel@tonic-gate 	rpcbind_hs.h_host = host;
960Sstevel@tonic-gate 	rpcbind_hs.h_serv = "time";
970Sstevel@tonic-gate 	/* Basically get the address of the remote machine on IP */
980Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &rpcbind_hs, &nlist))
990Sstevel@tonic-gate 		goto error;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (nconf->nc_semantics == NC_TPI_CLTS) {
1020Sstevel@tonic-gate 		struct t_unitdata tu_data;
1030Sstevel@tonic-gate 		struct pollfd pfd;
1040Sstevel@tonic-gate 		int res;
1050Sstevel@tonic-gate 		int msec;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		tu_data.addr = *nlist->n_addrs;
1080Sstevel@tonic-gate 		tu_data.udata.buf = (char *)&thetime;
109132Srobinson 		tu_data.udata.len = (uint_t)sizeof (thetime);
1100Sstevel@tonic-gate 		tu_data.udata.maxlen = tu_data.udata.len;
1110Sstevel@tonic-gate 		tu_data.opt.len = 0;
1120Sstevel@tonic-gate 		tu_data.opt.maxlen = 0;
1130Sstevel@tonic-gate 		if (t_sndudata(fd, &tu_data) == -1) {
1140Sstevel@tonic-gate 			debug("udp");
1150Sstevel@tonic-gate 			goto error;
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 		pfd.fd = fd;
1180Sstevel@tonic-gate 		pfd.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		msec = __rpc_timeval_to_msec(timeout);
1210Sstevel@tonic-gate 		do {
1220Sstevel@tonic-gate 			res = poll(&pfd, 1, msec);
1230Sstevel@tonic-gate 		} while (res < 0);
1240Sstevel@tonic-gate 		if ((res <= 0) || (pfd.revents & POLLNVAL))
1250Sstevel@tonic-gate 			goto error;
1260Sstevel@tonic-gate 		if (t_rcvudata(fd, &tu_data, &flag) < 0) {
1270Sstevel@tonic-gate 			debug("udp");
1280Sstevel@tonic-gate 			goto error;
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 		foundit = 1;
1310Sstevel@tonic-gate 	} else {
1320Sstevel@tonic-gate 		struct t_call sndcall;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		sndcall.addr = *nlist->n_addrs;
1350Sstevel@tonic-gate 		sndcall.opt.len = sndcall.opt.maxlen = 0;
1360Sstevel@tonic-gate 		sndcall.udata.len = sndcall.udata.maxlen = 0;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		if (t_connect(fd, &sndcall, NULL) == -1) {
1390Sstevel@tonic-gate 			debug("tcp");
1400Sstevel@tonic-gate 			goto error;
1410Sstevel@tonic-gate 		}
142132Srobinson 		if (t_rcv(fd, (char *)&thetime, (uint_t)sizeof (thetime), &flag)
143132Srobinson 				!= (uint_t)sizeof (thetime)) {
1440Sstevel@tonic-gate 			debug("tcp");
1450Sstevel@tonic-gate 			goto error;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 		foundit = 1;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	thetime = ntohl(thetime);
1510Sstevel@tonic-gate 	timep->tv_sec = thetime - TOFFSET;
1520Sstevel@tonic-gate 	timep->tv_usec = 0;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate error:
1550Sstevel@tonic-gate 	if (nconf) {
1560Sstevel@tonic-gate 		(void) freenetconfigent(nconf);
1570Sstevel@tonic-gate 		if (fd != -1) {
1580Sstevel@tonic-gate 			(void) t_close(fd);
1590Sstevel@tonic-gate 			if (nlist)
1600Sstevel@tonic-gate 				netdir_free((char *)nlist, ND_ADDRLIST);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	return (foundit);
1640Sstevel@tonic-gate }
165