From cea7699779324b9569264b985d141bb8739db7db Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 19 Oct 2011 21:55:58 -0200 Subject: [PATCH] Renamed IcmpPacket class to Icmpv4Packet, in order to distinguish from the ICMPv6 classes --- src/CMakeLists.txt | 2 +- src/icmp/icmppacket.cpp | 125 --------------------------------------------- src/icmp/icmppacket.h | 107 -------------------------------------- src/icmp/icmppinger.cpp | 20 ++++---- src/icmp/icmppinger.h | 10 ++-- src/icmp/icmpv4packet.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++ src/icmp/icmpv4packet.h | 107 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 248 insertions(+), 248 deletions(-) delete mode 100644 src/icmp/icmppacket.cpp delete mode 100644 src/icmp/icmppacket.h create mode 100644 src/icmp/icmpv4packet.cpp create mode 100644 src/icmp/icmpv4packet.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 536a5da..a5a93ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,7 +50,7 @@ set(SOURCES icmp/icmpgenericmessage.cpp icmp/icmpv4header.cpp icmp/icmpmessage.cpp - icmp/icmppacket.cpp + icmp/icmpv4packet.cpp icmp/icmppinger.cpp ip/ipv4header.cpp ip/ipv6header.cpp diff --git a/src/icmp/icmppacket.cpp b/src/icmp/icmppacket.cpp deleted file mode 100644 index bbebfcb..0000000 --- a/src/icmp/icmppacket.cpp +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright (c) 2003-2010 Christopher M. Kohlhoff -// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -#include "icmp/icmppacket.h" - -#include - -using namespace std; -using boost::asio::ip::address; - -//----------------------------------------------------------------------------- -// IcmpPacket -//----------------------------------------------------------------------------- - -/** - * @brief Default constructor. - */ -IcmpPacket::IcmpPacket() : - IpHeader(), - IcmpPayloadHeader(), - IcmpPayloadData() -{ -} - -/** - * @brief Parameterized constructor. - * - * @param icmp_header The ICMP header. - * @param icmp_data The ICMP payload data. - */ -IcmpPacket::IcmpPacket( - const Icmpv4Header &icmp_header, - const IcmpData &icmp_data -) : - IpHeader(), - IcmpPayloadHeader( icmp_header ), - IcmpPayloadData( icmp_data ) -{ -} - -/** - * @brief Destructor. - */ -IcmpPacket::~IcmpPacket() -{ -} - -/** - * @brief Obtain the IP header. - * - * @return The IP header object. - */ -Ipv4Header IcmpPacket::get_ip_header() const -{ - return IpHeader; -} - -/** - * @brief Obtain the ICMP header. - * - * @return The ICMP header object. - */ -Icmpv4Header IcmpPacket::get_icmp_header() const -{ - return IcmpPayloadHeader; -} - -/** - * @brief Obtain the ICMP payload data. - * - * @return The ICMP data. - */ -IcmpData IcmpPacket::get_icmp_data() const -{ - return IcmpPayloadData; -} - -/** - * @brief Check if this object matches with all the parameters. - * - * @param type The type of ICMP message. - * @param identifier The identifier. - * @param sequence_number The sequence number. - * @param source_address The source address. - * - * @return @c true if this matches all the parameters, or @c false if at least - * one does not match. - */ -bool IcmpPacket::match( - const IcmpType type, - const uint16_t identifier, - const uint16_t sequence_number, - const address &source_address -) const -{ - bool type_match = IcmpPayloadHeader.get_type() == type ? true : false; - bool identifier_match = IcmpPayloadHeader.get_identifier() == identifier ? true: false; - bool seq_num_match = IcmpPayloadHeader.get_sequence_number() == sequence_number ? true : false; - bool address_match = IpHeader.get_source_address() == source_address ? true : false; - - return ( type_match && identifier_match && seq_num_match && address_match ); -} - -istream& operator>>( - istream &is, - IcmpPacket &packet -) -{ - is >> packet.IpHeader >> packet.IcmpPayloadHeader >> packet.IcmpPayloadData; - - return is; -} - -ostream& operator<<( - ostream& os, - const IcmpPacket& packet -) -{ - os << packet.IcmpPayloadHeader << packet.IcmpPayloadData; - - return os; -} diff --git a/src/icmp/icmppacket.h b/src/icmp/icmppacket.h deleted file mode 100644 index 1106bc0..0000000 --- a/src/icmp/icmppacket.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2003-2010 Christopher M. Kohlhoff -// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -#ifndef ICMPPACKET_H -#define ICMPPACKET_H - -#include - -#include -#include - -#include - -#include "icmp/icmpv4header.h" -#include "icmp/icmpdata.h" -#include "icmp/icmptype.h" -#include "ip/ipv4header.h" - -//----------------------------------------------------------------------------- -// IcmpPacket -//----------------------------------------------------------------------------- - -/** - * @brief This class represents the ICMP Packet. - * - * The ICMP Packet format is: - * - * @code - * 0 8 16 31 - * +-------+-------+---------------+------------------------------+ --- - * | | | | | ^ - * |version|header | type of | total length in bytes | | - * | (4) | length| service | | | - * +-------+-------+---------------+-+-+-+------------------------+ | - * | | | | | | | - * | identification |0|D|M| fragment offset | | - * | | |F|F| | | - * +---------------+---------------+-+-+-+------------------------+ | - * | | | | | - * | time to live | protocol | header checksum | IPv4 Header - * | | | | 20 bytes - * +---------------+---------------+------------------------------+ | - * | | | - * | source IPv4 address | | - * | | | - * +--------------------------------------------------------------+ | - * | | | - * | destination IPv4 address | | - * | | v - * +---------------+---------------+------------------------------+ --- - * | | | | ^ - * | type | code | checksum | | - * | | | | | - * +---------------+---------------+------------------------------+ | - * | | | ICMP Payload - * | identifier | sequence number | (header + - * | | | data) - * +-------------------------------+------------------------------+ 8+ bytes - * | | | - * | data (optional) | | - * | | v - * +-------------------------------+------------------------------+ --- - * @endcode - */ -class IcmpPacket -{ -public: - IcmpPacket(); - IcmpPacket( - const Icmpv4Header &icmp_header, - const IcmpData &icmp_data - ); - virtual ~IcmpPacket(); - - Ipv4Header get_ip_header() const; - Icmpv4Header get_icmp_header() const; - IcmpData get_icmp_data() const; - - bool match( - const IcmpType type, - const uint16_t identifier, - const uint16_t sequence_number, - const boost::asio::ip::address &source_address - ) const; - - friend std::istream& operator>>( - std::istream &is, - IcmpPacket &packet - ); - friend std::ostream& operator<<( - std::ostream &os, - const IcmpPacket &packet - ); - -private: - /// The IP header. - Ipv4Header IpHeader; - /// The ICMP packet header. - Icmpv4Header IcmpPayloadHeader; - /// The ICMP packet payload (data). - IcmpData IcmpPayloadData; -}; - -#endif /* ICMPPACKET_H */ diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index 56e9e1b..a7cf8b8 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -24,7 +24,7 @@ #include "icmp/icmpchecksum.h" #include "icmp/icmpdata.h" #include "icmp/icmpv4header.h" -#include "icmp/icmppacket.h" +#include "icmp/icmpv4packet.h" #include "icmp/icmptype.h" #include "ip/ipv4header.h" @@ -122,13 +122,13 @@ void IcmpPinger::start_send() { ++SequenceNumber; - IcmpPacket icmp_echo_request_packet = create_echo_request( SequenceNumber ); + Icmpv4Packet icmp_echo_request_packet = create_echo_request( SequenceNumber ); BOOST_ASSERT( PingerStatus == PingStatus_NotSent ); send_echo_request( icmp_echo_request_packet ); } -IcmpPacket IcmpPinger::create_echo_request( +Icmpv4Packet IcmpPinger::create_echo_request( const uint16_t sequence_number ) const { @@ -144,10 +144,10 @@ IcmpPacket IcmpPinger::create_echo_request( type, code, checksum, Identifier, sequence_number ); - return IcmpPacket( icmp_header, icmp_data ); + return Icmpv4Packet( icmp_header, icmp_data ); } -void IcmpPinger::send_echo_request( const IcmpPacket &icmp_packet ) +void IcmpPinger::send_echo_request( const Icmpv4Packet &icmp_packet ) { boost::asio::streambuf request_buffer; ostream os( &request_buffer ); @@ -250,7 +250,7 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) } // Decode the reply packet. - IcmpPacket icmp_packet; + Icmpv4Packet icmp_packet; if (! (is >> icmp_packet) ) { GlobalLogger.notice() << "Warning: ignoring broken ICMP packet" @@ -304,12 +304,12 @@ void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) /** * @brief Prints the ICMP echo reply messages. * - * @param icmp_packet The IcmpPacket object containing the received echo reply data. + * @param icmp_packet The Icmpv4Packet object containing the received echo reply data. * @param bytes_transferred Number of bytes transferred. * @return void */ void IcmpPinger::print_echo_reply( - const IcmpPacket &icmp_packet, + const Icmpv4Packet &icmp_packet, const size_t &bytes_transferred ) const { @@ -336,12 +336,12 @@ void IcmpPinger::print_echo_reply( /** * @brief Prints the destination unreachable messages. * - * @param icmp_packet The IcmpPacket object containing the received destination + * @param icmp_packet The Icmpv4Packet object containing the received destination * unreachable data. * @return void */ void IcmpPinger::print_destination_unreachable( - const IcmpPacket &icmp_packet + const Icmpv4Packet &icmp_packet ) const { BOOST_ASSERT( icmp_packet.get_icmp_header().get_type() == IcmpType_DestinationUnreachable ); diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index d0a09c6..da294d7 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -16,7 +16,7 @@ #include "host/pinger.h" #include "host/pingstatus.h" -class IcmpPacket; +class Icmpv4Packet; //----------------------------------------------------------------------------- // IcmpPinger @@ -46,8 +46,8 @@ private: void set_destination_endpoint( const std::string &destination_ip ); void start_send(); - IcmpPacket create_echo_request( const uint16_t sequence_number ) const; - void send_echo_request( const IcmpPacket &icmp_packet ); + Icmpv4Packet create_echo_request( const uint16_t sequence_number ) const; + void send_echo_request( const Icmpv4Packet &icmp_packet ); void schedule_timeout_echo_reply(); void handle_ping_done(); @@ -55,11 +55,11 @@ private: void handle_receive_icmp_packet( const std::size_t &bytes_transferred ); void print_echo_reply( - const IcmpPacket &icmp_packet, + const Icmpv4Packet &icmp_packet, const std::size_t &bytes_transferred ) const; void print_destination_unreachable( - const IcmpPacket &icmp_packet + const Icmpv4Packet &icmp_packet ) const; void set_ping_status( PingStatus ping_status ); diff --git a/src/icmp/icmpv4packet.cpp b/src/icmp/icmpv4packet.cpp new file mode 100644 index 0000000..fea6701 --- /dev/null +++ b/src/icmp/icmpv4packet.cpp @@ -0,0 +1,125 @@ +// Copyright (c) 2003-2010 Christopher M. Kohlhoff +// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +#include "icmp/icmpv4packet.h" + +#include + +using namespace std; +using boost::asio::ip::address; + +//----------------------------------------------------------------------------- +// Icmpv4Packet +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +Icmpv4Packet::Icmpv4Packet() : + IpHeader(), + IcmpPayloadHeader(), + IcmpPayloadData() +{ +} + +/** + * @brief Parameterized constructor. + * + * @param icmp_header The ICMP header. + * @param icmp_data The ICMP payload data. + */ +Icmpv4Packet::Icmpv4Packet( + const Icmpv4Header &icmp_header, + const IcmpData &icmp_data +) : + IpHeader(), + IcmpPayloadHeader( icmp_header ), + IcmpPayloadData( icmp_data ) +{ +} + +/** + * @brief Destructor. + */ +Icmpv4Packet::~Icmpv4Packet() +{ +} + +/** + * @brief Obtain the IP header. + * + * @return The IP header object. + */ +Ipv4Header Icmpv4Packet::get_ip_header() const +{ + return IpHeader; +} + +/** + * @brief Obtain the ICMP header. + * + * @return The ICMP header object. + */ +Icmpv4Header Icmpv4Packet::get_icmp_header() const +{ + return IcmpPayloadHeader; +} + +/** + * @brief Obtain the ICMP payload data. + * + * @return The ICMP data. + */ +IcmpData Icmpv4Packet::get_icmp_data() const +{ + return IcmpPayloadData; +} + +/** + * @brief Check if this object matches with all the parameters. + * + * @param type The type of ICMP message. + * @param identifier The identifier. + * @param sequence_number The sequence number. + * @param source_address The source address. + * + * @return @c true if this matches all the parameters, or @c false if at least + * one does not match. + */ +bool Icmpv4Packet::match( + const IcmpType type, + const uint16_t identifier, + const uint16_t sequence_number, + const address &source_address +) const +{ + bool type_match = IcmpPayloadHeader.get_type() == type ? true : false; + bool identifier_match = IcmpPayloadHeader.get_identifier() == identifier ? true: false; + bool seq_num_match = IcmpPayloadHeader.get_sequence_number() == sequence_number ? true : false; + bool address_match = IpHeader.get_source_address() == source_address ? true : false; + + return ( type_match && identifier_match && seq_num_match && address_match ); +} + +istream& operator>>( + istream &is, + Icmpv4Packet &packet +) +{ + is >> packet.IpHeader >> packet.IcmpPayloadHeader >> packet.IcmpPayloadData; + + return is; +} + +ostream& operator<<( + ostream& os, + const Icmpv4Packet& packet +) +{ + os << packet.IcmpPayloadHeader << packet.IcmpPayloadData; + + return os; +} diff --git a/src/icmp/icmpv4packet.h b/src/icmp/icmpv4packet.h new file mode 100644 index 0000000..26fa9be --- /dev/null +++ b/src/icmp/icmpv4packet.h @@ -0,0 +1,107 @@ +// Copyright (c) 2003-2010 Christopher M. Kohlhoff +// Modifications (c) 2011 by Guilherme Maciel Ferreira / Intra2net AG +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +#ifndef ICMPV4PACKET_H +#define ICMPV4PACKET_H + +#include + +#include +#include + +#include + +#include "icmp/icmpv4header.h" +#include "icmp/icmpdata.h" +#include "icmp/icmptype.h" +#include "ip/ipv4header.h" + +//----------------------------------------------------------------------------- +// Icmpv4Packet +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the ICMP Packet. + * + * The ICMP Packet format is: + * + * @code + * 0 8 16 31 + * +-------+-------+---------------+------------------------------+ --- + * | | | | | ^ + * |version|header | type of | total length in bytes | | + * | (4) | length| service | | | + * +-------+-------+---------------+-+-+-+------------------------+ | + * | | | | | | | + * | identification |0|D|M| fragment offset | | + * | | |F|F| | | + * +---------------+---------------+-+-+-+------------------------+ | + * | | | | | + * | time to live | protocol | header checksum | IPv4 Header + * | | | | 20 bytes + * +---------------+---------------+------------------------------+ | + * | | | + * | source IPv4 address | | + * | | | + * +--------------------------------------------------------------+ | + * | | | + * | destination IPv4 address | | + * | | v + * +---------------+---------------+------------------------------+ --- + * | | | | ^ + * | type | code | checksum | | + * | | | | | + * +---------------+---------------+------------------------------+ | + * | | | ICMP Payload + * | identifier | sequence number | (header + + * | | | data) + * +-------------------------------+------------------------------+ 8+ bytes + * | | | + * | data (optional) | | + * | | v + * +-------------------------------+------------------------------+ --- + * @endcode + */ +class Icmpv4Packet +{ +public: + Icmpv4Packet(); + Icmpv4Packet( + const Icmpv4Header &icmp_header, + const IcmpData &icmp_data + ); + virtual ~Icmpv4Packet(); + + Ipv4Header get_ip_header() const; + Icmpv4Header get_icmp_header() const; + IcmpData get_icmp_data() const; + + bool match( + const IcmpType type, + const uint16_t identifier, + const uint16_t sequence_number, + const boost::asio::ip::address &source_address + ) const; + + friend std::istream& operator>>( + std::istream &is, + Icmpv4Packet &packet + ); + friend std::ostream& operator<<( + std::ostream &os, + const Icmpv4Packet &packet + ); + +private: + /// The IP header. + Ipv4Header IpHeader; + /// The ICMP packet header. + Icmpv4Header IcmpPayloadHeader; + /// The ICMP packet payload (data). + IcmpData IcmpPayloadData; +}; + +#endif // ICMPV4PACKET_H -- 1.7.1