switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / EthernetPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief EthernetPacket non-inline non-template implementation */
30
31 #include "EthernetPacket.hh"
32 //#include "EthernetPacket.ih"
33
34 // Custom includes
35 #include "LlcSnapPacket.hh"
36 #include <iomanip>
37 #include <boost/io/ios_state.hpp>
38 #include <senf/Utils/Format.hh>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace {
44     SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, 0x8100, senf::EthVLanPacket);
45 }
46
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 // senf::EthernetPacketType
49
50 prefix_ void senf::EthernetPacketType::dump(packet p, std::ostream & os)
51 {
52     boost::io::ios_all_saver ias(os);
53     if (p->type_length() <= 1500)
54         os << "Ethernet 802.3";
55     else if (p->type_length() >= 0x600)
56         os << "Ethernet II (DIX)";
57     else
58         os << "Ethernet 802.3 (bad ethertype >1500 and <1536)";
59     os <<     ": \n"
60        << senf::fieldName("destination")               << p->destination() << "\n"
61        << senf::fieldName("source")                    << p->source() << "\n"
62        << senf::fieldName("type/length")               << senf::format::dumpint(p->type_length().value()) << "\n";
63 }
64
65 prefix_ senf::PacketInterpreterBase::factory_t senf::EthernetPacketType::nextPacketType(packet p)
66 {
67     if      (p->type_length() >= 1536) return lookup(p->type_length());
68     else if (p->type_length() <= 1500) return LlcSnapPacket::factory();
69     else                               return no_factory();
70 }
71
72 prefix_ void senf::EthernetPacketType::finalize(packet p)
73 {
74     Packet n (p.next(nothrow));
75     if (n) {
76         optional_key_t k (key(n));
77         if (k)
78             p->type_length() << k;
79         else if (n.is<LlcSnapPacket>())
80             p->type_length() << n.data().size();
81     }
82     // Do NOT reset type_length if the type is not known ... doing this will destroy read packets
83 }
84
85 prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os)
86 {
87     boost::io::ios_all_saver ias(os);
88     os << "Ethernet 802.1q (VLAN):\n"
89        << senf::fieldName("priority")                  << p->priority() << "\n"
90        << senf::fieldName("cfi")                       << p->cfi() << "\n"
91        << senf::fieldName("vlan-ID")                   << p->vlanId() << "\n"
92        << senf::fieldName("ethertype")
93        << " 0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->type() << "\n";
94 }
95
96 prefix_ void senf::EthVLanPacketType::finalize(packet p)
97 {
98     p->type() << key(p.next(nothrow));
99 }
100
101
102 //-/////////////////////////////////////////////////////////////////////////////////////////////////
103 #undef prefix_
104
105 \f
106 // Local Variables:
107 // mode: c++
108 // fill-column: 100
109 // c-file-style: "senf"
110 // indent-tabs-mode: nil
111 // ispell-local-dictionary: "american"
112 // compile-command: "scons -u test"
113 // comment-column: 40
114 // End: