From b0a693a06438195ec7aa1259e82e89df214f40ee Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 20 Dec 2011 07:17:20 -0200 Subject: [PATCH] Changed 'ttl' field from signed to unsigned, avoiding loss of precision. --- src/dns/hostaddress.cpp | 2 +- src/dns/hostaddress.h | 4 +++- src/dns/timetolive.cpp | 14 +++++++------- src/dns/timetolive.h | 12 +++++++----- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/dns/hostaddress.cpp b/src/dns/hostaddress.cpp index fe75b82..9f1f6aa 100644 --- a/src/dns/hostaddress.cpp +++ b/src/dns/hostaddress.cpp @@ -35,7 +35,7 @@ HostAddress::HostAddress() : HostAddress::HostAddress( const string &ip, - int ttl + uint32_t ttl ) : Ip( ip ), Ttl( ttl ) diff --git a/src/dns/hostaddress.h b/src/dns/hostaddress.h index e4eddd2..07931a8 100644 --- a/src/dns/hostaddress.h +++ b/src/dns/hostaddress.h @@ -20,6 +20,8 @@ on this file might be covered by the GNU General Public License. #ifndef HOSTADDRESS_H_ #define HOSTADDRESS_H_ +#include + #include #include "dns/timetolive.h" @@ -34,7 +36,7 @@ public: HostAddress(); HostAddress( const std::string &ip, - int ttl + uint32_t ttl ); ~HostAddress(); diff --git a/src/dns/timetolive.cpp b/src/dns/timetolive.cpp index 2a8b140..fa786b9 100644 --- a/src/dns/timetolive.cpp +++ b/src/dns/timetolive.cpp @@ -29,7 +29,7 @@ using boost::posix_time::ptime; // TimeToLive //----------------------------------------------------------------------------- -TimeToLive::TimeToLive( int ttl ) : +TimeToLive::TimeToLive( uint32_t ttl ) : Ttl( ttl ), TtlSetTime( microsec_clock::universal_time() ) { @@ -42,7 +42,7 @@ TimeToLive::~TimeToLive() /** * @return the original time-to-live value, as specified by the set method. */ -int TimeToLive::get_value() const +uint32_t TimeToLive::get_value() const { return Ttl; } @@ -50,7 +50,7 @@ int TimeToLive::get_value() const /** * @param ttl the time-to-live value. */ -void TimeToLive::set_value( const int ttl ) +void TimeToLive::set_value( const uint32_t ttl ) { BOOST_ASSERT( 0 < ttl ); @@ -61,14 +61,14 @@ void TimeToLive::set_value( const int ttl ) /** * @return the value of the time-to-live updated since the last set was called. */ -int TimeToLive::get_updated_value() const +uint32_t TimeToLive::get_updated_value() const { ptime now = microsec_clock::universal_time(); - int elapsed_seconds = static_cast( + uint32_t elapsed_seconds = static_cast( (now - TtlSetTime).total_seconds() ); - int original_ttl = get_value(); - int remaining_seconds = original_ttl - elapsed_seconds; + uint32_t original_ttl = get_value(); + uint32_t remaining_seconds = original_ttl - elapsed_seconds; return remaining_seconds; } diff --git a/src/dns/timetolive.h b/src/dns/timetolive.h index 225723b..bc6b1e3 100644 --- a/src/dns/timetolive.h +++ b/src/dns/timetolive.h @@ -20,6 +20,8 @@ on this file might be covered by the GNU General Public License. #ifndef TIMETOLIVE_H #define TIMETOLIVE_H +#include + #include //----------------------------------------------------------------------------- @@ -29,17 +31,17 @@ on this file might be covered by the GNU General Public License. class TimeToLive { public: - TimeToLive( int ttl = 0 ); + TimeToLive( uint32_t ttl = 0 ); ~TimeToLive(); - int get_value() const; - void set_value( const int ttl ); + uint32_t get_value() const; + void set_value( const uint32_t ttl ); - int get_updated_value() const; + uint32_t get_updated_value() const; private: /// the numeric time-to-live - int Ttl; + uint32_t Ttl; /// the time when the time-to-live was set, so it is possible to know the /// elapsed time boost::posix_time::ptime TtlSetTime; -- 1.7.1