Update.
[glibc.git] / sunrpc / rtime.c
blobbaaced0d5d2fad1f89824a803d9d3502de4bf795
1 #if defined(LIBC_SCCS) && !defined(lint)
2 static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
3 #endif
4 /*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
34 * Copyright (c) 1988 by Sun Microsystems, Inc.
37 * rtime - get time from remote machine
39 * gets time, obtaining value from host
40 * on the udp/time socket. Since timeserver returns
41 * with time of day in seconds since Jan 1, 1900, must
42 * subtract seconds before Jan 1, 1970 to get
43 * what unix uses.
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <rpc/rpc.h>
48 #include <rpc/clnt.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/time.h>
52 #include <rpc/auth_des.h>
53 #include <errno.h>
54 #include <netinet/in.h>
56 #define NYEARS (u_long)(1970 - 1900)
57 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
59 static void do_close (int);
61 static void
62 do_close (int s)
64 int save;
66 save = errno;
67 close (s);
68 __set_errno (save);
71 int
72 rtime (struct sockaddr_in *addrp, struct timeval *timep,
73 struct timeval *timeout)
75 int s;
76 fd_set readfds;
77 int res;
78 unsigned long thetime;
79 struct sockaddr_in from;
80 int fromlen;
81 int type;
83 if (timeout == NULL)
84 type = SOCK_STREAM;
85 else
86 type = SOCK_DGRAM;
88 s = socket (AF_INET, type, 0);
89 if (s < 0)
90 return (-1);
92 addrp->sin_family = AF_INET;
93 addrp->sin_port = htons (IPPORT_TIMESERVER);
94 if (type == SOCK_DGRAM)
96 res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
97 (struct sockaddr *) addrp, sizeof (*addrp));
98 if (res < 0)
100 do_close (s);
101 return -1;
105 FD_ZERO (&readfds);
106 FD_SET (s, &readfds);
107 res = select (_rpc_dtablesize (), &readfds, (void *) NULL,
108 (void *) NULL, timeout);
110 while (res < 0 && errno == EINTR);
111 if (res <= 0)
113 if (res == 0)
114 __set_errno (ETIMEDOUT);
115 do_close (s);
116 return (-1);
118 fromlen = sizeof (from);
119 res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
120 (struct sockaddr *) &from, &fromlen);
121 do_close (s);
122 if (res < 0)
123 return -1;
125 else
127 if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
129 do_close (s);
130 return -1;
132 res = read (s, (char *) &thetime, sizeof (thetime));
133 do_close (s);
134 if (res < 0)
135 return (-1);
137 if (res != sizeof (thetime))
139 __set_errno (EIO);
140 return -1;
142 thetime = ntohl (thetime);
143 timep->tv_sec = thetime - TOFFSET;
144 timep->tv_usec = 0;
145 return 0;