[df49c60] | 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 | |
---|
| 30 | /* |
---|
| 31 | * Copyright (c) 1988 by Sun Microsystems, Inc. |
---|
| 32 | |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | * rtime - get time from remote machine |
---|
| 37 | * |
---|
| 38 | * gets time, obtaining value from host |
---|
| 39 | * on the udp/time socket. Since timeserver returns |
---|
| 40 | * with time of day in seconds since Jan 1, 1900, must |
---|
| 41 | * subtract seconds before Jan 1, 1970 to get |
---|
| 42 | * what unix uses. |
---|
| 43 | */ |
---|
| 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 | |
---|
| 55 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
| 56 | /* from: static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; */ |
---|
| 57 | static const char rcsid[] = "$FreeBSD: src/lib/libc/rpc/rtime.c,v 1.5 2000/01/27 23:06:41 jasone Exp $"; |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | extern int _rpc_dtablesize __P(( 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 __P(( int )); |
---|
| 66 | |
---|
| 67 | int |
---|
| 68 | rtime(addrp, timep, timeout) |
---|
| 69 | struct sockaddr_in *addrp; |
---|
| 70 | struct timeval *timep; |
---|
| 71 | struct timeval *timeout; |
---|
| 72 | { |
---|
| 73 | int s; |
---|
| 74 | fd_set readfds; |
---|
| 75 | int res; |
---|
| 76 | unsigned long thetime; |
---|
| 77 | struct sockaddr_in from; |
---|
| 78 | int fromlen; |
---|
| 79 | int type; |
---|
| 80 | struct servent *serv; |
---|
| 81 | |
---|
| 82 | if (timeout == NULL) { |
---|
| 83 | type = SOCK_STREAM; |
---|
| 84 | } else { |
---|
| 85 | type = SOCK_DGRAM; |
---|
| 86 | } |
---|
| 87 | s = socket(AF_INET, type, 0); |
---|
| 88 | if (s < 0) { |
---|
| 89 | return(-1); |
---|
| 90 | } |
---|
| 91 | addrp->sin_family = AF_INET; |
---|
| 92 | |
---|
| 93 | /* TCP and UDP port are the same in this case */ |
---|
| 94 | if ((serv = getservbyname("time", "tcp")) == NULL) { |
---|
| 95 | return(-1); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | addrp->sin_port = serv->s_port; |
---|
| 99 | |
---|
| 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_dtablesize(), &readfds, |
---|
| 111 | (fd_set *)NULL, (fd_set *)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, sizeof(*addrp)) < 0) { |
---|
| 129 | do_close(s); |
---|
| 130 | return(-1); |
---|
| 131 | } |
---|
[57cfaad] | 132 | res = _RPC_read(s, (char *)&thetime, sizeof(thetime)); |
---|
[df49c60] | 133 | do_close(s); |
---|
| 134 | if (res < 0) { |
---|
| 135 | return(-1); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | if (res != sizeof(thetime)) { |
---|
| 139 | errno = EIO; |
---|
| 140 | return(-1); |
---|
| 141 | } |
---|
| 142 | thetime = ntohl(thetime); |
---|
| 143 | timep->tv_sec = thetime - TOFFSET; |
---|
| 144 | timep->tv_usec = 0; |
---|
| 145 | return(0); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | static void |
---|
| 149 | do_close(s) |
---|
| 150 | int s; |
---|
| 151 | { |
---|
| 152 | int save; |
---|
| 153 | |
---|
| 154 | save = errno; |
---|
[57cfaad] | 155 | (void)_RPC_close(s); |
---|
[df49c60] | 156 | errno = save; |
---|
| 157 | } |
---|