rtime.c revision 92905
126219Swpaul/*
226219Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
326219Swpaul * unrestricted use provided that this legend is included on all tape
426219Swpaul * media and as a part of the software program in whole or part.  Users
526219Swpaul * may copy or modify Sun RPC without charge, but are not authorized
626219Swpaul * to license or distribute it to anyone else except as part of a product or
726219Swpaul * program developed by the user.
826219Swpaul *
926219Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1026219Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1126219Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1226219Swpaul *
1326219Swpaul * Sun RPC is provided with no support and without any obligation on the
1426219Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction,
1526219Swpaul * modification or enhancement.
1626219Swpaul *
1726219Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1826219Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1926219Swpaul * OR ANY PART THEREOF.
2026219Swpaul *
2126219Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2226219Swpaul * or profits or other special, indirect and consequential damages, even if
2326219Swpaul * Sun has been advised of the possibility of such damages.
2426219Swpaul *
2526219Swpaul * Sun Microsystems, Inc.
2626219Swpaul * 2550 Garcia Avenue
2726219Swpaul * Mountain View, California  94043
2826219Swpaul */
2926219Swpaul
3026219Swpaul/*
3126219Swpaul * Copyright (c) 1988 by Sun Microsystems, Inc.
3226219Swpaul
3326219Swpaul */
3426219Swpaul
3526219Swpaul/*
3626219Swpaul * rtime - get time from remote machine
3726219Swpaul *
3826219Swpaul * gets time, obtaining value from host
3926219Swpaul * on the udp/time socket.  Since timeserver returns
4026219Swpaul * with time of day in seconds since Jan 1, 1900,  must
4126219Swpaul * subtract seconds before Jan 1, 1970 to get
4226219Swpaul * what unix uses.
4326219Swpaul */
4471579Sdeischen#include "namespace.h"
4526219Swpaul#include <stdlib.h>
4626219Swpaul#include <string.h>
4726219Swpaul#include <unistd.h>
4826219Swpaul#include <errno.h>
4926219Swpaul#include <sys/types.h>
5026219Swpaul#include <sys/socket.h>
5126219Swpaul#include <sys/time.h>
5226219Swpaul#include <netinet/in.h>
5326219Swpaul#include <stdio.h>
5426219Swpaul#include <netdb.h>
5571579Sdeischen#include "un-namespace.h"
5626219Swpaul
5726219Swpaul#if defined(LIBC_SCCS) && !defined(lint)
5826219Swpaul/* from: static char sccsid[] = 	"@(#)rtime.c	2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; */
5950476Speterstatic const char rcsid[] = "$FreeBSD: head/lib/libc/rpc/rtime.c 92905 2002-03-21 22:49:10Z obrien $";
6026219Swpaul#endif
6126219Swpaul
6292905Sobrienextern int _rpc_dtablesize( void );
6326219Swpaul
6426219Swpaul#define NYEARS	(unsigned long)(1970 - 1900)
6526219Swpaul#define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
6626219Swpaul
6792905Sobrienstatic void do_close( int );
6826219Swpaul
6926219Swpaulint
7026219Swpaulrtime(addrp, timep, timeout)
7126219Swpaul	struct sockaddr_in *addrp;
7226219Swpaul	struct timeval *timep;
7326219Swpaul	struct timeval *timeout;
7426219Swpaul{
7526219Swpaul	int s;
7626219Swpaul	fd_set readfds;
7726219Swpaul	int res;
7826219Swpaul	unsigned long thetime;
7926219Swpaul	struct sockaddr_in from;
8026219Swpaul	int fromlen;
8126219Swpaul	int type;
8226219Swpaul	struct servent *serv;
8326219Swpaul
8426219Swpaul	if (timeout == NULL) {
8526219Swpaul		type = SOCK_STREAM;
8626219Swpaul	} else {
8726219Swpaul		type = SOCK_DGRAM;
8826219Swpaul	}
8971579Sdeischen	s = _socket(AF_INET, type, 0);
9026219Swpaul	if (s < 0) {
9126219Swpaul		return(-1);
9226219Swpaul	}
9326219Swpaul	addrp->sin_family = AF_INET;
9426219Swpaul
9526219Swpaul	/* TCP and UDP port are the same in this case */
9626219Swpaul	if ((serv = getservbyname("time", "tcp")) == NULL) {
9726219Swpaul		return(-1);
9826219Swpaul	}
9926219Swpaul
10026219Swpaul	addrp->sin_port = serv->s_port;
10126219Swpaul
10226219Swpaul	if (type == SOCK_DGRAM) {
10371579Sdeischen		res = _sendto(s, (char *)&thetime, sizeof(thetime), 0,
10426219Swpaul			     (struct sockaddr *)addrp, sizeof(*addrp));
10526219Swpaul		if (res < 0) {
10626219Swpaul			do_close(s);
10726219Swpaul			return(-1);
10826219Swpaul		}
10926219Swpaul		do {
11026219Swpaul			FD_ZERO(&readfds);
11126219Swpaul			FD_SET(s, &readfds);
11271579Sdeischen			res = _select(_rpc_dtablesize(), &readfds,
11326219Swpaul				     (fd_set *)NULL, (fd_set *)NULL, timeout);
11426219Swpaul		} while (res < 0 && errno == EINTR);
11526219Swpaul		if (res <= 0) {
11626219Swpaul			if (res == 0) {
11726219Swpaul				errno = ETIMEDOUT;
11826219Swpaul			}
11926219Swpaul			do_close(s);
12026219Swpaul			return(-1);
12126219Swpaul		}
12226219Swpaul		fromlen = sizeof(from);
12371579Sdeischen		res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
12426219Swpaul			       (struct sockaddr *)&from, &fromlen);
12526219Swpaul		do_close(s);
12626219Swpaul		if (res < 0) {
12726219Swpaul			return(-1);
12826219Swpaul		}
12926219Swpaul	} else {
13071579Sdeischen		if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
13126219Swpaul			do_close(s);
13226219Swpaul			return(-1);
13326219Swpaul		}
13456698Sjasone		res = _read(s, (char *)&thetime, sizeof(thetime));
13526219Swpaul		do_close(s);
13626219Swpaul		if (res < 0) {
13726219Swpaul			return(-1);
13826219Swpaul		}
13926219Swpaul	}
14026219Swpaul	if (res != sizeof(thetime)) {
14126219Swpaul		errno = EIO;
14226219Swpaul		return(-1);
14326219Swpaul	}
14426219Swpaul	thetime = ntohl(thetime);
14526219Swpaul	timep->tv_sec = thetime - TOFFSET;
14626219Swpaul	timep->tv_usec = 0;
14726219Swpaul	return(0);
14826219Swpaul}
14926219Swpaul
15026219Swpaulstatic void
15126219Swpauldo_close(s)
15226219Swpaul	int s;
15326219Swpaul{
15426219Swpaul	int save;
15526219Swpaul
15626219Swpaul	save = errno;
15756698Sjasone	(void)_close(s);
15826219Swpaul	errno = save;
15926219Swpaul}
160