1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions are met: |
---|
9 | * - Redistributions of source code must retain the above copyright notice, |
---|
10 | * this list of conditions and the following disclaimer. |
---|
11 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * - Neither the name of Sun Microsystems, 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 "AS IS" |
---|
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | * POSSIBILITY OF SUCH DAMAGE. |
---|
29 | */ |
---|
30 | |
---|
31 | /* |
---|
32 | * Copyright (c) 1988 by Sun Microsystems, Inc. |
---|
33 | |
---|
34 | */ |
---|
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 | #include "namespace.h" |
---|
46 | #include <stdlib.h> |
---|
47 | #include <string.h> |
---|
48 | #include <unistd.h> |
---|
49 | #include <errno.h> |
---|
50 | #include <sys/types.h> |
---|
51 | #include <sys/socket.h> |
---|
52 | #include <sys/time.h> |
---|
53 | #include <netinet/in.h> |
---|
54 | #include <stdio.h> |
---|
55 | #include <netdb.h> |
---|
56 | #include "un-namespace.h" |
---|
57 | |
---|
58 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
59 | static char sccsid[] = "@(#)rtime.c 2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; |
---|
60 | #endif |
---|
61 | #include <sys/cdefs.h> |
---|
62 | __FBSDID("$FreeBSD$"); |
---|
63 | |
---|
64 | extern int _rpc_dtablesize( void ); |
---|
65 | |
---|
66 | #define NYEARS (unsigned long)(1970 - 1900) |
---|
67 | #define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4))) |
---|
68 | |
---|
69 | static void do_close( int ); |
---|
70 | |
---|
71 | int |
---|
72 | rtime(addrp, timep, timeout) |
---|
73 | struct sockaddr_in *addrp; |
---|
74 | struct timeval *timep; |
---|
75 | struct timeval *timeout; |
---|
76 | { |
---|
77 | int s; |
---|
78 | fd_set readfds; |
---|
79 | int res; |
---|
80 | unsigned long thetime; |
---|
81 | struct sockaddr_in from; |
---|
82 | socklen_t fromlen; |
---|
83 | int type; |
---|
84 | struct servent *serv; |
---|
85 | |
---|
86 | if (timeout == NULL) { |
---|
87 | type = SOCK_STREAM; |
---|
88 | } else { |
---|
89 | type = SOCK_DGRAM; |
---|
90 | } |
---|
91 | s = _socket(AF_INET, type, 0); |
---|
92 | if (s < 0) { |
---|
93 | return(-1); |
---|
94 | } |
---|
95 | addrp->sin_family = AF_INET; |
---|
96 | |
---|
97 | /* TCP and UDP port are the same in this case */ |
---|
98 | if ((serv = getservbyname("time", "tcp")) == NULL) { |
---|
99 | return(-1); |
---|
100 | } |
---|
101 | |
---|
102 | addrp->sin_port = serv->s_port; |
---|
103 | |
---|
104 | if (type == SOCK_DGRAM) { |
---|
105 | res = _sendto(s, (char *)&thetime, sizeof(thetime), 0, |
---|
106 | (struct sockaddr *)addrp, sizeof(*addrp)); |
---|
107 | if (res < 0) { |
---|
108 | do_close(s); |
---|
109 | return(-1); |
---|
110 | } |
---|
111 | do { |
---|
112 | FD_ZERO(&readfds); |
---|
113 | FD_SET(s, &readfds); |
---|
114 | res = _select(_rpc_dtablesize(), &readfds, |
---|
115 | (fd_set *)NULL, (fd_set *)NULL, timeout); |
---|
116 | } while (res < 0 && errno == EINTR); |
---|
117 | if (res <= 0) { |
---|
118 | if (res == 0) { |
---|
119 | errno = ETIMEDOUT; |
---|
120 | } |
---|
121 | do_close(s); |
---|
122 | return(-1); |
---|
123 | } |
---|
124 | fromlen = sizeof(from); |
---|
125 | res = _recvfrom(s, (char *)&thetime, sizeof(thetime), 0, |
---|
126 | (struct sockaddr *)&from, &fromlen); |
---|
127 | do_close(s); |
---|
128 | if (res < 0) { |
---|
129 | return(-1); |
---|
130 | } |
---|
131 | } else { |
---|
132 | if (_connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) { |
---|
133 | do_close(s); |
---|
134 | return(-1); |
---|
135 | } |
---|
136 | res = _read(s, (char *)&thetime, sizeof(thetime)); |
---|
137 | do_close(s); |
---|
138 | if (res < 0) { |
---|
139 | return(-1); |
---|
140 | } |
---|
141 | } |
---|
142 | if (res != sizeof(thetime)) { |
---|
143 | errno = EIO; |
---|
144 | return(-1); |
---|
145 | } |
---|
146 | thetime = ntohl(thetime); |
---|
147 | timep->tv_sec = thetime - TOFFSET; |
---|
148 | timep->tv_usec = 0; |
---|
149 | return(0); |
---|
150 | } |
---|
151 | |
---|
152 | static void |
---|
153 | do_close(s) |
---|
154 | int s; |
---|
155 | { |
---|
156 | int save; |
---|
157 | |
---|
158 | save = errno; |
---|
159 | (void)_close(s); |
---|
160 | errno = save; |
---|
161 | } |
---|