adding ntp support to fc

This commit is contained in:
Daniel Larimer 2014-06-11 15:17:28 -04:00
parent 51de9e6abf
commit 0c64d208d9
6 changed files with 84 additions and 0 deletions

View file

@ -148,6 +148,7 @@ set( fc_sources
src/network/udp_socket.cpp src/network/udp_socket.cpp
src/network/http/http_connection.cpp src/network/http/http_connection.cpp
src/network/http/http_server.cpp src/network/http/http_server.cpp
src/network/ntp.cpp
src/network/ip.cpp src/network/ip.cpp
src/network/rate_limiting.cpp src/network/rate_limiting.cpp
src/network/resolve.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}) 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 ) #add_executable( test_compress tests/compress.cpp )
#target_link_libraries( test_compress fc ) #target_link_libraries( test_compress fc )
#add_executable( test_aes tests/aes_test.cpp ) #add_executable( test_aes tests/aes_test.cpp )

View file

@ -1,10 +1,12 @@
#pragma once #pragma once
#include <fc/string.hpp> #include <fc/string.hpp>
#include <fc/utility.hpp> #include <fc/utility.hpp>
#include <vector>
namespace fc { namespace fc {
uint8_t from_hex( char c ); uint8_t from_hex( char c );
fc::string to_hex( const char* d, uint32_t s ); fc::string to_hex( const char* d, uint32_t s );
std::string to_hex( const std::vector<char>& data );
/** /**
* @return the number of bytes decoded * @return the number of bytes decoded

View file

@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <fc/time.hpp>
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

15
ntp_test.cpp Normal file
View file

@ -0,0 +1,15 @@
#include <fc/network/ntp.hpp>
#include <fc/log/logger.hpp>
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;
}

View file

@ -39,5 +39,11 @@ namespace fc {
} }
return out_pos - (uint8_t*)out_data; return out_pos - (uint8_t*)out_data;
} }
std::string to_hex( const std::vector<char>& data )
{
if( data.size() )
return to_hex( data.data(), data.size() );
return "";
}
} }

42
src/network/ntp.cpp Normal file
View file

@ -0,0 +1,42 @@
#include <fc/network/ntp.hpp>
#include <fc/network/udp_socket.hpp>
#include <fc/network/resolve.hpp>
#include <fc/network/ip.hpp>
#include <array>
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<unsigned char, 48> 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<unsigned long, 1024> 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);
}
}