1 | /* |
2 | * Copyright (c) 2010, Oracle America, Inc. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following |
12 | * disclaimer in the documentation and/or other materials |
13 | * provided with the distribution. |
14 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
15 | * contributors may be used to endorse or promote products derived |
16 | * from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | /* |
32 | * rtime - get time from remote machine |
33 | * |
34 | * gets time, obtaining value from host |
35 | * on the udp/time socket. Since timeserver returns |
36 | * with time of day in seconds since Jan 1, 1900, must |
37 | * subtract seconds before Jan 1, 1970 to get |
38 | * what unix uses. |
39 | */ |
40 | #include <stdio.h> |
41 | #include <unistd.h> |
42 | #include <stdint.h> |
43 | #include <rpc/rpc.h> |
44 | #include <rpc/clnt.h> |
45 | #include <sys/types.h> |
46 | #include <sys/poll.h> |
47 | #include <sys/socket.h> |
48 | #include <sys/time.h> |
49 | #include <rpc/auth_des.h> |
50 | #include <errno.h> |
51 | #include <netinet/in.h> |
52 | |
53 | #define NYEARS (u_long)(1970 - 1900) |
54 | #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4))) |
55 | |
56 | static void do_close (int); |
57 | |
58 | static void |
59 | do_close (int s) |
60 | { |
61 | int save; |
62 | |
63 | save = errno; |
64 | __close (s); |
65 | __set_errno (save); |
66 | } |
67 | |
68 | int |
69 | rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep, |
70 | struct rpc_timeval *timeout) |
71 | { |
72 | int s; |
73 | struct pollfd fd; |
74 | int milliseconds; |
75 | int res; |
76 | /* RFC 868 says the time is transmitted as a 32-bit value. */ |
77 | uint32_t thetime; |
78 | struct sockaddr_in from; |
79 | socklen_t fromlen; |
80 | int type; |
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 | addrp->sin_port = htons (IPPORT_TIMESERVER); |
93 | if (type == SOCK_DGRAM) |
94 | { |
95 | res = __sendto (s, (char *) &thetime, sizeof (thetime), 0, |
96 | (struct sockaddr *) addrp, sizeof (*addrp)); |
97 | if (res < 0) |
98 | { |
99 | do_close (s); |
100 | return -1; |
101 | } |
102 | milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000); |
103 | fd.fd = s; |
104 | fd.events = POLLIN; |
105 | do |
106 | res = __poll (&fd, 1, milliseconds); |
107 | while (res < 0 && errno == EINTR); |
108 | if (res <= 0) |
109 | { |
110 | if (res == 0) |
111 | __set_errno (ETIMEDOUT); |
112 | do_close (s); |
113 | return (-1); |
114 | } |
115 | fromlen = sizeof (from); |
116 | res = __recvfrom (s, (char *) &thetime, sizeof (thetime), 0, |
117 | (struct sockaddr *) &from, &fromlen); |
118 | do_close (s); |
119 | if (res < 0) |
120 | return -1; |
121 | } |
122 | else |
123 | { |
124 | if (__connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0) |
125 | { |
126 | do_close (s); |
127 | return -1; |
128 | } |
129 | res = __read (s, (char *) &thetime, sizeof (thetime)); |
130 | do_close (s); |
131 | if (res < 0) |
132 | return (-1); |
133 | } |
134 | if (res != sizeof (thetime)) |
135 | { |
136 | __set_errno (EIO); |
137 | return -1; |
138 | } |
139 | thetime = ntohl (thetime); |
140 | timep->tv_sec = thetime - TOFFSET; |
141 | timep->tv_usec = 0; |
142 | return 0; |
143 | } |
144 | libc_hidden_nolink_sunrpc (rtime, GLIBC_2_1) |
145 | |