From e6034908c1106a8b03c77e8d411842217b75463f Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Mon, 26 Sep 2016 17:19:55 +0200 Subject: [PATCH] code cleanup; fixed some issues reported by Klocwork --- Net/include/Poco/Net/ICMPSocket.h | 28 +----------------------- Net/include/Poco/Net/ICMPSocketImpl.h | 31 +++++++++++++++++++++++++++ Net/src/ICMPSocket.cpp | 23 ++++++++++++++++---- Net/src/ICMPSocketImpl.cpp | 19 ++++++---------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/Net/include/Poco/Net/ICMPSocket.h b/Net/include/Poco/Net/ICMPSocket.h index cfcc2ff3e..083de4375 100644 --- a/Net/include/Poco/Net/ICMPSocket.h +++ b/Net/include/Poco/Net/ICMPSocket.h @@ -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 diff --git a/Net/include/Poco/Net/ICMPSocketImpl.h b/Net/include/Poco/Net/ICMPSocketImpl.h index c033e5af1..12781fd05 100644 --- a/Net/include/Poco/Net/ICMPSocketImpl.h +++ b/Net/include/Poco/Net/ICMPSocketImpl.h @@ -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 diff --git a/Net/src/ICMPSocket.cpp b/Net/src/ICMPSocket.cpp index ece9871c3..7e1204fe1 100644 --- a/Net/src/ICMPSocket.cpp +++ b/Net/src/ICMPSocket.cpp @@ -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(impl())->dataSize(); +} + + +int ICMPSocket::ttl() const +{ + return static_cast(impl())->ttl(); +} + + +int ICMPSocket::timeout() const +{ + return static_cast(impl())->timeout(); +} + + } } // namespace Poco::Net diff --git a/Net/src/ICMPSocketImpl.cpp b/Net/src/ICMPSocketImpl.cpp index 2deefbaae..11ebc30d2 100644 --- a/Net/src/ICMPSocketImpl.cpp +++ b/Net/src/ICMPSocketImpl.cpp @@ -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 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; }