From 39d326f2f463dbff05adbcc8d879f2338105ddaf Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 12 Mar 2015 17:44:08 +0100 Subject: [PATCH] moved some implementations from h to cpp files because of linker trouble --- src/CMakeLists.txt | 2 + src/icmp/icmpdata.cpp | 57 +++++++++++++++++++ src/icmp/icmpdata.h | 32 ++++++------ src/icmp/icmpdestinationunreachabledata.h | 4 +- src/icmp/icmpheader.cpp | 84 +++++++++++++++++++++++++++++ src/icmp/icmpheader.h | 69 +++++++----------------- src/icmp/icmppacket.h | 11 ++++ test/CMakeLists.test_icmpv4header.txt | 8 +-- test/CMakeLists.test_icmpv6header.txt | 8 +-- 9 files changed, 195 insertions(+), 80 deletions(-) create mode 100644 src/icmp/icmpdata.cpp create mode 100644 src/icmp/icmpheader.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9657ff3..4ea0ce9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -76,6 +76,8 @@ set(SOURCES host/pingprotocol.cpp host/pingrotate.cpp host/pingscheduler.cpp + icmp/icmpdata.cpp + icmp/icmpheader.cpp icmp/icmppacket.cpp icmp/icmppinger.cpp icmp/icmppacketfactory.cpp diff --git a/src/icmp/icmpdata.cpp b/src/icmp/icmpdata.cpp new file mode 100644 index 0000000..3506cc6 --- /dev/null +++ b/src/icmp/icmpdata.cpp @@ -0,0 +1,57 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + + Christian Herdtweck, Intra2net AG 2015 + */ + +#include "icmpdata.h" + +#include + +std::istream& IcmpData::read(std::istream &is) // read raw data +{ + return raw_data.read(is); +} + +std::ostream& IcmpData::write(std::ostream &os) const +{ + return raw_data.write(os); +} + +std::string IcmpData::to_string() const +{ + std::stringstream buf; + buf << "[IcmpData of length " << size << "]"; + return buf.str(); +} + +std::istream& operator>>( + std::istream &is, + IcmpData &data +) +{ return data.read( is ); } + +std::ostream& operator<<( + std::ostream &os, + const IcmpData &data +) +{ return data.write( os ); } + +// (created using vim -- the world's best text editor) + diff --git a/src/icmp/icmpdata.h b/src/icmp/icmpdata.h index b65cae9..c920bd0 100644 --- a/src/icmp/icmpdata.h +++ b/src/icmp/icmpdata.h @@ -25,7 +25,6 @@ #include #include -#include #include #include #include "host/messagepayload.h" @@ -44,6 +43,8 @@ public: , raw_data( 0 ) {} + virtual ~IcmpData() {} + virtual bool match_echo_reply(const uint16_t identifier, const uint16_t sequence_number) const { return false; }; @@ -52,17 +53,12 @@ public: const uint16_t sequence_number) const { return false; }; - std::size_t get_size() { return size; }; - - virtual std::istream& read(std::istream &is) // read raw data - { return raw_data.read(is); } + inline std::size_t get_size() { return size; }; - std::ostream& write(std::ostream &os) - { return raw_data.write(os); } + virtual std::istream& read( std::istream &is); + virtual std::ostream& write(std::ostream &os) const; - virtual ~IcmpData() {} - - uint32_t calc_checksum_part() const + inline uint32_t calc_checksum_part() const { return raw_data.calc_checksum_part(); } virtual void print( const size_t &bytes_received, @@ -71,12 +67,16 @@ public: const uint32_t ttl) const {} - virtual std::string to_string() const - { - std::stringstream buf; - buf << "[IcmpData of length " << size << "]"; - return buf.str(); - } + virtual std::string to_string() const; + + friend std::istream& operator>>( + std::istream &is, + IcmpData &data + ); + friend std::ostream& operator<<( + std::ostream &os, + const IcmpData &data + ); protected: diff --git a/src/icmp/icmpdestinationunreachabledata.h b/src/icmp/icmpdestinationunreachabledata.h index de375fd..89fd257 100644 --- a/src/icmp/icmpdestinationunreachabledata.h +++ b/src/icmp/icmpdestinationunreachabledata.h @@ -123,8 +123,8 @@ public: else // if it is an icmp message, then the icmp packet comes right after // the IP header. Inside the icmp packet we need bytes 5-8 - IcmpData::raw_data.decode16(offset+data_offset, - offset+data_offset+1); + return IcmpData::raw_data.decode16(offset+data_offset, + offset+data_offset+1); } /** diff --git a/src/icmp/icmpheader.cpp b/src/icmp/icmpheader.cpp new file mode 100644 index 0000000..b2e1346 --- /dev/null +++ b/src/icmp/icmpheader.cpp @@ -0,0 +1,84 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + + Christian Herdtweck, Intra2net AG 2015 + */ + +#include "icmp/icmpheader.h" +#include + +std::istream& IcmpHeader::read(std::istream &is) +{ + boost::scoped_array buf( new char[4] ); + is.read(buf.get(), 4); + type = static_cast(buf[0]); + code = static_cast(buf[1]); + checksum = ( static_cast(buf[2]) << 8 ) + + static_cast(buf[3]); + + return is; +} + +std::ostream& IcmpHeader::write(std::ostream &os) const +{ + uint8_t checksum_lsb = static_cast(checksum & 0x00FF); + uint8_t checksum_msb = static_cast((checksum & 0xFF00) >> 8); + return os << type << code << checksum_msb << checksum_lsb; +} + + +// code adapted from boost icmp example by Christopher M. Kohlhoff +// --> boost license?!? +void IcmpHeader::calc_checksum( const uint32_t body_checksum_part ) +{ + uint32_t sum = (type << 8) + code; + sum += body_checksum_part; + + sum = (sum >> 16) + (sum & 0xFFFF); + sum += (sum >> 16); + + checksum = static_cast( ~sum ); +} + + +std::string IcmpHeader::to_string() const +{ + std::stringstream buf; + buf << "[Icmp header type=" << static_cast(type) + << ",code=" << static_cast(code) + << " (cksum:" << std::showbase << std::hex << checksum << ")]"; + return buf.str(); +} + + +std::istream& operator>>( + std::istream &is, + IcmpHeader &header +) +{ return header.read( is ); } + +std::ostream& operator<<( + std::ostream &os, + const IcmpHeader &header +) +{ return header.write( os ); } + + +// (created using vim -- the world's best text editor) + diff --git a/src/icmp/icmpheader.h b/src/icmp/icmpheader.h index 98d02fa..e784ffd 100644 --- a/src/icmp/icmpheader.h +++ b/src/icmp/icmpheader.h @@ -25,11 +25,6 @@ #include #include -#include -#include - -#include -using I2n::Logger::GlobalLogger; /** @brief first 4 byte of ICMP header (v4 and v6) * @@ -52,55 +47,30 @@ public: , checksum( 0 ) {} - std::istream& read(std::istream &is) - { - boost::scoped_array buf( new char[4] ); - is.read(buf.get(), 4); - type = static_cast(buf[0]); - code = static_cast(buf[1]); - checksum = ( static_cast(buf[2]) << 8 ) - + static_cast(buf[3]); - - return is; - } - - uint8_t get_type() const { return type; } - uint8_t get_code() const { return code; } - uint16_t get_checksum() const { return checksum; } - - // code adapted from boost icmp example by Christopher M. Kohlhoff - // --> boost license?!? - void calc_checksum( const uint32_t body_checksum_part ) - { - uint32_t sum = (type << 8) + code; - sum += body_checksum_part; - - sum = (sum >> 16) + (sum & 0xFFFF); - sum += (sum >> 16); - - checksum = static_cast( ~sum ); - } - - std::ostream& write(std::ostream &os) const - { - uint8_t checksum_lsb = static_cast(checksum & 0x00FF); - uint8_t checksum_msb = static_cast((checksum & 0xFF00) >> 8); - os << type << code << checksum_msb << checksum_lsb; - } + std::istream& read(std::istream &is); + std::ostream& write(std::ostream &os) const; + + inline uint8_t get_type() const { return type; } + inline uint8_t get_code() const { return code; } + inline uint16_t get_checksum() const { return checksum; } + + void calc_checksum( const uint32_t body_checksum_part ); // returns the amount of data represented by this class; // for Icmp v4, the header is actually 8 bytes, see comments above - uint16_t get_header_length() const + inline uint16_t get_header_length() const { return 4; } - std::string to_string() const - { - std::stringstream buf; - buf << "[Icmp header type=" << static_cast(type) - << ",code=" << static_cast(code) - << " (cksum:" << std::showbase << std::hex << checksum << ")]"; - return buf.str(); - } + std::string to_string() const; + + friend std::istream& operator>>( + std::istream &is, + IcmpHeader &header + ); + friend std::ostream& operator<<( + std::ostream &os, + const IcmpHeader &header + ); protected: uint8_t type; @@ -108,7 +78,6 @@ protected: uint16_t checksum; }; - #endif // (created using vim -- the world's best text editor) diff --git a/src/icmp/icmppacket.h b/src/icmp/icmppacket.h index c2d0389..9e9aad1 100644 --- a/src/icmp/icmppacket.h +++ b/src/icmp/icmppacket.h @@ -157,6 +157,17 @@ public: bool write(std::ostream &os) const; std::string to_string() const; + + friend std::istream& operator>>( + std::istream &is, + IcmpPacket &packet + ); + friend std::ostream& operator<<( + std::ostream &os, + const IcmpPacket &packet + ); + + }; typedef boost::shared_ptr IcmpPacketItem; diff --git a/test/CMakeLists.test_icmpv4header.txt b/test/CMakeLists.test_icmpv4header.txt index 4b58904..5b1de8c 100644 --- a/test/CMakeLists.test_icmpv4header.txt +++ b/test/CMakeLists.test_icmpv4header.txt @@ -2,12 +2,8 @@ add_executable(test_icmpv4header test_icmpv4header.cpp ${CMAKE_SOURCE_DIR}/src/boost_assert_handler.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpv4header.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpmessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpdestinationunreachablemessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpechoreplymessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpechorequestmessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpgenericmessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpdata.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpheader.cpp ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp ) diff --git a/test/CMakeLists.test_icmpv6header.txt b/test/CMakeLists.test_icmpv6header.txt index 730f052..e97478a 100644 --- a/test/CMakeLists.test_icmpv6header.txt +++ b/test/CMakeLists.test_icmpv6header.txt @@ -2,12 +2,8 @@ add_executable(test_icmpv6header test_icmpv6header.cpp ${CMAKE_SOURCE_DIR}/src/boost_assert_handler.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpv6header.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpmessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpdestinationunreachablemessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpechoreplymessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpechorequestmessage.cpp - ${CMAKE_SOURCE_DIR}/src/icmp/icmpgenericmessage.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpdata.cpp + ${CMAKE_SOURCE_DIR}/src/icmp/icmpheader.cpp ${CMAKE_SOURCE_DIR}/src/host/messagepayload.cpp ) -- 1.7.1