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);
/// Creates the ICMPSocket with the SocketImpl
/// from another socket. The SocketImpl must be
/// a DatagramSocketImpl, otherwise an InvalidArgumentException
/// a ICMPSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
~ICMPSocket();
@ -84,35 +84,9 @@ protected:
///
/// The SocketImpl must be a ICMPSocketImpl, otherwise
/// 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

View File

@ -50,15 +50,46 @@ public:
///
/// 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:
~ICMPSocketImpl();
private:
ICMPPacket _icmpPacket;
int _ttl;
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

View File

@ -27,10 +27,7 @@ namespace Net {
ICMPSocket::ICMPSocket(IPAddress::Family family, int dataSize, int ttl, int timeout):
Socket(new ICMPSocketImpl(family, dataSize, ttl, timeout)),
_dataSize(dataSize),
_ttl(ttl),
_timeout(timeout)
Socket(new ICMPSocketImpl(family, dataSize, ttl, 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

View File

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