xref: /freebsd/lib/libc/rpc/rtime.c (revision 720138bbdef2c8fb69971f16e9834bdd44dca316)
1e8636dfdSBill Paul /*
2e8636dfdSBill Paul  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3e8636dfdSBill Paul  * unrestricted use provided that this legend is included on all tape
4e8636dfdSBill Paul  * media and as a part of the software program in whole or part.  Users
5e8636dfdSBill Paul  * may copy or modify Sun RPC without charge, but are not authorized
6e8636dfdSBill Paul  * to license or distribute it to anyone else except as part of a product or
7e8636dfdSBill Paul  * program developed by the user.
8e8636dfdSBill Paul  *
9e8636dfdSBill Paul  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10e8636dfdSBill Paul  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11e8636dfdSBill Paul  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12e8636dfdSBill Paul  *
13e8636dfdSBill Paul  * Sun RPC is provided with no support and without any obligation on the
14e8636dfdSBill Paul  * part of Sun Microsystems, Inc. to assist in its use, correction,
15e8636dfdSBill Paul  * modification or enhancement.
16e8636dfdSBill Paul  *
17e8636dfdSBill Paul  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18e8636dfdSBill Paul  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19e8636dfdSBill Paul  * OR ANY PART THEREOF.
20e8636dfdSBill Paul  *
21e8636dfdSBill Paul  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22e8636dfdSBill Paul  * or profits or other special, indirect and consequential damages, even if
23e8636dfdSBill Paul  * Sun has been advised of the possibility of such damages.
24e8636dfdSBill Paul  *
25e8636dfdSBill Paul  * Sun Microsystems, Inc.
26e8636dfdSBill Paul  * 2550 Garcia Avenue
27e8636dfdSBill Paul  * Mountain View, California  94043
28e8636dfdSBill Paul  */
29e8636dfdSBill Paul 
30e8636dfdSBill Paul /*
31e8636dfdSBill Paul  * Copyright (c) 1988 by Sun Microsystems, Inc.
32e8636dfdSBill Paul 
33e8636dfdSBill Paul  */
34e8636dfdSBill Paul 
35e8636dfdSBill Paul /*
36e8636dfdSBill Paul  * rtime - get time from remote machine
37e8636dfdSBill Paul  *
38e8636dfdSBill Paul  * gets time, obtaining value from host
39e8636dfdSBill Paul  * on the udp/time socket.  Since timeserver returns
40e8636dfdSBill Paul  * with time of day in seconds since Jan 1, 1900,  must
41e8636dfdSBill Paul  * subtract seconds before Jan 1, 1970 to get
42e8636dfdSBill Paul  * what unix uses.
43e8636dfdSBill Paul  */
44d201fe46SDaniel Eischen #include "namespace.h"
45e8636dfdSBill Paul #include <stdlib.h>
46e8636dfdSBill Paul #include <string.h>
47e8636dfdSBill Paul #include <unistd.h>
48e8636dfdSBill Paul #include <errno.h>
49e8636dfdSBill Paul #include <sys/types.h>
50e8636dfdSBill Paul #include <sys/socket.h>
51e8636dfdSBill Paul #include <sys/time.h>
52e8636dfdSBill Paul #include <netinet/in.h>
53e8636dfdSBill Paul #include <stdio.h>
54e8636dfdSBill Paul #include <netdb.h>
55d201fe46SDaniel Eischen #include "un-namespace.h"
56e8636dfdSBill Paul 
57e8636dfdSBill Paul #if defined(LIBC_SCCS) && !defined(lint)
58d3d20c82SDavid E. O'Brien static char sccsid[] = 	"@(#)rtime.c	2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
59e8636dfdSBill Paul #endif
60d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
61d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
62e8636dfdSBill Paul 
63c05ac53bSDavid E. O'Brien extern int _rpc_dtablesize( void );
64e8636dfdSBill Paul 
65e8636dfdSBill Paul #define NYEARS	(unsigned long)(1970 - 1900)
66e8636dfdSBill Paul #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
67e8636dfdSBill Paul 
68c05ac53bSDavid E. O'Brien static void do_close( int );
69e8636dfdSBill Paul 
70e8636dfdSBill Paul int
71e8636dfdSBill Paul rtime(addrp, timep, timeout)
72e8636dfdSBill Paul 	struct sockaddr_in *addrp;
73e8636dfdSBill Paul 	struct timeval *timep;
74e8636dfdSBill Paul 	struct timeval *timeout;
75e8636dfdSBill Paul {
76e8636dfdSBill Paul 	int s;
77e8636dfdSBill Paul 	fd_set readfds;
78e8636dfdSBill Paul 	int res;
79e8636dfdSBill Paul 	unsigned long thetime;
80e8636dfdSBill Paul 	struct sockaddr_in from;
81720138bbSStefan Farfeleder 	socklen_t fromlen;
82e8636dfdSBill Paul 	int type;
83e8636dfdSBill Paul 	struct servent *serv;
84e8636dfdSBill Paul 
85e8636dfdSBill Paul 	if (timeout == NULL) {
86e8636dfdSBill Paul 		type = SOCK_STREAM;
87e8636dfdSBill Paul 	} else {
88e8636dfdSBill Paul 		type = SOCK_DGRAM;
89e8636dfdSBill Paul 	}
90d201fe46SDaniel Eischen 	s = _socket(AF_INET, type, 0);
91e8636dfdSBill Paul 	if (s < 0) {
92e8636dfdSBill Paul 		return(-1);
93e8636dfdSBill Paul 	}
94e8636dfdSBill Paul 	addrp->sin_family = AF_INET;
95e8636dfdSBill Paul 
96e8636dfdSBill Paul 	/* TCP and UDP port are the same in this case */
97e8636dfdSBill Paul 	if ((serv = getservbyname("time", "tcp")) == NULL) {
98e8636dfdSBill Paul 		return(-1);
99e8636dfdSBill Paul 	}
100e8636dfdSBill Paul 
101e8636dfdSBill Paul 	addrp->sin_port = serv->s_port;
102e8636dfdSBill Paul 
103e8636dfdSBill Paul 	if (type == SOCK_DGRAM) {
104d201fe46SDaniel Eischen 		res = _sendto(s, (char *)&thetime, sizeof(thetime), 0,
105e8636dfdSBill Paul 			     (struct sockaddr *)addrp, sizeof(*addrp));
106e8636dfdSBill Paul 		if (res < 0) {
107e8636dfdSBill Paul 			do_close(s);
108e8636dfdSBill Paul 			return(-1);
109e8636dfdSBill Paul 		}
110e8636dfdSBill Paul 		do {
111e8636dfdSBill Paul 			FD_ZERO(&readfds);
112e8636dfdSBill Paul 			FD_SET(s, &readfds);
113d201fe46SDaniel Eischen 			res = _select(_rpc_dtablesize(), &readfds,
114e8636dfdSBill Paul 				     (fd_set *)NULL, (fd_set *)NULL, timeout);
115e8636dfdSBill Paul 		} while (res < 0 && errno == EINTR);
116e8636dfdSBill Paul 		if (res <= 0) {
117e8636dfdSBill Paul 			if (res == 0) {
118e8636dfdSBill Paul 				errno = ETIMEDOUT;
119e8636dfdSBill Paul 			}
120e8636dfdSBill Paul 			do_close(s);
121e8636dfdSBill Paul 			return(-1);
122e8636dfdSBill Paul 		}
123e8636dfdSBill Paul 		fromlen = sizeof(from);
124d201fe46SDaniel Eischen 		res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
125e8636dfdSBill Paul 			       (struct sockaddr *)&from, &fromlen);
126e8636dfdSBill Paul 		do_close(s);
127e8636dfdSBill Paul 		if (res < 0) {
128e8636dfdSBill Paul 			return(-1);
129e8636dfdSBill Paul 		}
130e8636dfdSBill Paul 	} else {
131d201fe46SDaniel Eischen 		if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
132e8636dfdSBill Paul 			do_close(s);
133e8636dfdSBill Paul 			return(-1);
134e8636dfdSBill Paul 		}
1359233c4d9SJason Evans 		res = _read(s, (char *)&thetime, sizeof(thetime));
136e8636dfdSBill Paul 		do_close(s);
137e8636dfdSBill Paul 		if (res < 0) {
138e8636dfdSBill Paul 			return(-1);
139e8636dfdSBill Paul 		}
140e8636dfdSBill Paul 	}
141e8636dfdSBill Paul 	if (res != sizeof(thetime)) {
142e8636dfdSBill Paul 		errno = EIO;
143e8636dfdSBill Paul 		return(-1);
144e8636dfdSBill Paul 	}
145e8636dfdSBill Paul 	thetime = ntohl(thetime);
146e8636dfdSBill Paul 	timep->tv_sec = thetime - TOFFSET;
147e8636dfdSBill Paul 	timep->tv_usec = 0;
148e8636dfdSBill Paul 	return(0);
149e8636dfdSBill Paul }
150e8636dfdSBill Paul 
151e8636dfdSBill Paul static void
152e8636dfdSBill Paul do_close(s)
153e8636dfdSBill Paul 	int s;
154e8636dfdSBill Paul {
155e8636dfdSBill Paul 	int save;
156e8636dfdSBill Paul 
157e8636dfdSBill Paul 	save = errno;
1589233c4d9SJason Evans 	(void)_close(s);
159e8636dfdSBill Paul 	errno = save;
160e8636dfdSBill Paul }
161