mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 08:31:43 +02:00
* NTP client implementation
This commit is contained in:
109
Net/src/NTPClient.cpp
Normal file
109
Net/src/NTPClient.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// NTPClient.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Net/src/NTPClient.cpp#1 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: NTP
|
||||
// Module: NTPClient
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Net/NTPClient.h"
|
||||
#include "Poco/Net/NTPPacket.h"
|
||||
#include "Poco/Net/DatagramSocket.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
|
||||
|
||||
using Poco::TimeoutException;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
NTPClient::NTPClient(IPAddress::Family family, int timeout):
|
||||
_family(family), _timeout(timeout)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NTPClient::~NTPClient()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int NTPClient::request(const std::string& address) const
|
||||
{
|
||||
SocketAddress addr(address, 123);
|
||||
return request(addr);
|
||||
}
|
||||
|
||||
|
||||
int NTPClient::request(SocketAddress& address) const
|
||||
{
|
||||
Poco::Net::SocketAddress sa;
|
||||
DatagramSocket ntpSocket(_family);
|
||||
ntpSocket.setReceiveTimeout(_timeout);
|
||||
ntpSocket.bind(sa);
|
||||
|
||||
SocketAddress returnAddress;
|
||||
|
||||
NTPEventArgs eventArgs(address);
|
||||
|
||||
NTPPacket packet;
|
||||
Poco::UInt8 p[1024];
|
||||
packet.packet(&p[0]);
|
||||
|
||||
ntpSocket.sendTo(p, 48, address);
|
||||
|
||||
int received = 0;
|
||||
try
|
||||
{
|
||||
Poco::Net::SocketAddress sender;
|
||||
int n = ntpSocket.receiveFrom(p, sizeof(p)-1, sender);
|
||||
|
||||
if (n < 48) // NTP packet must have at least 48 bytes
|
||||
throw Poco::Net::NTPException("Invalid response received");
|
||||
|
||||
packet.setPacket(p);
|
||||
eventArgs.setPacket(packet);
|
||||
++received;
|
||||
response.notify(this, eventArgs);
|
||||
}
|
||||
catch (Poco::TimeoutException &)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
93
Net/src/NTPEventArgs.cpp
Normal file
93
Net/src/NTPEventArgs.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// NTPEventArgs.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Net/src/NTPEventArgs.cpp#1 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: NTP
|
||||
// Module: NTPEventArgs
|
||||
//
|
||||
// Implementation of NTPEventArgs
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Net/NTPEventArgs.h"
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Net/DNS.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
|
||||
|
||||
using Poco::IOException;
|
||||
using Poco::InvalidArgumentException;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
NTPEventArgs::NTPEventArgs(const SocketAddress& address):
|
||||
_address(address), _packet()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NTPEventArgs::~NTPEventArgs()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
std::string NTPEventArgs::hostName() const
|
||||
{
|
||||
try
|
||||
{
|
||||
return DNS::resolve(_address.host().toString()).name();
|
||||
}
|
||||
catch (HostNotFoundException&)
|
||||
{
|
||||
}
|
||||
catch (NoAddressFoundException&)
|
||||
{
|
||||
}
|
||||
catch (DNSException&)
|
||||
{
|
||||
}
|
||||
catch (IOException&)
|
||||
{
|
||||
}
|
||||
return _address.host().toString();
|
||||
}
|
||||
|
||||
|
||||
std::string NTPEventArgs::hostAddress() const
|
||||
{
|
||||
return _address.host().toString();
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
170
Net/src/NTPPacket.cpp
Normal file
170
Net/src/NTPPacket.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
//
|
||||
// NTPPacket.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Net/src/NTPPacket.cpp#2 $
|
||||
//
|
||||
// Library: Net
|
||||
// Package: NTP
|
||||
// Module: NTPPacket
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Net/NTPPacket.h"
|
||||
#include "Poco/Net/NetException.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
#include "Poco/ByteOrder.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
#pragma pack(push,1)
|
||||
typedef struct _NTPPacketData {
|
||||
Poco::Int8 mode:3;
|
||||
Poco::Int8 vn:3;
|
||||
Poco::Int8 li:2;
|
||||
Poco::Int8 stratum;
|
||||
Poco::Int8 pool;
|
||||
Poco::Int8 prec;
|
||||
Poco::Int32 rootdelay;
|
||||
Poco::Int32 rootdisp;
|
||||
Poco::Int32 refid;
|
||||
Poco::Int64 rts;
|
||||
Poco::Int64 ots;
|
||||
Poco::Int64 vts;
|
||||
Poco::Int64 tts;
|
||||
} NTPPacketData;
|
||||
#pragma pack(pop)
|
||||
|
||||
|
||||
NTPPacket::NTPPacket() :
|
||||
// the next 3 fields must be in reverse order from spec
|
||||
_leapIndicator(3),
|
||||
_version(4),
|
||||
_mode(3),
|
||||
|
||||
_stratum(0),
|
||||
_pool(6),
|
||||
_precision(-18),
|
||||
_rootDelay(0),
|
||||
_rootDispersion(0),
|
||||
_referenceId(0),
|
||||
_referenceTimestamp(0),
|
||||
_receiveTimestamp(0),
|
||||
_transmitTimestamp(0)
|
||||
{
|
||||
Poco::Timestamp ts;
|
||||
_originateTimestamp = ts.utcTime();
|
||||
}
|
||||
|
||||
|
||||
NTPPacket::NTPPacket(Poco::UInt8 *packet)
|
||||
{
|
||||
setPacket(packet);
|
||||
}
|
||||
|
||||
|
||||
NTPPacket::~NTPPacket()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void NTPPacket::packet(Poco::UInt8 *packet) const
|
||||
{
|
||||
NTPPacketData *p = (NTPPacketData*)packet;
|
||||
|
||||
p->li = _leapIndicator;
|
||||
p->vn = _version;
|
||||
p->mode = _mode;
|
||||
p->stratum = _stratum;
|
||||
p->pool = _pool;
|
||||
p->prec = _precision;
|
||||
p->rootdelay = Poco::ByteOrder::toNetwork(_rootDelay);
|
||||
p->rootdisp = Poco::ByteOrder::toNetwork(_rootDispersion);
|
||||
p->refid = Poco::ByteOrder::toNetwork(_referenceId);
|
||||
p->rts = Poco::ByteOrder::toNetwork(_referenceTimestamp);
|
||||
p->ots = Poco::ByteOrder::toNetwork(_originateTimestamp);
|
||||
p->vts = Poco::ByteOrder::toNetwork(_receiveTimestamp);
|
||||
p->tts = Poco::ByteOrder::toNetwork(_transmitTimestamp);
|
||||
}
|
||||
|
||||
|
||||
void NTPPacket::setPacket(Poco::UInt8 *packet)
|
||||
{
|
||||
NTPPacketData *p = (NTPPacketData*)packet;
|
||||
|
||||
_leapIndicator = p->li;
|
||||
_version = p->vn;
|
||||
_mode = p->mode;
|
||||
_stratum = p->stratum;
|
||||
_pool = p->pool;
|
||||
_precision = p->prec;
|
||||
_rootDelay = Poco::ByteOrder::fromNetwork(p->rootdelay);
|
||||
_rootDispersion = Poco::ByteOrder::fromNetwork(p->rootdisp);
|
||||
_referenceId = Poco::ByteOrder::fromNetwork(p->refid);
|
||||
_referenceTimestamp = Poco::ByteOrder::fromNetwork(p->rts);
|
||||
_originateTimestamp = Poco::ByteOrder::fromNetwork(p->ots);
|
||||
_receiveTimestamp = Poco::ByteOrder::fromNetwork(p->vts);
|
||||
_transmitTimestamp = Poco::ByteOrder::fromNetwork(p->tts);
|
||||
}
|
||||
|
||||
|
||||
Poco::Timestamp NTPPacket::referenceTime() const
|
||||
{
|
||||
return convertTime(_referenceTimestamp);
|
||||
}
|
||||
|
||||
|
||||
Poco::Timestamp NTPPacket::originateTime() const
|
||||
{
|
||||
return convertTime(_originateTimestamp);
|
||||
}
|
||||
|
||||
|
||||
Poco::Timestamp NTPPacket::receiveTime() const
|
||||
{
|
||||
return convertTime(_receiveTimestamp);
|
||||
}
|
||||
|
||||
|
||||
Poco::Timestamp NTPPacket::transmitTime() const
|
||||
{
|
||||
return convertTime(_transmitTimestamp);
|
||||
}
|
||||
|
||||
|
||||
Poco::Timestamp NTPPacket::convertTime(Poco::Int64 tm) const
|
||||
{
|
||||
const unsigned long seventyYears = 2208988800UL;
|
||||
Poco::UInt32 secsSince1900 = UInt32(Poco::ByteOrder::toLittleEndian(tm) >> 32);
|
||||
unsigned long epoch = secsSince1900 - seventyYears;
|
||||
return Poco::Timestamp::fromEpochTime(epoch);
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
@@ -66,6 +66,7 @@ POCO_IMPLEMENT_EXCEPTION(FTPException, NetException, "FTP Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(SMTPException, NetException, "SMTP Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(POP3Exception, NetException, "POP3 Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(ICMPException, NetException, "ICMP Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(NTPException, NetException, "NTP Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(HTMLFormException, NetException, "HTML Form Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(WebSocketException, NetException, "WebSocket Exception")
|
||||
POCO_IMPLEMENT_EXCEPTION(UnsupportedFamilyException, NetException, "Unknown or unsupported socket family.")
|
||||
|
Reference in New Issue
Block a user