do not rely on libc providing TEMP_FAILURE_RETRY
[project/librpc-uclibc.git] / rtime.c
1 #if 0
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.
11 *
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.
15 *
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.
19 *
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.
23 *
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.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32
33 /*
34 * Copyright (c) 1988 by Sun Microsystems, Inc.
35 */
36 /*
37 * rtime - get time from remote machine
38 *
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.
44 */
45
46 #define __FORCE_GLIBC
47 #include <features.h>
48
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <rpc/rpc.h>
52 #include <rpc/clnt.h>
53 #include <sys/types.h>
54 #include <poll.h>
55 #include <sys/socket.h>
56 #include <sys/time.h>
57 #include <rpc/auth_des.h>
58 #include <errno.h>
59 #include <netinet/in.h>
60
61 #ifndef IPPORT_TIMESERVER
62 #define IPPORT_TIMESERVER 37
63 #endif
64
65 #define NYEARS (u_long)(1970 - 1900)
66 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
67
68 static void do_close (int);
69
70 static void
71 do_close (int s)
72 {
73 int save;
74
75 save = errno;
76 close (s);
77 __set_errno (save);
78 }
79
80 int
81 rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
82 struct rpc_timeval *timeout)
83 {
84 int s;
85 struct pollfd fd;
86 int milliseconds;
87 int res;
88 /* RFC 868 says the time is transmitted as a 32-bit value. */
89 uint32_t thetime;
90 struct sockaddr_in from;
91 socklen_t fromlen;
92 int type;
93
94 if (timeout == NULL)
95 type = SOCK_STREAM;
96 else
97 type = SOCK_DGRAM;
98
99 s = socket (AF_INET, type, 0);
100 if (s < 0)
101 return (-1);
102
103 addrp->sin_family = AF_INET;
104 addrp->sin_port = htons (IPPORT_TIMESERVER);
105 if (type == SOCK_DGRAM)
106 {
107 res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
108 (struct sockaddr *) addrp, sizeof (*addrp));
109 if (res < 0)
110 {
111 do_close (s);
112 return -1;
113 }
114 milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
115 fd.fd = s;
116 fd.events = POLLIN;
117 do
118 res = poll (&fd, 1, milliseconds);
119 while (res < 0 && errno == EINTR);
120 if (res <= 0)
121 {
122 if (res == 0)
123 __set_errno (ETIMEDOUT);
124 do_close (s);
125 return (-1);
126 }
127 fromlen = sizeof (from);
128 res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
129 (struct sockaddr *) &from, &fromlen);
130 do_close (s);
131 if (res < 0)
132 return -1;
133 }
134 else
135 {
136 if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
137 {
138 do_close (s);
139 return -1;
140 }
141 res = read (s, (char *) &thetime, sizeof (thetime));
142 do_close (s);
143 if (res < 0)
144 return (-1);
145 }
146 if (res != sizeof (thetime))
147 {
148 __set_errno (EIO);
149 return -1;
150 }
151 thetime = ntohl (thetime);
152 timep->tv_sec = thetime - TOFFSET;
153 timep->tv_usec = 0;
154 return 0;
155 }
156 libc_hidden_def (rtime)