Changed 'ttl' field from signed to unsigned, avoiding loss of precision.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 20 Dec 2011 09:17:20 +0000 (07:17 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 20 Dec 2011 09:17:20 +0000 (07:17 -0200)
src/dns/hostaddress.cpp
src/dns/hostaddress.h
src/dns/timetolive.cpp
src/dns/timetolive.h

index fe75b82..9f1f6aa 100644 (file)
@@ -35,7 +35,7 @@ HostAddress::HostAddress() :
 
 HostAddress::HostAddress(
         const string &ip,
-        int ttl
+        uint32_t ttl
 ) :
     Ip( ip ),
     Ttl( ttl )
index e4eddd2..07931a8 100644 (file)
@@ -20,6 +20,8 @@ on this file might be covered by the GNU General Public License.
 #ifndef HOSTADDRESS_H_
 #define HOSTADDRESS_H_
 
+#include <stdint.h>
+
 #include <string>
 
 #include "dns/timetolive.h"
@@ -34,7 +36,7 @@ public:
     HostAddress();
     HostAddress(
             const std::string &ip,
-            int ttl
+            uint32_t ttl
     );
     ~HostAddress();
 
index 2a8b140..fa786b9 100644 (file)
@@ -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<int>(
+    uint32_t elapsed_seconds = static_cast<int>(
             (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;
 }
index 225723b..bc6b1e3 100644 (file)
@@ -20,6 +20,8 @@ on this file might be covered by the GNU General Public License.
 #ifndef TIMETOLIVE_H
 #define TIMETOLIVE_H
 
+#include <stdint.h>
+
 #include <boost/asio.hpp>
 
 //-----------------------------------------------------------------------------
@@ -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;