5b0856d8f19d68b3545fe6e5061ccaeb13473fe2
[senf.git] / Packets / DefaultBundle / EthernetPacket.hh
1 // $id: EthernetPacket.hh 299 2007-07-10 21:23:49Z g0dil $
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief EthernetPacket public header */
25
26 #ifndef HH_EthernetPacket_
27 #define HH_EthernetPacket_ 1
28
29 // Custom includes
30 #include <algorithm>
31 #include "../../Socket/Protocols/Raw/MACAddress.hh"
32 #include "../../Packets/Packets.hh"
33
34 //#include "EthernetPacket.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     /** \brief Parse an Ethernet MAC address 
40
41         The ethernet MAC is returned by value as a 6-byte sequence
42
43         \see MACAddress \n
44             EthernetPacket
45      */
46     struct Parse_MAC : public PacketParserBase
47     {
48         Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
49        
50         ///////////////////////////////////////////////////////////////////////////
51
52         typedef MACAddress value_type;
53         static const size_type fixed_bytes = 6u;
54
55         value_type value() const { return MACAddress::from_data(i()); }
56         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
57         operator value_type () { return value(); }
58         byte & operator[](size_type index) { return *boost::next(i(),index);  }
59
60         Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
61     };
62     
63     /** \brief Parse an Ethernet packet
64
65         Parser implementing an ethernet header.
66
67         \see EthernetPacketType
68      */
69     struct Parse_Ethernet : public PacketParserBase
70     {
71 #       include SENF_FIXED_PARSER()
72
73         SENF_PARSER_FIELD( destination, Parse_MAC    );
74         SENF_PARSER_FIELD( source,      Parse_MAC    );
75         SENF_PARSER_FIELD( type_length, Parse_UInt16 );
76
77         SENF_PARSER_FINALIZE(Parse_Ethernet);
78     };
79
80     /** \brief EtherType registry
81
82         This registry registers packet types with their EtherType number.
83         
84         \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
85             \ref PacketRegistry
86      */
87     struct EtherTypes {
88         // See 
89         typedef boost::uint16_t key_t;
90     };
91
92     /** \brief Ethernet packet
93
94         \par Packet type (typedef):
95             \ref EthernetPacket
96
97         \par Fields:
98             \ref Parse_Ethernet
99
100         \par Associated registries:
101             \ref EtherTypes
102
103         \par Finalize action:
104             Set \a type from type of next packet if found in \ref EtherTypes
105
106         \ingroup protocolbundle_default
107      */
108     struct EthernetPacketType
109         : public PacketTypeBase,
110           public PacketTypeMixin<EthernetPacketType, EtherTypes>
111     {
112 #ifndef DOXYGEN
113         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
114         typedef ConcretePacket<EthernetPacketType> packet;
115         typedef Parse_Ethernet parser;
116 #endif
117         using mixin::nextPacketRange;
118   //      using mixin::nextPacketType;
119         using mixin::initSize;
120         using mixin::init;
121
122         static factory_t nextPacketType(packet p);
123         static void dump(packet p, std::ostream & os);
124         static void finalize(packet p);
125     };
126
127     /** \brief Ethernet packet typedef */
128     typedef ConcretePacket<EthernetPacketType> EthernetPacket;
129
130     /** \brief Parse an ethernet VLAN tag
131         
132         Parser interpreting the ethernet VLAN tag. Fields are
133
134         \see EthVLanPacketType
135      */
136     struct Parse_EthVLan : public PacketParserBase
137     {
138 #       include SENF_FIXED_PARSER()
139
140         SENF_PARSER_BITFIELD( priority,  3, unsigned );
141         SENF_PARSER_BITFIELD( cfi,       1, bool     );
142         SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
143
144         SENF_PARSER_FIELD( type, Parse_UInt16 );
145
146         SENF_PARSER_FINALIZE(Parse_EthVLan);
147     };
148
149     /** \brief Ethernet VLAN tag
150
151         \par Packet type (typedef):
152             \ref EthVLanPacket
153
154         \par Fields:
155             \ref Parse_EthVLan
156
157         \par Associated registries:
158             \ref EtherTypes
159
160         \par Finalize action:
161             Set \a type from type of next packet if found in \ref EtherTypes
162
163         \ingroup protocolbundle_default
164      */
165     struct EthVLanPacketType
166         : public PacketTypeBase, 
167           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
168     {
169 #ifndef DOXYGEN
170         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
171         typedef ConcretePacket<EthVLanPacketType> packet;
172         typedef Parse_EthVLan parser;
173 #endif
174         using mixin::nextPacketRange;
175         using mixin::nextPacketType;
176         using mixin::initSize;
177         using mixin::init;
178
179         /** \todo Add LLC/SNAP support -> only use the registry
180             for type() values >=1536, otherwise expect an LLC header */
181         static registry_key_t nextPacketKey(packet p) 
182             { return p->type(); }
183
184         static void dump(packet p, std::ostream & os);
185         static void finalize(packet p);
186     };
187
188     /** \brief Ethernet VLAN tag typedef */
189     typedef ConcretePacket<EthVLanPacketType> EthVLanPacket;
190
191
192     /** \brief Parse an ethernet LLC/SNAP header
193         
194         \todo document me
195
196         \see EthVLanPacketType
197      */
198     struct Parse_EthLlcSnapPacket : public PacketParserBase
199     {
200 #       include SENF_FIXED_PARSER()
201
202         SENF_PARSER_FIELD( dsap, Parse_UInt8 );
203         SENF_PARSER_FIELD( ssap, Parse_UInt8 );
204         SENF_PARSER_FIELD( ctrl, Parse_UInt8 );
205
206         SENF_PARSER_FIELD( protocolId, Parse_UInt24 );
207         SENF_PARSER_FIELD( type, Parse_UInt16 );
208
209         SENF_PARSER_FINALIZE(Parse_EthLlcSnapPacket);
210         
211         SENF_PARSER_INIT() {
212             dsap() = 0xaa;
213             ssap() = 0xaa;
214             ctrl() = 0x03;
215             protocolId() = 0x000000;
216         }
217     };
218
219     /** \brief Ethernet LLC/SNAP header
220
221         \todo document me
222
223         \par Packet type (typedef):
224             \ref EthLlcSnapPacketType
225
226         \par Fields:
227             \ref Parse_EthLlcSnapPacket
228
229         \par Associated registries:
230             \ref EtherTypes
231
232         \par Finalize action:
233             XXXX
234
235         \ingroup protocolbundle_default
236      */
237     struct EthLlcSnapPacketType
238         : public PacketTypeBase, 
239           public PacketTypeMixin<EthLlcSnapPacketType, EtherTypes>
240     {
241 #ifndef DOXYGEN
242         typedef PacketTypeMixin<EthLlcSnapPacketType, EtherTypes> mixin;
243         typedef ConcretePacket<EthLlcSnapPacketType> packet;
244         typedef Parse_EthLlcSnapPacket parser;
245 #endif
246         using mixin::nextPacketRange;
247         using mixin::nextPacketType;
248         using mixin::initSize;
249         using mixin::init;
250                 
251         static registry_key_t nextPacketKey(packet p) 
252             { return p->type(); }
253
254         static void dump(packet p, std::ostream & os);
255         static void finalize(packet p);
256     };
257
258     /** \brief Ethernet VLAN tag typedef */
259     typedef ConcretePacket<EthLlcSnapPacketType> EthLlcSnapPacket;
260 }
261
262
263 ///////////////////////////////hh.e////////////////////////////////////////
264 #endif
265 #ifndef SENF_PACKETS_DECL_ONLY
266 //#include "EthernetPacket.cci"
267 //#include "EthernetPacket.ct"
268 //#include "EthernetPacket.cti"
269 #endif
270
271 \f
272 // Local Variables:
273 // mode: c++
274 // fill-column: 100
275 // c-file-style: "senf"
276 // indent-tabs-mode: nil
277 // ispell-local-dictionary: "american"
278 // compile-command: "scons -u test"
279 // comment-column: 40
280 // End: