2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
13 * Revision 2.1 90/08/07 19:23:23
14 * Start with clean version to sync test and dev trees.
16 * Revision 1.7 89/12/22 20:33:21
17 * hp/ux specific (initial port to it); Added <afs/param.h> and special include files for HPUX and misc other changes
19 * Revision 1.6 89/12/11 14:26:16
20 * Added code to support AIX 2.2.1.
22 * Revision 1.5 89/05/24 12:27:44
23 * Latest May 18, Version 4.3 release from UMD.
25 * Revision 3.4.1.3 89/05/18 18:33:50
26 * Added support few a new type of unsigned long to double compiler brokenness,
27 * called GENERIC_UNS_BUG. If this is defined, then the unsigned long is
28 * shifted right one bit, the high-order bit of the result is cleared, then
29 * converted to a double. The double is multiplied by 2.0, and the a 1.0 is
30 * optionall added to it if the low order bit of the original unsigned long
33 * Revision 3.4.1.2 89/03/29 12:46:02
34 * Check for success sending query before trying to listen for answers. Will
35 * catch case of no server running and an ICMP port unreachable being returned.
37 * Revision 3.4.1.1 89/03/22 18:32:19
38 * patch3: Use new RCS headers.
40 * Revision 3.4 89/03/17 18:37:18
41 * Latest test release.
43 * Revision 3.3 89/03/15 14:20:03
44 * New baseline for next release.
46 * Revision 3.2 89/03/07 18:29:22
47 * New version of UNIX NTP daemon based on the 6 March 1989 draft of the new
48 * NTP protocol spec. This module has mostly cosmetic changes.
50 * Revision 3.1.1.1 89/02/15 08:59:27
51 * *** empty log message ***
54 * Revision 3.1 89/01/30 14:43:18
55 * Second UNIX NTP test release.
57 * Revision 3.0 88/12/12 15:58:59
58 * Test release of new UNIX NTP software. This version should conform to the
59 * revised NTP protocol specification.
63 #include <afs/param.h>
65 #include <sys/types.h>
66 #include <sys/param.h>
69 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
74 #include <sys/syslog.h>
86 * The nice thing here is that the quantity is NEVER signed.
93 #ifdef GENERIC_UNS_BUG
96 i = ntohl(t->fraction);
97 a = (afs_int32)((i >> 1) & 0x7fffffff);
101 a = a / (4.294967296e9); /* shift dec point over by 32 bits */
102 i = ntohl(t->int_part);
103 b = (afs_int32)((i >> 1) & 0x7fffffff);
107 #else /* GENERIC_UNS_BUG */
108 a = (afs_uint32) ntohl(t->fraction);
109 #ifdef VAX_COMPILER_FLT_BUG
110 if (a < 0.0) a += 4.294967296e9;
112 a = a / (4.294967296e9);/* shift dec point over by 32 bits */
113 b = (afs_uint32) ntohl(t->int_part);
114 #ifdef VAX_COMPILER_FLT_BUG
115 if (b < 0.0) b += 4.294967296e9;
117 #endif /* GENERIC_UNS_BUG */
122 * Here we have to worry about the high order bit being signed
132 if (ntohl(t->int_part) & 0x80000000) {
133 a = ntohl(~t->fraction);
134 #ifdef VAX_COMPILER_FLT_BUG
135 if (a < 0.0) a += 4.294967296e9;
137 a = a / (4.294967296e9);
138 b = ntohl(~t->int_part);
139 #ifdef VAX_COMPILER_FLT_BUG
140 if (b < 0.0) b += 4.294967296e9;
145 a = ntohl(t->fraction);
146 #ifdef VAX_COMPILER_FLT_BUG
147 if (a < 0.0) a += 4.294967296e9;
149 a = a / (4.294967296e9);
150 b = ntohl(t->int_part);
151 #ifdef VAX_COMPILER_FLT_BUG
152 if (b < 0.0) b += 4.294967296e9;
161 * Here we have to worry about the high order bit being signed
169 if (ntohs(t->int_part) & 0x8000) {
170 a = ntohs(~t->fraction & 0xFFFF);
171 a = a / 65536.0; /* shift dec point over by 16 bits */
172 a += ntohs(~t->int_part & 0xFFFF);
175 a = ntohs(t->fraction);
176 a = a / 65536.0; /* shift dec point over by 16 bits */
177 a += ntohs(t->int_part);
183 double_to_l_fixed(t, value)
189 if (value >= (double) 0.0) {
191 temp = value - t->int_part;
192 temp *= 4.294967296e9;
194 t->int_part = htonl(t->int_part);
195 t->fraction = htonl(t->fraction);
199 temp = value - t->int_part;
200 temp *= 4.294967296e9;
202 t->int_part = htonl(~t->int_part);
203 t->fraction = htonl(~t->fraction);
208 double_to_s_fixed(t, value)
214 if (value >= (double) 0.0) {
216 temp = value - t->int_part;
219 t->int_part = htons(t->int_part);
220 t->fraction = htons(t->fraction);
224 temp = value - t->int_part;
227 t->int_part = htons(~t->int_part);
228 t->fraction = htons(~t->fraction);
232 in the sun, trying to assign a float between 2^31 and 2^32
233 results in the value 2^31. Neither 4.2bsd nor VMS have this
234 problem. Reported it to Bob O'Brien of SMI
238 struct l_fixedpt *stampp;
244 stampp->int_part = ntohl(JAN_1970 + tvp->tv_sec);
245 dd = (float) tvp->tv_usec / 1000000.0;
246 tt = dd * 2147483648.0;
247 stampp->fraction = ntohl((tt << 1));
251 struct l_fixedpt *stampp;
254 stampp->int_part = ntohl((afs_uint32) (JAN_1970 + tvp->tv_sec));
255 stampp->fraction = ntohl((afs_uint32) ((float) tvp->tv_usec * 4294.967295));
260 * ntoa is similar to inet_ntoa, but cycles through a set of 8 buffers
261 * so it can be invoked several times in a function parameter list.
266 struct in_addr in_addr;
269 static char bufs[8][20];
270 unsigned char *p = (unsigned char *) &in_addr.s_addr;
272 i = (i + 1) % (sizeof bufs / sizeof bufs[0]);
273 sprintf (bufs[i], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
277 /* calculate effective precision, but repeated calls to gettimeofday */
279 int MeasurePrecision (intervalP)
282 #if defined(AFS_SUN5_ENV)
283 #define MAXTIMEDIFFS 100
285 #define MAXTIMEDIFFS 10
288 int diff[MAXTIMEDIFFS];
295 gettimeofday (&tv0, 0);
297 while (nDiff < MAXTIMEDIFFS) {
298 struct timeval tv, ntv;
299 int counting = 2; /* a counting kernel */
300 gettimeofday (&tv, 0);
302 gettimeofday (&ntv, 0);
305 * Bail if we are taking too long -- 30 seconds is arbitrary,
306 * but better than the previous approach, which was to bail
307 * after an arbitrary count of calls to gettimeofday(). This
308 * caused problems because the machines kept getting faster
309 * and the count kept getting exceeded.
311 if (ntv.tv_sec - tv0.tv_sec > 30) return 0;
313 interval = (ntv.tv_sec - tv.tv_sec)*1000000 +
314 ntv.tv_usec - tv.tv_usec;
315 if (interval <= counting) counting = interval+2;
316 } while (interval <= counting); /* RT & sun4/280 kernels count */
317 if (interval < 0) return 0; /* shouldn't happen but who knows... */
318 if (interval > 0) diff[nDiff++] = interval;
321 /* find average interval */
323 for (i=0; i<MAXTIMEDIFFS; i++)
325 interval = (interval + (MAXTIMEDIFFS/2)) / MAXTIMEDIFFS; /* round */
326 if (interval == 0) return 0; /* some problem... */
327 if (intervalP) *intervalP = interval;
329 /* calculate binary exponent of interval in seconds */
332 while (q > interval) {