diff options
author | Ron Yorston <rmy@pobox.com> | 2023-02-13 10:01:35 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-02-13 10:01:35 +0000 |
commit | 4e5fb4341fddc2b99e815f27ac577d85a57994c2 (patch) | |
tree | c9e2eae6d2752d7e7f907ad39e3797b0fd5c3e02 /networking | |
parent | 0f2feac4b7e518e838b80d7b8ceac8f9699ae997 (diff) | |
parent | 93ae7464e6e460f25b73e4ffefd2d9a6499eae30 (diff) | |
download | busybox-w32-4e5fb4341fddc2b99e815f27ac577d85a57994c2.tar.gz busybox-w32-4e5fb4341fddc2b99e815f27ac577d85a57994c2.tar.bz2 busybox-w32-4e5fb4341fddc2b99e815f27ac577d85a57994c2.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'networking')
-rw-r--r-- | networking/ntpd.c | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c index 204e1d7c2..c7519b8fb 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c | |||
@@ -551,20 +551,21 @@ gettime1900d(void) | |||
551 | return G.cur_time; | 551 | return G.cur_time; |
552 | } | 552 | } |
553 | 553 | ||
554 | static void | ||
555 | d_to_tv(struct timeval *tv, double d) | ||
556 | { | ||
557 | tv->tv_sec = (long)d; | ||
558 | tv->tv_usec = (d - tv->tv_sec) * 1000000; | ||
559 | } | ||
560 | |||
561 | static NOINLINE double | 554 | static NOINLINE double |
562 | lfp_to_d(l_fixedpt_t lfp) | 555 | lfp_to_d(l_fixedpt_t lfp) |
563 | { | 556 | { |
564 | double ret; | 557 | double ret; |
565 | lfp.int_partl = ntohl(lfp.int_partl); | 558 | lfp.int_partl = ntohl(lfp.int_partl); |
566 | lfp.fractionl = ntohl(lfp.fractionl); | 559 | lfp.fractionl = ntohl(lfp.fractionl); |
567 | ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX); | 560 | ret = (double)lfp.int_partl + ((double)lfp.fractionl / (1ULL << 32)); |
561 | /* | ||
562 | * Shift timestamps before 1970 to the second NTP era (2036-2106): | ||
563 | * int_partl value of OFFSET_1900_1970 (2208988800) is interpreted as | ||
564 | * the start of year 1970 and it is the minimal representable time, | ||
565 | * all values form the sequence 2208988800..0xffffffff,0..2208988799. | ||
566 | */ | ||
567 | if (lfp.int_partl < OFFSET_1900_1970) | ||
568 | ret += (double)(1ULL << 32); /* because int_partl is 32-bit wide */ | ||
568 | return ret; | 569 | return ret; |
569 | } | 570 | } |
570 | static NOINLINE double | 571 | static NOINLINE double |
@@ -573,7 +574,7 @@ sfp_to_d(s_fixedpt_t sfp) | |||
573 | double ret; | 574 | double ret; |
574 | sfp.int_parts = ntohs(sfp.int_parts); | 575 | sfp.int_parts = ntohs(sfp.int_parts); |
575 | sfp.fractions = ntohs(sfp.fractions); | 576 | sfp.fractions = ntohs(sfp.fractions); |
576 | ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX); | 577 | ret = (double)sfp.int_parts + ((double)sfp.fractions / (1 << 16)); |
577 | return ret; | 578 | return ret; |
578 | } | 579 | } |
579 | #if ENABLE_FEATURE_NTPD_SERVER | 580 | #if ENABLE_FEATURE_NTPD_SERVER |
@@ -582,8 +583,8 @@ d_to_lfp(l_fixedpt_t *lfp, double d) | |||
582 | { | 583 | { |
583 | uint32_t intl; | 584 | uint32_t intl; |
584 | uint32_t frac; | 585 | uint32_t frac; |
585 | intl = (uint32_t)d; | 586 | intl = (uint32_t)(time_t)d; |
586 | frac = (uint32_t)((d - intl) * UINT_MAX); | 587 | frac = (uint32_t)((d - (time_t)d) * 0xffffffff); |
587 | lfp->int_partl = htonl(intl); | 588 | lfp->int_partl = htonl(intl); |
588 | lfp->fractionl = htonl(frac); | 589 | lfp->fractionl = htonl(frac); |
589 | } | 590 | } |
@@ -593,7 +594,7 @@ d_to_sfp(s_fixedpt_t *sfp, double d) | |||
593 | uint16_t ints; | 594 | uint16_t ints; |
594 | uint16_t frac; | 595 | uint16_t frac; |
595 | ints = (uint16_t)d; | 596 | ints = (uint16_t)d; |
596 | frac = (uint16_t)((d - ints) * USHRT_MAX); | 597 | frac = (uint16_t)((d - ints) * 0xffff); |
597 | sfp->int_parts = htons(ints); | 598 | sfp->int_parts = htons(ints); |
598 | sfp->fractions = htons(frac); | 599 | sfp->fractions = htons(frac); |
599 | } | 600 | } |
@@ -1036,8 +1037,17 @@ step_time(double offset) | |||
1036 | time_t tval; | 1037 | time_t tval; |
1037 | 1038 | ||
1038 | xgettimeofday(&tvc); | 1039 | xgettimeofday(&tvc); |
1040 | /* This code adds floating point value on the order of 1.0 | ||
1041 | * to a value of ~4 billion (as of years 203x). | ||
1042 | * With 52-bit mantissa, "only" 20 bits of offset's precision | ||
1043 | * are used (~1 microsecond), the rest is lost. | ||
1044 | * Some 200 billion years later, when tvc.tv_sec would have | ||
1045 | * 63 significant bits, the precision loss would be catastrophic, | ||
1046 | * a more complex code would be needed. | ||
1047 | */ | ||
1039 | dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset; | 1048 | dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset; |
1040 | d_to_tv(&tvn, dtime); | 1049 | tvn.tv_sec = (time_t)dtime; |
1050 | tvn.tv_usec = (dtime - tvn.tv_sec) * 1000000; | ||
1041 | xsettimeofday(&tvn); | 1051 | xsettimeofday(&tvn); |
1042 | 1052 | ||
1043 | VERB2 { | 1053 | VERB2 { |