From: Linux Karlsson Date: Thu, 19 Aug 2010 10:39:19 +0000 (+0200) Subject: Merge bk://bk.ntp.org/ntp-dev X-Git-Tag: NTP_4_2_7P44~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80db2218b436697f7625070b8f59200bd4c029bd;p=thirdparty%2Fntp.git Merge bk://bk.ntp.org/ntp-dev into beam.lund.zozs.se:/home/linus/programming/gsoc/ntp/bugs/ntp-dev-1573 bk: 4c6d09d78JThrkB6lXNILCyk2zYpQQ --- 80db2218b436697f7625070b8f59200bd4c029bd diff --cc ChangeLog index 33831dfd3,06637e9d6..e360900f6 --- a/ChangeLog +++ b/ChangeLog @@@ -1,4 -1,5 +1,6 @@@ +* [Bug 1573] from 4.2.6p3-RC1: Miscalculation of offset in sntp. + * [Bug 1602] Refactor some of the sntp/ directory to facililtate testing. + (4.2.7p42) 2010/08/18 Released by Harlan Stenn * [Bug 1593] ntpd abort in free() with logconfig syntax error. * [Bug 1595] from 4.2.6p3-RC1: empty last line in key file causes duplicate key to be added diff --cc sntp/main.c index 839752c6b,e5517f472..0980f2c6e --- a/sntp/main.c +++ b/sntp/main.c @@@ -167,6 -138,187 +138,187 @@@ static union #define r_pkt rbuf.pkt + int + generate_pkt ( + struct pkt *x_pkt, + const struct timeval *tv_xmt, + int key_id, + struct key *pkt_key + ) + { + l_fp xmt; + int pkt_len = LEN_PKT_NOMAC; + memset(x_pkt, 0, sizeof(struct pkt)); + TVTOTS(tv_xmt, &xmt); + HTONL_FP(&xmt, &(x_pkt->xmt)); + x_pkt->stratum = STRATUM_TO_PKT(STRATUM_UNSPEC); + x_pkt->ppoll = 8; + /* FIXME! Modus broadcast + adr. check -> bdr. pkt */ + set_li_vn_mode(x_pkt, LEAP_NOTINSYNC, 4, 3); + if (pkt_key != NULL) { + int mac_size = 20; /* max room for MAC */ + x_pkt->exten[0] = htonl(key_id); + mac_size = make_mac((char *)x_pkt, pkt_len, mac_size, pkt_key, (char *)&x_pkt->exten[1]); + if (mac_size) + pkt_len += mac_size + 4; + } + return pkt_len; + } + + int + handle_pkt ( + int rpktl, + struct pkt *rpkt, + struct addrinfo *host + ) + { + struct timeval tv_dst; + int sw_case, digits; + char *hostname = NULL, *log_str, *ref, *ts_str = NULL; + double offset, precision, root_dispersion; + char addr_buf[INET6_ADDRSTRLEN]; + + if(rpktl > 0) + sw_case = 1; + else + sw_case = rpktl; + + switch(sw_case) { + case SERVER_UNUSEABLE: + return -1; + break; + + case PACKET_UNUSEABLE: + break; + + case SERVER_AUTH_FAIL: + break; + + case KOD_DEMOBILIZE: + /* Received a DENY or RESTR KOD packet */ + hostname = addrinfo_to_str(host); + ref = (char *)&rpkt->refid; + add_entry(hostname, ref); + + if (ENABLED_OPT(NORMALVERBOSE)) + printf("sntp handle_pkt: Received KOD packet with code: %c%c%c%c from %s, demobilizing all connections\n", + ref[0], ref[1], ref[2], ref[3], + hostname); + + log_str = emalloc(INET6_ADDRSTRLEN + 72); + snprintf(log_str, INET6_ADDRSTRLEN + 72, + "Received a KOD packet with code %c%c%c%c from %s, demobilizing all connections", + ref[0], ref[1], ref[2], ref[3], + hostname); + log_msg(log_str, 2); + free(log_str); + break; + + case KOD_RATE: + /* Hmm... probably we should sleep a bit here */ + break; + + case 1: + if (ENABLED_OPT(NORMALVERBOSE)) { + getnameinfo(host->ai_addr, host->ai_addrlen, addr_buf, + sizeof(addr_buf), NULL, 0, NI_NUMERICHOST); + printf("sntp handle_pkt: Received %i bytes from %s\n", rpktl, addr_buf); + } + + GETTIMEOFDAY(&tv_dst, (struct timezone *)NULL); + tv_dst.tv_sec += JAN_1970; + + offset_calculation(rpkt, rpktl, &tv_dst, &offset, &precision, &root_dispersion); + + for (digits = 0; (precision *= 10.) < 1.; ++digits) + /* empty */ ; + if (digits > 6) + digits = 6; + + ts_str = tv_to_str(&tv_dst); + printf("%s ", ts_str); + if(offset > 0) + printf("+"); + printf("%.*f", digits, offset); + if (root_dispersion > 0.) + printf(" +/- %f secs", root_dispersion); + printf("\n"); + free(ts_str); + + if(ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME)) + return set_time(offset); + + return 0; + } + + return 1; + } + + void + offset_calculation ( + struct pkt *rpkt, + int rpktl, + struct timeval *tv_dst, + double *offset, + double *precision, + double *root_dispersion + ) + { + l_fp p_rec, p_xmt, p_ref, p_org, tmp, dst; + u_fp p_rdly, p_rdsp; + double t21, t34, delta; + + /* Convert timestamps from network to host byte order */ + p_rdly = NTOHS_FP(rpkt->rootdelay); + p_rdsp = NTOHS_FP(rpkt->rootdisp); + NTOHL_FP(&rpkt->reftime, &p_ref); + NTOHL_FP(&rpkt->org, &p_org); + NTOHL_FP(&rpkt->rec, &p_rec); + NTOHL_FP(&rpkt->xmt, &p_xmt); + + *precision = LOGTOD(rpkt->precision); + #ifdef DEBUG + printf("sntp precision: %f\n", *precision); + #endif /* DEBUG */ + + *root_dispersion = FPTOD(p_rdsp); + + #ifdef DEBUG + printf("sntp rootdelay: %f\n", FPTOD(p_rdly)); + printf("sntp rootdisp: %f\n", *root_dispersion); + + pkt_output(rpkt, rpktl, stdout); + + printf("sntp offset_calculation: rpkt->reftime:\n"); + l_fp_output(&(rpkt->reftime), stdout); + printf("sntp offset_calculation: rpkt->org:\n"); + l_fp_output(&(rpkt->org), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output_bin(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->rec:\n"); + l_fp_output_dec(&(rpkt->rec), stdout); + printf("sntp offset_calculation: rpkt->xmt:\n"); + l_fp_output(&(rpkt->xmt), stdout); + #endif + + /* Compute offset etc. */ + tmp = p_rec; + L_SUB(&tmp, &p_org); + LFPTOD(&tmp, t21); + TVTOTS(tv_dst, &dst); - tmp = dst; - L_SUB(&tmp, &p_xmt); ++ tmp = p_xmt; ++ L_SUB(&tmp, &dst); + LFPTOD(&tmp, t34); + *offset = (t21 + t34) / 2.; + delta = t21 - t34; + + if(ENABLED_OPT(NORMALVERBOSE)) + printf("sntp offset_calculation:\tt21: %.6f\t\t t34: %.6f\n\t\tdelta: %.6f\t offset: %.6f\n", + t21, t34, delta, *offset); + } + /* The heart of (S)NTP, exchange NTP packets and compute values to correct the local clock */ int on_wire (