diff --git a/Net/include/Poco/Net/ICMPClient.h b/Net/include/Poco/Net/ICMPClient.h index 344404e69..31fb5202a 100644 --- a/Net/include/Poco/Net/ICMPClient.h +++ b/Net/include/Poco/Net/ICMPClient.h @@ -44,7 +44,7 @@ public: mutable Poco::BasicEvent pingError; mutable Poco::BasicEvent pingEnd; - explicit ICMPClient(SocketAddress::Family family); + explicit ICMPClient(SocketAddress::Family family, int dataSize = 48, int ttl = 128, int timeout = 50000); /// Creates an ICMP client. ~ICMPClient(); @@ -62,13 +62,22 @@ public: /// /// Returns the number of valid replies. - static int ping(SocketAddress& address, SocketAddress::Family family, int repeat = 1); + static int ping(SocketAddress& address, + SocketAddress::Family family, + int repeat = 1, + int dataSize = 48, + int ttl = 128, + int timeout = 50000); /// Pings the specified address [repeat] times. /// Notifications are not posted for events. /// /// Returns the number of valid replies. - static int pingIPv4(const std::string& address, int repeat = 1); + static int pingIPv4(const std::string& address, + int repeat = 1, + int dataSize = 48, + int ttl = 128, + int timeout = 50000); /// Calls ICMPClient::ping(SocketAddress&, int) and /// returns the result. /// @@ -76,6 +85,9 @@ public: private: mutable SocketAddress::Family _family; + int _dataSize; + int _ttl; + int _timeout; }; diff --git a/Net/src/ICMPClient.cpp b/Net/src/ICMPClient.cpp index cfe92db01..4639d6a5f 100644 --- a/Net/src/ICMPClient.cpp +++ b/Net/src/ICMPClient.cpp @@ -34,8 +34,11 @@ namespace Poco { namespace Net { -ICMPClient::ICMPClient(SocketAddress::Family family): - _family(family) +ICMPClient::ICMPClient(SocketAddress::Family family, int dataSize, int ttl, int timeout): + _family(family), + _dataSize(dataSize), + _ttl(ttl), + _timeout(timeout) { } @@ -58,7 +61,7 @@ int ICMPClient::ping(SocketAddress& address, int repeat) const { if (repeat <= 0) return 0; - ICMPSocket icmpSocket(_family); + ICMPSocket icmpSocket(_family, _dataSize, _ttl, _timeout); SocketAddress returnAddress; ICMPEventArgs eventArgs(address, repeat, icmpSocket.dataSize(), icmpSocket.ttl()); @@ -105,20 +108,23 @@ int ICMPClient::ping(SocketAddress& address, int repeat) const } -int ICMPClient::pingIPv4(const std::string& address, int repeat) +int ICMPClient::pingIPv4(const std::string& address, int repeat, + int dataSize, int ttl, int timeout) { if (repeat <= 0) return 0; SocketAddress a(address, 0); - return ping(a, IPAddress::IPv4, repeat); + return ping(a, IPAddress::IPv4, repeat, dataSize, ttl, timeout); } -int ICMPClient::ping(SocketAddress& address, IPAddress::Family family, int repeat) +int ICMPClient::ping(SocketAddress& address, + IPAddress::Family family, int repeat, + int dataSize, int ttl, int timeout) { if (repeat <= 0) return 0; - ICMPSocket icmpSocket(family); + ICMPSocket icmpSocket(family, dataSize, ttl, timeout); SocketAddress returnAddress; int received = 0; diff --git a/Net/testsuite/src/ICMPClientTest.cpp b/Net/testsuite/src/ICMPClientTest.cpp index a8e94fbbd..69b179c8b 100644 --- a/Net/testsuite/src/ICMPClientTest.cpp +++ b/Net/testsuite/src/ICMPClientTest.cpp @@ -33,8 +33,7 @@ using Poco::AutoPtr; ICMPClientTest::ICMPClientTest(const std::string& name): - CppUnit::TestCase(name), - _icmpClient(IPAddress::IPv4) + CppUnit::TestCase(name) { } @@ -48,31 +47,67 @@ void ICMPClientTest::testPing() { assert(ICMPClient::pingIPv4("127.0.0.1") > 0); - assert(_icmpClient.ping("127.0.0.1") > 0); - assert(_icmpClient.ping("www.appinf.com", 4) > 0); + Poco::Net::ICMPClient icmpClient(IPAddress::IPv4); + + registerDelegates(icmpClient); + + assert(icmpClient.ping("127.0.0.1") > 0); + assert(icmpClient.ping("www.appinf.com", 4) > 0); // warning: may fail depending on the existence of the addresses at test site // if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses) - assert(0 == _icmpClient.ping("192.168.243.1")); - assert(0 == _icmpClient.ping("10.11.12.13")); + assert(0 == icmpClient.ping("192.168.243.1")); + assert(0 == icmpClient.ping("10.11.12.13")); + + unregisterDelegates(icmpClient); +} + + +void ICMPClientTest::testBigPing() +{ + assert(ICMPClient::pingIPv4("127.0.0.1", 1, 96) > 0); + + Poco::Net::ICMPClient icmpClient(IPAddress::IPv4, 96); + + registerDelegates(icmpClient); + + assert(icmpClient.ping("127.0.0.1", 1) > 0); + assert(icmpClient.ping("www.appinf.com", 4) > 0); + + // warning: may fail depending on the existence of the addresses at test site + // if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses) + assert(0 == icmpClient.ping("192.168.243.1")); + assert(0 == icmpClient.ping("10.11.12.13")); + + unregisterDelegates(icmpClient); +} + + +void ICMPClientTest::registerDelegates(const ICMPClient& icmpClient) +{ + icmpClient.pingBegin += Delegate(this, &ICMPClientTest::onBegin); + icmpClient.pingReply += Delegate(this, &ICMPClientTest::onReply); + icmpClient.pingError += Delegate(this, &ICMPClientTest::onError); + icmpClient.pingEnd += Delegate(this, &ICMPClientTest::onEnd); +} + + +void ICMPClientTest::unregisterDelegates(const ICMPClient& icmpClient) +{ + icmpClient.pingBegin -= Delegate(this, &ICMPClientTest::onBegin); + icmpClient.pingReply -= Delegate(this, &ICMPClientTest::onReply); + icmpClient.pingError -= Delegate(this, &ICMPClientTest::onError); + icmpClient.pingEnd -= Delegate(this, &ICMPClientTest::onEnd); } void ICMPClientTest::setUp() { - _icmpClient.pingBegin += Delegate(this, &ICMPClientTest::onBegin); - _icmpClient.pingReply += Delegate(this, &ICMPClientTest::onReply); - _icmpClient.pingError += Delegate(this, &ICMPClientTest::onError); - _icmpClient.pingEnd += Delegate(this, &ICMPClientTest::onEnd); } void ICMPClientTest::tearDown() { - _icmpClient.pingBegin -= Delegate(this, &ICMPClientTest::onBegin); - _icmpClient.pingReply -= Delegate(this, &ICMPClientTest::onReply); - _icmpClient.pingError -= Delegate(this, &ICMPClientTest::onError); - _icmpClient.pingEnd -= Delegate(this, &ICMPClientTest::onEnd); } @@ -125,6 +160,7 @@ CppUnit::Test* ICMPClientTest::suite() CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ICMPClientTest"); CppUnit_addTest(pSuite, ICMPClientTest, testPing); + CppUnit_addTest(pSuite, ICMPClientTest, testBigPing); return pSuite; } diff --git a/Net/testsuite/src/ICMPClientTest.h b/Net/testsuite/src/ICMPClientTest.h index 8c2509592..9ac78728b 100644 --- a/Net/testsuite/src/ICMPClientTest.h +++ b/Net/testsuite/src/ICMPClientTest.h @@ -27,6 +27,7 @@ public: ~ICMPClientTest(); void testPing(); + void testBigPing(); void setUp(); void tearDown(); @@ -39,7 +40,8 @@ public: void onEnd(const void* pSender, Poco::Net::ICMPEventArgs& args); private: - Poco::Net::ICMPClient _icmpClient; + void registerDelegates(const Poco::Net::ICMPClient& icmpClient); + void unregisterDelegates(const Poco::Net::ICMPClient& icmpClient); };