switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / EthernetPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief EthernetPacket public header */
30
31 #ifndef HH_SENF_Packets_DefaultBundle_EthernetPacket_
32 #define HH_SENF_Packets_DefaultBundle_EthernetPacket_ 1
33
34 // Custom includes
35 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
36 #include <senf/Packets/Packets.hh>
37
38 //#include "EthernetPacket.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace senf {
42
43     /** \brief Parse an Ethernet MAC address
44
45         The ethernet MAC is returned by value as a 6-byte sequence
46
47         \see MACAddress \n
48             EthernetPacket
49      */
50     struct MACAddressParser
51         : public ValueParserBase<MACAddressParser, MACAddress, 6u>
52     {
53         MACAddressParser(data_iterator i, state_type s) : Base(i,s) {}
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
58         using Base::operator=;
59     };
60
61     /** \brief Parse an Ethernet packet
62
63         Parser implementing an ethernet header.
64
65         \see EthernetPacketType
66      */
67     struct EthernetPacketParser : public PacketParserBase
68     {
69 #       include SENF_FIXED_PARSER()
70
71         SENF_PARSER_FIELD( destination, MACAddressParser    );
72         SENF_PARSER_FIELD( source,      MACAddressParser    );
73         SENF_PARSER_FIELD( type_length, UInt16Parser );
74
75         SENF_PARSER_FINALIZE(EthernetPacketParser);
76     };
77
78     /** \brief EtherType registry
79
80         This registry registers packet types with their EtherType number.
81
82         \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
83             \ref PacketRegistry
84      */
85     struct EtherTypes {
86         typedef boost::uint16_t key_t;
87     };
88
89     /** \brief Ethernet packet
90
91         \par Packet type (typedef):
92             \ref EthernetPacket
93
94         \par Fields:
95             \ref EthernetPacketParser
96             \image html EthernetPacket.png
97
98         \par Associated registries:
99             \ref EtherTypes
100
101         \par Finalize action:
102             Set \a type from type of next packet if found in \ref EtherTypes
103
104         \ingroup protocolbundle_default
105      */
106     struct EthernetPacketType
107         : public PacketTypeBase,
108           public PacketTypeMixin<EthernetPacketType, EtherTypes>
109     {
110 #ifndef DOXYGEN
111         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
112 #endif
113         typedef ConcretePacket<EthernetPacketType> packet;
114         typedef EthernetPacketParser parser;
115
116         using mixin::nextPacketRange;
117         using mixin::initSize;
118         using mixin::init;
119
120         static factory_t nextPacketType(packet p);
121         /// Dump given EthernetPacket in readable form to given output stream
122         static void dump(packet p, std::ostream & os);
123         static void finalize(packet p);
124     };
125
126     /** \brief Ethernet packet typedef
127         \ingroup protocolbundle_default
128      */
129     typedef ConcretePacket<EthernetPacketType> EthernetPacket;
130
131
132     /** \brief Parse an ethernet VLAN tag
133
134         Parser interpreting the ethernet VLAN tag. Fields are
135
136         \see EthVLanPacketType
137      */
138     struct EthVLanPacketParser : public PacketParserBase
139     {
140 #       include SENF_FIXED_PARSER()
141
142         SENF_PARSER_BITFIELD( priority,  3, unsigned );
143         SENF_PARSER_BITFIELD( cfi,       1, bool     );
144         SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
145
146         SENF_PARSER_FIELD( type, UInt16Parser );
147
148         SENF_PARSER_FINALIZE(EthVLanPacketParser);
149     };
150
151     /** \brief Ethernet VLAN tag
152
153         \par Packet type (typedef):
154             \ref EthVLanPacket
155
156         \par Fields:
157             \ref EthVLanPacketParser
158             \image html EthVLanPacket.png
159
160         \par Associated registries:
161             \ref EtherTypes
162
163         \par Finalize action:
164             Set \a type from type of next packet if found in \ref EtherTypes
165
166         \ingroup protocolbundle_default
167      */
168     struct EthVLanPacketType
169         : public PacketTypeBase,
170           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
171     {
172 #ifndef DOXYGEN
173         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
174 #endif
175         typedef ConcretePacket<EthVLanPacketType> packet;
176         typedef EthVLanPacketParser parser;
177
178         using mixin::nextPacketRange;
179         using mixin::nextPacketType;
180         using mixin::initSize;
181         using mixin::init;
182
183         /** \todo Add LLC/SNAP support -> only use the registry
184             for type() values >=1536, otherwise expect an LLC header */
185         static key_t nextPacketKey(packet p)
186             { return p->type(); }
187
188         /// Dump given EthVLanPacket in readable form to given output stream
189         static void dump(packet p, std::ostream & os);
190         static void finalize(packet p);
191     };
192
193     /** \brief Ethernet VLAN tag typedef
194         \ingroup protocolbundle_default
195      */
196     typedef ConcretePacket<EthVLanPacketType> EthVLanPacket;
197
198 }
199
200 //-/////////////////////////////////////////////////////////////////////////////////////////////////
201 //#include "EthernetPacket.cci"
202 //#include "EthernetPacket.ct"
203 //#include "EthernetPacket.cti"
204 #endif
205
206 \f
207 // Local Variables:
208 // mode: c++
209 // fill-column: 100
210 // c-file-style: "senf"
211 // indent-tabs-mode: nil
212 // ispell-local-dictionary: "american"
213 // compile-command: "scons -u test"
214 // comment-column: 40
215 // End: