Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[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 <algorithm>
31 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
32 #include <senf/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 MACAddressParser : public PacketParserBase
47     {
48         MACAddressParser(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         MACAddressParser 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 EthernetPacketParser : public PacketParserBase
70     {
71 #       include SENF_FIXED_PARSER()
72
73         SENF_PARSER_FIELD( destination, MACAddressParser    );
74         SENF_PARSER_FIELD( source,      MACAddressParser    );
75         SENF_PARSER_FIELD( type_length, UInt16Parser );
76
77         SENF_PARSER_FINALIZE(EthernetPacketParser);
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         typedef boost::uint16_t key_t;
89     };
90
91     /** \brief Ethernet packet
92
93         \par Packet type (typedef):
94             \ref EthernetPacket
95
96         \par Fields:
97             \ref EthernetPacketParser
98             \image html EthernetPacket.png
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 #endif
115         typedef ConcretePacket<EthernetPacketType> packet;
116         typedef EthernetPacketParser parser;
117
118         using mixin::nextPacketRange;
119         using mixin::initSize;
120         using mixin::init;
121
122         static factory_t nextPacketType(packet p);
123         /// Dump given EthernetPacket in readable form to given output stream
124         static void dump(packet p, std::ostream & os);
125         static void finalize(packet p);
126     };
127
128     /** \brief Ethernet packet typedef
129         \ingroup protocolbundle_default
130      */
131     typedef ConcretePacket<EthernetPacketType> EthernetPacket;
132
133
134     /** \brief Parse an ethernet VLAN tag
135
136         Parser interpreting the ethernet VLAN tag. Fields are
137
138         \see EthVLanPacketType
139      */
140     struct EthVLanPacketParser : public PacketParserBase
141     {
142 #       include SENF_FIXED_PARSER()
143
144         SENF_PARSER_BITFIELD( priority,  3, unsigned );
145         SENF_PARSER_BITFIELD( cfi,       1, bool     );
146         SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
147
148         SENF_PARSER_FIELD( type, UInt16Parser );
149
150         SENF_PARSER_FINALIZE(EthVLanPacketParser);
151     };
152
153     /** \brief Ethernet VLAN tag
154
155         \par Packet type (typedef):
156             \ref EthVLanPacket
157
158         \par Fields:
159             \ref EthVLanPacketParser
160             \image html EthVLanPacket.png
161
162         \par Associated registries:
163             \ref EtherTypes
164
165         \par Finalize action:
166             Set \a type from type of next packet if found in \ref EtherTypes
167
168         \ingroup protocolbundle_default
169      */
170     struct EthVLanPacketType
171         : public PacketTypeBase,
172           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
173     {
174 #ifndef DOXYGEN
175         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
176 #endif
177         typedef ConcretePacket<EthVLanPacketType> packet;
178         typedef EthVLanPacketParser parser;
179
180         using mixin::nextPacketRange;
181         using mixin::nextPacketType;
182         using mixin::initSize;
183         using mixin::init;
184
185         /** \todo Add LLC/SNAP support -> only use the registry
186             for type() values >=1536, otherwise expect an LLC header */
187         static key_t nextPacketKey(packet p)
188             { return p->type(); }
189
190         /// Dump given EthVLanPacket in readable form to given output stream
191         static void dump(packet p, std::ostream & os);
192         static void finalize(packet p);
193     };
194
195     /** \brief Ethernet VLAN tag typedef
196         \ingroup protocolbundle_default
197      */
198     typedef ConcretePacket<EthVLanPacketType> EthVLanPacket;
199
200 }
201
202 ///////////////////////////////hh.e////////////////////////////////////////
203 //#include "EthernetPacket.cci"
204 //#include "EthernetPacket.ct"
205 //#include "EthernetPacket.cti"
206 #endif
207
208 \f
209 // Local Variables:
210 // mode: c++
211 // fill-column: 100
212 // c-file-style: "senf"
213 // indent-tabs-mode: nil
214 // ispell-local-dictionary: "american"
215 // compile-command: "scons -u test"
216 // comment-column: 40
217 // End: