Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / timed / timed / measure.c
1 /*-
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)measure.c        8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/timed/timed/measure.c,v 1.6 1999/08/28 01:20:17 peter Exp $
35  * $DragonFly: src/usr.sbin/timed/timed/measure.c,v 1.2 2003/06/17 04:30:03 dillon Exp $
36  */
37
38 #include "globals.h"
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_icmp.h>
42
43 #define MSEC_DAY        (SECDAY*1000)
44
45 #define PACKET_IN       1024
46
47 #define MSGS            5               /* timestamps to average */
48 #define TRIALS          10              /* max # of timestamps sent */
49
50 extern int sock_raw;
51
52 int measure_delta;
53
54 static n_short seqno = 0;
55
56 /*
57  * Measures the differences between machines' clocks using
58  * ICMP timestamp messages.
59  */
60 int                                     /* status val defined in globals.h */
61 measure(maxmsec, wmsec, hname, addr, print)
62         u_long maxmsec;                 /* wait this many msec at most */
63         u_long wmsec;                   /* msec to wait for an answer */
64         char *hname;
65         struct sockaddr_in *addr;
66         int print;                      /* print complaints on stderr */
67 {
68         int length;
69         int measure_status;
70         int rcvcount, trials;
71         int cc, count;
72         fd_set ready;
73         long sendtime, recvtime, histime1, histime2;
74         long idelta, odelta, total;
75         long min_idelta, min_odelta;
76         struct timeval tdone, tcur, ttrans, twait, tout;
77         u_char packet[PACKET_IN], opacket[64];
78         register struct icmp *icp = (struct icmp *) packet;
79         register struct icmp *oicp = (struct icmp *) opacket;
80         struct ip *ip = (struct ip *) packet;
81
82         min_idelta = min_odelta = 0x7fffffff;
83         measure_status = HOSTDOWN;
84         measure_delta = HOSTDOWN;
85         errno = 0;
86
87         /* open raw socket used to measure time differences */
88         if (sock_raw < 0) {
89                 sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
90                 if (sock_raw < 0)  {
91                         syslog(LOG_ERR, "opening raw socket: %m");
92                         goto quit;
93                 }
94         }
95
96
97         /*
98          * empty the icmp input queue
99          */
100         FD_ZERO(&ready);
101         for (;;) {
102                 tout.tv_sec = tout.tv_usec = 0;
103                 FD_SET(sock_raw, &ready);
104                 if (select(sock_raw+1, &ready, 0,0, &tout)) {
105                         length = sizeof(struct sockaddr_in);
106                         cc = recvfrom(sock_raw, (char *)packet, PACKET_IN, 0,
107                                       0,&length);
108                         if (cc < 0)
109                                 goto quit;
110                         continue;
111                 }
112                 break;
113         }
114
115         /*
116          * Choose the smallest transmission time in each of the two
117          * directions. Use these two latter quantities to compute the delta
118          * between the two clocks.
119          */
120
121         oicp->icmp_type = ICMP_TSTAMP;
122         oicp->icmp_code = 0;
123         oicp->icmp_id = getpid();
124         oicp->icmp_rtime = 0;
125         oicp->icmp_ttime = 0;
126         oicp->icmp_seq = seqno;
127
128         FD_ZERO(&ready);
129
130 #ifdef sgi
131         sginap(1);                      /* start at a clock tick */
132 #endif /* sgi */
133
134         (void)gettimeofday(&tdone, 0);
135         mstotvround(&tout, maxmsec);
136         timevaladd(&tdone, &tout);              /* when we give up */
137
138         mstotvround(&twait, wmsec);
139
140         rcvcount = 0;
141         trials = 0;
142         while (rcvcount < MSGS) {
143                 (void)gettimeofday(&tcur, 0);
144
145                 /*
146                  * keep sending until we have sent the max
147                  */
148                 if (trials < TRIALS) {
149                         trials++;
150                         oicp->icmp_otime = htonl((tcur.tv_sec % SECDAY) * 1000
151                                                  + tcur.tv_usec / 1000);
152                         oicp->icmp_cksum = 0;
153                         oicp->icmp_cksum = in_cksum((u_short*)oicp,
154                                                     sizeof(*oicp));
155
156                         count = sendto(sock_raw, opacket, sizeof(*oicp), 0,
157                                        (struct sockaddr*)addr,
158                                        sizeof(struct sockaddr));
159                         if (count < 0) {
160                                 if (measure_status == HOSTDOWN)
161                                         measure_status = UNREACHABLE;
162                                 goto quit;
163                         }
164                         ++oicp->icmp_seq;
165
166                         ttrans = tcur;
167                         timevaladd(&ttrans, &twait);
168                 } else {
169                         ttrans = tdone;
170                 }
171
172                 while (rcvcount < trials) {
173                         timevalsub(&tout, &ttrans, &tcur);
174                         if (tout.tv_sec < 0)
175                                 tout.tv_sec = 0;
176
177                         FD_SET(sock_raw, &ready);
178                         count = select(sock_raw+1, &ready, (fd_set *)0,
179                                        (fd_set *)0, &tout);
180                         (void)gettimeofday(&tcur, (struct timezone *)0);
181                         if (count <= 0)
182                                 break;
183
184                         length = sizeof(struct sockaddr_in);
185                         cc = recvfrom(sock_raw, (char *)packet, PACKET_IN, 0,
186                                       0,&length);
187                         if (cc < 0)
188                                 goto quit;
189
190                         /*
191                          * got something.  See if it is ours
192                          */
193                         icp = (struct icmp *)(packet + (ip->ip_hl << 2));
194                         if (cc < sizeof(*ip)
195                             || icp->icmp_type != ICMP_TSTAMPREPLY
196                             || icp->icmp_id != oicp->icmp_id
197                             || icp->icmp_seq < seqno
198                             || icp->icmp_seq >= oicp->icmp_seq)
199                                 continue;
200
201
202                         sendtime = ntohl(icp->icmp_otime);
203                         recvtime = ((tcur.tv_sec % SECDAY) * 1000 +
204                                     tcur.tv_usec / 1000);
205
206                         total = recvtime-sendtime;
207                         if (total < 0)  /* do not hassle midnight */
208                                 continue;
209
210                         rcvcount++;
211                         histime1 = ntohl(icp->icmp_rtime);
212                         histime2 = ntohl(icp->icmp_ttime);
213                         /*
214                          * a host using a time format different from
215                          * msec. since midnight UT (as per RFC792) should
216                          * set the high order bit of the 32-bit time
217                          * value it transmits.
218                          */
219                         if ((histime1 & 0x80000000) != 0) {
220                                 measure_status = NONSTDTIME;
221                                 goto quit;
222                         }
223                         measure_status = GOOD;
224
225                         idelta = recvtime-histime2;
226                         odelta = histime1-sendtime;
227
228                         /* do not be confused by midnight */
229                         if (idelta < -MSEC_DAY/2) idelta += MSEC_DAY;
230                         else if (idelta > MSEC_DAY/2) idelta -= MSEC_DAY;
231
232                         if (odelta < -MSEC_DAY/2) odelta += MSEC_DAY;
233                         else if (odelta > MSEC_DAY/2) odelta -= MSEC_DAY;
234
235                         /* save the quantization error so that we can get a
236                          * measurement finer than our system clock.
237                          */
238                         if (total < MIN_ROUND) {
239                                 measure_delta = (odelta - idelta)/2;
240                                 goto quit;
241                         }
242
243                         if (idelta < min_idelta)
244                                 min_idelta = idelta;
245                         if (odelta < min_odelta)
246                                 min_odelta = odelta;
247
248                         measure_delta = (min_odelta - min_idelta)/2;
249                 }
250
251                 if (tcur.tv_sec > tdone.tv_sec
252                     || (tcur.tv_sec == tdone.tv_sec
253                         && tcur.tv_usec >= tdone.tv_usec))
254                         break;
255         }
256
257 quit:
258         seqno += TRIALS;                /* allocate our sequence numbers */
259
260         /*
261          * If no answer is received for TRIALS consecutive times,
262          * the machine is assumed to be down
263          */
264         if (measure_status == GOOD) {
265                 if (trace) {
266                         fprintf(fd,
267                                 "measured delta %4d, %d trials to %-15s %s\n",
268                                 measure_delta, trials,
269                                 inet_ntoa(addr->sin_addr), hname);
270                 }
271         } else if (print) {
272                 if (errno != 0)
273                         warn("measure %s", hname);
274         } else {
275                 if (errno != 0) {
276                         syslog(LOG_ERR, "measure %s: %m", hname);
277                 } else {
278                         syslog(LOG_ERR, "measure: %s did not respond", hname);
279                 }
280                 if (trace) {
281                         fprintf(fd,
282                                 "measure: %s failed after %d trials\n",
283                                 hname, trials);
284                         (void)fflush(fd);
285                 }
286         }
287
288         return(measure_status);
289 }
290
291
292
293
294
295 /*
296  * round a number of milliseconds into a struct timeval
297  */
298 void
299 mstotvround(res, x)
300         struct timeval *res;
301         long x;
302 {
303 #ifndef sgi
304         if (x < 0)
305                 x = -((-x + 3)/5);
306         else
307                 x = (x+3)/5;
308         x *= 5;
309 #endif /* sgi */
310         res->tv_sec = x/1000;
311         res->tv_usec = (x-res->tv_sec*1000)*1000;
312         if (res->tv_usec < 0) {
313                 res->tv_usec += 1000000;
314                 res->tv_sec--;
315         }
316 }
317
318 void
319 timevaladd(tv1, tv2)
320         struct timeval *tv1, *tv2;
321 {
322         tv1->tv_sec += tv2->tv_sec;
323         tv1->tv_usec += tv2->tv_usec;
324         if (tv1->tv_usec >= 1000000) {
325                 tv1->tv_sec++;
326                 tv1->tv_usec -= 1000000;
327         }
328         if (tv1->tv_usec < 0) {
329                 tv1->tv_sec--;
330                 tv1->tv_usec += 1000000;
331         }
332 }
333
334 void
335 timevalsub(res, tv1, tv2)
336         struct timeval *res, *tv1, *tv2;
337 {
338         res->tv_sec = tv1->tv_sec - tv2->tv_sec;
339         res->tv_usec = tv1->tv_usec - tv2->tv_usec;
340         if (res->tv_usec >= 1000000) {
341                 res->tv_sec++;
342                 res->tv_usec -= 1000000;
343         }
344         if (res->tv_usec < 0) {
345                 res->tv_sec--;
346                 res->tv_usec += 1000000;
347         }
348 }