added encode_string to MessagePayload; removed checksum and icmpchecksum since functi...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 09:55:24 +0000 (10:55 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 09:55:24 +0000 (10:55 +0100)
src/host/checksum.hpp [deleted file]
src/host/messagepayload.cpp
src/host/messagepayload.h
src/icmp/icmpchecksum.h [deleted file]

diff --git a/src/host/checksum.hpp b/src/host/checksum.hpp
deleted file mode 100644 (file)
index a70dd28..0000000
+++ /dev/null
@@ -1,85 +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 CHECKSUM_HPP
-#define CHECKSUM_HPP
-
-#include <stdint.h>
-
-//-----------------------------------------------------------------------------
-// Checksum
-//-----------------------------------------------------------------------------
-
-/**
- * @brief Class that provides checksum calculation.
- */
-template<typename Iterator>
-class Checksum
-{
-public:
-    Checksum(
-            const Iterator &body_begin,
-            const Iterator &body_end
-    );
-
-    // TODO common checksum
-    // The <typename Message> must have a method which returns an array of words
-    // this way the checksum calculator can be used with any ICMP, TCP, UDP...
-    // uint16_t compute( const Message &message ) const
-    // {
-    //      message.get_word_array() -> uint16_t *word_array
-    //
-    //
-    uint16_t compute(
-            uint8_t type,
-            uint8_t code,
-            uint16_t identifier,
-            uint16_t sequence_number
-    ) const;
-
-private:
-    Iterator BodyBegin;
-    Iterator BodyEnd;
-
-};
-
-template<typename Iterator>
-    Checksum<Iterator>::Checksum(
-            const Iterator &body_begin,
-            const Iterator &body_end
-    ) :
-        BodyBegin( body_begin ),
-        BodyEnd( body_end )
-    {
-    }
-
-template<typename Iterator>
-    uint16_t Checksum<Iterator>::compute(
-            uint8_t type,
-            uint8_t code,
-            uint16_t identifier,
-            uint16_t sequence_number
-    ) const
-    {
-        uint32_t sum = (type << 8) + code + identifier + sequence_number;
-
-        Iterator body_iter = BodyBegin;
-        while ( body_iter != BodyEnd )
-        {
-            sum += (static_cast<uint8_t>( *body_iter++ ) << 8);
-            if ( body_iter != BodyEnd )
-                sum += static_cast<uint8_t>( *body_iter++ );
-        }
-
-        sum = (sum >> 16) + (sum & 0xFFFF);
-        sum += (sum >> 16);
-
-        uint16_t checksum = static_cast<uint16_t>( ~sum );
-
-        return checksum;
-    }
-
-#endif // CHECKSUM_HPP
index ef11eac..c095c8e 100644 (file)
@@ -389,6 +389,23 @@ void MessagePayload::encode32(
     BOOST_ASSERT( current_byte_index == last_byte_index );
 } //lint !e1762
 
+void MessagePayload::encode_string( const int first_byte_index,
+                                    const std::string value
+)
+{
+    int length = static_cast<int>(value.length());
+
+    BOOST_ASSERT( 0 < PayloadSizeInBytes );
+    BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes );
+    BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast<int>(PayloadSizeInBytes) ) );
+    BOOST_ASSERT( first_byte_index +length < static_cast<int>(PayloadSizeInBytes) );
+
+    const uint8_t *data_converted = reinterpret_cast<const uint8_t *>(&value[0]);
+    for (int index=0; index<length; ++index)
+        Payload[first_byte_index + index] = data_converted[index];
+}
+
+
 /**
  * @brief Read/Extract all the data from the input stream @a is and stores it
  * in the payload buffer.
@@ -443,3 +460,19 @@ ostream& MessagePayload::write( ostream &os ) const
 
     return os;
 }
+
+uint32_t MessagePayload::calc_checksum_part() const
+{
+    uint32_t sum = 0;
+
+    std::vector<uint8_t>::const_iterator iter = Payload.begin();
+    std::vector<uint8_t>::const_iterator end = Payload.end();
+    while ( iter != end )
+    {
+        sum += (static_cast<uint8_t>( *iter++ ) << 8);
+        if ( iter != end )
+            sum += static_cast<uint8_t>( *iter++ );
+    }
+
+    return sum;
+}
index c94f419..74fb41c 100644 (file)
@@ -71,10 +71,15 @@ public:
             const int last_byte,
             const uint32_t value
     );
+    void encode_string( const int first_byte,
+                        const std::string value
+    );
 
     std::istream& read( std::istream &is );
     std::ostream& write( std::ostream &os ) const;
 
+    uint32_t calc_checksum_part() const;
+
 private:
     /// The size of the payload buffer
     std::size_t PayloadSizeInBytes;
diff --git a/src/icmp/icmpchecksum.h b/src/icmp/icmpchecksum.h
deleted file mode 100644 (file)
index a14ebf1..0000000
+++ /dev/null
@@ -1,19 +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 ICMP_CHECKSUM_H
-#define ICMP_CHECKSUM_H
-
-#include "host/checksum.hpp"
-#include "icmp/icmpdata.h"
-
-//-----------------------------------------------------------------------------
-// IcmpChecksum
-//-----------------------------------------------------------------------------
-
-typedef Checksum<IcmpData::const_iterator> IcmpChecksum;
-
-#endif // ICMP_CHECKSUM_H