2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
31 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * rtime - get time from remote machine
38 * gets time, obtaining value from host
39 * on the udp/time socket. Since timeserver returns
40 * with time of day in seconds since Jan 1, 1900, must
41 * subtract seconds before Jan 1, 1970 to get
44 #include "namespace.h"
49 #include <sys/types.h>
50 #include <sys/socket.h>
52 #include <netinet/in.h>
55 #include "un-namespace.h"
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 extern int _rpc_dtablesize( void );
65 #define NYEARS (unsigned long)(1970 - 1900)
66 #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
68 static void do_close( int );
71 rtime(addrp, timep, timeout)
72 struct sockaddr_in *addrp;
73 struct timeval *timep;
74 struct timeval *timeout;
79 unsigned long thetime;
80 struct sockaddr_in from;
85 if (timeout == NULL) {
90 s = _socket(AF_INET, type, 0);
94 addrp->sin_family = AF_INET;
96 /* TCP and UDP port are the same in this case */
97 if ((serv = getservbyname("time", "tcp")) == NULL) {
101 addrp->sin_port = serv->s_port;
103 if (type == SOCK_DGRAM) {
104 res = _sendto(s, (char *)&thetime, sizeof(thetime), 0,
105 (struct sockaddr *)addrp, sizeof(*addrp));
113 res = _select(_rpc_dtablesize(), &readfds,
114 (fd_set *)NULL, (fd_set *)NULL, timeout);
115 } while (res < 0 && errno == EINTR);
123 fromlen = sizeof(from);
124 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
125 (struct sockaddr *)&from, &fromlen);
131 if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
135 res = _read(s, (char *)&thetime, sizeof(thetime));
141 if (res != sizeof(thetime)) {
145 thetime = ntohl(thetime);
146 timep->tv_sec = thetime - TOFFSET;