b317c32e9b0edd2b3f64a657bd60e83f475b8904
[senf.git] / senf / Packets / DefaultBundle / EthernetPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.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_SENF_Packets_DefaultBundle_EthernetPacket_
27 #define HH_SENF_Packets_DefaultBundle_EthernetPacket_ 1
28
29 // Custom includes
30 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
31 #include <senf/Packets/Packets.hh>
32
33 //#include "EthernetPacket.mpp"
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 namespace senf {
37
38     /** \brief Parse an Ethernet MAC address
39
40         The ethernet MAC is returned by value as a 6-byte sequence
41
42         \see MACAddress \n
43             EthernetPacket
44      */
45     struct MACAddressParser
46         : public ValueParserBase<MACAddressParser, MACAddress, 6u>
47     {
48         MACAddressParser(data_iterator i, state_type s) : Base(i,s) {}
49
50         value_type value() const { return MACAddress::from_data(i()); }
51         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
52
53         using Base::operator=;
54     };
55
56     /** \brief Parse an Ethernet packet
57
58         Parser implementing an ethernet header.
59
60         \see EthernetPacketType
61      */
62     struct EthernetPacketParser : public PacketParserBase
63     {
64 #       include SENF_FIXED_PARSER()
65
66         SENF_PARSER_FIELD( destination, MACAddressParser    );
67         SENF_PARSER_FIELD( source,      MACAddressParser    );
68         SENF_PARSER_FIELD( type_length, UInt16Parser );
69
70         SENF_PARSER_FINALIZE(EthernetPacketParser);
71     };
72
73     /** \brief EtherType registry
74
75         This registry registers packet types with their EtherType number.
76
77         \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a> \n
78             \ref PacketRegistry
79      */
80     struct EtherTypes {
81         typedef boost::uint16_t key_t;
82     };
83
84     /** \brief Ethernet packet
85
86         \par Packet type (typedef):
87             \ref EthernetPacket
88
89         \par Fields:
90             \ref EthernetPacketParser
91             \image html EthernetPacket.png
92
93         \par Associated registries:
94             \ref EtherTypes
95
96         \par Finalize action:
97             Set \a type from type of next packet if found in \ref EtherTypes
98
99         \ingroup protocolbundle_default
100      */
101     struct EthernetPacketType
102         : public PacketTypeBase,
103           public PacketTypeMixin<EthernetPacketType, EtherTypes>
104     {
105 #ifndef DOXYGEN
106         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
107 #endif
108         typedef ConcretePacket<EthernetPacketType> packet;
109         typedef EthernetPacketParser parser;
110
111         using mixin::nextPacketRange;
112         using mixin::initSize;
113         using mixin::init;
114
115         static factory_t nextPacketType(packet p);
116         /// Dump given EthernetPacket in readable form to given output stream
117         static void dump(packet p, std::ostream & os);
118         static void finalize(packet p);
119     };
120
121     /** \brief Ethernet packet typedef
122         \ingroup protocolbundle_default
123      */
124     typedef ConcretePacket<EthernetPacketType> EthernetPacket;
125
126
127     /** \brief Parse an ethernet VLAN tag
128
129         Parser interpreting the ethernet VLAN tag. Fields are
130
131         \see EthVLanPacketType
132      */
133     struct EthVLanPacketParser : public PacketParserBase
134     {
135 #       include SENF_FIXED_PARSER()
136
137         SENF_PARSER_BITFIELD( priority,  3, unsigned );
138         SENF_PARSER_BITFIELD( cfi,       1, bool     );
139         SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
140
141         SENF_PARSER_FIELD( type, UInt16Parser );
142
143         SENF_PARSER_FINALIZE(EthVLanPacketParser);
144     };
145
146     /** \brief Ethernet VLAN tag
147
148         \par Packet type (typedef):
149             \ref EthVLanPacket
150
151         \par Fields:
152             \ref EthVLanPacketParser
153             \image html EthVLanPacket.png
154
155         \par Associated registries:
156             \ref EtherTypes
157
158         \par Finalize action:
159             Set \a type from type of next packet if found in \ref EtherTypes
160
161         \ingroup protocolbundle_default
162      */
163     struct EthVLanPacketType
164         : public PacketTypeBase,
165           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
166     {
167 #ifndef DOXYGEN
168         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
169 #endif
170         typedef ConcretePacket<EthVLanPacketType> packet;
171         typedef EthVLanPacketParser parser;
172
173         using mixin::nextPacketRange;
174         using mixin::nextPacketType;
175         using mixin::initSize;
176         using mixin::init;
177
178         /** \todo Add LLC/SNAP support -> only use the registry
179             for type() values >=1536, otherwise expect an LLC header */
180         static key_t nextPacketKey(packet p)
181             { return p->type(); }
182
183         /// Dump given EthVLanPacket in readable form to given output stream
184         static void dump(packet p, std::ostream & os);
185         static void finalize(packet p);
186     };
187
188     /** \brief Ethernet VLAN tag typedef
189         \ingroup protocolbundle_default
190      */
191     typedef ConcretePacket<EthVLanPacketType> EthVLanPacket;
192
193 }
194
195 //-/////////////////////////////////////////////////////////////////////////////////////////////////
196 //#include "EthernetPacket.cci"
197 //#include "EthernetPacket.ct"
198 //#include "EthernetPacket.cti"
199 #endif
200
201 \f
202 // Local Variables:
203 // mode: c++
204 // fill-column: 100
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"
209 // comment-column: 40
210 // End: