Deleted Added
full compact
ntp_util.c (82498) ntp_util.c (132451)
1/*
2 * ntp_util.c - stuff I didn't have any other place for
3 */
4
5#ifdef HAVE_CONFIG_H
6# include <config.h>
7#endif
8

--- 25 unchanged lines hidden (view full) ---

34#endif
35
36#if defined(VMS)
37#include <descrip.h>
38#endif /* VMS */
39
40/*
41 * This contains odds and ends. Right now the only thing you'll find
1/*
2 * ntp_util.c - stuff I didn't have any other place for
3 */
4
5#ifdef HAVE_CONFIG_H
6# include <config.h>
7#endif
8

--- 25 unchanged lines hidden (view full) ---

34#endif
35
36#if defined(VMS)
37#include <descrip.h>
38#endif /* VMS */
39
40/*
41 * This contains odds and ends. Right now the only thing you'll find
42 * in here is the hourly stats printer and some code to support rereading
43 * the keys file, but I may eventually put other things in here such as
44 * code to do something with the leap bits.
42 * in here is the hourly stats printer and some code to support
43 * rereading the keys file, but I may eventually put other things in
44 * here such as code to do something with the leap bits.
45 */
45 */
46
47/*
48 * Name of the keys file
49 */
50static char *key_file_name;
51
52/*
53 * The name of the drift_comp file and the temporary.
54 */

--- 16 unchanged lines hidden (view full) ---

71#endif
72
73static char statsdir[MAXPATHLEN] = NTP_VAR;
74
75static FILEGEN peerstats;
76static FILEGEN loopstats;
77static FILEGEN clockstats;
78static FILEGEN rawstats;
46/*
47 * Name of the keys file
48 */
49static char *key_file_name;
50
51/*
52 * The name of the drift_comp file and the temporary.
53 */

--- 16 unchanged lines hidden (view full) ---

70#endif
71
72static char statsdir[MAXPATHLEN] = NTP_VAR;
73
74static FILEGEN peerstats;
75static FILEGEN loopstats;
76static FILEGEN clockstats;
77static FILEGEN rawstats;
78static FILEGEN sysstats;
79#ifdef OPENSSL
80static FILEGEN cryptostats;
81#endif /* OPENSSL */
79
80/*
81 * This controls whether stats are written to the fileset. Provided
82 * so that ntpdc can turn off stats when the file system fills up.
83 */
84int stats_control;
85
86/*

--- 5 unchanged lines hidden (view full) ---

92 stats_drift_file = 0;
93 stats_temp_file = 0;
94 key_file_name = 0;
95
96#define PEERNAME "peerstats"
97#define LOOPNAME "loopstats"
98#define CLOCKNAME "clockstats"
99#define RAWNAME "rawstats"
82
83/*
84 * This controls whether stats are written to the fileset. Provided
85 * so that ntpdc can turn off stats when the file system fills up.
86 */
87int stats_control;
88
89/*

--- 5 unchanged lines hidden (view full) ---

95 stats_drift_file = 0;
96 stats_temp_file = 0;
97 key_file_name = 0;
98
99#define PEERNAME "peerstats"
100#define LOOPNAME "loopstats"
101#define CLOCKNAME "clockstats"
102#define RAWNAME "rawstats"
103#define STANAME "systats"
104#ifdef OPENSSL
105#define CRYPTONAME "cryptostats"
106#endif /* OPENSSL */
107
100 peerstats.fp = NULL;
101 peerstats.prefix = &statsdir[0];
102 peerstats.basename = (char*)emalloc(strlen(PEERNAME)+1);
103 strcpy(peerstats.basename, PEERNAME);
104 peerstats.id = 0;
105 peerstats.type = FILEGEN_DAY;
106 peerstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
107 filegen_register("peerstats", &peerstats);

--- 20 unchanged lines hidden (view full) ---

128 rawstats.prefix = &statsdir[0];
129 rawstats.basename = (char*)emalloc(strlen(RAWNAME)+1);
130 strcpy(rawstats.basename, RAWNAME);
131 rawstats.id = 0;
132 rawstats.type = FILEGEN_DAY;
133 rawstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
134 filegen_register("rawstats", &rawstats);
135
108 peerstats.fp = NULL;
109 peerstats.prefix = &statsdir[0];
110 peerstats.basename = (char*)emalloc(strlen(PEERNAME)+1);
111 strcpy(peerstats.basename, PEERNAME);
112 peerstats.id = 0;
113 peerstats.type = FILEGEN_DAY;
114 peerstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
115 filegen_register("peerstats", &peerstats);

--- 20 unchanged lines hidden (view full) ---

136 rawstats.prefix = &statsdir[0];
137 rawstats.basename = (char*)emalloc(strlen(RAWNAME)+1);
138 strcpy(rawstats.basename, RAWNAME);
139 rawstats.id = 0;
140 rawstats.type = FILEGEN_DAY;
141 rawstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
142 filegen_register("rawstats", &rawstats);
143
144 sysstats.fp = NULL;
145 sysstats.prefix = &statsdir[0];
146 sysstats.basename = (char*)emalloc(strlen(STANAME)+1);
147 strcpy(sysstats.basename, STANAME);
148 sysstats.id = 0;
149 sysstats.type = FILEGEN_DAY;
150 sysstats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
151 filegen_register("sysstats", &sysstats);
152
153#ifdef OPENSSL
154 cryptostats.fp = NULL;
155 cryptostats.prefix = &statsdir[0];
156 cryptostats.basename = (char*)emalloc(strlen(CRYPTONAME)+1);
157 strcpy(cryptostats.basename, CRYPTONAME);
158 cryptostats.id = 0;
159 cryptostats.type = FILEGEN_DAY;
160 cryptostats.flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
161 filegen_register("cryptostats", &cryptostats);
162#endif /* OPENSSL */
163
136#undef PEERNAME
137#undef LOOPNAME
138#undef CLOCKNAME
139#undef RAWNAME
164#undef PEERNAME
165#undef LOOPNAME
166#undef CLOCKNAME
167#undef RAWNAME
140
168#undef STANAME
169#ifdef OPENSSL
170#undef CRYPTONAME
171#endif /* OPENSSL */
141}
142
143
144/*
145 * hourly_stats - print some interesting stats
146 */
147void
148hourly_stats(void)

--- 29 unchanged lines hidden (view full) ---

178 *
179 * CAVEAT: settimeofday() steps the sun clock by about 800 us,
180 * so setting DOSYNCTODR seems a bad idea in the
181 * case of us resolution
182 */
183
184#if !defined(VMS)
185 /* (prr) getpriority returns -1 on error, but -1 is also a valid
172}
173
174
175/*
176 * hourly_stats - print some interesting stats
177 */
178void
179hourly_stats(void)

--- 29 unchanged lines hidden (view full) ---

209 *
210 * CAVEAT: settimeofday() steps the sun clock by about 800 us,
211 * so setting DOSYNCTODR seems a bad idea in the
212 * case of us resolution
213 */
214
215#if !defined(VMS)
216 /* (prr) getpriority returns -1 on error, but -1 is also a valid
186 * return value (!), so instead we have to zero errno before the call
187 * and check it for non-zero afterwards.
217 * return value (!), so instead we have to zero errno before the
218 * call and check it for non-zero afterwards.
188 */
219 */
189
190 errno = 0;
191 prio_set = 0;
192 o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
193
220 errno = 0;
221 prio_set = 0;
222 o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
223
194 /* (prr) if getpriority succeeded, call setpriority to raise
224 /*
225 * (prr) if getpriority succeeded, call setpriority to raise
195 * scheduling priority as high as possible. If that succeeds
196 * as well, set the prio_set flag so we remember to reset
226 * scheduling priority as high as possible. If that succeeds
227 * as well, set the prio_set flag so we remember to reset
197 * priority to its previous value below. Note that on Solaris 2.6
198 * (and beyond?), both getpriority and setpriority will fail with
199 * ESRCH, because sched_setscheduler (called from main) put us in
200 * the real-time scheduling class which setpriority doesn't know about.
201 * Being in the real-time class is better than anything setpriority
202 * can do, anyhow, so this error is silently ignored.
228 * priority to its previous value below. Note that on Solaris
229 * 2.6 (and beyond?), both getpriority and setpriority will fail
230 * with ESRCH, because sched_setscheduler (called from main) put
231 * us in the real-time scheduling class which setpriority
232 * doesn't know about. Being in the real-time class is better
233 * than anything setpriority can do, anyhow, so this error is
234 * silently ignored.
203 */
235 */
204
205 if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
236 if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
206 prio_set = 1; /* overdrive */
237 prio_set = 1; /* overdrive */
207#endif /* VMS */
208#ifdef HAVE_GETCLOCK
209 (void) getclock(TIMEOFDAY, &ts);
210 tv.tv_sec = ts.tv_sec;
211 tv.tv_usec = ts.tv_nsec / 1000;
212#else /* not HAVE_GETCLOCK */
213 GETTIMEOFDAY(&tv,(struct timezone *)NULL);
214#endif /* not HAVE_GETCLOCK */
238#endif /* VMS */
239#ifdef HAVE_GETCLOCK
240 (void) getclock(TIMEOFDAY, &ts);
241 tv.tv_sec = ts.tv_sec;
242 tv.tv_usec = ts.tv_nsec / 1000;
243#else /* not HAVE_GETCLOCK */
244 GETTIMEOFDAY(&tv,(struct timezone *)NULL);
245#endif /* not HAVE_GETCLOCK */
215 if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
216 {
246 if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0) {
217 msyslog(LOG_ERR, "can't sync battery time: %m");
218 }
219#if !defined(VMS)
220 if (prio_set)
247 msyslog(LOG_ERR, "can't sync battery time: %m");
248 }
249#if !defined(VMS)
250 if (prio_set)
221 setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
251 setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
222#endif /* VMS */
223#endif /* DOSYNCTODR */
224
225 NLOG(NLOG_SYSSTATIST)
226 msyslog(LOG_INFO,
227 "offset %.6f sec freq %.3f ppm error %.6f poll %d",
252#endif /* VMS */
253#endif /* DOSYNCTODR */
254
255 NLOG(NLOG_SYSSTATIST)
256 msyslog(LOG_INFO,
257 "offset %.6f sec freq %.3f ppm error %.6f poll %d",
228 last_offset, drift_comp * 1e6, sys_jitter, sys_poll);
258 last_offset, drift_comp * 1e6, sys_jitter,
259 sys_poll);
229
230
260
261
262 record_sys_stats();
231 if (stats_drift_file != 0) {
232 if ((fp = fopen(stats_temp_file, "w")) == NULL) {
233 msyslog(LOG_ERR, "can't open %s: %m",
234 stats_temp_file);
235 return;
236 }
237 fprintf(fp, "%.3f\n", drift_comp * 1e6);
238 (void)fclose(fp);
239 /* atomic */
240#ifdef SYS_WINNT
263 if (stats_drift_file != 0) {
264 if ((fp = fopen(stats_temp_file, "w")) == NULL) {
265 msyslog(LOG_ERR, "can't open %s: %m",
266 stats_temp_file);
267 return;
268 }
269 fprintf(fp, "%.3f\n", drift_comp * 1e6);
270 (void)fclose(fp);
271 /* atomic */
272#ifdef SYS_WINNT
241 (void) unlink(stats_drift_file); /* rename semantics differ under NT */
273 (void) _unlink(stats_drift_file); /* rename semantics differ under NT */
242#endif /* SYS_WINNT */
243
244#ifndef NO_RENAME
245 (void) rename(stats_temp_file, stats_drift_file);
246#else
247 /* we have no rename NFS of ftp in use*/
248 if ((fp = fopen(stats_drift_file, "w")) == NULL) {
249 msyslog(LOG_ERR, "can't open %s: %m",

--- 26 unchanged lines hidden (view full) ---

276 char *invalue /* only one type so far */
277 )
278{
279 FILE *fp;
280 char *value;
281 double old_drift;
282 int len;
283
274#endif /* SYS_WINNT */
275
276#ifndef NO_RENAME
277 (void) rename(stats_temp_file, stats_drift_file);
278#else
279 /* we have no rename NFS of ftp in use*/
280 if ((fp = fopen(stats_drift_file, "w")) == NULL) {
281 msyslog(LOG_ERR, "can't open %s: %m",

--- 26 unchanged lines hidden (view full) ---

308 char *invalue /* only one type so far */
309 )
310{
311 FILE *fp;
312 char *value;
313 double old_drift;
314 int len;
315
284 /* Expand environment strings under Windows NT, since the command
285 * interpreter doesn't do this, the program must.
316 /*
317 * Expand environment strings under Windows NT, since the
318 * command interpreter doesn't do this, the program must.
286 */
287#ifdef SYS_WINNT
288 char newvalue[MAX_PATH], parameter[MAX_PATH];
289
319 */
320#ifdef SYS_WINNT
321 char newvalue[MAX_PATH], parameter[MAX_PATH];
322
290 if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH))
291 {
323 if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
292 switch(item) {
293 case STATS_FREQ_FILE:
294 strcpy(parameter,"STATS_FREQ_FILE");
295 break;
296 case STATS_STATSDIR:
297 strcpy(parameter,"STATS_STATSDIR");
298 break;
299 case STATS_PID_FILE:
300 strcpy(parameter,"STATS_PID_FILE");
301 break;
302 default:
303 strcpy(parameter,"UNKNOWN");
304 break;
305 }
306 value = invalue;
307
308 msyslog(LOG_ERR,
309 "ExpandEnvironmentStrings(%s) failed: %m\n", parameter);
324 switch(item) {
325 case STATS_FREQ_FILE:
326 strcpy(parameter,"STATS_FREQ_FILE");
327 break;
328 case STATS_STATSDIR:
329 strcpy(parameter,"STATS_STATSDIR");
330 break;
331 case STATS_PID_FILE:
332 strcpy(parameter,"STATS_PID_FILE");
333 break;
334 default:
335 strcpy(parameter,"UNKNOWN");
336 break;
337 }
338 value = invalue;
339
340 msyslog(LOG_ERR,
341 "ExpandEnvironmentStrings(%s) failed: %m\n", parameter);
310 }
311 else
342 } else {
312 value = newvalue;
343 value = newvalue;
344 }
313#else
314 value = invalue;
315#endif /* SYS_WINNT */
316
345#else
346 value = invalue;
347#endif /* SYS_WINNT */
348
317
318
319 switch(item) {
320 case STATS_FREQ_FILE:
321 if (stats_drift_file != 0) {
322 (void) free(stats_drift_file);
323 (void) free(stats_temp_file);
324 stats_drift_file = 0;
325 stats_temp_file = 0;
326 }
327
328 if (value == 0 || (len = strlen(value)) == 0)
329 break;
330
331 stats_drift_file = (char*)emalloc((u_int)(len + 1));
332#if !defined(VMS)
349 switch(item) {
350 case STATS_FREQ_FILE:
351 if (stats_drift_file != 0) {
352 (void) free(stats_drift_file);
353 (void) free(stats_temp_file);
354 stats_drift_file = 0;
355 stats_temp_file = 0;
356 }
357
358 if (value == 0 || (len = strlen(value)) == 0)
359 break;
360
361 stats_drift_file = (char*)emalloc((u_int)(len + 1));
362#if !defined(VMS)
333 stats_temp_file = (char*)emalloc((u_int)(len + sizeof(".TEMP")));
363 stats_temp_file = (char*)emalloc((u_int)(len +
364 sizeof(".TEMP")));
334#else
365#else
335 stats_temp_file = (char*)emalloc((u_int)(len + sizeof("-TEMP")));
366 stats_temp_file = (char*)emalloc((u_int)(len +
367 sizeof("-TEMP")));
336#endif /* VMS */
337 memmove(stats_drift_file, value, (unsigned)(len+1));
338 memmove(stats_temp_file, value, (unsigned)len);
339#if !defined(VMS)
368#endif /* VMS */
369 memmove(stats_drift_file, value, (unsigned)(len+1));
370 memmove(stats_temp_file, value, (unsigned)len);
371#if !defined(VMS)
340 memmove(stats_temp_file + len, ".TEMP", sizeof(".TEMP"));
372 memmove(stats_temp_file + len, ".TEMP",
373 sizeof(".TEMP"));
341#else
374#else
342 memmove(stats_temp_file + len, "-TEMP", sizeof("-TEMP"));
375 memmove(stats_temp_file + len, "-TEMP",
376 sizeof("-TEMP"));
343#endif /* VMS */
344
345 /*
377#endif /* VMS */
378
379 /*
346 * Open drift file and read frequency
380 * Open drift file and read frequency. If the file is
381 * missing or contains errors, tell the loop to reset.
347 */
348 if ((fp = fopen(stats_drift_file, "r")) == NULL) {
382 */
383 if ((fp = fopen(stats_drift_file, "r")) == NULL) {
384 loop_config(LOOP_DRIFTCOMP, 1e9);
349 break;
350 }
351 if (fscanf(fp, "%lf", &old_drift) != 1) {
385 break;
386 }
387 if (fscanf(fp, "%lf", &old_drift) != 1) {
352 msyslog(LOG_ERR, "Un-parsable frequency in %s",
388 msyslog(LOG_ERR, "Frequency format error in %s",
353 stats_drift_file);
389 stats_drift_file);
354 (void) fclose(fp);
390 loop_config(LOOP_DRIFTCOMP, 1e9);
391 fclose(fp);
355 break;
356 }
392 break;
393 }
357 (void) fclose(fp);
358 if (
359#ifdef HAVE_FINITE
360 !finite(old_drift)
361#else /* not HAVE_FINITE */
362# ifdef HAVE_ISFINITE
363 !isfinite(old_drift)
364# else /* not HAVE_ISFINITE */
365 0
366# endif /* not HAVE_ISFINITE */
367#endif /* not HAVE_FINITE */
368 || (fabs(old_drift) > (NTP_MAXFREQ * 1e6))) {
369 msyslog(LOG_ERR, "invalid frequency (%f) in %s",
370 old_drift, stats_drift_file);
371 old_drift = 0.0;
372 }
373 msyslog(LOG_INFO, "frequency initialized %.3f from %s",
374 old_drift, stats_drift_file);
394 fclose(fp);
395 msyslog(LOG_INFO,
396 "frequency initialized %.3f PPM from %s",
397 old_drift, stats_drift_file);
375 loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
376 break;
377
378 case STATS_STATSDIR:
379 if (strlen(value) >= sizeof(statsdir)) {
380 msyslog(LOG_ERR,
381 "value for statsdir too long (>%d, sigh)",
382 (int)sizeof(statsdir)-1);

--- 21 unchanged lines hidden (view full) ---

404 filegen_setup(&clockstats, now.l_ui);
405 }
406 if(rawstats.prefix == &statsdir[0] &&
407 rawstats.fp != NULL) {
408 fclose(rawstats.fp);
409 rawstats.fp = NULL;
410 filegen_setup(&rawstats, now.l_ui);
411 }
398 loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
399 break;
400
401 case STATS_STATSDIR:
402 if (strlen(value) >= sizeof(statsdir)) {
403 msyslog(LOG_ERR,
404 "value for statsdir too long (>%d, sigh)",
405 (int)sizeof(statsdir)-1);

--- 21 unchanged lines hidden (view full) ---

427 filegen_setup(&clockstats, now.l_ui);
428 }
429 if(rawstats.prefix == &statsdir[0] &&
430 rawstats.fp != NULL) {
431 fclose(rawstats.fp);
432 rawstats.fp = NULL;
433 filegen_setup(&rawstats, now.l_ui);
434 }
435 if(sysstats.prefix == &statsdir[0] &&
436 sysstats.fp != NULL) {
437 fclose(sysstats.fp);
438 sysstats.fp = NULL;
439 filegen_setup(&sysstats, now.l_ui);
440 }
441#ifdef OPENSSL
442 if(cryptostats.prefix == &statsdir[0] &&
443 cryptostats.fp != NULL) {
444 fclose(cryptostats.fp);
445 cryptostats.fp = NULL;
446 filegen_setup(&cryptostats, now.l_ui);
447 }
448#endif /* OPENSSL */
412 }
413 break;
414
415 case STATS_PID_FILE:
416 if ((fp = fopen(value, "w")) == NULL) {
417 msyslog(LOG_ERR, "Can't open %s: %m", value);
418 break;
419 }

--- 17 unchanged lines hidden (view full) ---

437 * peer status word (hex)
438 * peer offset (s)
439 * peer delay (s)
440 * peer error bound (s)
441 * peer error (s)
442*/
443void
444record_peer_stats(
449 }
450 break;
451
452 case STATS_PID_FILE:
453 if ((fp = fopen(value, "w")) == NULL) {
454 msyslog(LOG_ERR, "Can't open %s: %m", value);
455 break;
456 }

--- 17 unchanged lines hidden (view full) ---

474 * peer status word (hex)
475 * peer offset (s)
476 * peer delay (s)
477 * peer error bound (s)
478 * peer error (s)
479*/
480void
481record_peer_stats(
445 struct sockaddr_in *addr,
446 int status,
447 double offset,
448 double delay,
449 double dispersion,
450 double skew
482 struct sockaddr_storage *addr,
483 int status,
484 double offset,
485 double delay,
486 double dispersion,
487 double skew
451 )
452{
488 )
489{
453 struct timeval tv;
454#ifdef HAVE_GETCLOCK
455 struct timespec ts;
456#endif
457 u_long day, sec, msec;
490 l_fp now;
491 u_long day;
458
459 if (!stats_control)
460 return;
492
493 if (!stats_control)
494 return;
461#ifdef HAVE_GETCLOCK
462 (void) getclock(TIMEOFDAY, &ts);
463 tv.tv_sec = ts.tv_sec;
464 tv.tv_usec = ts.tv_nsec / 1000;
465#else /* not HAVE_GETCLOCK */
466 GETTIMEOFDAY(&tv, (struct timezone *)NULL);
467#endif /* not HAVE_GETCLOCK */
468 day = tv.tv_sec / 86400 + MJD_1970;
469 sec = tv.tv_sec % 86400;
470 msec = tv.tv_usec / 1000;
471
495
472 filegen_setup(&peerstats, (u_long)(tv.tv_sec + JAN_1970));
496 get_systime(&now);
497 filegen_setup(&peerstats, now.l_ui);
498 day = now.l_ui / 86400 + MJD_1900;
499 now.l_ui %= 86400;
473 if (peerstats.fp != NULL) {
474 fprintf(peerstats.fp,
500 if (peerstats.fp != NULL) {
501 fprintf(peerstats.fp,
475 "%lu %lu.%03lu %s %x %.9f %.9f %.9f %.9f\n",
476 day, sec, msec, ntoa(addr), status, offset,
502 "%lu %s %s %x %.9f %.9f %.9f %.9f\n",
503 day, ulfptoa(&now, 3), stoa(addr), status, offset,
477 delay, dispersion, skew);
478 fflush(peerstats.fp);
479 }
480}
481/*
482 * record_loop_stats - write loop filter statistics to file
483 *
484 * file format:
485 * day (mjd)
486 * time (s past midnight)
487 * offset (s)
488 * frequency (approx ppm)
489 * time constant (log base 2)
490 */
491void
492record_loop_stats(
504 delay, dispersion, skew);
505 fflush(peerstats.fp);
506 }
507}
508/*
509 * record_loop_stats - write loop filter statistics to file
510 *
511 * file format:
512 * day (mjd)
513 * time (s past midnight)
514 * offset (s)
515 * frequency (approx ppm)
516 * time constant (log base 2)
517 */
518void
519record_loop_stats(
493 double offset,
494 double freq,
495 double jitter,
496 double stability,
497 int poll
520 double offset,
521 double freq,
522 double jitter,
523 double stability,
524 int spoll
498 )
499{
525 )
526{
500 struct timeval tv;
501#ifdef HAVE_GETCLOCK
502 struct timespec ts;
503#endif
504 u_long day, sec, msec;
527 l_fp now;
528 u_long day;
505
506 if (!stats_control)
507 return;
529
530 if (!stats_control)
531 return;
508#ifdef HAVE_GETCLOCK
509 (void) getclock(TIMEOFDAY, &ts);
510 tv.tv_sec = ts.tv_sec;
511 tv.tv_usec = ts.tv_nsec / 1000;
512#else /* not HAVE_GETCLOCK */
513 GETTIMEOFDAY(&tv, (struct timezone *)NULL);
514#endif /* not HAVE_GETCLOCK */
515 day = tv.tv_sec / 86400 + MJD_1970;
516 sec = tv.tv_sec % 86400;
517 msec = tv.tv_usec / 1000;
518
532
519 filegen_setup(&loopstats, (u_long)(tv.tv_sec + JAN_1970));
533 get_systime(&now);
534 filegen_setup(&loopstats, now.l_ui);
535 day = now.l_ui / 86400 + MJD_1900;
536 now.l_ui %= 86400;
520 if (loopstats.fp != NULL) {
537 if (loopstats.fp != NULL) {
521 fprintf(loopstats.fp, "%lu %lu.%03lu %.9f %.6f %.9f %.6f %d\n",
522 day, sec, msec, offset, freq * 1e6, jitter,
523 stability * 1e6, poll);
538 fprintf(loopstats.fp, "%lu %s %.9f %.6f %.9f %.6f %d\n",
539 day, ulfptoa(&now, 3), offset, freq * 1e6, jitter,
540 stability * 1e6, spoll);
524 fflush(loopstats.fp);
525 }
526}
527
528/*
529 * record_clock_stats - write clock statistics to file
530 *
531 * file format:
532 * day (mjd)
533 * time (s past midnight)
534 * peer (ip address)
535 * text message
536 */
537void
538record_clock_stats(
541 fflush(loopstats.fp);
542 }
543}
544
545/*
546 * record_clock_stats - write clock statistics to file
547 *
548 * file format:
549 * day (mjd)
550 * time (s past midnight)
551 * peer (ip address)
552 * text message
553 */
554void
555record_clock_stats(
539 struct sockaddr_in *addr,
556 struct sockaddr_storage *addr,
540 const char *text
541 )
542{
557 const char *text
558 )
559{
543 struct timeval tv;
544#ifdef HAVE_GETCLOCK
545 struct timespec ts;
546#endif
547 u_long day, sec, msec;
560 l_fp now;
561 u_long day;
548
549 if (!stats_control)
550 return;
562
563 if (!stats_control)
564 return;
551#ifdef HAVE_GETCLOCK
552 (void) getclock(TIMEOFDAY, &ts);
553 tv.tv_sec = ts.tv_sec;
554 tv.tv_usec = ts.tv_nsec / 1000;
555#else /* not HAVE_GETCLOCK */
556 GETTIMEOFDAY(&tv, (struct timezone *)NULL);
557#endif /* not HAVE_GETCLOCK */
558 day = tv.tv_sec / 86400 + MJD_1970;
559 sec = tv.tv_sec % 86400;
560 msec = tv.tv_usec / 1000;
561
565
562 filegen_setup(&clockstats, (u_long)(tv.tv_sec + JAN_1970));
566 get_systime(&now);
567 filegen_setup(&clockstats, now.l_ui);
568 day = now.l_ui / 86400 + MJD_1900;
569 now.l_ui %= 86400;
563 if (clockstats.fp != NULL) {
570 if (clockstats.fp != NULL) {
564 fprintf(clockstats.fp, "%lu %lu.%03lu %s %s\n",
565 day, sec, msec, ntoa(addr), text);
571 fprintf(clockstats.fp, "%lu %s %s %s\n",
572 day, ulfptoa(&now, 3), stoa(addr), text);
566 fflush(clockstats.fp);
567 }
568}
569
570/*
571 * record_raw_stats - write raw timestamps to file
572 *
573 *
574 * file format
575 * time (s past midnight)
576 * peer ip address
577 * local ip address
578 * t1 t2 t3 t4 timestamps
579 */
580void
581record_raw_stats(
573 fflush(clockstats.fp);
574 }
575}
576
577/*
578 * record_raw_stats - write raw timestamps to file
579 *
580 *
581 * file format
582 * time (s past midnight)
583 * peer ip address
584 * local ip address
585 * t1 t2 t3 t4 timestamps
586 */
587void
588record_raw_stats(
582 struct sockaddr_in *srcadr,
583 struct sockaddr_in *dstadr,
584 l_fp *t1,
585 l_fp *t2,
586 l_fp *t3,
587 l_fp *t4
589 struct sockaddr_storage *srcadr,
590 struct sockaddr_storage *dstadr,
591 l_fp *t1,
592 l_fp *t2,
593 l_fp *t3,
594 l_fp *t4
588 )
589{
595 )
596{
590 struct timeval tv;
591#ifdef HAVE_GETCLOCK
592 struct timespec ts;
593#endif
594 u_long day, sec, msec;
597 l_fp now;
598 u_long day;
595
596 if (!stats_control)
597 return;
599
600 if (!stats_control)
601 return;
598#ifdef HAVE_GETCLOCK
599 (void) getclock(TIMEOFDAY, &ts);
600 tv.tv_sec = ts.tv_sec;
601 tv.tv_usec = ts.tv_nsec / 1000;
602#else /* not HAVE_GETCLOCK */
603 GETTIMEOFDAY(&tv, (struct timezone *)NULL);
604#endif /* not HAVE_GETCLOCK */
605 day = tv.tv_sec / 86400 + MJD_1970;
606 sec = tv.tv_sec % 86400;
607 msec = tv.tv_usec / 1000;
608
602
609 filegen_setup(&rawstats, (u_long)(tv.tv_sec + JAN_1970));
603 get_systime(&now);
604 filegen_setup(&rawstats, now.l_ui);
605 day = now.l_ui / 86400 + MJD_1900;
606 now.l_ui %= 86400;
610 if (rawstats.fp != NULL) {
607 if (rawstats.fp != NULL) {
611 fprintf(rawstats.fp, "%lu %lu.%03lu %s %s %s %s %s %s\n",
612 day, sec, msec, ntoa(srcadr), ntoa(dstadr),
608 fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s\n",
609 day, ulfptoa(&now, 3), stoa(srcadr), stoa(dstadr),
613 ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
614 ulfptoa(t4, 9));
615 fflush(rawstats.fp);
616 }
617}
618
610 ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
611 ulfptoa(t4, 9));
612 fflush(rawstats.fp);
613 }
614}
615
616
619/*
617/*
618 * record_sys_stats - write system statistics to file
619 *
620 * file format
621 * time (s past midnight)
622 * time since startup (hr)
623 * packets recieved
624 * packets processed
625 * current version
626 * previous version
627 * bad version
628 * access denied
629 * bad length or format
630 * bad authentication
631 * rate exceeded
632 */
633void
634record_sys_stats(void)
635{
636 l_fp now;
637 u_long day;
638
639 if (!stats_control)
640 return;
641
642 get_systime(&now);
643 filegen_setup(&sysstats, now.l_ui);
644 day = now.l_ui / 86400 + MJD_1900;
645 now.l_ui %= 86400;
646 if (sysstats.fp != NULL) {
647 fprintf(sysstats.fp,
648 "%lu %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
649 day, ulfptoa(&now, 3), sys_stattime / 3600,
650 sys_received, sys_processed, sys_newversionpkt,
651 sys_oldversionpkt, sys_unknownversion,
652 sys_restricted, sys_badlength, sys_badauth,
653 sys_limitrejected);
654 fflush(sysstats.fp);
655 proto_clr_stats();
656 }
657}
658
659
660#ifdef OPENSSL
661/*
662 * record_crypto_stats - write crypto statistics to file
663 *
664 * file format:
665 * day (mjd)
666 * time (s past midnight)
667 * peer (ip address)
668 * text message
669 */
670void
671record_crypto_stats(
672 struct sockaddr_storage *addr,
673 const char *text
674 )
675{
676 l_fp now;
677 u_long day;
678
679 if (!stats_control)
680 return;
681
682 get_systime(&now);
683 filegen_setup(&cryptostats, now.l_ui);
684 day = now.l_ui / 86400 + MJD_1900;
685 now.l_ui %= 86400;
686 if (cryptostats.fp != NULL) {
687 if (addr == NULL)
688 fprintf(cryptostats.fp, "%lu %s %s\n",
689 day, ulfptoa(&now, 3), text);
690 else
691 fprintf(cryptostats.fp, "%lu %s %s %s\n",
692 day, ulfptoa(&now, 3), stoa(addr), text);
693 fflush(cryptostats.fp);
694 }
695}
696#endif /* OPENSSL */
697
698
699/*
620 * getauthkeys - read the authentication keys from the specified file
621 */
622void
623getauthkeys(
624 char *keyfile
625 )
626{
627 int len;

--- 34 unchanged lines hidden (view full) ---

662 * rereadkeys - read the authentication key file over again.
663 */
664void
665rereadkeys(void)
666{
667 if (key_file_name != 0)
668 authreadkeys(key_file_name);
669}
700 * getauthkeys - read the authentication keys from the specified file
701 */
702void
703getauthkeys(
704 char *keyfile
705 )
706{
707 int len;

--- 34 unchanged lines hidden (view full) ---

742 * rereadkeys - read the authentication key file over again.
743 */
744void
745rereadkeys(void)
746{
747 if (key_file_name != 0)
748 authreadkeys(key_file_name);
749}
750
751/*
752 * sock_hash - hash an sockaddr_storage structure
753 */
754int
755sock_hash(
756 struct sockaddr_storage *addr
757 )
758{
759 int hashVal;
760 int i;
761 int len;
762 char *ch;
763
764 hashVal = 0;
765 len = 0;
766 /*
767 * We can't just hash the whole thing because there are hidden
768 * fields in sockaddr_in6 that might be filled in by recvfrom(),
769 * so just use the family, port and address.
770 */
771 ch = (char *)&addr->ss_family;
772 hashVal = 37 * hashVal + (int)*ch;
773 if (sizeof(addr->ss_family) > 1) {
774 ch++;
775 hashVal = 37 * hashVal + (int)*ch;
776 }
777 switch(addr->ss_family) {
778 case AF_INET:
779 ch = (char *)&((struct sockaddr_in *)addr)->sin_addr;
780 len = sizeof(struct in_addr);
781 break;
782 case AF_INET6:
783 ch = (char *)&((struct sockaddr_in6 *)addr)->sin6_addr;
784 len = sizeof(struct in6_addr);
785 break;
786 }
787
788 for (i = 0; i < len ; i++)
789 hashVal = 37 * hashVal + (int)*(ch + i);
790
791 hashVal = hashVal % 128; /* % MON_HASH_SIZE hardcoded */
792
793 if (hashVal < 0)
794 hashVal += 128;
795
796 return hashVal;
797}