xref: /titanic_44/usr/src/lib/libnsl/rpc/rtime_tli.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 /*
37  * rtime_tli.c - get time from remote machine
38  *
39  * gets time, obtaining value from host
40  * on the (udp, tcp)/time tli connection. Since timeserver returns
41  * with time of day in seconds since Jan 1, 1900, must
42  * subtract seconds before Jan 1, 1970 to get
43  * what unix uses.
44  */
45 #include <rpc/rpc.h>
46 #include <rpc/trace.h>
47 #include <errno.h>
48 #include <sys/poll.h>
49 #include <rpc/nettype.h>
50 #include <netdir.h>
51 #include <stdio.h>
52 
53 extern int __rpc_timeval_to_msec();
54 
55 #ifdef DEBUG
56 #define	debug(msg)	t_error(msg)
57 #else
58 #define	debug(msg)
59 #endif
60 
61 #define	NYEARS	(1970 - 1900)
62 #define	TOFFSET ((u_int)60*60*24*(365*NYEARS + (NYEARS/4)))
63 
64 /*
65  * This is based upon the internet time server, but it contacts it by
66  * using TLI instead of socket.
67  */
68 int
69 rtime_tli(host, timep, timeout)
70 	char *host;
71 	struct timeval *timep;
72 	struct timeval *timeout;
73 {
74 	uint32_t thetime;
75 	int flag;
76 	struct nd_addrlist *nlist = NULL;
77 	struct nd_hostserv rpcbind_hs;
78 	struct netconfig *nconf = NULL;
79 	int foundit = 0;
80 	int fd = -1;
81 
82 	trace1(TR_rtime_tli, 0);
83 	nconf = __rpc_getconfip(timeout == NULL ? "tcp" : "udp");
84 	if (nconf == (struct netconfig *)NULL)
85 		goto error;
86 
87 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
88 		debug("open");
89 		goto error;
90 	}
91 	if (t_bind(fd, (struct t_bind *) NULL, (struct t_bind *) NULL) < 0) {
92 		debug("bind");
93 		goto error;
94 	}
95 
96 	/* Get the address of the rpcbind */
97 	rpcbind_hs.h_host = host;
98 	rpcbind_hs.h_serv = "time";
99 	/* Basically get the address of the remote machine on IP */
100 	if (netdir_getbyname(nconf, &rpcbind_hs, &nlist))
101 		goto error;
102 
103 	if (nconf->nc_semantics == NC_TPI_CLTS) {
104 		struct t_unitdata tu_data;
105 		struct pollfd pfd;
106 		int res;
107 		int msec;
108 
109 		tu_data.addr = *nlist->n_addrs;
110 		tu_data.udata.buf = (char *)&thetime;
111 		tu_data.udata.len = (u_int) sizeof (thetime);
112 		tu_data.udata.maxlen = tu_data.udata.len;
113 		tu_data.opt.len = 0;
114 		tu_data.opt.maxlen = 0;
115 		if (t_sndudata(fd, &tu_data) == -1) {
116 			debug("udp");
117 			goto error;
118 		}
119 		pfd.fd = fd;
120 		pfd.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
121 
122 		msec = __rpc_timeval_to_msec(timeout);
123 		do {
124 			res = poll(&pfd, 1, msec);
125 		} while (res < 0);
126 		if ((res <= 0) || (pfd.revents & POLLNVAL))
127 			goto error;
128 		if (t_rcvudata(fd, &tu_data, &flag) < 0) {
129 			debug("udp");
130 			goto error;
131 		}
132 		foundit = 1;
133 	} else {
134 		struct t_call sndcall;
135 
136 		sndcall.addr = *nlist->n_addrs;
137 		sndcall.opt.len = sndcall.opt.maxlen = 0;
138 		sndcall.udata.len = sndcall.udata.maxlen = 0;
139 
140 		if (t_connect(fd, &sndcall, NULL) == -1) {
141 			debug("tcp");
142 			goto error;
143 		}
144 		if (t_rcv(fd, (char *)&thetime, (u_int) sizeof (thetime), &flag)
145 				!= (u_int) sizeof (thetime)) {
146 			debug("tcp");
147 			goto error;
148 		}
149 		foundit = 1;
150 	}
151 
152 	thetime = ntohl(thetime);
153 	timep->tv_sec = thetime - TOFFSET;
154 	timep->tv_usec = 0;
155 
156 error:
157 	if (nconf) {
158 		(void) freenetconfigent(nconf);
159 		if (fd != -1) {
160 			(void) t_close(fd);
161 			if (nlist)
162 				netdir_free((char *)nlist, ND_ADDRLIST);
163 		}
164 	}
165 	trace1(TR_rtime_tli, 1);
166 	return (foundit);
167 }
168