Packets/DefaultBundle: Document finalize() action
[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,        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         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
113         typedef ConcretePacket<EthernetPacketType> packet;
114         typedef Parse_Ethernet parser;
115
116         using mixin::nextPacketRange;
117         using mixin::nextPacketType;
118         using mixin::initSize;
119         using mixin::init;
120
121         /** \todo Add LLC/SNAP support -> only use the registry
122             for type() values >=1536, otherwise expect an LLC header */
123         static registry_key_t nextPacketKey(packet p) 
124             { return p->type(); }
125
126         static void dump(packet p, std::ostream & os);
127         static void finalize(packet p);
128     };
129
130     /** \brief Ethernet packet typedef */
131     typedef EthernetPacketType::packet EthernetPacket;
132
133     /** \brief Parse an ethernet VLAN tag
134         
135         Parser interpreting the ethernet VLAN tag. Fields are
136
137         \see EthVLanPacketType
138      */
139     struct Parse_EthVLan : public PacketParserBase
140     {
141 #       include SENF_FIXED_PARSER()
142
143         SENF_PARSER_BITFIELD( priority,  3, unsigned );
144         SENF_PARSER_BITFIELD( cfi,       1, bool     );
145         SENF_PARSER_BITFIELD( vlanId,   12, unsigned );
146
147         SENF_PARSER_FIELD( type, Parse_UInt16 );
148
149         SENF_PARSER_FINALIZE(Parse_EthVLan);
150     };
151
152     /** \brief Ethernet VLAN tag
153
154         \par Packet type (typedef):
155             \ref EthVLanPacket
156
157         \par Fields:
158             \ref Parse_EthVLan
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         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
173         typedef ConcretePacket<EthVLanPacketType> packet;
174         typedef Parse_EthVLan parser;
175
176         using mixin::nextPacketRange;
177         using mixin::nextPacketType;
178         using mixin::initSize;
179         using mixin::init;
180
181         /** \todo Add LLC/SNAP support -> only use the registry
182             for type() values >=1536, otherwise expect an LLC header */
183         static registry_key_t nextPacketKey(packet p) 
184             { return p->type(); }
185
186         static void dump(packet p, std::ostream & os);
187         static void finalize(packet p);
188     };
189
190     /** \brief Ethernet VLAN tag typedef */
191     typedef EthVLanPacketType::packet EthVLanPacket;
192 }
193
194
195 ///////////////////////////////hh.e////////////////////////////////////////
196 #endif
197 #ifndef SENF_PACKETS_DECL_ONLY
198 //#include "EthernetPacket.cci"
199 //#include "EthernetPacket.ct"
200 //#include "EthernetPacket.cti"
201 #endif
202
203 \f
204 // Local Variables:
205 // mode: c++
206 // fill-column: 100
207 // c-file-style: "senf"
208 // indent-tabs-mode: nil
209 // ispell-local-dictionary: "american"
210 // compile-command: "scons -u test"
211 // comment-column: 40
212 // End: