~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ntpd/ntp_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
/*
41
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
46
/*
48
47
 * Name of the keys file
49
48
 */
76
75
static FILEGEN loopstats;
77
76
static FILEGEN clockstats;
78
77
static FILEGEN rawstats;
 
78
static FILEGEN sysstats;
 
79
#ifdef OPENSSL
 
80
static FILEGEN cryptostats;
 
81
#endif /* OPENSSL */
79
82
 
80
83
/*
81
84
 * This controls whether stats are written to the fileset. Provided
97
100
#define LOOPNAME "loopstats"
98
101
#define CLOCKNAME "clockstats"
99
102
#define RAWNAME "rawstats"
 
103
#define STANAME "systats"
 
104
#ifdef OPENSSL
 
105
#define CRYPTONAME "cryptostats"
 
106
#endif /* OPENSSL */
 
107
 
100
108
        peerstats.fp       = NULL;
101
109
        peerstats.prefix   = &statsdir[0];
102
110
        peerstats.basename = (char*)emalloc(strlen(PEERNAME)+1);
133
141
        rawstats.flag    = FGEN_FLAG_LINK; /* not yet enabled !!*/
134
142
        filegen_register("rawstats", &rawstats);
135
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
164
#undef PEERNAME
137
165
#undef LOOPNAME
138
166
#undef CLOCKNAME
139
167
#undef RAWNAME
140
 
 
 
168
#undef STANAME
 
169
#ifdef OPENSSL
 
170
#undef CRYPTONAME
 
171
#endif /* OPENSSL */
141
172
}
142
173
 
143
174
 
183
214
 
184
215
#if !defined(VMS)
185
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
220
        errno = 0;
191
221
        prio_set = 0;
192
222
        o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
193
223
 
194
 
        /* (prr) if getpriority succeeded, call setpriority to raise
 
224
        /*
 
225
         * (prr) if getpriority succeeded, call setpriority to raise
195
226
         * scheduling priority as high as possible.  If that succeeds
196
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
236
        if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
206
 
            prio_set = 1;       /* overdrive */
 
237
                prio_set = 1;   /* overdrive */
207
238
#endif /* VMS */
208
239
#ifdef HAVE_GETCLOCK
209
240
        (void) getclock(TIMEOFDAY, &ts);
212
243
#else /*  not HAVE_GETCLOCK */
213
244
        GETTIMEOFDAY(&tv,(struct timezone *)NULL);
214
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
247
                msyslog(LOG_ERR, "can't sync battery time: %m");
218
248
        }
219
249
#if !defined(VMS)
220
250
        if (prio_set)
221
 
            setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
 
251
                setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
222
252
#endif /* VMS */
223
253
#endif /* DOSYNCTODR */
224
254
 
225
255
        NLOG(NLOG_SYSSTATIST)
226
256
                msyslog(LOG_INFO,
227
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
260
 
230
261
        
 
262
        record_sys_stats();
231
263
        if (stats_drift_file != 0) {
232
264
                if ((fp = fopen(stats_temp_file, "w")) == NULL) {
233
265
                        msyslog(LOG_ERR, "can't open %s: %m",
238
270
                (void)fclose(fp);
239
271
                /* atomic */
240
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
274
#endif /* SYS_WINNT */
243
275
 
244
276
#ifndef NO_RENAME
281
313
        double old_drift;
282
314
        int len;
283
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
319
         */
287
320
#ifdef SYS_WINNT
288
321
        char newvalue[MAX_PATH], parameter[MAX_PATH];
289
322
 
290
 
        if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH))
291
 
        {
 
323
        if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
292
324
                switch(item) {
293
325
                    case STATS_FREQ_FILE:
294
326
                        strcpy(parameter,"STATS_FREQ_FILE");
307
339
 
308
340
                msyslog(LOG_ERR,
309
341
                    "ExpandEnvironmentStrings(%s) failed: %m\n", parameter);
310
 
        }
311
 
        else 
 
342
        } else {
312
343
                value = newvalue;
 
344
        }
313
345
#else    
314
346
        value = invalue;
315
347
#endif /* SYS_WINNT */
316
348
 
317
 
        
318
 
        
319
349
        switch(item) {
320
350
            case STATS_FREQ_FILE:
321
351
                if (stats_drift_file != 0) {
330
360
 
331
361
                stats_drift_file = (char*)emalloc((u_int)(len + 1));
332
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
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
368
#endif /* VMS */
337
369
                memmove(stats_drift_file, value, (unsigned)(len+1));
338
370
                memmove(stats_temp_file, value, (unsigned)len);
339
371
#if !defined(VMS)
340
 
                memmove(stats_temp_file + len, ".TEMP", sizeof(".TEMP"));
 
372
                memmove(stats_temp_file + len, ".TEMP",
 
373
                    sizeof(".TEMP"));
341
374
#else
342
 
                memmove(stats_temp_file + len, "-TEMP", sizeof("-TEMP"));
 
375
                memmove(stats_temp_file + len, "-TEMP",
 
376
                    sizeof("-TEMP"));
343
377
#endif /* VMS */
344
378
 
345
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
382
                 */
348
383
                if ((fp = fopen(stats_drift_file, "r")) == NULL) {
 
384
                        loop_config(LOOP_DRIFTCOMP, 1e9);
349
385
                        break;
350
386
                }
351
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
389
                            stats_drift_file);
354
 
                        (void) fclose(fp);
 
390
                        loop_config(LOOP_DRIFTCOMP, 1e9);
 
391
                        fclose(fp);
355
392
                        break;
356
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
398
                loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
376
399
                break;
377
400
        
409
432
                                rawstats.fp = NULL;
410
433
                                filegen_setup(&rawstats, now.l_ui);
411
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
449
                }
413
450
                break;
414
451
 
442
479
*/
443
480
void
444
481
record_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
488
        )
452
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
492
 
459
493
        if (!stats_control)
460
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
500
        if (peerstats.fp != NULL) {
474
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
504
                    delay, dispersion, skew);
478
505
                fflush(peerstats.fp);
479
506
        }
490
517
 */
491
518
void
492
519
record_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
525
        )
499
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
529
 
506
530
        if (!stats_control)
507
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
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
541
                fflush(loopstats.fp);
525
542
        }
526
543
}
536
553
 */
537
554
void
538
555
record_clock_stats(
539
 
        struct sockaddr_in *addr,
 
556
        struct sockaddr_storage *addr,
540
557
        const char *text
541
558
        )
542
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
562
 
549
563
        if (!stats_control)
550
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
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
573
                fflush(clockstats.fp);
567
574
        }
568
575
}
579
586
 */
580
587
void
581
588
record_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
595
        )
589
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
599
 
596
600
        if (!stats_control)
597
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
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
610
                    ulfptoa(t1, 9), ulfptoa(t2, 9), ulfptoa(t3, 9),
614
611
                    ulfptoa(t4, 9));
615
612
                fflush(rawstats.fp);
616
613
        }
617
614
}
618
615
 
 
616
 
 
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
 */
 
633
void
 
634
record_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
 */
 
670
void
 
671
record_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
 
619
699
/*
620
700
 * getauthkeys - read the authentication keys from the specified file
621
701
 */
667
747
        if (key_file_name != 0)
668
748
            authreadkeys(key_file_name);
669
749
}
 
750
 
 
751
/*
 
752
 * sock_hash - hash an sockaddr_storage structure
 
753
 */
 
754
int
 
755
sock_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
}