Browse Source

NTP time sync

pull/1274/head
orignal 6 years ago
parent
commit
85394f2438
  1. 4
      daemon/Daemon.cpp
  2. 13
      libi2pd/Config.cpp
  3. 61
      libi2pd/Timestamp.cpp
  4. 21
      libi2pd/Timestamp.h

4
daemon/Daemon.cpp

@ -23,6 +23,7 @@
#include "ClientContext.h" #include "ClientContext.h"
#include "Crypto.h" #include "Crypto.h"
#include "UPnP.h" #include "UPnP.h"
#include "Timestamp.h"
#include "util.h" #include "util.h"
#include "Event.h" #include "Event.h"
@ -282,6 +283,9 @@ namespace i2p
d.UPnP->Start (); d.UPnP->Start ();
} }
bool nettime; i2p::config::GetOption("nettime.enabled", nettime);
if (nettime) i2p::util::RequestNTPTimeSync ();
bool ntcp; i2p::config::GetOption("ntcp", ntcp); bool ntcp; i2p::config::GetOption("ntcp", ntcp);
bool ssu; i2p::config::GetOption("ssu", ssu); bool ssu; i2p::config::GetOption("ssu", ssu);
LogPrint(eLogInfo, "Daemon: starting Transports"); LogPrint(eLogInfo, "Daemon: starting Transports");

13
libi2pd/Config.cpp

@ -241,6 +241,18 @@ namespace config {
("ntcp2.port", value<uint16_t>()->default_value(0), "Port to listen for incoming NTCP2 connections (default: auto)") ("ntcp2.port", value<uint16_t>()->default_value(0), "Port to listen for incoming NTCP2 connections (default: auto)")
; ;
options_description nettime("Time sync options");
nettime.add_options()
("nettime.enabled", value<bool>()->default_value(false), "Disable time sync (default: disabled)")
("nettime.ntpservers", value<std::string>()->default_value(
"0.ntp.pool.org,"
"1.ntp.pool.org,"
"2.ntp.pool.org,"
"3.ntp.pool.org"
), "Comma separated list of NTCP servers")
("nettime.ntpsyncinterval", value<int>()->default_value(72), "NTP sync interval in hours (default: 72)")
;
m_OptionsDesc m_OptionsDesc
.add(general) .add(general)
.add(limits) .add(limits)
@ -259,6 +271,7 @@ namespace config {
.add(websocket) .add(websocket)
.add(exploratory) .add(exploratory)
.add(ntcp2) .add(ntcp2)
.add(nettime)
; ;
} }

61
libi2pd/Timestamp.cpp

@ -1,6 +1,12 @@
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#include <string>
#include <vector>
#include <chrono>
#include <future>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include "Config.h"
#include "Log.h" #include "Log.h"
#include "I2PEndian.h" #include "I2PEndian.h"
#include "Timestamp.h" #include "Timestamp.h"
@ -15,10 +21,30 @@ namespace i2p
{ {
namespace util namespace util
{ {
static uint64_t GetLocalMillisecondsSinceEpoch ()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count ();
}
static uint32_t GetLocalHoursSinceEpoch ()
{
return std::chrono::duration_cast<std::chrono::hours>(
std::chrono::system_clock::now().time_since_epoch()).count ();
}
static uint64_t GetLocalSecondsSinceEpoch ()
{
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count ();
}
static int64_t g_TimeOffset = 0; // in seconds static int64_t g_TimeOffset = 0; // in seconds
void SyncTimeWithNTP (const std::string& address) static void SyncTimeWithNTP (const std::string& address)
{ {
LogPrint (eLogInfo, "Timestamp: NTP request to ", address);
boost::asio::io_service service; boost::asio::io_service service;
boost::asio::ip::udp::resolver::query query (boost::asio::ip::udp::v4 (), address, "ntp"); boost::asio::ip::udp::resolver::query query (boost::asio::ip::udp::v4 (), address, "ntp");
boost::system::error_code ec; boost::system::error_code ec;
@ -48,19 +74,46 @@ namespace util
} }
catch (std::exception& e) catch (std::exception& e)
{ {
LogPrint (eLogError, "NTP error: ", e.what ()); LogPrint (eLogError, "Timestamp: NTP error: ", e.what ());
} }
if (len >= 8) if (len >= 8)
{ {
auto ourTs = GetSecondsSinceEpoch (); auto ourTs = GetLocalSecondsSinceEpoch ();
uint32_t ts = bufbe32toh (buf + 32); uint32_t ts = bufbe32toh (buf + 32);
if (ts > 2208988800U) ts -= 2208988800U; // 1/1/1970 from 1/1/1900 if (ts > 2208988800U) ts -= 2208988800U; // 1/1/1970 from 1/1/1900
g_TimeOffset = ts - ourTs; g_TimeOffset = ts - ourTs;
LogPrint (eLogInfo, address, " time offset from system time is ", g_TimeOffset, " seconds"); LogPrint (eLogInfo, address, "Timestamp: time offset from system time is ", g_TimeOffset, " seconds");
} }
} }
} }
} }
void RequestNTPTimeSync ()
{
std::string ntpservers; i2p::config::GetOption("nettime.ntpservers", ntpservers);
if (ntpservers.length () > 0)
{
std::vector<std::string> ntpList;
boost::split (ntpList, ntpservers, boost::is_any_of(","), boost::token_compress_on);
if (ntpList.size () > 0)
std::async (std::launch::async, SyncTimeWithNTP, ntpList[rand () % ntpList.size ()]);
}
}
uint64_t GetMillisecondsSinceEpoch ()
{
return GetLocalMillisecondsSinceEpoch () + g_TimeOffset*1000;
}
uint32_t GetHoursSinceEpoch ()
{
return GetLocalHoursSinceEpoch () + g_TimeOffset/3600;
}
uint64_t GetSecondsSinceEpoch ()
{
return GetLocalSecondsSinceEpoch () + g_TimeOffset;
}
} }
} }

21
libi2pd/Timestamp.h

@ -2,29 +2,16 @@
#define TIMESTAMP_H__ #define TIMESTAMP_H__
#include <inttypes.h> #include <inttypes.h>
#include <chrono>
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
inline uint64_t GetMillisecondsSinceEpoch () uint64_t GetMillisecondsSinceEpoch ();
{ uint32_t GetHoursSinceEpoch ();
return std::chrono::duration_cast<std::chrono::milliseconds>( uint64_t GetSecondsSinceEpoch ();
std::chrono::system_clock::now().time_since_epoch()).count ();
}
inline uint32_t GetHoursSinceEpoch () void RequestNTPTimeSync ();
{
return std::chrono::duration_cast<std::chrono::hours>(
std::chrono::system_clock::now().time_since_epoch()).count ();
}
inline uint64_t GetSecondsSinceEpoch ()
{
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count ();
}
} }
} }

Loading…
Cancel
Save