moved some implementations from h to cpp files because of linker trouble
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 16:44:08 +0000 (17:44 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Mar 2015 16:44:08 +0000 (17:44 +0100)
src/CMakeLists.txt
src/icmp/icmpdata.cpp [new file with mode: 0644]
src/icmp/icmpdata.h
src/icmp/icmpdestinationunreachabledata.h
src/icmp/icmpheader.cpp [new file with mode: 0644]
src/icmp/icmpheader.h
src/icmp/icmppacket.h
test/CMakeLists.test_icmpv4header.txt
test/CMakeLists.test_icmpv6header.txt

index 9657ff3..4ea0ce9 100644 (file)
@@ -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 (file)
index 0000000..3506cc6
--- /dev/null
@@ -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 <sstream>
+
+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)
+
index b65cae9..c920bd0 100644 (file)
@@ -25,7 +25,6 @@
 
 #include <stdint.h>
 #include <iostream>
-#include <sstream>
 #include <boost/shared_ptr.hpp>
 #include <boost/date_time/posix_time/ptime.hpp>
 #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:
 
index de375fd..89fd257 100644 (file)
@@ -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 (file)
index 0000000..b2e1346
--- /dev/null
@@ -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 <boost/scoped_array.hpp>
+
+std::istream& IcmpHeader::read(std::istream &is)
+{
+    boost::scoped_array<char> buf( new char[4] );
+    is.read(buf.get(), 4);
+    type = static_cast<uint8_t>(buf[0]);
+    code = static_cast<uint8_t>(buf[1]);
+    checksum = ( static_cast<uint16_t>(buf[2]) << 8 )
+               + static_cast<uint16_t>(buf[3]);
+
+    return is;
+}
+
+std::ostream& IcmpHeader::write(std::ostream &os) const
+{
+    uint8_t checksum_lsb = static_cast<uint8_t>(checksum & 0x00FF);
+    uint8_t checksum_msb = static_cast<uint8_t>((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<uint16_t>( ~sum );
+}
+
+
+std::string IcmpHeader::to_string() const
+{
+    std::stringstream buf;
+    buf << "[Icmp header type=" << static_cast<int>(type)
+        << ",code=" << static_cast<int>(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)
+
index 98d02fa..e784ffd 100644 (file)
 
 #include <stdint.h>
 #include <iostream>
-#include <sstream>
-#include <boost/scoped_array.hpp>
-
-#include <logfunc.hpp>
-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<char> buf( new char[4] );
-        is.read(buf.get(), 4);
-        type = static_cast<uint8_t>(buf[0]);
-        code = static_cast<uint8_t>(buf[1]);
-        checksum = ( static_cast<uint16_t>(buf[2]) << 8 )
-                   + static_cast<uint16_t>(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<uint16_t>( ~sum );
-    }
-
-    std::ostream& write(std::ostream &os) const
-    {
-        uint8_t checksum_lsb = static_cast<uint8_t>(checksum & 0x00FF);
-        uint8_t checksum_msb = static_cast<uint8_t>((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<int>(type)
-            << ",code=" << static_cast<int>(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)
index c2d0389..9e9aad1 100644 (file)
@@ -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<IcmpPacket> IcmpPacketItem;
index 4b58904..5b1de8c 100644 (file)
@@ -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
 )
 
index 730f052..e97478a 100644 (file)
@@ -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
 )