From 7c3800544dfa59c656d9e32af3e5ff08fa0e3504 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 12 Mar 2015 10:55:24 +0100 Subject: [PATCH] added encode_string to MessagePayload; removed checksum and icmpchecksum since functionality moved into MessagePayload, IcmpHeader and IcmpData --- src/host/checksum.hpp | 85 ------------------------------------------- src/host/messagepayload.cpp | 33 +++++++++++++++++ src/host/messagepayload.h | 5 +++ src/icmp/icmpchecksum.h | 19 ---------- 4 files changed, 38 insertions(+), 104 deletions(-) delete mode 100644 src/host/checksum.hpp delete mode 100644 src/icmp/icmpchecksum.h diff --git a/src/host/checksum.hpp b/src/host/checksum.hpp deleted file mode 100644 index a70dd28..0000000 --- a/src/host/checksum.hpp +++ /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 - -//----------------------------------------------------------------------------- -// Checksum -//----------------------------------------------------------------------------- - -/** - * @brief Class that provides checksum calculation. - */ -template -class Checksum -{ -public: - Checksum( - const Iterator &body_begin, - const Iterator &body_end - ); - - // TODO common checksum - // The 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 - Checksum::Checksum( - const Iterator &body_begin, - const Iterator &body_end - ) : - BodyBegin( body_begin ), - BodyEnd( body_end ) - { - } - -template - uint16_t Checksum::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( *body_iter++ ) << 8); - if ( body_iter != BodyEnd ) - sum += static_cast( *body_iter++ ); - } - - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - - uint16_t checksum = static_cast( ~sum ); - - return checksum; - } - -#endif // CHECKSUM_HPP diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index ef11eac..c095c8e 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -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(value.length()); + + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); + BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast(PayloadSizeInBytes) ) ); + BOOST_ASSERT( first_byte_index +length < static_cast(PayloadSizeInBytes) ); + + const uint8_t *data_converted = reinterpret_cast(&value[0]); + for (int index=0; index::const_iterator iter = Payload.begin(); + std::vector::const_iterator end = Payload.end(); + while ( iter != end ) + { + sum += (static_cast( *iter++ ) << 8); + if ( iter != end ) + sum += static_cast( *iter++ ); + } + + return sum; +} diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h index c94f419..74fb41c 100644 --- a/src/host/messagepayload.h +++ b/src/host/messagepayload.h @@ -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 index a14ebf1..0000000 --- a/src/icmp/icmpchecksum.h +++ /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 IcmpChecksum; - -#endif // ICMP_CHECKSUM_H -- 1.7.1