EthernetPacket.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "EthernetPacket.hh"
18 //#include "EthernetPacket.ih"
19 
20 // Custom includes
21 #include <iomanip>
22 #include <boost/io/ios_state.hpp>
23 #include <senf/Utils/Format.hh>
24 #include <senf/Utils/senflikely.hh>
25 #include "LlcSnapPacket.hh"
26 
27 #define prefix_
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 
34 
39 
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::EthernetPacketType
42 
43 prefix_ void senf::EthernetPacketType::dump(packet p, std::ostream & os)
44 {
45  boost::io::ios_all_saver ias(os);
46  if (p->type_length() <= 1500)
47  os << "Ethernet 802.3";
48  else if (p->type_length() >= 0x600)
49  os << "Ethernet II (DIX)";
50  else
51  os << "Ethernet 802.3 (bad ethertype >1500 and <1536)";
52  os << ": \n"
53  << senf::fieldName("destination") << p->destination() << std::endl
54  << senf::fieldName("source") << p->source() << std::endl
55  << senf::fieldName("type/length") << format::dumpint(p->type_length().value()) << std::endl;
56 }
57 
59 {
60  if (SENF_LIKELY(p->type_length() >= 1536)) return lookup(p->type_length());
61  else if (p->type_length() <= 1500) return LlcSnapPacket::factory();
62  else return no_factory();
63 }
64 
66 {
67  Packet n (p.next(nothrow));
68  if (SENF_LIKELY(n)) {
69  optional_key_t k (key(n));
70  if (SENF_LIKELY(k))
71  p->type_length() << k;
72  else if (n.is<LlcSnapPacket>())
73  p->type_length() << n.data().size();
74  }
75  // Do NOT reset type_length if the type is not known ... doing this will destroy read packets
76 }
77 
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 // senf::EthVLanSPacketType
80 
81 prefix_ void senf::EthVLanSPacketType::dump(packet p, std::ostream & os)
82 {
83  boost::io::ios_all_saver ias(os);
84  os << "Ethernet 802.1ad (VLAN S-Tag):\n"
85  << senf::fieldName("priority") << p->priority() << std::endl
86  << senf::fieldName("cfi") << p->cfi() << std::endl
87  << senf::fieldName("vlanId") << p->vlanId() << std::endl
88  << senf::fieldName("etherType")
89  << "0x" << std::hex << std::setw(4) << std::setfill('0')
90  << std::right << p->type_length() << std::endl;
91 }
92 
94 {
95  p->type_length() << key(p.next(nothrow));
96 }
97 
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 // senf::EthVLanCPacketType
100 
102 {
103  boost::io::ios_all_saver ias(os);
104  os << "Ethernet 802.1q (VLAN C-Tag):\n"
105  << senf::fieldName("priority") << p->priority() << std::endl
106  << senf::fieldName("cfi") << p->cfi() << std::endl
107  << senf::fieldName("vlanId") << p->vlanId() << std::endl
108  << senf::fieldName("etherType")
109  << "0x" << std::hex << std::setw(4) << std::setfill('0')
110  << std::right << p->type_length() << std::endl;
111 }
112 
114 {
115  p->type_length() << key(p.next(nothrow));
116 }
117 
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 // senf::EthOUIExtensionPacketType
120 
122 {
123  boost::io::ios_all_saver ias(os);
124  os << "Ethernet OUI Extension:\n"
125  << std::hex << std::setfill('0') << std::right
126  << senf::fieldName("OUI") << std::setw(3) << "0x" << p->oui() << std::endl
127  << senf::fieldName("Ext. EtherType") << std::setw(2) << "0x" << p->ext_type() << std::endl;
128 }
129 
131 {
132  optional_key_t k (key(p.next(nothrow)));
133  if (SENF_LIKELY(k)) {
134  p->oui() << EtherOUIExtTypes::oui(*k);
135  p->ext_type() << EtherOUIExtTypes::ext_type(*k);
136  }
137 }
138 
139 //-/////////////////////////////////////////////////////////////////////////////////////////////////
140 #undef prefix_
141 
142 
143 // Local Variables:
144 // mode: c++
145 // fill-column: 100
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // comment-column: 40
151 // End:
static void finalize(packet p)
EthernetPacket public header.
SENF_PACKET_INSTANTIATE_TEMPLATE(senf::EthernetPacket)
static optional_key_t key(Packet const &p)
Find key of packet from registry.
SENF_PACKET_REGISTRY_REGISTER(senf::EtherTypes, 0x6558, senf::EthernetPacket)
static void finalize(packet p)
static void finalize(packet p)
static boost::uint32_t oui(key_t key)
Definition: Registries.hh:44
std::string fieldName(std::string const &s)
Definition: DumpFormat.cc:28
static const EtherTypes::key_t etherType
static void dump(packet p, std::ostream &os)
Dump given EthernetPacket in readable form to given output stream.
#define SENF_LIKELY(x)
streamable_type dumpint(T const &v)
static const EtherTypes::key_t etherType
static boost::uint16_t ext_type(key_t key)
Definition: Registries.hh:47
static factory_t nextPacketType(packet p)
Main Packet class.
Definition: Packet.hh:131
Protocol specific packet handle.
Definition: Packet.hh:87
Packet next() const
Get next packet in chain.
static void dump(packet p, std::ostream &os)
Dump given EthVLanPacket in readable form to given output stream.
static const EtherTypes::key_t etherType
EtherType registry.
Definition: Registries.hh:34
#define prefix_
nothrow
static void finalize(packet p)
static void dump(packet p, std::ostream &os)
Dump given EthVLanPacket in readable form to given output stream.
static factory_t no_factory()
static PacketTypeBase::factory_t lookup(key_t key)
Lookup the key in the registry.
static factory_t factory()
Return factory for packets of specific type.
LLC/SNAP public header.
static void dump(packet p, std::ostream &os)