From: Harlan Stenn Date: Tue, 17 Feb 2004 06:43:41 +0000 (-0500) Subject: Cleanup from Dave Mills. X-Git-Tag: NTP_4_2_3~182^2~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6d5bb582106cab033485977046f11e2790d1211;p=thirdparty%2Fntp.git Cleanup from Dave Mills. bk: 4031b81d5vdk7D0hEhKpnrjyvpwPeg --- diff --git a/include/ntp.h b/include/ntp.h index 9fad6681e..0cb3727ff 100644 --- a/include/ntp.h +++ b/include/ntp.h @@ -227,7 +227,6 @@ struct peer { u_char version; /* version number */ u_char hmode; /* local association mode */ u_char hpoll; /* local poll interval */ - u_char kpoll; /* last poll interval */ u_char minpoll; /* min poll interval */ u_char maxpoll; /* max poll interval */ u_int flags; /* association flags */ diff --git a/include/ntpd.h b/include/ntpd.h index 11e3d9f91..ef170c545 100644 --- a/include/ntpd.h +++ b/include/ntpd.h @@ -164,7 +164,7 @@ extern void kod_proto P((void)); * This is also true AIX compiler. * So give up and define it to be int. WLJ */ -extern void poll_update P((struct peer *, int)); +extern void poll_update P((struct peer *)); extern void clear P((struct peer *)); extern void clock_filter P((struct peer *, double, double, double)); diff --git a/ntpd/ntp_crypto.c b/ntpd/ntp_crypto.c index a97d1fb5c..e0dd400ea 100644 --- a/ntpd/ntp_crypto.c +++ b/ntpd/ntp_crypto.c @@ -288,8 +288,8 @@ make_keylist( * included in the hash is zero if broadcast mode, the peer * cookie if client mode or the host cookie if symmetric modes. */ - lifetime = min(sys_automax, (unsigned long) NTP_MAXSESSION * - (1 <<(peer->kpoll))); + lifetime = min(sys_automax, NTP_MAXSESSION * (1 << + peer->hpoll)); if (peer->hmode == MODE_BROADCAST) cookie = 0; else @@ -299,9 +299,9 @@ make_keylist( peer->keynumber = i; keyid = session_key(&dstadr->sin, &peer->srcadr, keyid, cookie, lifetime); - lifetime -= 1 << peer->kpoll; + lifetime -= 1 << peer->hpoll; if (auth_havekey(keyid) || keyid <= NTP_MAXKEY || - lifetime <= (unsigned long)(1 << (peer->kpoll))) + lifetime <= (1 << peer->hpoll)) break; } @@ -338,7 +338,7 @@ make_keylist( if (debug) printf("make_keys: %d %08x %08x ts %u fs %u poll %d\n", ntohl(ap->seq), ntohl(ap->key), cookie, - ntohl(vp->tstamp), ntohl(vp->fstamp), peer->kpoll); + ntohl(vp->tstamp), ntohl(vp->fstamp), peer->hpoll); #endif } @@ -1196,7 +1196,7 @@ crypto_xmit( vtemp.tstamp = ep->tstamp; vtemp.fstamp = ep->fstamp; vtemp.vallen = ep->vallen; - vtemp.ptr = (unsigned char *)ep->pkt; + vtemp.ptr = (u_char *)ep->pkt; len += crypto_send(fp, &vtemp); break; @@ -3134,7 +3134,7 @@ cert_sign( X509_gmtime_adj(X509_get_notAfter(cert), YEAR); subj = X509_get_issuer_name(cert); X509_NAME_add_entry_by_txt(subj, "commonName", MBSTRING_ASC, - (unsigned char *) sys_hostname, strlen(sys_hostname), -1, 0); + (u_char *)sys_hostname, strlen(sys_hostname), -1, 0); subj = X509_get_subject_name(req); X509_set_subject_name(cert, subj); X509_set_pubkey(cert, pkey); @@ -3676,7 +3676,7 @@ crypto_tai( len = i * 4; tai_leap.vallen = htonl(len); ptr = emalloc(len); - tai_leap.ptr = (unsigned char *) ptr; + tai_leap.ptr = (u_char *)ptr; for (; i >= 0; i--) { *ptr++ = (char) htonl(leapsec[i]); } @@ -3805,7 +3805,7 @@ crypto_setup(void) exit (-1); } hostval.vallen = htonl(strlen(sys_hostname)); - hostval.ptr = (unsigned char *) sys_hostname; + hostval.ptr = (u_char *)sys_hostname; /* * Construct public key extension field for agreement scheme. diff --git a/ntpd/ntp_loopfilter.c b/ntpd/ntp_loopfilter.c index c8370cd04..eb22cd084 100644 --- a/ntpd/ntp_loopfilter.c +++ b/ntpd/ntp_loopfilter.c @@ -315,10 +315,6 @@ local_clock( * these actions interact with the command line options. */ retval = 0; - if (sys_poll > peer->maxpoll) - sys_poll = peer->maxpoll; - else if (sys_poll < peer->minpoll) - sys_poll = peer->minpoll; clock_frequency = flladj = plladj = 0; mu = peer->epoch - last_time; if (fabs(fp_offset) > clock_max && clock_max > 0) { diff --git a/ntpd/ntp_peer.c b/ntpd/ntp_peer.c index cf5c15267..d9ad7040d 100644 --- a/ntpd/ntp_peer.c +++ b/ntpd/ntp_peer.c @@ -872,8 +872,7 @@ resetmanycast(void) for (peer = peer_hash[i]; peer != 0; peer = peer->next) { if (peer->cast_flags & MDF_ACAST) { - peer->ttl = 0; - poll_update(peer, 0); + peer_clear(peer, "ACST"); } } } diff --git a/ntpd/ntp_proto.c b/ntpd/ntp_proto.c index 44e58ab6d..866ab5b19 100644 --- a/ntpd/ntp_proto.c +++ b/ntpd/ntp_proto.c @@ -108,8 +108,6 @@ transmit( struct peer *peer /* peer structure pointer */ ) { - int hpoll; - /* * The polling state machine. There are two kinds of machines, @@ -117,28 +115,22 @@ transmit( * server modes) and those that do (all other modes). The dance * is intricate... */ - hpoll = peer->hpoll; if (peer->cast_flags & (MDF_BCAST | MDF_MCAST)) { /* - * In broadcast mode the poll interval is fixed - * at minpoll. + * In broadcast mode the poll interval is never changed + * from minpoll. */ - hpoll = peer->minpoll; + /* fall through */; + } else if (peer->cast_flags & MDF_ACAST) { /* * In manycast mode we start with the minpoll interval - * and ttl. However, the actual poll interval is eight - * times the nominal poll interval shown here. If fewer - * than sys_minclock servers are found, the ttl is - * increased by one and we try again. If this continues - * to the max ttl, the poll interval is bumped by one - * and we try again. If at least sys_minclock servers - * are found, the poll interval increases with the - * system poll interval to the max and we continue - * indefinately. However, about once per day when the - * agreement parameters are refreshed, the manycast + * and unity ttl. The ttl is increased by one for each + * poll until either enough servers have been found or + * the maximum ttl is reached. About once per day when + * the agreement parameters are refreshed, the manycast * clients are reset and we start from the beginning. * This is to catch and clamp the ttl to the lowest * practical value and avoid knocking on spurious doors. @@ -146,7 +138,6 @@ transmit( if (sys_survivors < sys_minclock && peer->ttl < sys_ttlmax) peer->ttl++; - hpoll = sys_poll; } else { if (peer->burst == 0) { u_char oreach; @@ -193,11 +184,12 @@ transmit( FLAG_CONFIG)) { unpeer(peer); return; + } else if (peer->flash & TEST5) { peer_clear(peer, "CRYPTO"); peer->flash += TEST4; } else { - hpoll++; + peer->hpoll++; } } else { /* @@ -210,15 +202,13 @@ transmit( * the system poll interval. */ peer->unreach = 0; - if (!(peer->reach & 0x07)) { + if (!(peer->reach & 0x07)) clock_filter(peer, 0., 0., MAXDISPERSE); - clock_select(); - } if (peer_unfit(peer)) - hpoll++; + peer->hpoll++; else - hpoll = sys_poll; + peer->hpoll = sys_poll; if (peer->flags & FLAG_BURST) peer->burst = NTP_BURST; } @@ -263,40 +253,42 @@ transmit( } } clock_select(); - poll_update(peer, hpoll); + poll_update(peer); return; } } } - peer->outdate = current_time; + peer->outdate = peer->nextdate = current_time; /* * Do not transmit if in broadcast client mode. */ if (peer->hmode == MODE_BCLIENT) { - poll_update(peer, hpoll); + poll_update(peer); return; /* * Do not transmit in broadcast mode unless we are synchronized. */ } else if (peer->hmode == MODE_BROADCAST && sys_peer == NULL) { - poll_update(peer, hpoll); + poll_update(peer); return; } /* - * Do not transmit if in access-deny or crypto jail. + * Do not transmit if in access-deny or crypto jail. Clamp the + * poll to minimum if a get out of jail free card shows up. */ if (peer->flash & TEST4) { if (!(peer->flags & FLAG_CONFIG)) { unpeer(peer); return; } + peer->hpoll= peer->minpoll; } else { peer_xmit(peer); } - poll_update(peer, hpoll); + poll_update(peer); } /* @@ -1041,7 +1033,7 @@ receive( */ if (peer->cmmd != 0) { peer->ppoll = pkt->ppoll; - poll_update(peer, 0); + poll_update(peer); } } #endif /* OPENSSL */ @@ -1066,7 +1058,6 @@ receive( return; } -#ifdef OPENSSL /* * If TEST10 is lit, the autokey sequence has broken, which * probably means the server has refreshed its private value. @@ -1083,7 +1074,6 @@ receive( else unpeer(peer); } -#endif /* OPENSSL */ } @@ -1220,7 +1210,7 @@ process_packet( peer->timereachable = current_time; } peer->reach |= 1; - poll_update(peer, 0); + poll_update(peer); /* * For a client/server association, calculate the clock offset, @@ -1300,8 +1290,6 @@ process_packet( return; } clock_filter(peer, p_offset, p_del, p_disp); - if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC) - clock_select(); record_peer_stats(&peer->srcadr, ctlpeerstatus(peer), peer->offset, peer->delay, peer->disp, SQRT(peer->jitter)); @@ -1318,13 +1306,16 @@ clock_update(void) u_char ostratum; /* - * Reset/adjust the system clock. Do this only if there is a - * system peer and the peer epoch is not older than the last - * update. + * There must be a system peer at this point. If we just changed + * the system peer, but have a newer sample from the old one, + * wait until newer data are available. */ - if (sys_peer == NULL) - return; - + if (sys_poll < sys_peer->minpoll) + sys_poll = sys_peer->minpoll; + if (sys_poll > sys_peer->maxpoll) + sys_poll = sys_peer->maxpoll; + sys_peer->hpoll = sys_poll; + poll_update(sys_peer); if (sys_peer->epoch <= sys_clocktime) return; @@ -1394,56 +1385,53 @@ clock_update(void) */ void poll_update( - struct peer *peer, - int hpoll + struct peer *peer ) { + u_char hpoll; #ifdef OPENSSL int oldpoll; #endif /* OPENSSL */ /* - * A little foxtrot to determine what controls the poll - * interval. If the peer is reachable, but the last four polls - * have not been answered, use the minimum. If declared - * truechimer, use the system poll interval. This allows each - * association to ramp up the poll interval for useless sources - * and to clamp it to the minimum when first starting up. + * This routine figures out when the next poll should set, and + * that turns out to be wickedly complicated. The big problem is + * that sometimes the time for the next poll is in the past. + * Watch out for races here between the receive process and the + * poll process. The key assertion is that, if nextdate == + * current_time, the call is from the poll process; otherwise, + * it is from the receive process. */ + hpoll = peer->hpoll; #ifdef OPENSSL - oldpoll = peer->kpoll; + oldpoll = hpoll; #endif /* OPENSSL */ - if (hpoll > 0) { - if (hpoll > peer->maxpoll) - peer->hpoll = peer->maxpoll; - else if (hpoll < peer->minpoll) - peer->hpoll = peer->minpoll; - else - peer->hpoll = (u_char)hpoll; - } + if (hpoll > peer->maxpoll) + hpoll = peer->maxpoll; + else if (hpoll < peer->minpoll) + hpoll = peer->minpoll; /* - * Bit of adventure here. If during a burst and not a poll, just - * slink away. If a poll, figure what the next poll should be. - * If a burst is pending and a reference clock or a pending - * crypto response, delay for one second. If the first sent in a - * burst, delay ten seconds for the modem to come up. For others - * in the burst, delay two seconds. - * - * In case of manycast server, make the poll interval, which is - * axtually the manycast beacon interval, eight times the system - * poll interval. Normally when the host poll interval settles - * up to 1024 s, the beacon interval settles up to 2.3 hours. + * If during the crypto protocol and a message is pending, make + * it wait no more than two seconds. */ #ifdef OPENSSL if (peer->cmmd != NULL && (sys_leap != LEAP_NOTINSYNC || peer->crypto)) { peer->nextdate = current_time + RESP_DELAY; + + /* + * If we get called from the receive routine while a burst is + * pending, just slink away. If from the poll routine and a + * reference clock or a pending crypto response, delay for one + * second. If this is the first sent in a burst, wait for the + * modem to come up. For others in the burst, delay two seconds. + */ } else if (peer->burst > 0) { #else /* OPENSSL */ if (peer->burst > 0) { #endif /* OPENSSL */ - if (hpoll == 0 && peer->nextdate != current_time) + if (peer->nextdate != current_time) return; #ifdef REFCLOCK else if (peer->flags & FLAG_REFCLOCK) @@ -1454,20 +1442,44 @@ poll_update( peer->nextdate += sys_calldelay; else peer->nextdate += BURST_DELAY; + /* + * A manycast server beacons at the minimum poll interval times + * the ttl until a sufficient number of servers have been found, + * then beacons at the maximum poll interval. + */ } else if (peer->cast_flags & MDF_ACAST) { if (sys_survivors >= sys_minclock || peer->ttl >= sys_ttlmax) - peer->kpoll = (u_char)(peer->hpoll + 3); + hpoll = peer->maxpoll; else - peer->kpoll = peer->hpoll; - peer->nextdate = peer->outdate + RANDPOLL(peer->kpoll); + hpoll = peer->minpoll + peer->ttl; + peer->nextdate = peer->outdate + RANDPOLL(hpoll); + + /* + * The normal case after all those pesky exceptions. Set the + * poll to the minimum of the host and peer values, but not + * below minpoll. That's max(min(ppoll, hpoll), minpoll). + */ } else { - peer->kpoll = (u_char) max(min(peer->ppoll, - peer->hpoll), peer->minpoll); - peer->nextdate = peer->outdate + RANDPOLL(peer->kpoll); + if (peer->ppoll < peer->minpoll) + peer->nextdate = peer->outdate + + RANDPOLL(peer->minpoll); + if (peer->ppoll < hpoll) + peer->nextdate = peer->outdate + + RANDPOLL(peer->ppoll); + else + peer->nextdate = peer->outdate + + RANDPOLL(hpoll); } - if (peer->nextdate < current_time) - peer->nextdate = current_time; + + /* + * If the time for the next poll has already happened, bring it + * up to the next second after this one. This way the only way + * to get nexdate == current time is from the poll routine. + */ + peer->hpoll = hpoll; + if (peer->nextdate <= current_time) + peer->nextdate = current_time + 1; #ifdef OPENSSL /* * Bit of crass arrogance at this point. If the poll interval @@ -1475,14 +1487,14 @@ poll_update( * keylist are probably bogus. In this case purge the keylist * and regenerate it later. */ - if (peer->kpoll != oldpoll) + if (peer->hpoll != oldpoll) key_expire(peer); #endif /* OPENSSL */ #ifdef DEBUG if (debug > 1) printf("poll_update: at %lu %s flags %04x poll %d burst %d last %lu next %lu\n", current_time, ntoa(&peer->srcadr), peer->flags, - peer->kpoll, peer->burst, peer->outdate, + peer->hpoll, peer->burst, peer->outdate, peer->nextdate); #endif } @@ -1539,7 +1551,7 @@ peer_clear( if (peer == sys_peer) sys_peer = NULL; peer->estbdelay = sys_bdelay; - peer->hpoll = peer->kpoll = peer->minpoll; + peer->hpoll = peer->minpoll; peer->ppoll = peer->maxpoll; peer->jitter = MAXDISPERSE; peer->epoch = current_time; @@ -1568,7 +1580,7 @@ peer_clear( */ peer->nextdate = peer->update = peer->outdate = current_time; if (oreach) - poll_update(peer, 0); + poll_update(peer); else if (initializing) peer->nextdate = current_time + peer_associations; else @@ -1744,9 +1756,11 @@ clock_filter( /* * The mitigated sample statistics are saved for later - * processing. + * processing. If not in a burst, tickle the select. */ peer->epoch = peer->filter_epoch[k]; + if (peer->burst == 0 || sys_leap == LEAP_NOTINSYNC) + clock_select(); #ifdef DEBUG if (debug) printf( diff --git a/ntpd/ntp_refclock.c b/ntpd/ntp_refclock.c index 41a4ba4de..e4c5eaecb 100644 --- a/ntpd/ntp_refclock.c +++ b/ntpd/ntp_refclock.c @@ -347,7 +347,7 @@ refclock_transmit( peer->outdate = next; if (peer->burst > 0) peer->burst--; - poll_update(peer, 0); + poll_update(peer); } diff --git a/ntpd/refclock_arbiter.c b/ntpd/refclock_arbiter.c index cf5f92f1f..4b95c65c8 100644 --- a/ntpd/refclock_arbiter.c +++ b/ntpd/refclock_arbiter.c @@ -413,8 +413,9 @@ arb_poll( up->tcswitch = 0; if (write(pp->io.fd, "TQ", 2) != 2) { refclock_report(peer, CEVNT_FAULT); - } else + } else { pp->polls++; + } if (pp->coderecv == pp->codeproc) { refclock_report(peer, CEVNT_TIMEOUT); return;