1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40 /*
41 * rtime - get time from remote machine
42 *
43 * gets time, obtaining value from host
44 * on the udp/time socket. Since timeserver returns
45 * with time of day in seconds since Jan 1, 1900, must
46 * subtract seconds before Jan 1, 1970 to get
47 * what unix uses.
48 */
49
50 #include <sys/types.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <errno.h>
56 #include <netinet/in.h>
57 #include <malloc.h>
58
59 #define NYEARS (1970 - 1900)
60 #define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
61
62 extern int _socket(int, int, int);
63 extern int _sendto(int, const char *, int, int,
64 const struct sockaddr *, int);
65 extern int _recvfrom(int, char *, int, int,
66 struct sockaddr *, int *);
67 extern int _connect(int, struct sockaddr *, int);
68 extern int __rpc_dtbsize();
69 extern ssize_t read(int, void *, size_t);
70 extern int close(int);
71 static void do_close();
72
73 int
rtime(addrp,timep,timeout)74 rtime(addrp, timep, timeout)
75 struct sockaddr_in *addrp;
76 struct timeval *timep;
77 struct timeval *timeout;
78 {
79 int s;
80 fd_set readfds;
81 int res;
82 uint_t thetime;
83 struct sockaddr_in from;
84 int fromlen;
85 int type;
86
87 if (timeout == NULL) {
88 type = SOCK_STREAM;
89 } else {
90 type = SOCK_DGRAM;
91 }
92 s = _socket(AF_INET, type, 0);
93 if (s < 0) {
94 return (-1);
95 }
96 addrp->sin_family = AF_INET;
97 addrp->sin_port = htons(IPPORT_TIMESERVER);
98 if (type == SOCK_DGRAM) {
99 res = _sendto(s, (char *)&thetime, sizeof (thetime), 0,
100 (struct sockaddr *)addrp, sizeof (*addrp));
101 if (res < 0) {
102 do_close(s);
103 return (-1);
104 }
105 do {
106 FD_ZERO(&readfds);
107 FD_SET(s, &readfds);
108 res = select(__rpc_dtbsize(), &readfds, NULL,
109 NULL, timeout);
110 } while (res < 0 && errno == EINTR);
111 if (res <= 0) {
112 if (res == 0) {
113 errno = ETIMEDOUT;
114 }
115 do_close(s);
116 return (-1);
117 }
118 fromlen = sizeof (from);
119 res = _recvfrom(s, (char *)&thetime, sizeof (thetime), 0,
120 (struct sockaddr *)&from, &fromlen);
121 do_close(s);
122 if (res < 0) {
123 return (-1);
124 }
125 } else {
126 if (_connect(s, (struct sockaddr *)addrp,
127 sizeof (*addrp)) < 0) {
128 do_close(s);
129 return (-1);
130 }
131 res = read(s, (char *)&thetime, sizeof (thetime));
132 do_close(s);
133 if (res < 0) {
134 return (-1);
135 }
136 }
137 if (res != sizeof (thetime)) {
138 errno = EIO;
139 return (-1);
140 }
141 thetime = ntohl(thetime);
142
143 thetime = thetime - TOFFSET;
144 #ifdef _ILP32
145 if (thetime > INT32_MAX)
146 thetime = INT32_MAX;
147 #endif
148 timep->tv_sec = thetime;
149 timep->tv_usec = 0;
150 return (0);
151 }
152
153 static void
do_close(s)154 do_close(s)
155 int s;
156 {
157 int save;
158
159 save = errno;
160 (void) close(s);
161 errno = save;
162 }
163