From c2e37a431271beea99c8f30a6eee6dcc65ff0dac Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 17 Aug 2011 23:13:57 -0300 Subject: [PATCH] Renamed ChecksumCalculation template to Checksum - The name represents better an object - Also changed the file extension from .h to .hpp, so keep templates with .hpp extension and classes with .h --- src/host/checksum.hpp | 85 +++++++++++++++++++++++++++++++++++++ src/icmp/checksumcalculator.h | 85 ------------------------------------- src/icmp/icmpchecksumcalculator.h | 4 +- 3 files changed, 87 insertions(+), 87 deletions(-) create mode 100644 src/host/checksum.hpp delete mode 100644 src/icmp/checksumcalculator.h diff --git a/src/host/checksum.hpp b/src/host/checksum.hpp new file mode 100644 index 0000000..a70dd28 --- /dev/null +++ b/src/host/checksum.hpp @@ -0,0 +1,85 @@ +// 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/icmp/checksumcalculator.h b/src/icmp/checksumcalculator.h deleted file mode 100644 index 00e2e3e..0000000 --- a/src/icmp/checksumcalculator.h +++ /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 CHECKSUMCALCULATOR_H -#define CHECKSUMCALCULATOR_H - -#include - -//----------------------------------------------------------------------------- -// ChecksumCalculator -//----------------------------------------------------------------------------- - -/** - * @brief Class that provides checksum calculation. - */ -template -class ChecksumCalculator -{ -public: - ChecksumCalculator( - const Iterator &body_begin, - const Iterator &body_end - ); - - // TODO common checksum - // The must have a method which returns an aray of works - // 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 - ChecksumCalculator::ChecksumCalculator( - const Iterator &body_begin, - const Iterator &body_end - ) : - BodyBegin( body_begin ), - BodyEnd( body_end ) - { - } - -template - uint16_t ChecksumCalculator::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 /* CHECKSUMCALCULATOR_H */ diff --git a/src/icmp/icmpchecksumcalculator.h b/src/icmp/icmpchecksumcalculator.h index d46cfa8..d173619 100644 --- a/src/icmp/icmpchecksumcalculator.h +++ b/src/icmp/icmpchecksumcalculator.h @@ -7,13 +7,13 @@ #ifndef ICMPCHECKSUMCALCULATOR_H #define ICMPCHECKSUMCALCULATOR_H -#include "icmp/checksumcalculator.h" +#include "host/checksum.hpp" #include "icmp/icmpdata.h" //----------------------------------------------------------------------------- // IcmpChecksumCalculator //----------------------------------------------------------------------------- -typedef ChecksumCalculator IcmpChecksumCalculator; +typedef Checksum IcmpChecksumCalculator; #endif /* ICMPCHECKSUMCALCULATOR_H */ -- 1.7.1