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) 58e8636dfdSBill Paul /* from: static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; */ 597f3dea24SPeter Wemm static const char rcsid[] = "$FreeBSD$"; 60e8636dfdSBill Paul #endif 61e8636dfdSBill Paul 62c05ac53bSDavid E. O'Brien extern int _rpc_dtablesize( void ); 63e8636dfdSBill Paul 64e8636dfdSBill Paul #define NYEARS (unsigned long)(1970 - 1900) 65e8636dfdSBill Paul #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4))) 66e8636dfdSBill Paul 67c05ac53bSDavid E. O'Brien static void do_close( int ); 68e8636dfdSBill Paul 69e8636dfdSBill Paul int 70e8636dfdSBill Paul rtime(addrp, timep, timeout) 71e8636dfdSBill Paul struct sockaddr_in *addrp; 72e8636dfdSBill Paul struct timeval *timep; 73e8636dfdSBill Paul struct timeval *timeout; 74e8636dfdSBill Paul { 75e8636dfdSBill Paul int s; 76e8636dfdSBill Paul fd_set readfds; 77e8636dfdSBill Paul int res; 78e8636dfdSBill Paul unsigned long thetime; 79e8636dfdSBill Paul struct sockaddr_in from; 80e8636dfdSBill Paul int fromlen; 81e8636dfdSBill Paul int type; 82e8636dfdSBill Paul struct servent *serv; 83e8636dfdSBill Paul 84e8636dfdSBill Paul if (timeout == NULL) { 85e8636dfdSBill Paul type = SOCK_STREAM; 86e8636dfdSBill Paul } else { 87e8636dfdSBill Paul type = SOCK_DGRAM; 88e8636dfdSBill Paul } 89d201fe46SDaniel Eischen s = _socket(AF_INET, type, 0); 90e8636dfdSBill Paul if (s < 0) { 91e8636dfdSBill Paul return(-1); 92e8636dfdSBill Paul } 93e8636dfdSBill Paul addrp->sin_family = AF_INET; 94e8636dfdSBill Paul 95e8636dfdSBill Paul /* TCP and UDP port are the same in this case */ 96e8636dfdSBill Paul if ((serv = getservbyname("time", "tcp")) == NULL) { 97e8636dfdSBill Paul return(-1); 98e8636dfdSBill Paul } 99e8636dfdSBill Paul 100e8636dfdSBill Paul addrp->sin_port = serv->s_port; 101e8636dfdSBill Paul 102e8636dfdSBill Paul if (type == SOCK_DGRAM) { 103d201fe46SDaniel Eischen res = _sendto(s, (char *)&thetime, sizeof(thetime), 0, 104e8636dfdSBill Paul (struct sockaddr *)addrp, sizeof(*addrp)); 105e8636dfdSBill Paul if (res < 0) { 106e8636dfdSBill Paul do_close(s); 107e8636dfdSBill Paul return(-1); 108e8636dfdSBill Paul } 109e8636dfdSBill Paul do { 110e8636dfdSBill Paul FD_ZERO(&readfds); 111e8636dfdSBill Paul FD_SET(s, &readfds); 112d201fe46SDaniel Eischen res = _select(_rpc_dtablesize(), &readfds, 113e8636dfdSBill Paul (fd_set *)NULL, (fd_set *)NULL, timeout); 114e8636dfdSBill Paul } while (res < 0 && errno == EINTR); 115e8636dfdSBill Paul if (res <= 0) { 116e8636dfdSBill Paul if (res == 0) { 117e8636dfdSBill Paul errno = ETIMEDOUT; 118e8636dfdSBill Paul } 119e8636dfdSBill Paul do_close(s); 120e8636dfdSBill Paul return(-1); 121e8636dfdSBill Paul } 122e8636dfdSBill Paul fromlen = sizeof(from); 123d201fe46SDaniel Eischen res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 124e8636dfdSBill Paul (struct sockaddr *)&from, &fromlen); 125e8636dfdSBill Paul do_close(s); 126e8636dfdSBill Paul if (res < 0) { 127e8636dfdSBill Paul return(-1); 128e8636dfdSBill Paul } 129e8636dfdSBill Paul } else { 130d201fe46SDaniel Eischen if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) { 131e8636dfdSBill Paul do_close(s); 132e8636dfdSBill Paul return(-1); 133e8636dfdSBill Paul } 1349233c4d9SJason Evans res = _read(s, (char *)&thetime, sizeof(thetime)); 135e8636dfdSBill Paul do_close(s); 136e8636dfdSBill Paul if (res < 0) { 137e8636dfdSBill Paul return(-1); 138e8636dfdSBill Paul } 139e8636dfdSBill Paul } 140e8636dfdSBill Paul if (res != sizeof(thetime)) { 141e8636dfdSBill Paul errno = EIO; 142e8636dfdSBill Paul return(-1); 143e8636dfdSBill Paul } 144e8636dfdSBill Paul thetime = ntohl(thetime); 145e8636dfdSBill Paul timep->tv_sec = thetime - TOFFSET; 146e8636dfdSBill Paul timep->tv_usec = 0; 147e8636dfdSBill Paul return(0); 148e8636dfdSBill Paul } 149e8636dfdSBill Paul 150e8636dfdSBill Paul static void 151e8636dfdSBill Paul do_close(s) 152e8636dfdSBill Paul int s; 153e8636dfdSBill Paul { 154e8636dfdSBill Paul int save; 155e8636dfdSBill Paul 156e8636dfdSBill Paul save = errno; 1579233c4d9SJason Evans (void)_close(s); 158e8636dfdSBill Paul errno = save; 159e8636dfdSBill Paul } 160