From 0470811e821b1f174ca6872600b694a223788a71 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 26 Jan 2015 15:20:26 +0100 Subject: [PATCH] clean up IcmpPaketDistributors after stopping pingers --- src/icmp/icmppaketdistributor.cpp | 39 +++++++++++++++++++++++++++++++++++- src/icmp/icmppaketdistributor.h | 11 +++++++-- src/main.cpp | 3 ++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/icmp/icmppaketdistributor.cpp b/src/icmp/icmppaketdistributor.cpp index 9910b5c..1772b32 100644 --- a/src/icmp/icmppaketdistributor.cpp +++ b/src/icmp/icmppaketdistributor.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "boost_assert_handler.h" @@ -65,8 +66,8 @@ bool IcmpPaketDistributorInstanceIdentifierComparator::operator() ( // Definition of IcmpPaketDistributor //----------------------------------------------------------------------------- -std::map IcmpPaketDistributor::Instances; +map_type IcmpPaketDistributor::Instances; // initialize + IcmpPaketDistributorItem IcmpPaketDistributor::get_distributor( const icmp::socket::protocol_type &protocol, @@ -187,5 +188,39 @@ bool IcmpPaketDistributor::unregister_pinger( const PingerItem old_pinger ) return was_erased; } +/** + * @brief for all instances: close sockets, unregister all pingers + */ +void IcmpPaketDistributor::clean_up_all() +{ + BOOST_FOREACH( map_type::value_type &instance, Instances ) + instance.second->clean_up(); + + Instances.clear(); +} + +void IcmpPaketDistributor::clean_up() +{ + if (PingerList.size() > 0) + GlobalLogger.warning() << "There were still " << PingerList.size() + << " pingers registered in IcmpPaketDistributor!" << std::endl; + PingerList.clear(); + + boost::system::error_code error; + //Socket.shutdown(icmp::socket::shutdown_both, error); // both=send and receive + //if ( error ) + // GlobalLogger.warning() << "Received error " << error << " when shutting down ICMP socket"; + // always gave an error system:9 (probably EBADF: Bad file descriptor) + + Socket.close(error); + if ( error ) + GlobalLogger.warning() << "Received error " << error << " when closing ICMP socket"; +} + +IcmpPaketDistributor::~IcmpPaketDistributor() +{ + GlobalLogger.info() << "Destroying IcmpPaketDistributor" << std::endl; +} + // (created using vim -- the world's best text editor) diff --git a/src/icmp/icmppaketdistributor.h b/src/icmp/icmppaketdistributor.h index 29310ba..fbcf711 100644 --- a/src/icmp/icmppaketdistributor.h +++ b/src/icmp/icmppaketdistributor.h @@ -30,6 +30,7 @@ #include "host/pinger.h" #include "host/networkinterface.hpp" +#include "icmp/icmppinger.h" using boost::asio::ip::icmp; @@ -46,6 +47,8 @@ struct IcmpPaketDistributorInstanceIdentifierComparator class IcmpPaketDistributor; typedef boost::shared_ptr IcmpPaketDistributorItem; +typedef std::map map_type; //----------------------------------------------------------------------------- // IcmpPaketDistributor @@ -66,7 +69,9 @@ public: const icmp::socket::protocol_type &protocol, const std::string &network_interface ); - ~IcmpPaketDistributor() {}; + static void clean_up_all(); + + ~IcmpPaketDistributor(); private: // hide away constructor, copy constructor and copy operator @@ -80,6 +85,7 @@ private: void register_receive_handler(); void handle_receive( const boost::system::error_code &error, const size_t &bytes_transferred ); + void clean_up(); private: @@ -95,8 +101,7 @@ private: std::set PingerList; /// Instances, one for each (protocol, interface) - pair - static std::map Instances; + static map_type Instances; }; diff --git a/src/main.cpp b/src/main.cpp index dfbc078..3b69d10 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,7 @@ on this file might be covered by the GNU General Public License. #include "host/pingerfactory.h" #include "host/pingprotocol.h" #include "host/pingscheduler.h" +#include "icmp/icmppaketdistributor.h" using namespace std; @@ -306,6 +307,8 @@ void stop_pingers( { scheduler->stop_pinging(); } + + IcmpPaketDistributor::clean_up_all(); } -- 1.7.1