Fuzz transmit timestamp

Add random bits below clock precision to the timestamp to make
it less predictable.
This commit is contained in:
Miroslav Lichvar
2012-05-22 17:16:41 +02:00
parent d0acfc2652
commit df6c2a432f
6 changed files with 41 additions and 13 deletions

24
util.c
View File

@@ -459,16 +459,31 @@ UTI_AdjustTimeval(struct timeval *old_tv, struct timeval *when, struct timeval *
/* ================================================== */
uint32_t
UTI_GetNTPTsFuzz(int precision)
{
uint32_t fuzz;
int fuzz_bits;
fuzz_bits = 32 - 1 + precision;
fuzz = random() % (1 << fuzz_bits);
return fuzz;
}
/* ================================================== */
/* Seconds part of RFC1305 timestamp correponding to the origin of the
struct timeval format. */
#define JAN_1970 0x83aa7e80UL
void
UTI_TimevalToInt64(struct timeval *src,
NTP_int64 *dest)
NTP_int64 *dest, uint32_t fuzz)
{
unsigned long usec = src->tv_usec;
unsigned long sec = src->tv_sec;
uint32_t lo;
/* Recognize zero as a special case - it always signifies
an 'unknown' value */
@@ -478,7 +493,12 @@ UTI_TimevalToInt64(struct timeval *src,
dest->hi = htonl(src->tv_sec + JAN_1970);
/* This formula gives an error of about 0.1us worst case */
dest->lo = htonl(4295 * usec - (usec>>5) - (usec>>9));
lo = 4295 * usec - (usec>>5) - (usec>>9);
/* Add the fuzz */
lo ^= fuzz;
dest->lo = htonl(lo);
}
}