void get_mbg_utc( unsigned char **buffpp, UTC *utcp ) { utcp->csum = get_lsb_short(buffpp); utcp->valid = get_lsb_short(buffpp); get_mbg_tgps(buffpp, &utcp->t0t); if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A0, mbg_double) != IEEE_OK) { L_CLR(&utcp->A0); } if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A1, mbg_double) != IEEE_OK) { L_CLR(&utcp->A1); } utcp->WNlsf = get_lsb_short(buffpp); utcp->DNt = get_lsb_short(buffpp); utcp->delta_tls = *(*buffpp)++; utcp->delta_tlsf = *(*buffpp)++; }
int ntpq_read_assoc_peervars( associd_t associd, char *resultbuf, int maxsize ) { const char *datap; int res; int dsize; u_short rstatus; l_fp rec; l_fp ts; char value[NTPQ_BUFLEN]; res = doquery(CTL_OP_READVAR, associd, 0, 0, NULL, &rstatus, &dsize, &datap); if (res != 0) return 0; get_systime(&ts); if (dsize == 0) { if (numhosts > 1) fprintf(stderr, "server=%s ", currenthost); fprintf(stderr, "***No information returned for association %d\n", associd); return 0; } else { if ( dsize > maxsize ) dsize = maxsize; memcpy(resultbuf, datap, dsize); resultbuf[dsize] = '\0'; ntpq_getvar(resultbuf, dsize, "rec", value, sizeof(value)); if (!decodets(value, &rec)) L_CLR(&rec); memcpy(resultbuf, value, maxsize); resultbuf[dsize] = '\0'; dsize = strlen(resultbuf); } return dsize; }
void get_mbg_xyz( unsigned char **buffpp, XYZ xyz ) { int i; for (i = XP; i <= ZP; i++) { if (fetch_ieee754(buffpp, IEEE_DOUBLE, &xyz[i], mbg_double) != IEEE_OK) { L_CLR(&xyz[i]); } } }
void get_mbg_lla( unsigned char **buffpp, LLA lla ) { int i; for (i = LAT; i <= ALT; i++) { if (fetch_ieee754(buffpp, IEEE_DOUBLE, &lla[i], mbg_double) != IEEE_OK) { L_CLR(&lla[i]); } else if (i != ALT) { /* convert to degrees (* 180/PI) */ mfp_mul(&lla[i].l_i, &lla[i].l_uf, lla[i].l_i, lla[i].l_uf, rad2deg_i, rad2deg_f); } } }
/* * Decode an incoming data buffer and print a line in the peer list */ static int doprintpeers( struct varlist *pvl, int associd, int rstatus, int datalen, const char *data, FILE *fp, int af ) { char *name; char *value = NULL; int i; int c; sockaddr_u srcadr; sockaddr_u dstadr; sockaddr_u refidadr; u_long srcport = 0; char *dstadr_refid = "0.0.0.0"; char *serverlocal; size_t drlen; u_long stratum = 0; long ppoll = 0; long hpoll = 0; u_long reach = 0; l_fp estoffset; l_fp estdelay; l_fp estjitter; l_fp estdisp; l_fp reftime; l_fp rec; l_fp ts; u_char havevar[MAXHAVE]; u_long poll_sec; char type = '?'; char refid_string[10]; char whenbuf[8], pollbuf[8]; char clock_name[LENHOSTNAME]; memset((char *)havevar, 0, sizeof(havevar)); get_systime(&ts); ZERO_SOCK(&srcadr); ZERO_SOCK(&dstadr); /* Initialize by zeroing out estimate variables */ memset((char *)&estoffset, 0, sizeof(l_fp)); memset((char *)&estdelay, 0, sizeof(l_fp)); memset((char *)&estjitter, 0, sizeof(l_fp)); memset((char *)&estdisp, 0, sizeof(l_fp)); while (nextvar(&datalen, &data, &name, &value)) { sockaddr_u dum_store; i = findvar(name, peer_var, 1); if (i == 0) continue; /* don't know this one */ switch (i) { case CP_SRCADR: if (decodenetnum(value, &srcadr)) { havevar[HAVE_SRCADR] = 1; } break; case CP_DSTADR: if (decodenetnum(value, &dum_store)) { type = decodeaddrtype(&dum_store); havevar[HAVE_DSTADR] = 1; dstadr = dum_store; if (pvl == opeervarlist) { dstadr_refid = trunc_left(stoa(&dstadr), 15); } } break; case CP_REFID: if (pvl == peervarlist) { havevar[HAVE_REFID] = 1; if (*value == '\0') { dstadr_refid = ""; } else if (strlen(value) <= 4) { refid_string[0] = '.'; (void) strcpy(&refid_string[1], value); i = strlen(refid_string); refid_string[i] = '.'; refid_string[i+1] = '\0'; dstadr_refid = refid_string; } else if (decodenetnum(value, &refidadr)) { if (SOCK_UNSPEC(&refidadr)) dstadr_refid = "0.0.0.0"; else if (ISREFCLOCKADR(&refidadr)) dstadr_refid = refnumtoa(&refidadr); else dstadr_refid = stoa(&refidadr); } else { havevar[HAVE_REFID] = 0; } } break; case CP_STRATUM: if (decodeuint(value, &stratum)) havevar[HAVE_STRATUM] = 1; break; case CP_HPOLL: if (decodeint(value, &hpoll)) { havevar[HAVE_HPOLL] = 1; if (hpoll < 0) hpoll = NTP_MINPOLL; } break; case CP_PPOLL: if (decodeint(value, &ppoll)) { havevar[HAVE_PPOLL] = 1; if (ppoll < 0) ppoll = NTP_MINPOLL; } break; case CP_REACH: if (decodeuint(value, &reach)) havevar[HAVE_REACH] = 1; break; case CP_DELAY: if (decodetime(value, &estdelay)) havevar[HAVE_DELAY] = 1; break; case CP_OFFSET: if (decodetime(value, &estoffset)) havevar[HAVE_OFFSET] = 1; break; case CP_JITTER: if (pvl == peervarlist) if (decodetime(value, &estjitter)) havevar[HAVE_JITTER] = 1; break; case CP_DISPERSION: if (decodetime(value, &estdisp)) havevar[HAVE_DISPERSION] = 1; break; case CP_REC: if (decodets(value, &rec)) havevar[HAVE_REC] = 1; break; case CP_SRCPORT: if (decodeuint(value, &srcport)) havevar[HAVE_SRCPORT] = 1; break; case CP_REFTIME: havevar[HAVE_REFTIME] = 1; if (!decodets(value, &reftime)) L_CLR(&reftime); break; default: break; } } /* * Check to see if the srcport is NTP's port. If not this probably * isn't a valid peer association. */ if (havevar[HAVE_SRCPORT] && srcport != NTP_PORT) return (1); /* * Got everything, format the line */ poll_sec = 1<<max(min3(ppoll, hpoll, NTP_MAXPOLL), NTP_MINPOLL); if (pktversion > NTP_OLDVERSION) c = flash3[CTL_PEER_STATVAL(rstatus) & 0x7]; else c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3]; if (numhosts > 1) { if (peervarlist == pvl && havevar[HAVE_DSTADR]) { serverlocal = nntohost_col(&dstadr, (size_t)min(LIB_BUFLENGTH - 1, maxhostlen), TRUE); } else { if (currenthostisnum) serverlocal = trunc_left(currenthost, maxhostlen); else serverlocal = currenthost; } fprintf(fp, "%-*s ", maxhostlen, serverlocal); } if (AF_UNSPEC == af || AF(&srcadr) == af) { strncpy(clock_name, nntohost(&srcadr), sizeof(clock_name)); fprintf(fp, "%c%-15.15s ", c, clock_name); drlen = strlen(dstadr_refid); makeascii(drlen, dstadr_refid, fp); while (drlen++ < 15) fputc(' ', fp); fprintf(fp, " %2ld %c %4.4s %4.4s %3lo %7.7s %8.7s %7.7s\n", stratum, type, prettyinterval(whenbuf, sizeof(whenbuf), when(&ts, &rec, &reftime)), prettyinterval(pollbuf, sizeof(pollbuf), (int)poll_sec), reach, lfptoms(&estdelay, 3), lfptoms(&estoffset, 3), (havevar[HAVE_JITTER]) ? lfptoms(&estjitter, 3) : lfptoms(&estdisp, 3)); return (1); } else return(1); }
/* * arc_receive - receive data from the serial interface */ static void arc_receive( struct recvbuf *rbufp ) { register struct arcunit *up; struct refclockproc *pp; struct peer *peer; char c; int i, n, wday, month, flags, status; int arc_last_offset; static int quality_average = 0; static int quality_sum = 0; static int quality_polls = 0; /* * Initialize pointers and read the timecode and timestamp */ peer = (struct peer *)rbufp->recv_srcclock; pp = peer->procptr; up = (struct arcunit *)pp->unitptr; /* If the command buffer is empty, and we are resyncing, insert a g\r quality request into it to poll for signal quality again. */ if((up->resyncing) && (space_left(up) == CMDQUEUELEN)) { #ifdef DEBUG if(debug > 1) { printf("arc: inserting signal-quality poll.\n"); } #endif send_slow(up, pp->io.fd, "g\r"); } /* The `arc_last_offset' is the offset in lastcode[] of the last byte received, and which we assume actually received the input timestamp. (When we get round to using tty_clk and it is available, we assume that we will receive the whole timecode with the trailing \r, and that that \r will be timestamped. But this assumption also works if receive the characters one-by-one.) */ arc_last_offset = pp->lencode+rbufp->recv_length - 1; /* We catch a timestamp iff: * The command code is `o' for a timestamp. * If ARCRON_MULTIPLE_SAMPLES is undefined then we must have exactly char in the buffer (the command code) so that we only sample the first character of the timecode as our `on-time' character. * The first character in the buffer is not the echoed `\r' from the `o` command (so if we are to timestamp an `\r' it must not be first in the receive buffer with lencode==1. (Even if we had other characters following it, we probably would have a premature timestamp on the '\r'.) * We have received at least one character (I cannot imagine how it could be otherwise, but anyway...). */ c = rbufp->recv_buffer[0]; if((pp->a_lastcode[0] == 'o') && #ifndef ARCRON_MULTIPLE_SAMPLES (pp->lencode == 1) && #endif ((pp->lencode != 1) || (c != '\r')) && (arc_last_offset >= 1)) { /* Note that the timestamp should be corrected if >1 char rcvd. */ l_fp timestamp; timestamp = rbufp->recv_time; #ifdef DEBUG if(debug) { /* Show \r as `R', other non-printing char as `?'. */ printf("arc: stamp -->%c<-- (%d chars rcvd)\n", ((c == '\r') ? 'R' : (isgraph((int)c) ? c : '?')), rbufp->recv_length); } #endif /* Now correct timestamp by offset of last byte received---we subtract from the receive time the delay implied by the extra characters received. Reject the input if the resulting code is too long, but allow for the trailing \r, normally not used but a good handle for tty_clk or somesuch kernel timestamper. */ if(arc_last_offset > LENARC) { #ifdef DEBUG if(debug) { printf("arc: input code too long (%d cf %d); rejected.\n", arc_last_offset, LENARC); } #endif pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } L_SUBUF(×tamp, charoffsets[arc_last_offset]); #ifdef DEBUG if(debug > 1) { printf( "arc: %s%d char(s) rcvd, the last for lastcode[%d]; -%sms offset applied.\n", ((rbufp->recv_length > 1) ? "*** " : ""), rbufp->recv_length, arc_last_offset, mfptoms((unsigned long)0, charoffsets[arc_last_offset], 1)); } #endif #ifdef ARCRON_MULTIPLE_SAMPLES /* If taking multiple samples, capture the current adjusted sample iff: * No timestamp has yet been captured (it is zero), OR * This adjusted timestamp is earlier than the one already captured, on the grounds that this one suffered less delay in being delivered to us and is more accurate. */ if(L_ISZERO(&(up->lastrec)) || L_ISGEQ(&(up->lastrec), ×tamp)) #endif { #ifdef DEBUG if(debug > 1) { printf("arc: system timestamp captured.\n"); #ifdef ARCRON_MULTIPLE_SAMPLES if(!L_ISZERO(&(up->lastrec))) { l_fp diff; diff = up->lastrec; L_SUB(&diff, ×tamp); printf("arc: adjusted timestamp by -%sms.\n", mfptoms(diff.l_i, diff.l_f, 3)); } #endif } #endif up->lastrec = timestamp; } } /* Just in case we still have lots of rubbish in the buffer... */ /* ...and to avoid the same timestamp being reused by mistake, */ /* eg on receipt of the \r coming in on its own after the */ /* timecode. */ if(pp->lencode >= LENARC) { #ifdef DEBUG if(debug && (rbufp->recv_buffer[0] != '\r')) { printf("arc: rubbish in pp->a_lastcode[].\n"); } #endif pp->lencode = 0; return; } /* Append input to code buffer, avoiding overflow. */ for(i = 0; i < rbufp->recv_length; i++) { if(pp->lencode >= LENARC) { break; } /* Avoid overflow... */ c = rbufp->recv_buffer[i]; /* Drop trailing '\r's and drop `h' command echo totally. */ if(c != '\r' && c != 'h') { pp->a_lastcode[pp->lencode++] = c; } /* If we've just put an `o' in the lastcode[0], clear the timestamp in anticipation of a timecode arriving soon. We would expect to get to process this before any of the timecode arrives. */ if((c == 'o') && (pp->lencode == 1)) { L_CLR(&(up->lastrec)); #ifdef DEBUG if(debug > 1) { printf("arc: clearing timestamp.\n"); } #endif } } if (pp->lencode == 0) return; /* Handle a quality message. */ if(pp->a_lastcode[0] == 'g') { int r, q; if(pp->lencode < 3) { return; } /* Need more data... */ r = (pp->a_lastcode[1] & 0x7f); /* Strip parity. */ q = (pp->a_lastcode[2] & 0x7f); /* Strip parity. */ if(((q & 0x70) != 0x30) || ((q & 0xf) > MAX_CLOCK_QUALITY) || ((r & 0x70) != 0x30)) { /* Badly formatted response. */ #ifdef DEBUG if(debug) { printf("arc: bad `g' response %2x %2x.\n", r, q); } #endif return; } if(r == '3') { /* Only use quality value whilst sync in progress. */ if (up->quality_stamp < current_time) { struct calendar cal; l_fp new_stamp; get_systime (&new_stamp); caljulian (new_stamp.l_ui, &cal); up->quality_stamp = current_time + 60 - cal.second + 5; quality_sum = 0; quality_polls = 0; } quality_sum += (q & 0xf); quality_polls++; quality_average = (quality_sum / quality_polls); #ifdef DEBUG if(debug) { printf("arc: signal quality %d (%d).\n", quality_average, (q & 0xf)); } #endif } else if( /* (r == '2') && */ up->resyncing) { up->quality = quality_average; #ifdef DEBUG if(debug) { printf("arc: sync finished, signal quality %d: %s\n", up->quality, quality_action(up->quality)); } #endif msyslog(LOG_NOTICE, "ARCRON: sync finished, signal quality %d: %s", up->quality, quality_action(up->quality)); up->resyncing = 0; /* Resync is over. */ quality_average = 0; quality_sum = 0; quality_polls = 0; #ifdef ARCRON_KEEN /* Clock quality dubious; resync earlier than usual. */ if((up->quality == QUALITY_UNKNOWN) || (up->quality < MIN_CLOCK_QUALITY_OK)) { up->next_resync = current_time + RETRY_RESYNC_TIME; } #endif } pp->lencode = 0; return; } /* Stop now if this is not a timecode message. */ if(pp->a_lastcode[0] != 'o') { pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } /* If we don't have enough data, wait for more... */ if(pp->lencode < LENARC) { return; } /* WE HAVE NOW COLLECTED ONE TIMESTAMP (phew)... */ #ifdef DEBUG if(debug > 1) { printf("arc: NOW HAVE TIMESTAMP...\n"); } #endif /* But check that we actually captured a system timestamp on it. */ if(L_ISZERO(&(up->lastrec))) { #ifdef DEBUG if(debug) { printf("arc: FAILED TO GET SYSTEM TIMESTAMP\n"); } #endif pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } /* Append a mark of the clock's received signal quality for the benefit of Derek Mulcahy's Tcl/Tk utility (we map the `unknown' quality value to `6' for his s/w) and terminate the string for sure. This should not go off the buffer end. */ pp->a_lastcode[pp->lencode] = ((up->quality == QUALITY_UNKNOWN) ? '6' : ('0' + up->quality)); pp->a_lastcode[pp->lencode + 1] = '\0'; /* Terminate for printf(). */ #ifdef PRE_NTP420 /* We don't use the micro-/milli- second part... */ pp->usec = 0; pp->msec = 0; #else /* We don't use the nano-second part... */ pp->nsec = 0; #endif /* Validate format and numbers. */ if (pp->a_lastcode[0] != 'o' || !get2(pp->a_lastcode + 1, &pp->hour) || !get2(pp->a_lastcode + 3, &pp->minute) || !get2(pp->a_lastcode + 5, &pp->second) || !get1(pp->a_lastcode + 7, &wday) || !get2(pp->a_lastcode + 8, &pp->day) || !get2(pp->a_lastcode + 10, &month) || !get2(pp->a_lastcode + 12, &pp->year)) { #ifdef DEBUG /* Would expect to have caught major problems already... */ if(debug) { printf("arc: badly formatted data.\n"); } #endif pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } flags = pp->a_lastcode[14]; status = pp->a_lastcode[15]; #ifdef DEBUG if(debug) { printf("arc: status 0x%.2x flags 0x%.2x\n", flags, status); } #endif n = 9; /* Validate received values at least enough to prevent internal array-bounds problems, etc. */ if((pp->hour < 0) || (pp->hour > 23) || (pp->minute < 0) || (pp->minute > 59) || (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ || (wday < 1) || (wday > 7) || (pp->day < 1) || (pp->day > 31) || (month < 1) || (month > 12) || (pp->year < 0) || (pp->year > 99)) { /* Data out of range. */ pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } if(peer->MODE == 0) { /* compatiblity to original version */ int bst = flags; /* Check that BST/UTC bits are the complement of one another. */ if(!(bst & 2) == !(bst & 4)) { pp->lencode = 0; refclock_report(peer, CEVNT_BADREPLY); return; } } if(status & 0x8) { msyslog(LOG_NOTICE, "ARCRON: battery low"); } /* Year-2000 alert! */ /* Attempt to wrap 2-digit date into sensible window. */ if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* Y2KFixes */ pp->year += 1900; /* use full four-digit year */ /* Y2KFixes */ /* Attempt to do the right thing by screaming that the code will soon break when we get to the end of its useful life. What a hero I am... PLEASE FIX LEAP-YEAR AND WRAP CODE IN 209X! */ if(pp->year >= YEAR_PIVOT+2000-2 ) { /* Y2KFixes */ /*This should get attention B^> */ msyslog(LOG_NOTICE, "ARCRON: fix me! EITHER YOUR DATE IS BADLY WRONG or else I will break soon!"); } #ifdef DEBUG if(debug) { printf("arc: n=%d %02d:%02d:%02d %02d/%02d/%04d %1d %1d\n", n, pp->hour, pp->minute, pp->second, pp->day, month, pp->year, flags, status); } #endif /* The status value tested for is not strictly supported by the clock spec since the value of bit 2 (0x4) is claimed to be undefined for MSF, yet does seem to indicate if the last resync was successful or not. */ pp->leap = LEAP_NOWARNING; status &= 0x7; if(status == 0x3) { if(status != up->status) { msyslog(LOG_NOTICE, "ARCRON: signal acquired"); } } else { if(status != up->status) { msyslog(LOG_NOTICE, "ARCRON: signal lost"); pp->leap = LEAP_NOTINSYNC; /* MSF clock is free-running. */ up->status = status; pp->lencode = 0; refclock_report(peer, CEVNT_FAULT); return; } } up->status = status; if (peer->MODE == 0) { /* compatiblity to original version */ int bst = flags; pp->day += moff[month - 1]; if(isleap_4(pp->year) && month > 2) { pp->day++; }/* Y2KFixes */ /* Convert to UTC if required */ if(bst & 2) { pp->hour--; if (pp->hour < 0) { pp->hour = 23; pp->day--; /* If we try to wrap round the year * (BST on 1st Jan), reject.*/ if(pp->day < 0) { pp->lencode = 0; refclock_report(peer, CEVNT_BADTIME); return; } } } } if(peer->MODE > 0) { if(pp->sloppyclockflag & CLK_FLAG1) { struct tm local; struct tm *gmtp; time_t unixtime; /* * Convert to GMT for sites that distribute localtime. * This means we have to do Y2K conversion on the * 2-digit year; otherwise, we get the time wrong. */ memset(&local, 0, sizeof(local)); local.tm_year = pp->year-1900; local.tm_mon = month-1; local.tm_mday = pp->day; local.tm_hour = pp->hour; local.tm_min = pp->minute; local.tm_sec = pp->second; switch (peer->MODE) { case 1: local.tm_isdst = (flags & 2); break; case 2: local.tm_isdst = (flags & 2); break; case 3: switch (flags & 3) { case 0: /* It is unclear exactly when the Arcron changes from DST->ST and ST->DST. Testing has shown this to be irregular. For the time being, let the OS decide. */ local.tm_isdst = 0; #ifdef DEBUG if (debug) printf ("arc: DST = 00 (0)\n"); #endif break; case 1: /* dst->st time */ local.tm_isdst = -1; #ifdef DEBUG if (debug) printf ("arc: DST = 01 (1)\n"); #endif break; case 2: /* st->dst time */ local.tm_isdst = -1; #ifdef DEBUG if (debug) printf ("arc: DST = 10 (2)\n"); #endif break; case 3: /* dst time */ local.tm_isdst = 1; #ifdef DEBUG if (debug) printf ("arc: DST = 11 (3)\n"); #endif break; } break; default: msyslog(LOG_NOTICE, "ARCRON: Invalid mode %d", peer->MODE); return; break; } unixtime = mktime (&local); if ((gmtp = gmtime (&unixtime)) == NULL) { pp->lencode = 0; refclock_report (peer, CEVNT_FAULT); return; } pp->year = gmtp->tm_year+1900; month = gmtp->tm_mon+1; pp->day = ymd2yd(pp->year,month,gmtp->tm_mday); /* pp->day = gmtp->tm_yday; */ pp->hour = gmtp->tm_hour; pp->minute = gmtp->tm_min; pp->second = gmtp->tm_sec; #ifdef DEBUG if (debug) { printf ("arc: time is %04d/%02d/%02d %02d:%02d:%02d UTC\n", pp->year,month,gmtp->tm_mday,pp->hour,pp->minute, pp->second); } #endif } else { /* * For more rational sites distributing UTC */ pp->day = ymd2yd(pp->year,month,pp->day); } } if (peer->MODE == 0) { /* compatiblity to original version */ /* If clock signal quality is * unknown, revert to default PRECISION...*/ if(up->quality == QUALITY_UNKNOWN) { peer->precision = PRECISION; } else { /* ...else improve precision if flag3 is set... */ peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ? HIGHPRECISION : PRECISION); } } else { if ((status == 0x3) && (pp->sloppyclockflag & CLK_FLAG2)) { peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ? HIGHPRECISION : PRECISION); } else if (up->quality == QUALITY_UNKNOWN) { peer->precision = PRECISION; } else { peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ? HIGHPRECISION : PRECISION); } } /* Notice and log any change (eg from initial defaults) for flags. */ if(up->saved_flags != pp->sloppyclockflag) { #ifdef DEBUG msyslog(LOG_NOTICE, "ARCRON: flags enabled: %s%s%s%s", ((pp->sloppyclockflag & CLK_FLAG1) ? "1" : "."), ((pp->sloppyclockflag & CLK_FLAG2) ? "2" : "."), ((pp->sloppyclockflag & CLK_FLAG3) ? "3" : "."), ((pp->sloppyclockflag & CLK_FLAG4) ? "4" : ".")); /* Note effects of flags changing... */ if(debug) { printf("arc: PRECISION = %d.\n", peer->precision); } #endif up->saved_flags = pp->sloppyclockflag; } /* Note time of last believable timestamp. */ pp->lastrec = up->lastrec; #ifdef ARCRON_LEAPSECOND_KEEN /* Find out if a leap-second might just have happened... (ie is this the first hour of the first day of Jan or Jul?) */ if((pp->hour == 0) && (pp->day == 1) && ((month == 1) || (month == 7))) { if(possible_leap >= 0) { /* A leap may have happened, and no resync has started yet...*/ possible_leap = 1; } } else { /* Definitely not leap-second territory... */ possible_leap = 0; } #endif if (!refclock_process(pp)) { pp->lencode = 0; refclock_report(peer, CEVNT_BADTIME); return; } record_clock_stats(&peer->srcadr, pp->a_lastcode); refclock_receive(peer); }
/* * make conversions to and from external IEEE754 formats and internal * NTP FP format. */ int fetch_ieee754( unsigned char **buffpp, int size, l_fp *lfpp, offsets_t offsets ) { unsigned char *bufp = *buffpp; unsigned int sign; unsigned int bias; unsigned int maxexp; int mbits; u_long mantissa_low; u_long mantissa_high; u_long characteristic; long exponent; #ifdef LIBDEBUG int length; #endif unsigned char val; int fieldindex = 0; switch (size) { case IEEE_DOUBLE: #ifdef LIBDEBUG length = 8; #endif mbits = 52; bias = 1023; maxexp = 2047; break; case IEEE_SINGLE: #ifdef LIBDEBUG length = 4; #endif mbits = 23; bias = 127; maxexp = 255; break; default: return IEEE_BADCALL; } val = get_byte(bufp, offsets, &fieldindex); /* fetch sign byte & first part of characteristic */ sign = (val & 0x80) != 0; characteristic = (val & 0x7F); val = get_byte(bufp, offsets, &fieldindex); /* fetch rest of characteristic and start of mantissa */ switch (size) { case IEEE_SINGLE: characteristic <<= 1; characteristic |= (val & 0x80) != 0; /* grab last characteristic bit */ mantissa_high = 0; mantissa_low = (val &0x7F) << 16; mantissa_low |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8; mantissa_low |= get_byte(bufp, offsets, &fieldindex); break; case IEEE_DOUBLE: characteristic <<= 4; characteristic |= (val & 0xF0) >> 4; /* grab lower characteristic bits */ mantissa_high = (val & 0x0F) << 16; mantissa_high |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8; mantissa_high |= get_byte(bufp, offsets, &fieldindex); mantissa_low = (u_long)get_byte(bufp, offsets, &fieldindex) << 24; mantissa_low |= (u_long)get_byte(bufp, offsets, &fieldindex) << 16; mantissa_low |= (u_long)get_byte(bufp, offsets, &fieldindex) << 8; mantissa_low |= get_byte(bufp, offsets, &fieldindex); break; default: return IEEE_BADCALL; } #ifdef LIBDEBUG if (debug > 4) { double d; float f; if (size == IEEE_SINGLE) { int i; for (i = 0; i < length; i++) { *((unsigned char *)(&f)+i) = *(*buffpp + offsets[i]); } d = f; } else { int i; for (i = 0; i < length; i++) { *((unsigned char *)(&d)+i) = *(*buffpp + offsets[i]); } } printf("fetchieee754: FP: %s -> %s -> %e(=%s)\n", fmt_hex(*buffpp, length), fmt_flt(sign, mantissa_high, mantissa_low, characteristic), d, fmt_hex((unsigned char *)&d, length)); } #endif *buffpp += fieldindex; /* * detect funny numbers */ if (characteristic == maxexp) { /* * NaN or Infinity */ if (mantissa_low || mantissa_high) { /* * NaN */ return IEEE_NAN; } else { /* * +Inf or -Inf */ return sign ? IEEE_NEGINFINITY : IEEE_POSINFINITY; } } else { /* * collect real numbers */ L_CLR(lfpp); /* * check for overflows */ exponent = characteristic - bias; if (exponent > 31) /* sorry - hardcoded */ { /* * overflow only in respect to NTP-FP representation */ return sign ? IEEE_NEGOVERFLOW : IEEE_POSOVERFLOW; } else { int frac_offset; /* where the fraction starts */ frac_offset = mbits - exponent; if (characteristic == 0) { /* * de-normalized or tiny number - fits only as 0 */ return IEEE_OK; } else { /* * adjust for implied 1 */ if (mbits > 31) mantissa_high |= 1 << (mbits - 32); else mantissa_low |= 1 << mbits; /* * take mantissa apart - if only all machine would support * 64 bit operations 8-( */ if (frac_offset > mbits) { lfpp->l_ui = 0; /* only fractional number */ frac_offset -= mbits + 1; /* will now contain right shift count - 1*/ if (mbits > 31) { lfpp->l_uf = mantissa_high << (63 - mbits); lfpp->l_uf |= mantissa_low >> (mbits - 33); lfpp->l_uf >>= frac_offset; } else { lfpp->l_uf = mantissa_low >> frac_offset; } } else { if (frac_offset > 32)