mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-24 04:37:37 +01:00
Time experemental options
This commit is contained in:
parent
5a2b795440
commit
cca0430b9e
5 changed files with 66 additions and 20 deletions
|
@ -230,6 +230,12 @@ namespace config {
|
|||
("exploratory.inbound.quantity", value<int>()->default_value(3), "Exploratory inbound tunnels quantity")
|
||||
("exploratory.outbound.quantity", value<int>()->default_value(3), "Exploratory outbound tunnels quantity")
|
||||
;
|
||||
options_description time("Time Options");
|
||||
time.add_options()
|
||||
("time.use_ntp", value<bool>()->default_value(false), "Use NTP")
|
||||
("time.correcting", value<bool>()->default_value(false), "EXPEREMENTAL: Try correcting with peer, unsecurity")
|
||||
("time.ntp_server", value<std::string>()->default_value(offical_ntp_server), "NTP Server host")
|
||||
;
|
||||
|
||||
m_OptionsDesc
|
||||
.add(general)
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
namespace i2p {
|
||||
namespace config {
|
||||
constexpr const char offical_ntp_server[] ="0.ca.pool.ntp.org";
|
||||
|
||||
extern boost::program_options::variables_map m_Options;
|
||||
|
||||
/**
|
||||
|
|
|
@ -280,8 +280,8 @@ namespace transport
|
|||
uint32_t signedOnTime = bufbe32toh(payload);
|
||||
if (signedOnTime < ts - SSU_CLOCK_SKEW || signedOnTime > ts + SSU_CLOCK_SKEW)
|
||||
{
|
||||
LogPrint (eLogError, "SSU: clock skew detected ", (int)ts - signedOnTime, ". Check your clock");
|
||||
i2p::context.SetError (eRouterErrorClockSkew);
|
||||
i2p::util::timeCorrecting(signedOnTime, ts, SSU_CLOCK_SKEW, "SSU: clock skew detected ") ? ts=i2p::util::GetSecondsSinceEpoch () : 0;
|
||||
|
||||
}
|
||||
}
|
||||
payload += 4; // signed on time
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "Log.h"
|
||||
#include "I2PEndian.h"
|
||||
#include "Timestamp.h"
|
||||
#include"RouterContext.h"
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#ifndef _WIN64
|
||||
|
@ -15,10 +17,32 @@ namespace i2p
|
|||
{
|
||||
namespace util
|
||||
{
|
||||
static int64_t g_TimeOffset = 0; // in seconds
|
||||
|
||||
void SyncTimeWithNTP (const std::string& address)
|
||||
|
||||
|
||||
|
||||
bool timeCorrecting(uint32_t signedOnTime, uint32_t ts, uint32_t skew, const char * ErrorMsg){
|
||||
|
||||
bool Time_Correcting; i2p::config::GetOption("time.correcting", Time_Correcting);
|
||||
bool UseNTP; i2p::config::GetOption("time.ntp_server", UseNTP);
|
||||
|
||||
if(!Time_Correcting && !UseNTP ){
|
||||
LogPrint (eLogError, ErrorMsg, (int)ts - signedOnTime, ". Check your clock");
|
||||
i2p::context.SetError (eRouterErrorClockSkew);
|
||||
return false;
|
||||
}
|
||||
|
||||
UseNTP ? SyncTimeWithNTP() : i2p::util::setTimeOffset( signedOnTime < ts - skew ? signedOnTime : -signedOnTime );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//TODO: ...Syncing with option
|
||||
|
||||
void SyncTimeWithNTP (void)
|
||||
{
|
||||
std::string address; i2p::config::GetOption("time.ntp_server", address);
|
||||
|
||||
boost::asio::io_service service;
|
||||
boost::asio::ip::udp::resolver::query query (boost::asio::ip::udp::v4 (), address, "ntp");
|
||||
boost::system::error_code ec;
|
||||
|
@ -56,7 +80,7 @@ namespace util
|
|||
uint32_t ts = bufbe32toh (buf + 32);
|
||||
if (ts > 2208988800U) ts -= 2208988800U; // 1/1/1970 from 1/1/1900
|
||||
g_TimeOffset = ts - ourTs;
|
||||
LogPrint (eLogInfo, address, " time offset from system time is ", g_TimeOffset, " seconds");
|
||||
LogPrint (eLogDebug, address, " time offset from system time is ", g_TimeOffset, " seconds");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,30 +3,44 @@
|
|||
|
||||
#include <inttypes.h>
|
||||
#include <chrono>
|
||||
#include"Config.h"
|
||||
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
inline uint64_t GetMillisecondsSinceEpoch ()
|
||||
namespace util
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
}
|
||||
|
||||
inline uint32_t GetHoursSinceEpoch ()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::hours>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
}
|
||||
static int64_t g_TimeOffset = 0; // in seconds
|
||||
inline void setTimeOffset(int64_t ts){g_TimeOffset=ts;}
|
||||
|
||||
inline uint64_t GetSecondsSinceEpoch ()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
bool timeCorrecting(uint32_t signedOnTime, uint32_t ts, uint32_t skew, const char * ErrorMsg);
|
||||
|
||||
|
||||
|
||||
inline uint64_t GetSecondsSinceEpoch ()
|
||||
{
|
||||
bool Time_Correcting, Time_UseNTP; i2p::config::GetOption("time.correcting", Time_Correcting);i2p::config::GetOption("time.use_ntp", Time_UseNTP);
|
||||
|
||||
auto tmp_time = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count ();
|
||||
|
||||
return (Time_Correcting || Time_UseNTP) ? tmp_time + i2p::util::g_TimeOffset : tmp_time;
|
||||
}
|
||||
|
||||
inline uint64_t GetMillisecondsSinceEpoch ()
|
||||
{
|
||||
return GetSecondsSinceEpoch()*1000;
|
||||
}
|
||||
|
||||
inline uint32_t GetHoursSinceEpoch ()
|
||||
{
|
||||
return GetSecondsSinceEpoch()/120;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue