1 // $id: EthernetPacket.hh 299 2007-07-10 21:23:49Z g0dil $
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief EthernetPacket public header */
26 #ifndef HH_EthernetPacket_
27 #define HH_EthernetPacket_ 1
31 #include <boost/array.hpp>
32 #include "Packets/Packets.hh"
34 //#include "EthernetPacket.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
39 /** \brief Ethernet MAC address
41 The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
43 \todo Move to someplace else when implementing the addressing classes
46 : boost::array<PacketParserBase::byte,6>
48 MACAddress(std::string addr);
49 template <class InputIterator>
50 MACAddress(InputIterator i);
52 struct SyntaxException : public std::exception
53 { virtual char const * what() const throw() { return "invalid mac address syntax"; } };
56 /** \brief Parse an Ethernet MAC address
58 The ethernet MAC is returned by value as a 6-byte sequence
63 struct Parse_MAC : public PacketParserBase
65 Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
67 ///////////////////////////////////////////////////////////////////////////
69 typedef MACAddress value_type;
70 static const size_type fixed_bytes = 6u;
72 value_type value() const { return MACAddress(i()); }
73 void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
74 operator value_type () { return value(); }
75 byte & operator[](size_type index) { return *boost::next(i(),index); }
77 Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
80 /** \brief Parse an Ethernet packet
82 Parser implementing an ethernet header.
84 \see EthernetPacketType
86 struct Parse_Ethernet : public PacketParserBase
88 typedef Parse_UInt16 Parse_Type;
92 SENF_PACKET_PARSER_INIT(Parse_Ethernet);
94 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
95 ((Field)( destination, Parse_MAC ))
96 ((Field)( source, Parse_MAC ))
97 ((Field)( type, Parse_Type )) );
101 Parse_MAC destination();
108 /** \brief EtherType registry
110 This registry registers packet types with their EtherType number.
112 \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
117 typedef boost::uint16_t key_t;
120 /** \brief Ethernet packet
122 \par Packet type (typedef):
128 \par Associated registries:
131 \ingroup protocolbundle_default
133 struct EthernetPacketType
134 : public PacketTypeBase,
135 public PacketTypeMixin<EthernetPacketType, EtherTypes>
137 typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
138 typedef ConcretePacket<EthernetPacketType> packet;
139 typedef Parse_Ethernet parser;
141 using mixin::nextPacketRange;
142 using mixin::nextPacketType;
143 using mixin::initSize;
146 /** \todo Add LLC/SNAP support -> only use the registry
147 for type() values >=1536, otherwise expect an LLC header */
148 static registry_key_t nextPacketKey(packet p)
149 { return p->type(); }
151 static void dump(packet p, std::ostream & os);
154 /** \brief Ethernet packet typedef */
155 typedef EthernetPacketType::packet EthernetPacket;
157 /** \brief Parse an ethernet VLAN tag
159 Parser interpreting the ethernet VLAN tag. Fields are
161 \see EthVLanPacketType
163 struct Parse_EthVLan : public PacketParserBase
165 typedef Parse_UIntField < 0, 3 > Parse_Priority;
166 typedef Parse_Flag < 3 > Parse_CFI;
167 typedef Parse_UIntField < 4, 16 > Parse_VLanId;
168 typedef Parse_UInt16 Parse_Type;
172 SENF_PACKET_PARSER_INIT(Parse_EthVLan);
174 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
175 ((OverlayField)( priority, Parse_Priority ))
176 ((OverlayField)( cfi, Parse_CFI ))
177 ((Field )( vlanId, Parse_VLanId ))
178 ((Field )( type, Parse_Type )) );
182 Parse_Priority priority();
184 Parse_VLanId vlanId();
190 /** \brief Ethernet VLAN tag
192 \par Packet type (typedef):
198 \par Associated registries:
201 \ingroup protocolbundle_default
203 struct EthVLanPacketType
204 : public PacketTypeBase,
205 public PacketTypeMixin<EthVLanPacketType, EtherTypes>
207 typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
208 typedef ConcretePacket<EthVLanPacketType> packet;
209 typedef Parse_EthVLan parser;
211 using mixin::nextPacketRange;
212 using mixin::nextPacketType;
213 using mixin::initSize;
216 /** \todo Add LLC/SNAP support -> only use the registry
217 for type() values >=1536, otherwise expect an LLC header */
218 static registry_key_t nextPacketKey(packet p)
219 { return p->type(); }
221 static void dump(packet p, std::ostream & os);
224 /** \brief Ethernet VLAN tag typedef */
225 typedef EthVLanPacketType::packet EthVLanPacket;
229 ///////////////////////////////hh.e////////////////////////////////////////
231 #ifndef SENF_PACKETS_DECL_ONLY
232 //#include "EthernetPacket.cci"
233 #include "EthernetPacket.ct"
234 //#include "EthernetPacket.cti"
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"
245 // comment-column: 40