1 /*- 2 * Copyright (c) 2009, Sun Microsystems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * - Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * - Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * - Neither the name of Sun Microsystems, Inc. nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 1988 by Sun Microsystems, Inc. 31 32 */ 33 34 /* 35 * rtime - get time from remote machine 36 * 37 * gets time, obtaining value from host 38 * on the udp/time socket. Since timeserver returns 39 * with time of day in seconds since Jan 1, 1900, must 40 * subtract seconds before Jan 1, 1970 to get 41 * what unix uses. 42 */ 43 #include "namespace.h" 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <errno.h> 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 #include <sys/time.h> 51 #include <netinet/in.h> 52 #include <stdio.h> 53 #include <netdb.h> 54 #include "un-namespace.h" 55 56 #if defined(LIBC_SCCS) && !defined(lint) 57 static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; 58 #endif 59 #include <sys/cdefs.h> 60 __FBSDID("$FreeBSD$"); 61 62 extern int _rpc_dtablesize( void ); 63 64 #define NYEARS (unsigned long)(1970 - 1900) 65 #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4))) 66 67 static void do_close( int ); 68 69 int 70 rtime(addrp, timep, timeout) 71 struct sockaddr_in *addrp; 72 struct timeval *timep; 73 struct timeval *timeout; 74 { 75 int s; 76 fd_set readfds; 77 int res; 78 unsigned long thetime; 79 struct sockaddr_in from; 80 socklen_t fromlen; 81 int type; 82 struct servent *serv; 83 84 if (timeout == NULL) { 85 type = SOCK_STREAM; 86 } else { 87 type = SOCK_DGRAM; 88 } 89 s = _socket(AF_INET, type, 0); 90 if (s < 0) { 91 return(-1); 92 } 93 addrp->sin_family = AF_INET; 94 95 /* TCP and UDP port are the same in this case */ 96 if ((serv = getservbyname("time", "tcp")) == NULL) { 97 return(-1); 98 } 99 100 addrp->sin_port = serv->s_port; 101 102 if (type == SOCK_DGRAM) { 103 res = _sendto(s, (char *)&thetime, sizeof(thetime), 0, 104 (struct sockaddr *)addrp, sizeof(*addrp)); 105 if (res < 0) { 106 do_close(s); 107 return(-1); 108 } 109 do { 110 FD_ZERO(&readfds); 111 FD_SET(s, &readfds); 112 res = _select(_rpc_dtablesize(), &readfds, 113 (fd_set *)NULL, (fd_set *)NULL, timeout); 114 } while (res < 0 && errno == EINTR); 115 if (res <= 0) { 116 if (res == 0) { 117 errno = ETIMEDOUT; 118 } 119 do_close(s); 120 return(-1); 121 } 122 fromlen = sizeof(from); 123 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 124 (struct sockaddr *)&from, &fromlen); 125 do_close(s); 126 if (res < 0) { 127 return(-1); 128 } 129 } else { 130 if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) { 131 do_close(s); 132 return(-1); 133 } 134 res = _read(s, (char *)&thetime, sizeof(thetime)); 135 do_close(s); 136 if (res < 0) { 137 return(-1); 138 } 139 } 140 if (res != sizeof(thetime)) { 141 errno = EIO; 142 return(-1); 143 } 144 thetime = ntohl(thetime); 145 timep->tv_sec = thetime - TOFFSET; 146 timep->tv_usec = 0; 147 return(0); 148 } 149 150 static void 151 do_close(s) 152 int s; 153 { 154 int save; 155 156 save = errno; 157 (void)_close(s); 158 errno = save; 159 } 160