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. The fields implemented are
82 <tr> <th>Field name</th> <th>Parser type</th> </tr>
83 <tr> <td>destination</td> <td>\ref Parse_MAC</td> </tr>
84 <tr> <td>source</td> <td>\ref Parse_MAC</td> </tr>
85 <tr> <td>type</td> <td>\ref Parse_Type</td> </tr>
88 \see EthernetPacketType
90 struct Parse_Ethernet : public PacketParserBase
92 SENF_PACKET_PARSER_INIT(Parse_Ethernet);
94 ///////////////////////////////////////////////////////////////////////////
96 typedef Parse_UInt16 Parse_Type;
98 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
99 ((Field)( destination, Parse_MAC ))
100 ((Field)( source, Parse_MAC ))
101 ((Field)( type, Parse_Type )) );
104 /** \brief EtherType registry
106 This registry registers packet types with their EtherType number.
108 \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a>
113 typedef boost::uint16_t key_t;
116 /** \brief Ethernet packet
118 \par Packet type (typedef):
124 \par Associated registries:
127 \ingroup protocolbundle_default
129 struct EthernetPacketType
130 : public PacketTypeBase,
131 public PacketTypeMixin<EthernetPacketType, EtherTypes>
133 typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
134 typedef ConcretePacket<EthernetPacketType> packet;
135 typedef Parse_Ethernet parser;
137 using mixin::nextPacketRange;
138 using mixin::nextPacketType;
139 using mixin::initSize;
142 /** \todo Add LLC/SNAP support -> only use the registry
143 for type() values >=1536, otherwise expect an LLC header */
144 static registry_key_t nextPacketKey(packet p)
145 { return p->type(); }
147 static void dump(packet p, std::ostream & os);
150 /** \brief Ethernet packet typedef */
151 typedef EthernetPacketType::packet EthernetPacket;
153 /** \brief Parse an ethernet VLAN tag
155 Parser interpreting the ethernet VLAN tag. Fields are
158 <tr> <th>Field name</th><th>Parser type</th></tr>
159 <tr><td>priority</td><td>\ref Parse_Priority</td></tr>
160 <tr><td>cfi</td><td>\ref Parse_CFI</td></tr>
161 <tr><td>vlanId</td><td>\ref Parse_VLanId</td></tr>
162 <tr><td>type</td><td>\ref Parse_Type</td></tr>
165 \see EthVLanPacketType
167 struct Parse_EthVLan : public PacketParserBase
169 SENF_PACKET_PARSER_INIT(Parse_EthVLan);
171 ///////////////////////////////////////////////////////////////////////////
173 typedef Parse_UIntField < 0, 3 > Parse_Priority;
174 typedef Parse_Flag < 3 > Parse_CFI;
175 typedef Parse_UIntField < 4, 16 > Parse_VLanId;
176 typedef Parse_UInt16 Parse_Type;
178 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
179 ((OverlayField)( priority, Parse_Priority ))
180 ((OverlayField)( cfi, Parse_CFI ))
181 ((Field )( vlanId, Parse_VLanId ))
182 ((Field )( type, Parse_Type )) );
185 /** \brief Ethernet VLAN tag
187 \par Packet type (typedef):
193 \par Associated registries:
196 \ingroup protocolbundle_default
198 struct EthVLanPacketType
199 : public PacketTypeBase,
200 public PacketTypeMixin<EthVLanPacketType, EtherTypes>
202 typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
203 typedef ConcretePacket<EthVLanPacketType> packet;
204 typedef Parse_EthVLan parser;
206 using mixin::nextPacketRange;
207 using mixin::nextPacketType;
208 using mixin::initSize;
211 /** \todo Add LLC/SNAP support -> only use the registry
212 for type() values >=1536, otherwise expect an LLC header */
213 static registry_key_t nextPacketKey(packet p)
214 { return p->type(); }
216 static void dump(packet p, std::ostream & os);
219 /** \brief Ethernet VLAN tag typedef */
220 typedef EthVLanPacketType::packet EthVLanPacket;
224 ///////////////////////////////hh.e////////////////////////////////////////
226 #ifndef SENF_PACKETS_DECL_ONLY
227 //#include "EthernetPacket.cci"
228 #include "EthernetPacket.ct"
229 //#include "EthernetPacket.cti"
236 // c-file-style: "senf"
237 // indent-tabs-mode: nil
238 // ispell-local-dictionary: "american"
239 // compile-command: "scons -u test"
240 // comment-column: 40