xref: /freebsd/lib/libc/rpc/rtime.c (revision e8636dfd57b1bc6a19328606214e847c0d9eb1aa)
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  */
44e8636dfdSBill Paul #include <stdlib.h>
45e8636dfdSBill Paul #include <string.h>
46e8636dfdSBill Paul #include <unistd.h>
47e8636dfdSBill Paul #include <errno.h>
48e8636dfdSBill Paul #include <sys/types.h>
49e8636dfdSBill Paul #include <sys/socket.h>
50e8636dfdSBill Paul #include <sys/time.h>
51e8636dfdSBill Paul #include <netinet/in.h>
52e8636dfdSBill Paul #include <stdio.h>
53e8636dfdSBill Paul #include <netdb.h>
54e8636dfdSBill Paul 
55e8636dfdSBill Paul #if defined(LIBC_SCCS) && !defined(lint)
56e8636dfdSBill Paul /* from: static char sccsid[] = 	"@(#)rtime.c	2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; */
57e8636dfdSBill Paul static const char rcsid[] = "$Id$";
58e8636dfdSBill Paul #endif
59e8636dfdSBill Paul 
60e8636dfdSBill Paul extern int _rpc_dtablesize __P(( void ));
61e8636dfdSBill Paul 
62e8636dfdSBill Paul #define NYEARS	(unsigned long)(1970 - 1900)
63e8636dfdSBill Paul #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
64e8636dfdSBill Paul 
65e8636dfdSBill Paul static void do_close __P(( int ));
66e8636dfdSBill Paul 
67e8636dfdSBill Paul int
68e8636dfdSBill Paul rtime(addrp, timep, timeout)
69e8636dfdSBill Paul 	struct sockaddr_in *addrp;
70e8636dfdSBill Paul 	struct timeval *timep;
71e8636dfdSBill Paul 	struct timeval *timeout;
72e8636dfdSBill Paul {
73e8636dfdSBill Paul 	int s;
74e8636dfdSBill Paul 	fd_set readfds;
75e8636dfdSBill Paul 	int res;
76e8636dfdSBill Paul 	unsigned long thetime;
77e8636dfdSBill Paul 	struct sockaddr_in from;
78e8636dfdSBill Paul 	int fromlen;
79e8636dfdSBill Paul 	int type;
80e8636dfdSBill Paul 	struct servent *serv;
81e8636dfdSBill Paul 
82e8636dfdSBill Paul 	if (timeout == NULL) {
83e8636dfdSBill Paul 		type = SOCK_STREAM;
84e8636dfdSBill Paul 	} else {
85e8636dfdSBill Paul 		type = SOCK_DGRAM;
86e8636dfdSBill Paul 	}
87e8636dfdSBill Paul 	s = socket(AF_INET, type, 0);
88e8636dfdSBill Paul 	if (s < 0) {
89e8636dfdSBill Paul 		return(-1);
90e8636dfdSBill Paul 	}
91e8636dfdSBill Paul 	addrp->sin_family = AF_INET;
92e8636dfdSBill Paul 
93e8636dfdSBill Paul 	/* TCP and UDP port are the same in this case */
94e8636dfdSBill Paul 	if ((serv = getservbyname("time", "tcp")) == NULL) {
95e8636dfdSBill Paul 		return(-1);
96e8636dfdSBill Paul 	}
97e8636dfdSBill Paul 
98e8636dfdSBill Paul 	addrp->sin_port = serv->s_port;
99e8636dfdSBill Paul 
100e8636dfdSBill Paul 	if (type == SOCK_DGRAM) {
101e8636dfdSBill Paul 		res = sendto(s, (char *)&thetime, sizeof(thetime), 0,
102e8636dfdSBill Paul 			     (struct sockaddr *)addrp, sizeof(*addrp));
103e8636dfdSBill Paul 		if (res < 0) {
104e8636dfdSBill Paul 			do_close(s);
105e8636dfdSBill Paul 			return(-1);
106e8636dfdSBill Paul 		}
107e8636dfdSBill Paul 		do {
108e8636dfdSBill Paul 			FD_ZERO(&readfds);
109e8636dfdSBill Paul 			FD_SET(s, &readfds);
110e8636dfdSBill Paul 			res = select(_rpc_dtablesize(), &readfds,
111e8636dfdSBill Paul 				     (fd_set *)NULL, (fd_set *)NULL, timeout);
112e8636dfdSBill Paul 		} while (res < 0 && errno == EINTR);
113e8636dfdSBill Paul 		if (res <= 0) {
114e8636dfdSBill Paul 			if (res == 0) {
115e8636dfdSBill Paul 				errno = ETIMEDOUT;
116e8636dfdSBill Paul 			}
117e8636dfdSBill Paul 			do_close(s);
118e8636dfdSBill Paul 			return(-1);
119e8636dfdSBill Paul 		}
120e8636dfdSBill Paul 		fromlen = sizeof(from);
121e8636dfdSBill Paul 		res = recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
122e8636dfdSBill Paul 			       (struct sockaddr *)&from, &fromlen);
123e8636dfdSBill Paul 		do_close(s);
124e8636dfdSBill Paul 		if (res < 0) {
125e8636dfdSBill Paul 			return(-1);
126e8636dfdSBill Paul 		}
127e8636dfdSBill Paul 	} else {
128e8636dfdSBill Paul 		if (connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
129e8636dfdSBill Paul 			do_close(s);
130e8636dfdSBill Paul 			return(-1);
131e8636dfdSBill Paul 		}
132e8636dfdSBill Paul 		res = read(s, (char *)&thetime, sizeof(thetime));
133e8636dfdSBill Paul 		do_close(s);
134e8636dfdSBill Paul 		if (res < 0) {
135e8636dfdSBill Paul 			return(-1);
136e8636dfdSBill Paul 		}
137e8636dfdSBill Paul 	}
138e8636dfdSBill Paul 	if (res != sizeof(thetime)) {
139e8636dfdSBill Paul 		errno = EIO;
140e8636dfdSBill Paul 		return(-1);
141e8636dfdSBill Paul 	}
142e8636dfdSBill Paul 	thetime = ntohl(thetime);
143e8636dfdSBill Paul 	timep->tv_sec = thetime - TOFFSET;
144e8636dfdSBill Paul 	timep->tv_usec = 0;
145e8636dfdSBill Paul 	return(0);
146e8636dfdSBill Paul }
147e8636dfdSBill Paul 
148e8636dfdSBill Paul static void
149e8636dfdSBill Paul do_close(s)
150e8636dfdSBill Paul 	int s;
151e8636dfdSBill Paul {
152e8636dfdSBill Paul 	int save;
153e8636dfdSBill Paul 
154e8636dfdSBill Paul 	save = errno;
155e8636dfdSBill Paul 	(void) close(s);
156e8636dfdSBill Paul 	errno = save;
157e8636dfdSBill Paul }
158