i2pd/libi2pd/Timestamp.cpp

124 lines
3.3 KiB
C++
Raw Normal View History

2016-09-21 23:18:51 +03:00
#include <inttypes.h>
#include <string.h>
2018-11-14 19:06:53 +03:00
#include <string>
#include <vector>
#include <chrono>
#include <future>
2016-09-21 23:18:51 +03:00
#include <boost/asio.hpp>
2018-11-14 19:06:53 +03:00
#include <boost/algorithm/string.hpp>
#include "Config.h"
2016-11-01 23:27:44 +03:00
#include "Log.h"
2016-09-21 23:18:51 +03:00
#include "I2PEndian.h"
2016-09-21 19:02:52 +03:00
#include "Timestamp.h"
#ifdef WIN32
#ifndef _WIN64
#define _USE_32BIT_TIME_T
#endif
#endif
2016-09-21 19:02:52 +03:00
namespace i2p
{
namespace util
{
2018-11-14 19:06:53 +03:00
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 ();
}
2016-11-02 01:40:00 +03:00
static int64_t g_TimeOffset = 0; // in seconds
2016-09-21 23:18:51 +03:00
2018-11-14 19:06:53 +03:00
static void SyncTimeWithNTP (const std::string& address)
2016-09-21 23:18:51 +03:00
{
2018-11-14 19:06:53 +03:00
LogPrint (eLogInfo, "Timestamp: NTP request to ", address);
2016-09-21 23:18:51 +03:00
boost::asio::io_service service;
boost::asio::ip::udp::resolver::query query (boost::asio::ip::udp::v4 (), address, "ntp");
boost::system::error_code ec;
auto it = boost::asio::ip::udp::resolver (service).resolve (query, ec);
if (!ec && it != boost::asio::ip::udp::resolver::iterator())
{
auto ep = (*it).endpoint (); // take first one
boost::asio::ip::udp::socket socket (service);
socket.open (boost::asio::ip::udp::v4 (), ec);
if (!ec)
{
2016-11-01 23:27:44 +03:00
uint8_t buf[48];// 48 bytes NTP request/response
memset (buf, 0, 48);
htobe32buf (buf, (3 << 27) | (3 << 24)); // RFC 4330
2016-09-21 23:18:51 +03:00
size_t len = 0;
try
{
2016-11-01 23:27:44 +03:00
socket.send_to (boost::asio::buffer (buf, 48), ep);
2016-11-02 01:40:00 +03:00
int i = 0;
while (!socket.available() && i < 10) // 10 seconds max
{
2018-01-06 06:48:51 +03:00
std::this_thread::sleep_for (std::chrono::seconds(1));
2016-11-02 01:40:00 +03:00
i++;
}
2016-11-02 01:40:00 +03:00
if (socket.available ())
len = socket.receive_from (boost::asio::buffer (buf, 48), ep);
2016-09-21 23:18:51 +03:00
}
catch (std::exception& e)
{
2018-11-14 19:06:53 +03:00
LogPrint (eLogError, "Timestamp: NTP error: ", e.what ());
}
2016-09-21 23:18:51 +03:00
if (len >= 8)
{
2018-11-14 19:06:53 +03:00
auto ourTs = GetLocalSecondsSinceEpoch ();
2016-11-01 23:27:44 +03:00
uint32_t ts = bufbe32toh (buf + 32);
2016-09-21 23:18:51 +03:00
if (ts > 2208988800U) ts -= 2208988800U; // 1/1/1970 from 1/1/1900
2016-11-02 01:40:00 +03:00
g_TimeOffset = ts - ourTs;
2018-11-14 20:27:11 +03:00
LogPrint (eLogInfo, "Timestamp: ", address, " time offset from system time is ", g_TimeOffset, " seconds");
}
2016-09-21 23:18:51 +03:00
}
2018-11-14 20:27:11 +03:00
else
LogPrint (eLogError, "Timestamp: Couldn't open UDP socket");
2016-09-21 23:18:51 +03:00
}
2018-11-14 20:27:11 +03:00
else
LogPrint (eLogError, "Timestamp: Couldn't resove address ", address);
}
2018-11-14 19:06:53 +03:00
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;
}
2016-09-21 19:02:52 +03:00
}
}