2005-03-05 Roland McGrath <roland@redhat.com>
[glibc/history.git] / sunrpc / rtime.c
blob4996f01ba915d6b9719d95248d84dd8add9a9443
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/poll.h>
51 #include <sys/socket.h>
52 #include <sys/time.h>
53 #include <rpc/auth_des.h>
54 #include <errno.h>
55 #include <netinet/in.h>
57 #define NYEARS (u_long)(1970 - 1900)
58 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
60 static void do_close (int);
62 static void
63 do_close (int s)
65 int save;
67 save = errno;
68 __close (s);
69 __set_errno (save);
72 int
73 rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
74 struct rpc_timeval *timeout)
76 int s;
77 struct pollfd fd;
78 int milliseconds;
79 int res;
80 /* RFC 868 says the time is transmitted as a 32-bit value. */
81 uint32_t thetime;
82 struct sockaddr_in from;
83 int fromlen;
84 int type;
86 if (timeout == NULL)
87 type = SOCK_STREAM;
88 else
89 type = SOCK_DGRAM;
91 s = __socket (AF_INET, type, 0);
92 if (s < 0)
93 return (-1);
95 addrp->sin_family = AF_INET;
96 addrp->sin_port = htons (IPPORT_TIMESERVER);
97 if (type == SOCK_DGRAM)
99 res = __sendto (s, (char *) &thetime, sizeof (thetime), 0,
100 (struct sockaddr *) addrp, sizeof (*addrp));
101 if (res < 0)
103 do_close (s);
104 return -1;
106 milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
107 fd.fd = s;
108 fd.events = POLLIN;
110 res = __poll (&fd, 1, milliseconds);
111 while (res < 0 && errno == EINTR);
112 if (res <= 0)
114 if (res == 0)
115 __set_errno (ETIMEDOUT);
116 do_close (s);
117 return (-1);
119 fromlen = sizeof (from);
120 res = __recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
121 (struct sockaddr *) &from, &fromlen);
122 do_close (s);
123 if (res < 0)
124 return -1;
126 else
128 if (__connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
130 do_close (s);
131 return -1;
133 res = __read (s, (char *) &thetime, sizeof (thetime));
134 do_close (s);
135 if (res < 0)
136 return (-1);
138 if (res != sizeof (thetime))
140 __set_errno (EIO);
141 return -1;
143 thetime = ntohl (thetime);
144 timep->tv_sec = thetime - TOFFSET;
145 timep->tv_usec = 0;
146 return 0;
148 libc_hidden_def (rtime)