Utils/Logger: Complete unit testing
[senf.git] / Packets / DefaultBundle / IpV4Packet.cc
index eaa3a51..ec35159 100644 (file)
 // Free Software Foundation, Inc.,
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-// Definition of non-inline non-template functions
+/** \file
+    \brief IpV4Packet non-inline non-template implementation */
 
 #include "IpV4Packet.hh"
 //#include "IpV4Packet.ih"
-#include "EthernetPacket.hh"
 
 // Custom includes
+#include <iomanip>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <boost/io/ios_state.hpp>
+#include "../../Utils/IpChecksum.hh"
+#include "EthernetPacket.hh"
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
@@ -42,38 +46,50 @@ namespace {
         regsiterIpV4Packet2 (4); // IP-in-IP encapsulation
 }
 
-prefix_ void senf::IpV4Packet::v_nextInterpreter()
+///////////////////////////////////////////////////////////////////////////
+// senf::Parse_IpV4
+
+prefix_ boost::uint16_t senf::Parse_IpV4::calcChecksum()
     const
 {
-    registerInterpreter(protocol(),begin()+bytes(),end());
+    validate(bytes(*this));
+    IpChecksum summer;
+    summer.feed( i(),                   i()+checksum_offset );
+    // Not needed since the number of 0-bytes is even
+    // summer.feed( 0u );
+    // summer.feed( 0u );
+    summer.feed( i()+checksum_offset+2, i()+bytes(*this)    );
+    return summer.sum();
 }
 
-prefix_ void senf::IpV4Packet::v_finalize()
-{}
+///////////////////////////////////////////////////////////////////////////
+// senf::IpV4PacketType
 
-prefix_ void senf::IpV4Packet::v_dump(std::ostream & os)
-    const
+prefix_ void senf::IpV4PacketType::dump(packet p, std::ostream & os)
 {
-    struct in_addr in;
-    in.s_addr = htonl(source());
-    char buffer[128];
-    std::string src (inet_ntop(AF_INET,&in,buffer,128));
-    in.s_addr = htonl(destination());
-    std::string dst (inet_ntop(AF_INET,&in,buffer,128));
+    boost::io::ios_all_saver ias(os);
     os << "Internet protocol Version 4:\n"
-       << "  version       : " << version() << "\n"
-       << "  IHL           : " << ihl() << "\n"
-       << "  TOS           : " << unsigned(tos()) << "\n"
-       << "  length        : " << length() << "\n"
-       << "  identifier    : " << identifier() << "\n"
-       << "  DF            : " << df() << "\n"
-       << "  MF            : " << mf() << "\n"
-       << "  fragment      : " << frag() << "\n"
-       << "  TTL           : " << unsigned(ttl()) << "\n"
-       << "  protocol      : " << unsigned(protocol()) << "\n"
-       << "  CRC           : " << std::hex << crc() << std::dec << "\n"
-       << "  source        : " << src << "\n"
-       << "  destination   : " << dst << "\n";
+       << "  version       : " << p->version() << "\n"
+       << "  IHL           : " << p->ihl() << "\n"
+       << "  TOS           : " << unsigned(p->tos()) << "\n"
+       << "  length        : " << p->length() << "\n"
+       << "  identifier    : " << p->identifier() << "\n"
+       << "  DF            : " << p->df() << "\n"
+       << "  MF            : " << p->mf() << "\n"
+       << "  fragment      : " << p->frag() << "\n"
+       << "  TTL           : " << unsigned(p->ttl()) << "\n"
+       << "  protocol      : " << unsigned(p->protocol()) << "\n"
+       << "  checksum      : 0x" 
+         << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << std::dec << "\n"
+       << "  source        : " << p->source() << "\n"
+       << "  destination   : " << p->destination() << "\n";
+}
+
+prefix_ void senf::IpV4PacketType::finalize(packet p)
+{
+    p->length()   << p.size();
+    p->protocol() << key(p.next());
+    p->checksum() << p->calcChecksum();
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
@@ -87,4 +103,5 @@ prefix_ void senf::IpV4Packet::v_dump(std::ostream & os)
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
 // compile-command: "scons -u test"
+// comment-column: 40
 // End: