Providing different methods to set the types in ICMP v4 and v6
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 22 Oct 2011 18:29:18 +0000 (16:29 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 22 Oct 2011 18:29:18 +0000 (16:29 -0200)
- it was not provided a polymorphic approach

src/icmp/icmpdestinationunreachablemessage.cpp
src/icmp/icmpdestinationunreachablemessage.h
src/icmp/icmpechoreplymessage.cpp
src/icmp/icmpechoreplymessage.h
src/icmp/icmpechorequestmessage.cpp
src/icmp/icmpechorequestmessage.h
src/icmp/icmpgenericmessage.cpp
src/icmp/icmpgenericmessage.h
src/icmp/icmpmessage.cpp
src/icmp/icmpmessage.h
src/icmp/icmpv4header.cpp

index a082a68..6a6d9f8 100644 (file)
@@ -23,14 +23,28 @@ IcmpDestinationUnreachableMessage::~IcmpDestinationUnreachableMessage()
 {
 }
 
-Icmpv4Type IcmpDestinationUnreachableMessage::get_type() const
+Icmpv4Type IcmpDestinationUnreachableMessage::get_type_v4() const
 {
     Icmpv4Type type = static_cast<Icmpv4Type>( Payload[ 0 ] );
 
     return type;
 }
 
-void IcmpDestinationUnreachableMessage::set_type( Icmpv4Type type )
+void IcmpDestinationUnreachableMessage::set_type_v4( const Icmpv4Type type )
+{
+    uint8_t n = static_cast<uint8_t>( type );
+
+    Payload[ 0 ] = n;
+}
+
+Icmpv6Type IcmpDestinationUnreachableMessage::get_type_v6() const
+{
+    Icmpv6Type type = static_cast<Icmpv6Type>( Payload[ 0 ] );
+
+    return type;
+}
+
+void IcmpDestinationUnreachableMessage::set_type_v6( const Icmpv6Type type )
 {
     uint8_t n = static_cast<uint8_t>( type );
 
index b1eb9d2..ed3810d 100644 (file)
@@ -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 );
index 07228f2..63c90c2 100644 (file)
@@ -23,14 +23,28 @@ IcmpEchoReplyMessage::~IcmpEchoReplyMessage()
 {
 }
 
-Icmpv4Type IcmpEchoReplyMessage::get_type() const
+Icmpv4Type IcmpEchoReplyMessage::get_type_v4() const
 {
     Icmpv4Type type = static_cast<Icmpv4Type>( Payload[ 0 ] );
 
     return type;
 }
 
-void IcmpEchoReplyMessage::set_type( Icmpv4Type type )
+void IcmpEchoReplyMessage::set_type_v4( const Icmpv4Type type )
+{
+    uint8_t n = static_cast<uint8_t>( type );
+
+    Payload[ 0 ] = n;
+}
+
+Icmpv6Type IcmpEchoReplyMessage::get_type_v6() const
+{
+    Icmpv6Type type = static_cast<Icmpv6Type>( Payload[ 0 ] );
+
+    return type;
+}
+
+void IcmpEchoReplyMessage::set_type_v6( const Icmpv6Type type )
 {
     uint8_t n = static_cast<uint8_t>( type );
 
index 3e269b2..3606c27 100644 (file)
@@ -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 );
index eea8f68..7f0ffed 100644 (file)
@@ -23,14 +23,28 @@ IcmpEchoRequestMessage::~IcmpEchoRequestMessage()
 {
 }
 
-Icmpv4Type IcmpEchoRequestMessage::get_type() const
+Icmpv4Type IcmpEchoRequestMessage::get_type_v4() const
 {
     Icmpv4Type type = static_cast<Icmpv4Type>( Payload[ 0 ] );
 
     return type;
 }
 
-void IcmpEchoRequestMessage::set_type( Icmpv4Type type )
+void IcmpEchoRequestMessage::set_type_v4( const Icmpv4Type type )
+{
+    uint8_t n = static_cast<uint8_t>( type );
+
+    Payload[ 0 ] = n;
+}
+
+Icmpv6Type IcmpEchoRequestMessage::get_type_v6() const
+{
+    Icmpv6Type type = static_cast<Icmpv6Type>( Payload[ 0 ] );
+
+    return type;
+}
+
+void IcmpEchoRequestMessage::set_type_v6( const Icmpv6Type type )
 {
     uint8_t n = static_cast<uint8_t>( type );
 
index 7cca56a..5b786f1 100644 (file)
@@ -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 );
index ecf1ae1..1bbee87 100644 (file)
@@ -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 )
 {
 }
 
index 13fd2f4..c162d65 100644 (file)
@@ -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 );
index 1f53f6b..e0e9a6b 100644 (file)
@@ -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 );
index 89d9773..9301edd 100644 (file)
@@ -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;
index 5ec5a0e..56326a6 100644 (file)
@@ -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 )