From: Guilherme Maciel Ferreira Date: Sat, 22 Oct 2011 18:29:18 +0000 (-0200) Subject: Providing different methods to set the types in ICMP v4 and v6 X-Git-Tag: v1.2~52 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a616c4aa354c5eed8c01c65a6707b8989516f489;p=pingcheck Providing different methods to set the types in ICMP v4 and v6 - it was not provided a polymorphic approach --- diff --git a/src/icmp/icmpdestinationunreachablemessage.cpp b/src/icmp/icmpdestinationunreachablemessage.cpp index a082a68..6a6d9f8 100644 --- a/src/icmp/icmpdestinationunreachablemessage.cpp +++ b/src/icmp/icmpdestinationunreachablemessage.cpp @@ -23,14 +23,28 @@ IcmpDestinationUnreachableMessage::~IcmpDestinationUnreachableMessage() { } -Icmpv4Type IcmpDestinationUnreachableMessage::get_type() const +Icmpv4Type IcmpDestinationUnreachableMessage::get_type_v4() const { Icmpv4Type type = static_cast( Payload[ 0 ] ); return type; } -void IcmpDestinationUnreachableMessage::set_type( Icmpv4Type type ) +void IcmpDestinationUnreachableMessage::set_type_v4( const Icmpv4Type type ) +{ + uint8_t n = static_cast( type ); + + Payload[ 0 ] = n; +} + +Icmpv6Type IcmpDestinationUnreachableMessage::get_type_v6() const +{ + Icmpv6Type type = static_cast( Payload[ 0 ] ); + + return type; +} + +void IcmpDestinationUnreachableMessage::set_type_v6( const Icmpv6Type type ) { uint8_t n = static_cast( type ); diff --git a/src/icmp/icmpdestinationunreachablemessage.h b/src/icmp/icmpdestinationunreachablemessage.h index b1eb9d2..ed3810d 100644 --- a/src/icmp/icmpdestinationunreachablemessage.h +++ b/src/icmp/icmpdestinationunreachablemessage.h @@ -48,8 +48,11 @@ public: IcmpDestinationUnreachableMessage(); virtual ~IcmpDestinationUnreachableMessage(); - Icmpv4Type get_type() const; - void set_type( const Icmpv4Type type ); + Icmpv4Type get_type_v4() const; + void set_type_v4( const Icmpv4Type type ); + + Icmpv6Type get_type_v6() const; + void set_type_v6( const Icmpv6Type type ); uint8_t get_code() const; void set_code( const uint8_t code ); diff --git a/src/icmp/icmpechoreplymessage.cpp b/src/icmp/icmpechoreplymessage.cpp index 07228f2..63c90c2 100644 --- a/src/icmp/icmpechoreplymessage.cpp +++ b/src/icmp/icmpechoreplymessage.cpp @@ -23,14 +23,28 @@ IcmpEchoReplyMessage::~IcmpEchoReplyMessage() { } -Icmpv4Type IcmpEchoReplyMessage::get_type() const +Icmpv4Type IcmpEchoReplyMessage::get_type_v4() const { Icmpv4Type type = static_cast( Payload[ 0 ] ); return type; } -void IcmpEchoReplyMessage::set_type( Icmpv4Type type ) +void IcmpEchoReplyMessage::set_type_v4( const Icmpv4Type type ) +{ + uint8_t n = static_cast( type ); + + Payload[ 0 ] = n; +} + +Icmpv6Type IcmpEchoReplyMessage::get_type_v6() const +{ + Icmpv6Type type = static_cast( Payload[ 0 ] ); + + return type; +} + +void IcmpEchoReplyMessage::set_type_v6( const Icmpv6Type type ) { uint8_t n = static_cast( type ); diff --git a/src/icmp/icmpechoreplymessage.h b/src/icmp/icmpechoreplymessage.h index 3e269b2..3606c27 100644 --- a/src/icmp/icmpechoreplymessage.h +++ b/src/icmp/icmpechoreplymessage.h @@ -44,8 +44,11 @@ public: IcmpEchoReplyMessage(); virtual ~IcmpEchoReplyMessage(); - Icmpv4Type get_type() const; - void set_type( const Icmpv4Type type ); + Icmpv4Type get_type_v4() const; + void set_type_v4( const Icmpv4Type type ); + + Icmpv6Type get_type_v6() const; + void set_type_v6( const Icmpv6Type type ); uint8_t get_code() const; void set_code( const uint8_t code ); diff --git a/src/icmp/icmpechorequestmessage.cpp b/src/icmp/icmpechorequestmessage.cpp index eea8f68..7f0ffed 100644 --- a/src/icmp/icmpechorequestmessage.cpp +++ b/src/icmp/icmpechorequestmessage.cpp @@ -23,14 +23,28 @@ IcmpEchoRequestMessage::~IcmpEchoRequestMessage() { } -Icmpv4Type IcmpEchoRequestMessage::get_type() const +Icmpv4Type IcmpEchoRequestMessage::get_type_v4() const { Icmpv4Type type = static_cast( Payload[ 0 ] ); return type; } -void IcmpEchoRequestMessage::set_type( Icmpv4Type type ) +void IcmpEchoRequestMessage::set_type_v4( const Icmpv4Type type ) +{ + uint8_t n = static_cast( type ); + + Payload[ 0 ] = n; +} + +Icmpv6Type IcmpEchoRequestMessage::get_type_v6() const +{ + Icmpv6Type type = static_cast( Payload[ 0 ] ); + + return type; +} + +void IcmpEchoRequestMessage::set_type_v6( const Icmpv6Type type ) { uint8_t n = static_cast( type ); diff --git a/src/icmp/icmpechorequestmessage.h b/src/icmp/icmpechorequestmessage.h index 7cca56a..5b786f1 100644 --- a/src/icmp/icmpechorequestmessage.h +++ b/src/icmp/icmpechorequestmessage.h @@ -44,8 +44,11 @@ public: IcmpEchoRequestMessage(); virtual ~IcmpEchoRequestMessage(); - Icmpv4Type get_type() const; - void set_type( const Icmpv4Type type ); + Icmpv4Type get_type_v4() const; + void set_type_v4( const Icmpv4Type type ); + + Icmpv6Type get_type_v6() const; + void set_type_v6( const Icmpv6Type type ); uint8_t get_code() const; void set_code( const uint8_t code ); diff --git a/src/icmp/icmpgenericmessage.cpp b/src/icmp/icmpgenericmessage.cpp index ecf1ae1..1bbee87 100644 --- a/src/icmp/icmpgenericmessage.cpp +++ b/src/icmp/icmpgenericmessage.cpp @@ -18,12 +18,21 @@ IcmpGenericMessage::~IcmpGenericMessage() { } -Icmpv4Type IcmpGenericMessage::get_type() const +Icmpv4Type IcmpGenericMessage::get_type_v4() const { return Icmpv4Type_Generic; } -void IcmpGenericMessage::set_type( Icmpv4Type ) +void IcmpGenericMessage::set_type_v4( const Icmpv4Type ) +{ +} + +Icmpv6Type IcmpGenericMessage::get_type_v6() const +{ + return Icmpv6Type_Generic; +} + +void IcmpGenericMessage::set_type_v6( const Icmpv6Type ) { } diff --git a/src/icmp/icmpgenericmessage.h b/src/icmp/icmpgenericmessage.h index 13fd2f4..c162d65 100644 --- a/src/icmp/icmpgenericmessage.h +++ b/src/icmp/icmpgenericmessage.h @@ -28,8 +28,11 @@ public: IcmpGenericMessage(); virtual ~IcmpGenericMessage(); - Icmpv4Type get_type() const; - void set_type( const Icmpv4Type type ); + Icmpv4Type get_type_v4() const; + void set_type_v4( const Icmpv4Type type ); + + Icmpv6Type get_type_v6() const; + void set_type_v6( const Icmpv6Type type ); uint8_t get_code() const; void set_code( const uint8_t code ); diff --git a/src/icmp/icmpmessage.cpp b/src/icmp/icmpmessage.cpp index 1f53f6b..e0e9a6b 100644 --- a/src/icmp/icmpmessage.cpp +++ b/src/icmp/icmpmessage.cpp @@ -27,7 +27,23 @@ void IcmpMessage::init( const Icmpv4Type type ) // virtual function. If you call virtual functions from within a // constructor, the Base class version of methods will be called, not the // Derived class, because the Base is constructed before the Derived class. - set_type( type ); + set_type_v4( type ); + set_code( 0 ); + set_checksum( 0 ); + set_identifier( 0 ); + set_sequence_number( 0 ); +} + +/** + * This method MUST be called to initialize the data member of ICMP Messages. + */ +void IcmpMessage::init( const Icmpv6Type type ) +{ + // Note: this code can not be placed in the constructor, because it calls + // virtual function. If you call virtual functions from within a + // constructor, the Base class version of methods will be called, not the + // Derived class, because the Base is constructed before the Derived class. + set_type_v6( type ); set_code( 0 ); set_checksum( 0 ); set_identifier( 0 ); diff --git a/src/icmp/icmpmessage.h b/src/icmp/icmpmessage.h index 89d9773..9301edd 100644 --- a/src/icmp/icmpmessage.h +++ b/src/icmp/icmpmessage.h @@ -18,6 +18,9 @@ // IcmpMessage //----------------------------------------------------------------------------- +/** + * @brief Abstract class to which the ICMP messages are interpreted. + */ class IcmpMessage { public: @@ -25,9 +28,13 @@ public: virtual ~IcmpMessage(); void init( const Icmpv4Type type ); + void init( const Icmpv6Type type ); - virtual Icmpv4Type get_type() const = 0; - virtual void set_type( const Icmpv4Type type ) = 0; + virtual Icmpv4Type get_type_v4() const = 0; + virtual void set_type_v4( const Icmpv4Type type ) = 0; + + virtual Icmpv6Type get_type_v6() const = 0; + virtual void set_type_v6( const Icmpv6Type type ) = 0; virtual uint8_t get_code() const = 0; virtual void set_code( const uint8_t code ) = 0; diff --git a/src/icmp/icmpv4header.cpp b/src/icmp/icmpv4header.cpp index 5ec5a0e..56326a6 100644 --- a/src/icmp/icmpv4header.cpp +++ b/src/icmp/icmpv4header.cpp @@ -43,12 +43,12 @@ Icmpv4Header::Icmpv4Header( Icmpv4Type Icmpv4Header::get_type() const { - return get_icmp_message_format()->get_type(); + return get_icmp_message_format()->get_type_v4(); } void Icmpv4Header::set_type( Icmpv4Type type ) { - get_icmp_message_format()->set_type( type ); + get_icmp_message_format()->set_type_v4( type ); } uint8_t Icmpv4Header::get_code() const @@ -140,7 +140,7 @@ void Icmpv4Header::set_icmp_message_format( Icmpv4Type type ) } BOOST_ASSERT( MessageFormat.get() != NULL ); - BOOST_ASSERT( MessageFormat->get_type() != Icmpv4Type_InvalidLast ); + BOOST_ASSERT( MessageFormat->get_type_v4() != Icmpv4Type_InvalidLast ); } void Icmpv4Header::set_icmp_message_format( std::istream &is )