1 /* 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. 8 * 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. 12 * 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. 16 * 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. 20 * 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. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * @(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI 30 * $FreeBSD: src/lib/libc/rpc/rtime.c,v 1.9 2005/03/10 00:57:01 stefanf Exp $ 31 * $DragonFly: src/lib/libc/rpc/rtime.c,v 1.5 2005/11/13 12:27:04 swildner Exp $ 32 */ 33 34 /* 35 * Copyright (c) 1988 by Sun Microsystems, Inc. 36 */ 37 38 /* 39 * rtime - get time from remote machine 40 * 41 * gets time, obtaining value from host 42 * on the udp/time socket. Since timeserver returns 43 * with time of day in seconds since Jan 1, 1900, must 44 * subtract seconds before Jan 1, 1970 to get 45 * what unix uses. 46 */ 47 #include "namespace.h" 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <errno.h> 52 #include <sys/types.h> 53 #include <sys/socket.h> 54 #include <sys/time.h> 55 #include <netinet/in.h> 56 #include <stdio.h> 57 #include <netdb.h> 58 #include "un-namespace.h" 59 60 extern int _rpc_dtablesize(void); 61 62 #define NYEARS (unsigned long)(1970 - 1900) 63 #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4))) 64 65 static void do_close(int); 66 67 int 68 rtime(struct sockaddr_in *addrp, struct timeval *timep, struct timeval *timeout) 69 { 70 int s; 71 fd_set readfds; 72 int res; 73 unsigned long thetime; 74 struct sockaddr_in from; 75 socklen_t fromlen; 76 int type; 77 struct servent *serv; 78 79 if (timeout == NULL) { 80 type = SOCK_STREAM; 81 } else { 82 type = SOCK_DGRAM; 83 } 84 s = _socket(AF_INET, type, 0); 85 if (s < 0) { 86 return(-1); 87 } 88 addrp->sin_family = AF_INET; 89 90 /* TCP and UDP port are the same in this case */ 91 if ((serv = getservbyname("time", "tcp")) == NULL) { 92 return(-1); 93 } 94 95 addrp->sin_port = serv->s_port; 96 97 if (type == SOCK_DGRAM) { 98 res = _sendto(s, (char *)&thetime, sizeof(thetime), 0, 99 (struct sockaddr *)addrp, sizeof(*addrp)); 100 if (res < 0) { 101 do_close(s); 102 return(-1); 103 } 104 do { 105 FD_ZERO(&readfds); 106 FD_SET(s, &readfds); 107 res = _select(_rpc_dtablesize(), &readfds, 108 NULL, NULL, timeout); 109 } while (res < 0 && errno == EINTR); 110 if (res <= 0) { 111 if (res == 0) { 112 errno = ETIMEDOUT; 113 } 114 do_close(s); 115 return(-1); 116 } 117 fromlen = sizeof(from); 118 res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, 119 (struct sockaddr *)&from, &fromlen); 120 do_close(s); 121 if (res < 0) { 122 return(-1); 123 } 124 } else { 125 if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) { 126 do_close(s); 127 return(-1); 128 } 129 res = _read(s, (char *)&thetime, sizeof(thetime)); 130 do_close(s); 131 if (res < 0) { 132 return(-1); 133 } 134 } 135 if (res != sizeof(thetime)) { 136 errno = EIO; 137 return(-1); 138 } 139 thetime = ntohl(thetime); 140 timep->tv_sec = thetime - TOFFSET; 141 timep->tv_usec = 0; 142 return(0); 143 } 144 145 static void 146 do_close(int s) 147 { 148 int save; 149 150 save = errno; 151 _close(s); 152 errno = save; 153 } 154