From 0c64d208d9368fadd6d8911b49e5f322b970f9d4 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Wed, 11 Jun 2014 15:17:28 -0400 Subject: [PATCH] adding ntp support to fc --- CMakeLists.txt | 4 ++++ include/fc/crypto/hex.hpp | 2 ++ include/fc/network/ntp.hpp | 15 ++++++++++++++ ntp_test.cpp | 15 ++++++++++++++ src/crypto/hex.cpp | 6 ++++++ src/network/ntp.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 include/fc/network/ntp.hpp create mode 100644 ntp_test.cpp create mode 100644 src/network/ntp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cba89e..0dc4d6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,7 @@ set( fc_sources src/network/udp_socket.cpp src/network/http/http_connection.cpp src/network/http/http_server.cpp + src/network/ntp.cpp src/network/ip.cpp src/network/rate_limiting.cpp src/network/resolve.cpp @@ -214,6 +215,9 @@ target_include_directories(fc target_link_libraries( fc PUBLIC easylzma_static ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${CMAKE_DL_LIBS}) +add_executable( ntp_test ntp_test.cpp ) +target_link_libraries( ntp_test fc ) + #add_executable( test_compress tests/compress.cpp ) #target_link_libraries( test_compress fc ) #add_executable( test_aes tests/aes_test.cpp ) diff --git a/include/fc/crypto/hex.hpp b/include/fc/crypto/hex.hpp index f3ca29e..201f167 100644 --- a/include/fc/crypto/hex.hpp +++ b/include/fc/crypto/hex.hpp @@ -1,10 +1,12 @@ #pragma once #include #include +#include namespace fc { uint8_t from_hex( char c ); fc::string to_hex( const char* d, uint32_t s ); + std::string to_hex( const std::vector& data ); /** * @return the number of bytes decoded diff --git a/include/fc/network/ntp.hpp b/include/fc/network/ntp.hpp new file mode 100644 index 0000000..96fb54f --- /dev/null +++ b/include/fc/network/ntp.hpp @@ -0,0 +1,15 @@ +#pragma once +#include +#include + + +namespace fc { + + class ntp + { + public: + static void set_server( const std::string& hostname, uint16_t port = 123 ); + static fc::time_point get_time(); + }; + +} // namespace fc diff --git a/ntp_test.cpp b/ntp_test.cpp new file mode 100644 index 0000000..fbea98b --- /dev/null +++ b/ntp_test.cpp @@ -0,0 +1,15 @@ +#include +#include + +int main( int argc, char** argv ) +{ + fc::time_point ntp_time = fc::ntp::get_time(); + auto delta = ntp_time - fc::time_point::now(); + auto minutes = delta.count() / 1000000 / 60; + auto hours = delta.count() / 1000000 / 60 / 60; + auto seconds = delta.count() / 1000000; + auto msec= delta.count() / 1000; + + idump( (ntp_time)(delta)(msec)(seconds)(minutes)(hours) ); + return 0; +} diff --git a/src/crypto/hex.cpp b/src/crypto/hex.cpp index 41690fe..67ef919 100644 --- a/src/crypto/hex.cpp +++ b/src/crypto/hex.cpp @@ -39,5 +39,11 @@ namespace fc { } return out_pos - (uint8_t*)out_data; } + std::string to_hex( const std::vector& data ) + { + if( data.size() ) + return to_hex( data.data(), data.size() ); + return ""; + } } diff --git a/src/network/ntp.cpp b/src/network/ntp.cpp new file mode 100644 index 0000000..7f9e347 --- /dev/null +++ b/src/network/ntp.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +#include + +namespace fc +{ + static fc::ip::endpoint ntp_server; + + + void ntp::set_server( const std::string& hostname, uint16_t port ) + { + auto eps = resolve( hostname, port ); + if( eps.size() ) + ntp_server = eps.front(); + } + + fc::time_point ntp::get_time() + { + static bool init_ntp_server = [](){ + set_server( "pool.ntp.org", 123 ); + return true; + }(); + (void)init_ntp_server; + + udp_socket sock; + sock.open(); + + std::array send_buf { {010,0,0,0,0,0,0,0,0} }; + + sock.send_to( (const char*)send_buf.data(), send_buf.size(), ntp_server ); + + fc::ip::endpoint from; + std::array recv_buf; + sock.receive_from( (char*)recv_buf.data(), recv_buf.size(), from ); + + return fc::time_point() + fc::seconds( ntohl((time_t)recv_buf[4]) - 2208988800U); + } + +}