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 #include <stdio.h>
47 #include <unistd.h>
48 #include <rpc/rpc.h>
49 #include <rpc/clnt.h>
50 #include <sys/types.h>
51 #include <sys/poll.h>
52 #include <sys/socket.h>
53 #include <sys/time.h>
54 #include <rpc/auth_des.h>
55 #include <errno.h>
56 #include <netinet/in.h>
57
58
59 #define NYEARS (u_long)(1970 - 1900)
60 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
61
62 static void do_close (int);
63
64 static void
do_close(int s)65 do_close (int s)
66 {
67 int save;
68
69 save = errno;
70 close (s);
71 __set_errno (save);
72 }
73
74 int
rtime(struct sockaddr_in * addrp,struct rpc_timeval * timep,struct rpc_timeval * timeout)75 rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
76 struct rpc_timeval *timeout)
77 {
78 int s;
79 struct pollfd fd;
80 int milliseconds;
81 int res;
82 /* RFC 868 says the time is transmitted as a 32-bit value. */
83 uint32_t thetime;
84 struct sockaddr_in from;
85 socklen_t fromlen;
86 int type;
87
88 if (timeout == NULL)
89 type = SOCK_STREAM;
90 else
91 type = SOCK_DGRAM;
92
93 s = socket (AF_INET, type, 0);
94 if (s < 0)
95 return (-1);
96
97 addrp->sin_family = AF_INET;
98 addrp->sin_port = htons (IPPORT_TIMESERVER);
99 if (type == SOCK_DGRAM)
100 {
101 res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
102 (struct sockaddr *) addrp, sizeof (*addrp));
103 if (res < 0)
104 {
105 do_close (s);
106 return -1;
107 }
108 milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
109 fd.fd = s;
110 fd.events = POLLIN;
111 do
112 res = poll (&fd, 1, milliseconds);
113 while (res < 0 && errno == EINTR);
114 if (res <= 0)
115 {
116 if (res == 0)
117 __set_errno (ETIMEDOUT);
118 do_close (s);
119 return (-1);
120 }
121 fromlen = sizeof (from);
122 res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
123 (struct sockaddr *) &from, &fromlen);
124 do_close (s);
125 if (res < 0)
126 return -1;
127 }
128 else
129 {
130 if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
131 {
132 do_close (s);
133 return -1;
134 }
135 res = read (s, (char *) &thetime, sizeof (thetime));
136 do_close (s);
137 if (res < 0)
138 return (-1);
139 }
140 if (res != sizeof (thetime))
141 {
142 __set_errno (EIO);
143 return -1;
144 }
145 thetime = ntohl (thetime);
146 timep->tv_sec = thetime - TOFFSET;
147 timep->tv_usec = 0;
148 return 0;
149 }
150