Home | History | Annotate | Download | only in librpcsoc
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License, Version 1.0 only
      6  * (the "License").  You may not use this file except in compliance
      7  * with the License.
      8  *
      9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     10  * or http://www.opensolaris.org/os/licensing.
     11  * See the License for the specific language governing permissions
     12  * and limitations under the License.
     13  *
     14  * When distributing Covered Code, include this CDDL HEADER in each
     15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     16  * If applicable, add the following below this CDDL HEADER, with the
     17  * fields enclosed by brackets "[]" replaced with your own identifying
     18  * information: Portions Copyright [yyyy] [name of copyright owner]
     19  *
     20  * CDDL HEADER END
     21  */
     22 /*
     23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
     28 /*	  All Rights Reserved  	*/
     29 
     30 /*
     31  * University Copyright- Copyright (c) 1982, 1986, 1988
     32  * The Regents of the University of California
     33  * All Rights Reserved
     34  *
     35  * University Acknowledgment- Portions of this document are derived from
     36  * software developed by the University of California, Berkeley, and its
     37  * contributors.
     38  */
     39 
     40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     41 
     42 /*
     43  * rtime - get time from remote machine
     44  *
     45  * gets time, obtaining value from host
     46  * on the udp/time socket.  Since timeserver returns
     47  * with time of day in seconds since Jan 1, 1900,  must
     48  * subtract seconds before Jan 1, 1970 to get
     49  * what unix uses.
     50  */
     51 
     52 #include <sys/types.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 #include <sys/socket.h>
     56 #include <sys/time.h>
     57 #include <errno.h>
     58 #include <netinet/in.h>
     59 #include <malloc.h>
     60 
     61 #define	NYEARS	(1970 - 1900)
     62 #define	TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
     63 
     64 extern int _socket(int, int, int);
     65 extern int _sendto(int, const char *, int, int,
     66 	const struct sockaddr *, int);
     67 extern int _recvfrom(int, char *, int, int,
     68 	struct sockaddr *, int *);
     69 extern int _connect(int, struct sockaddr *, int);
     70 extern int __rpc_dtbsize();
     71 extern ssize_t read(int, void *, size_t);
     72 extern int close(int);
     73 static void do_close();
     74 
     75 int
     76 rtime(addrp, timep, timeout)
     77 	struct sockaddr_in *addrp;
     78 	struct timeval *timep;
     79 	struct timeval *timeout;
     80 {
     81 	int s;
     82 	fd_set readfds;
     83 	int res;
     84 	uint_t thetime;
     85 	struct sockaddr_in from;
     86 	int fromlen;
     87 	int type;
     88 
     89 	if (timeout == NULL) {
     90 		type = SOCK_STREAM;
     91 	} else {
     92 		type = SOCK_DGRAM;
     93 	}
     94 	s = _socket(AF_INET, type, 0);
     95 	if (s < 0) {
     96 		return (-1);
     97 	}
     98 	addrp->sin_family = AF_INET;
     99 	addrp->sin_port = htons(IPPORT_TIMESERVER);
    100 	if (type == SOCK_DGRAM) {
    101 		res = _sendto(s, (char *)&thetime, sizeof (thetime), 0,
    102 		    (struct sockaddr *)addrp, sizeof (*addrp));
    103 		if (res < 0) {
    104 			do_close(s);
    105 			return (-1);
    106 		}
    107 		do {
    108 			FD_ZERO(&readfds);
    109 			FD_SET(s, &readfds);
    110 			res = select(__rpc_dtbsize(), &readfds, NULL,
    111 			    NULL, timeout);
    112 		} while (res < 0 && errno == EINTR);
    113 		if (res <= 0) {
    114 			if (res == 0) {
    115 				errno = ETIMEDOUT;
    116 			}
    117 			do_close(s);
    118 			return (-1);
    119 		}
    120 		fromlen = sizeof (from);
    121 		res = _recvfrom(s, (char *)&thetime, sizeof (thetime), 0,
    122 		    (struct sockaddr *)&from, &fromlen);
    123 		do_close(s);
    124 		if (res < 0) {
    125 			return (-1);
    126 		}
    127 	} else {
    128 		if (_connect(s, (struct sockaddr *)addrp,
    129 			sizeof (*addrp)) < 0) {
    130 			do_close(s);
    131 			return (-1);
    132 		}
    133 		res = read(s, (char *)&thetime, sizeof (thetime));
    134 		do_close(s);
    135 		if (res < 0) {
    136 			return (-1);
    137 		}
    138 	}
    139 	if (res != sizeof (thetime)) {
    140 		errno = EIO;
    141 		return (-1);
    142 	}
    143 	thetime = ntohl(thetime);
    144 
    145 	thetime = thetime - TOFFSET;
    146 #ifdef _ILP32
    147 	if (thetime > INT32_MAX)
    148 		thetime = INT32_MAX;
    149 #endif
    150 	timep->tv_sec = thetime;
    151 	timep->tv_usec = 0;
    152 	return (0);
    153 }
    154 
    155 static void
    156 do_close(s)
    157 	int s;
    158 {
    159 	int save;
    160 
    161 	save = errno;
    162 	(void) close(s);
    163 	errno = save;
    164 }
    165