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.
23 #ifndef HH_EthernetPacket_
24 #define HH_EthernetPacket_ 1
28 #include <boost/array.hpp>
29 #include "Packets/Packets.hh"
31 //#include "EthernetPacket.mpp"
32 ///////////////////////////////hh.p////////////////////////////////////////
36 /** \brief Ethernet MAC address
38 The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
40 \todo Move to someplace else when implementing the addressing classes
43 : boost::array<PacketParserBase::byte,6>
45 MACAddress(std::string addr);
46 template <class InputIterator>
47 MACAddress(InputIterator i);
49 struct SyntaxException : public std::exception
50 { virtual char const * what() const throw() { return "invalid mac address syntax"; } };
53 /** \brief Parse an Ethernet MAC address
55 The ethernet MAC is returned by value as a 6-byte sequence
60 struct Parse_MAC : public PacketParserBase
62 Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
64 ///////////////////////////////////////////////////////////////////////////
66 typedef MACAddress value_type;
67 static const size_type fixed_bytes = 6u;
69 value_type value() const { return MACAddress(i()); }
70 void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
71 operator value_type () { return value(); }
72 byte & operator[](size_type index) { return *boost::next(i(),index); }
74 Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
77 /** \brief Parse an Ethernet packet
79 Parser implementing an ethernet header.
81 \see EthernetPacketType
83 struct Parse_Ethernet : public PacketParserBase
85 typedef Parse_UInt16 Parse_Type;
89 SENF_PACKET_PARSER_INIT(Parse_Ethernet);
91 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
92 ((Field)( destination, Parse_MAC ))
93 ((Field)( source, Parse_MAC ))
94 ((Field)( type, Parse_Type )) );
98 Parse_MAC destination();
105 /** \brief EtherType registry
107 This registry registers packet types with their EtherType number.
109 \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
114 typedef boost::uint16_t key_t;
117 /** \brief Ethernet packet
119 \par Packet type (typedef):
125 \par Associated registries:
128 \ingroup protocolbundle_default
130 struct EthernetPacketType
131 : public PacketTypeBase,
132 public PacketTypeMixin<EthernetPacketType, EtherTypes>
134 typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
135 typedef ConcretePacket<EthernetPacketType> packet;
136 typedef Parse_Ethernet parser;
138 using mixin::nextPacketRange;
139 using mixin::nextPacketType;
140 using mixin::initSize;
143 /** \todo Add LLC/SNAP support -> only use the registry
144 for type() values >=1536, otherwise expect an LLC header */
145 static registry_key_t nextPacketKey(packet p)
146 { return p->type(); }
148 static void dump(packet p, std::ostream & os);
151 /** \brief Ethernet packet typedef */
152 typedef EthernetPacketType::packet EthernetPacket;
154 /** \brief Parse an ethernet VLAN tag
156 Parser interpreting the ethernet VLAN tag. Fields are
158 \see EthVLanPacketType
160 struct Parse_EthVLan : public PacketParserBase
162 typedef Parse_UIntField < 0, 3 > Parse_Priority;
163 typedef Parse_Flag < 3 > Parse_CFI;
164 typedef Parse_UIntField < 4, 16 > Parse_VLanId;
165 typedef Parse_UInt16 Parse_Type;
169 SENF_PACKET_PARSER_INIT(Parse_EthVLan);
171 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
172 ((OverlayField)( priority, Parse_Priority ))
173 ((OverlayField)( cfi, Parse_CFI ))
174 ((Field )( vlanId, Parse_VLanId ))
175 ((Field )( type, Parse_Type )) );
179 Parse_Priority priority();
181 Parse_VLanId vlanId();
187 /** \brief Ethernet VLAN tag
189 \par Packet type (typedef):
195 \par Associated registries:
198 \ingroup protocolbundle_default
200 struct EthVLanPacketType
201 : public PacketTypeBase,
202 public PacketTypeMixin<EthVLanPacketType, EtherTypes>
204 typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
205 typedef ConcretePacket<EthVLanPacketType> packet;
206 typedef Parse_EthVLan parser;
208 using mixin::nextPacketRange;
209 using mixin::nextPacketType;
210 using mixin::initSize;
213 /** \todo Add LLC/SNAP support -> only use the registry
214 for type() values >=1536, otherwise expect an LLC header */
215 static registry_key_t nextPacketKey(packet p)
216 { return p->type(); }
218 static void dump(packet p, std::ostream & os);
221 /** \brief Ethernet VLAN tag typedef */
222 typedef EthVLanPacketType::packet EthVLanPacket;
226 ///////////////////////////////hh.e////////////////////////////////////////
228 #ifndef SENF_PACKETS_DECL_ONLY
229 //#include "EthernetPacket.cci"
230 #include "EthernetPacket.ct"
231 //#include "EthernetPacket.cti"
238 // c-file-style: "senf"
239 // indent-tabs-mode: nil
240 // ispell-local-dictionary: "american"
241 // compile-command: "scons -u test"
242 // comment-column: 40