From 2ceb3c89cae574c16391d15ec80c0db3c9739103 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 5 Jun 2013 12:49:12 +0200 Subject: [PATCH] Move NTP_int32 conversion functions to util.c --- acquire.c | 8 ++++---- broadcast.c | 4 ++-- ntp.h | 18 ------------------ ntp_core.c | 8 ++++---- util.c | 16 ++++++++++++++++ util.h | 3 +++ 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/acquire.c b/acquire.c index 0eef796..a681b34 100644 --- a/acquire.c +++ b/acquire.c @@ -265,8 +265,8 @@ probe_source(SourceRecord *src) pkt.stratum = 0; pkt.poll = 4; pkt.precision = -6; /* as ntpdate */ - pkt.root_delay = double_to_int32(1.0); /* 1 second */ - pkt.root_dispersion = double_to_int32(1.0); /* likewise */ + pkt.root_delay = UTI_DoubleToInt32(1.0); /* 1 second */ + pkt.root_dispersion = UTI_DoubleToInt32(1.0); /* likewise */ pkt.reference_id = 0; pkt.reference_ts.hi = 0; /* Set to 0 */ pkt.reference_ts.lo = 0; /* Set to 0 */ @@ -393,8 +393,8 @@ process_receive(NTP_Packet *msg, SourceRecord *src, struct timeval *now) return; } - root_delay = int32_to_double(msg->root_delay); - root_dispersion = int32_to_double(msg->root_dispersion); + root_delay = UTI_Int32ToDouble(msg->root_delay); + root_dispersion = UTI_Int32ToDouble(msg->root_dispersion); UTI_Int64ToTimeval(&src->last_tx, &local_orig); UTI_Int64ToTimeval(&msg->receive_ts, &remote_rx); diff --git a/broadcast.c b/broadcast.c index 11bbcbc..04ef4d3 100644 --- a/broadcast.c +++ b/broadcast.c @@ -99,8 +99,8 @@ timeout_handler(void *arbitrary) /* If we're sending a client mode packet and we aren't synchronized yet, we might have to set up artificial values for some of these parameters */ - message.root_delay = double_to_int32(our_root_delay); - message.root_dispersion = double_to_int32(our_root_dispersion); + message.root_delay = UTI_DoubleToInt32(our_root_delay); + message.root_dispersion = UTI_DoubleToInt32(our_root_dispersion); message.reference_id = htonl((NTP_int32) our_ref_id); diff --git a/ntp.h b/ntp.h index 7ebeece..8801970 100644 --- a/ntp.h +++ b/ntp.h @@ -93,22 +93,4 @@ typedef union { #define NTP_NORMAL_PACKET_SIZE offsetof(NTP_Packet, auth_keyid) -/* ================================================== */ - -inline static double -int32_to_double(NTP_int32 x) -{ - return (double) ntohl(x) / 65536.0; -} - -/* ================================================== */ - -inline static NTP_int32 -double_to_int32(double x) -{ - return htonl((NTP_int32)(0.5 + 65536.0 * x)); -} - -/* ================================================== */ - #endif /* GOT_NTP_H */ diff --git a/ntp_core.c b/ntp_core.c index 228c087..73a877a 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -597,8 +597,8 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ /* If we're sending a client mode packet and we aren't synchronized yet, we might have to set up artificial values for some of these parameters */ - message.root_delay = double_to_int32(our_root_delay); - message.root_dispersion = double_to_int32(our_root_dispersion); + message.root_delay = UTI_DoubleToInt32(our_root_delay); + message.root_dispersion = UTI_DoubleToInt32(our_root_dispersion); message.reference_id = htonl((NTP_int32) our_ref_id); @@ -859,8 +859,8 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins source_is_synchronized = 1; } - pkt_root_delay = int32_to_double(message->root_delay); - pkt_root_dispersion = int32_to_double(message->root_dispersion); + pkt_root_delay = UTI_Int32ToDouble(message->root_delay); + pkt_root_dispersion = UTI_Int32ToDouble(message->root_dispersion); /* Perform packet validity tests */ diff --git a/util.c b/util.c index 0fee32b..6dbf67f 100644 --- a/util.c +++ b/util.c @@ -471,6 +471,22 @@ UTI_GetNTPTsFuzz(int precision) /* ================================================== */ +double +UTI_Int32ToDouble(NTP_int32 x) +{ + return (double) ntohl(x) / 65536.0; +} + +/* ================================================== */ + +NTP_int32 +UTI_DoubleToInt32(double x) +{ + return htonl((NTP_int32)(0.5 + 65536.0 * x)); +} + +/* ================================================== */ + /* Seconds part of RFC1305 timestamp correponding to the origin of the struct timeval format. */ #define JAN_1970 0x83aa7e80UL diff --git a/util.h b/util.h index 3f94f77..02fb6fb 100644 --- a/util.h +++ b/util.h @@ -91,6 +91,9 @@ extern void UTI_AdjustTimeval(struct timeval *old_tv, struct timeval *when, stru /* Get a random value to fuzz an NTP timestamp in the given precision */ extern uint32_t UTI_GetNTPTsFuzz(int precision); +extern double UTI_Int32ToDouble(NTP_int32 x); +extern NTP_int32 UTI_DoubleToInt32(double x); + extern void UTI_TimevalToInt64(struct timeval *src, NTP_int64 *dest, uint32_t fuzz); extern void UTI_Int64ToTimeval(NTP_int64 *src, struct timeval *dest);