code cleanup; fixed some issues reported by Klocwork

This commit is contained in:
Guenter Obiltschnig 2016-09-26 17:19:55 +02:00
parent 9bc12df4a2
commit e6034908c1
4 changed files with 58 additions and 43 deletions

View File

@ -42,7 +42,7 @@ public:
ICMPSocket(const Socket& socket); ICMPSocket(const Socket& socket);
/// Creates the ICMPSocket with the SocketImpl /// Creates the ICMPSocket with the SocketImpl
/// from another socket. The SocketImpl must be /// from another socket. The SocketImpl must be
/// a DatagramSocketImpl, otherwise an InvalidArgumentException /// a ICMPSocketImpl, otherwise an InvalidArgumentException
/// will be thrown. /// will be thrown.
~ICMPSocket(); ~ICMPSocket();
@ -84,35 +84,9 @@ protected:
/// ///
/// The SocketImpl must be a ICMPSocketImpl, otherwise /// The SocketImpl must be a ICMPSocketImpl, otherwise
/// an InvalidArgumentException will be thrown. /// an InvalidArgumentException will be thrown.
private:
int _dataSize;
int _ttl;
int _timeout;
}; };
//
// inlines
//
inline int ICMPSocket::dataSize() const
{
return _dataSize;
}
inline int ICMPSocket::ttl() const
{
return _ttl;
}
inline int ICMPSocket::timeout() const
{
return _timeout;
}
} } // namespace Poco::Net } } // namespace Poco::Net

View File

@ -50,15 +50,46 @@ public:
/// ///
/// Returns the time elapsed since the originating request was sent. /// Returns the time elapsed since the originating request was sent.
int dataSize() const;
/// Returns the data size in bytes.
int ttl() const;
/// Returns the Time-To-Live value.
int timeout() const;
/// Returns the socket timeout value.
protected: protected:
~ICMPSocketImpl(); ~ICMPSocketImpl();
private: private:
ICMPPacket _icmpPacket; ICMPPacket _icmpPacket;
int _ttl;
int _timeout; int _timeout;
}; };
//
// inlines
//
inline int ICMPSocketImpl::dataSize() const
{
return _icmpPacket.getDataSize();
}
inline int ICMPSocketImpl::ttl() const
{
return _ttl;
}
inline int ICMPSocketImpl::timeout() const
{
return _timeout;
}
} } // namespace Poco::Net } } // namespace Poco::Net

View File

@ -27,10 +27,7 @@ namespace Net {
ICMPSocket::ICMPSocket(IPAddress::Family family, int dataSize, int ttl, int timeout): ICMPSocket::ICMPSocket(IPAddress::Family family, int dataSize, int ttl, int timeout):
Socket(new ICMPSocketImpl(family, dataSize, ttl, timeout)), Socket(new ICMPSocketImpl(family, dataSize, ttl, timeout))
_dataSize(dataSize),
_ttl(ttl),
_timeout(timeout)
{ {
} }
@ -78,4 +75,22 @@ int ICMPSocket::receiveFrom(SocketAddress& address, int flags)
} }
int ICMPSocket::dataSize() const
{
return static_cast<ICMPSocketImpl*>(impl())->dataSize();
}
int ICMPSocket::ttl() const
{
return static_cast<ICMPSocketImpl*>(impl())->ttl();
}
int ICMPSocket::timeout() const
{
return static_cast<ICMPSocketImpl*>(impl())->timeout();
}
} } // namespace Poco::Net } } // namespace Poco::Net

View File

@ -19,6 +19,7 @@
#include "Poco/Timespan.h" #include "Poco/Timespan.h"
#include "Poco/Timestamp.h" #include "Poco/Timestamp.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Buffer.h"
using Poco::TimeoutException; using Poco::TimeoutException;
@ -33,6 +34,7 @@ namespace Net {
ICMPSocketImpl::ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout): ICMPSocketImpl::ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout):
RawSocketImpl(family, IPPROTO_ICMP), RawSocketImpl(family, IPPROTO_ICMP),
_icmpPacket(family, dataSize), _icmpPacket(family, dataSize),
_ttl(ttl),
_timeout(timeout) _timeout(timeout)
{ {
setOption(IPPROTO_IP, IP_TTL, ttl); setOption(IPPROTO_IP, IP_TTL, ttl);
@ -55,7 +57,7 @@ int ICMPSocketImpl::sendTo(const void*, int, const SocketAddress& address, int f
int ICMPSocketImpl::receiveFrom(void*, int, SocketAddress& address, int flags) int ICMPSocketImpl::receiveFrom(void*, int, SocketAddress& address, int flags)
{ {
int maxPacketSize = _icmpPacket.maxPacketSize(); int maxPacketSize = _icmpPacket.maxPacketSize();
unsigned char* buffer = new unsigned char[maxPacketSize]; Poco::Buffer<unsigned char> buffer(maxPacketSize);
try try
{ {
@ -68,31 +70,24 @@ int ICMPSocketImpl::receiveFrom(void*, int, SocketAddress& address, int flags)
// fake ping responses will cause an endless loop. // fake ping responses will cause an endless loop.
throw TimeoutException(); throw TimeoutException();
} }
SocketImpl::receiveFrom(buffer, maxPacketSize, address, flags); SocketImpl::receiveFrom(buffer.begin(), maxPacketSize, address, flags);
} }
while (!_icmpPacket.validReplyID(buffer, maxPacketSize)); while (!_icmpPacket.validReplyID(buffer.begin(), maxPacketSize));
}
catch (TimeoutException&)
{
delete [] buffer;
throw;
} }
catch (Exception&) catch (Exception&)
{ {
std::string err = _icmpPacket.errorDescription(buffer, maxPacketSize); std::string err = _icmpPacket.errorDescription(buffer.begin(), maxPacketSize);
delete [] buffer;
if (!err.empty()) if (!err.empty())
throw ICMPException(err); throw ICMPException(err);
else else
throw; throw;
} }
struct timeval then = _icmpPacket.time(buffer, maxPacketSize); struct timeval then = _icmpPacket.time(buffer.begin(), maxPacketSize);
struct timeval now = _icmpPacket.time(); struct timeval now = _icmpPacket.time();
int elapsed = (((now.tv_sec * 1000000) + now.tv_usec) - ((then.tv_sec * 1000000) + then.tv_usec))/1000; int elapsed = (((now.tv_sec * 1000000) + now.tv_usec) - ((then.tv_sec * 1000000) + then.tv_usec))/1000;
delete[] buffer;
return elapsed; return elapsed;
} }