Packets: Add StringParser ostream operation
[senf.git] / Packets / DefaultBundle / EthernetPacket.cc
index eced314..78fe56c 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 //#include "EthernetPacket.ih"
 
 // Custom includes
+#include "LlcSnapPacket.hh"
 #include <iomanip>
 #include <boost/io/ios_state.hpp>
 
@@ -59,26 +60,22 @@ prefix_ void senf::EthernetPacketType::dump(packet p, std::ostream & os)
 
 prefix_ senf::PacketInterpreterBase::factory_t senf::EthernetPacketType::nextPacketType(packet p)
 {
-    if (p->type_length() >= 1536) {
-        PkReg_Entry const * e;
-        e = PacketRegistry<senf::EtherTypes>::lookup( p->type_length(), nothrow );
-        return e ? e->factory() : no_factory();
-    }
-    if (p->type_length() <= 1500)
-        return EthLlcSnapPacket::factory();
-    return no_factory();
+    if      (p->type_length() >= 1536) return lookup(p->type_length());
+    else if (p->type_length() <= 1500) return LlcSnapPacket::factory();
+    else                               return no_factory();
 }
 
 prefix_ void senf::EthernetPacketType::finalize(packet p)
 {
-    optional_registry_key_t k = key(p.next());
-    if (k)
-        p->type_length() << k;
-    else
-        if (p.next().is<EthLlcSnapPacket>())
-            p->type_length() << p.next().data().size();
-        else
-            p->type_length() << 0;
+    Packet n (p.next(nothrow));
+    if (n) {
+        optional_key_t k (key(n));
+        if (k)
+            p->type_length() << k;
+        else if (n.is<LlcSnapPacket>())
+            p->type_length() << n.data().size();
+    }
+    // Do NOT reset type_length if the type is not known ... doing this will destroy read packets
 }
 
 prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os)
@@ -94,25 +91,7 @@ prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os)
 
 prefix_ void senf::EthVLanPacketType::finalize(packet p)
 {
-    p->type() << key(p.next());
-}
-
-prefix_ void senf::EthLlcSnapPacketType::dump(packet p, std::ostream & os)
-{
-    boost::io::ios_all_saver ias(os);
-    os << "Ethernet LLC/SNAP"
-       << "  LLC\n"
-       << "    DSAP: " << p->dsap() << "\n"
-       << "    SSAP: " << p->ssap() << "\n"
-       << "  SNAP\n"
-       << "    ProtocolId: " << p->protocolId() << "\n"
-       << "    type      : 0x" 
-       << std::hex << std::setw(4) << std::setfill('0') << p->type() << "\n";
-}
-
-prefix_ void senf::EthLlcSnapPacketType::finalize(packet p)
-{
-    p->type() << key(p.next());
+    p->type() << key(p.next(nothrow));
 }