Packets/DefaultBundle: Fix packet parser documentation using #ifndef DOXYGEN guards
[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 #ifndef HH_EthernetPacket_
24 #define HH_EthernetPacket_ 1
25
26 // Custom includes
27 #include <algorithm>
28 #include <boost/array.hpp>
29 #include "Packets/Packets.hh"
30
31 //#include "EthernetPacket.mpp"
32 ///////////////////////////////hh.p////////////////////////////////////////
33
34 namespace senf {
35
36     /** \brief Ethernet MAC address
37         
38         The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
39
40         \todo Move to someplace else when implementing the addressing classes
41      */
42     struct MACAddress
43         : boost::array<PacketParserBase::byte,6>
44     {
45         MACAddress(std::string addr);
46         template <class InputIterator>
47         MACAddress(InputIterator i);
48
49         struct SyntaxException : public std::exception
50         { virtual char const * what() const throw() { return "invalid mac address syntax"; } };
51     };
52
53     /** \brief Parse an Ethernet MAC address 
54
55         The ethernet MAC is returned by value as a 6-byte sequence
56
57         \see MACAddress \n
58             EthernetPacket
59      */
60     struct Parse_MAC : public PacketParserBase
61     {
62         Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
63        
64         ///////////////////////////////////////////////////////////////////////////
65
66         typedef MACAddress value_type;
67         static const size_type fixed_bytes = 6u;
68
69         value_type value() const { return MACAddress(i()); }
70         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
71         operator value_type () { return value(); }
72         byte & operator[](size_type index) { return *boost::next(i(),index);  }
73
74         Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
75     };
76     
77     /** \brief Parse an Ethernet packet
78
79         Parser implementing an ethernet header.
80
81         \see EthernetPacketType
82      */
83     struct Parse_Ethernet : public PacketParserBase
84     {
85         typedef Parse_UInt16                      Parse_Type;
86
87 #       ifndef DOXYGEN
88
89         SENF_PACKET_PARSER_INIT(Parse_Ethernet);
90
91         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
92             ((Field)( destination, Parse_MAC  ))
93             ((Field)( source,      Parse_MAC  ))
94             ((Field)( type,        Parse_Type )) );
95
96 #       else
97
98         Parse_MAC destination();
99         Parse_MAC source();
100         Parse_Type type();
101
102 #       endif
103     };
104
105     /** \brief EtherType registry
106
107         This registry registers packet types with their EtherType number.
108         
109         \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a>
110          \ref PacketRegistry
111      */
112     struct EtherTypes {
113         // See 
114         typedef boost::uint16_t key_t;
115     };
116
117     /** \brief Ethernet packet
118
119         \par Packet type (typedef):
120             \ref EthernetPacket
121
122         \par Fields:
123             \ref Parse_Ethernet
124
125         \par Associated registries:
126             \ref EtherTypes
127
128         \ingroup protocolbundle_default
129      */
130     struct EthernetPacketType
131         : public PacketTypeBase,
132           public PacketTypeMixin<EthernetPacketType, EtherTypes>
133     {
134         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
135         typedef ConcretePacket<EthernetPacketType> packet;
136         typedef Parse_Ethernet parser;
137
138         using mixin::nextPacketRange;
139         using mixin::nextPacketType;
140         using mixin::initSize;
141         using mixin::init;
142
143         /** \todo Add LLC/SNAP support -> only use the registry
144             for type() values >=1536, otherwise expect an LLC header */
145         static registry_key_t nextPacketKey(packet p) 
146             { return p->type(); }
147
148         static void dump(packet p, std::ostream & os);
149     };
150
151     /** \brief Ethernet packet typedef */
152     typedef EthernetPacketType::packet EthernetPacket;
153
154     /** \brief Parse an ethernet VLAN tag
155         
156         Parser interpreting the ethernet VLAN tag. Fields are
157
158         \see EthVLanPacketType
159      */
160     struct Parse_EthVLan : public PacketParserBase
161     {
162         typedef Parse_UIntField < 0,  3 > Parse_Priority;
163         typedef Parse_Flag          < 3 > Parse_CFI;
164         typedef Parse_UIntField < 4, 16 > Parse_VLanId;
165         typedef Parse_UInt16              Parse_Type;
166
167 #       ifndef DOXYGEN
168
169         SENF_PACKET_PARSER_INIT(Parse_EthVLan);
170
171         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
172             ((OverlayField)( priority, Parse_Priority ))
173             ((OverlayField)( cfi,      Parse_CFI      ))
174             ((Field       )( vlanId,   Parse_VLanId   ))
175             ((Field       )( type,     Parse_Type     )) );
176
177 #       else
178
179         Parse_Priority priority();
180         Parse_CFI cfi();
181         Parse_VLanId vlanId();
182         Parse_Type type();
183
184 #       endif
185     };
186
187     /** \brief Ethernet VLAN tag
188
189         \par Packet type (typedef):
190             \ref EthVLanPacket
191
192         \par Fields:
193             \ref Parse_EthVLan
194
195         \par Associated registries:
196             \ref EtherTypes
197
198         \ingroup protocolbundle_default
199      */
200     struct EthVLanPacketType
201         : public PacketTypeBase, 
202           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
203     {
204         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
205         typedef ConcretePacket<EthVLanPacketType> packet;
206         typedef Parse_EthVLan parser;
207
208         using mixin::nextPacketRange;
209         using mixin::nextPacketType;
210         using mixin::initSize;
211         using mixin::init;
212
213         /** \todo Add LLC/SNAP support -> only use the registry
214             for type() values >=1536, otherwise expect an LLC header */
215         static registry_key_t nextPacketKey(packet p) 
216             { return p->type(); }
217
218         static void dump(packet p, std::ostream & os);
219     };
220
221     /** \brief Ethernet VLAN tag typedef */
222     typedef EthVLanPacketType::packet EthVLanPacket;
223 }
224
225
226 ///////////////////////////////hh.e////////////////////////////////////////
227 #endif
228 #ifndef SENF_PACKETS_DECL_ONLY
229 //#include "EthernetPacket.cci"
230 #include "EthernetPacket.ct"
231 //#include "EthernetPacket.cti"
232 #endif
233
234 \f
235 // Local Variables:
236 // mode: c++
237 // fill-column: 100
238 // c-file-style: "senf"
239 // indent-tabs-mode: nil
240 // ispell-local-dictionary: "american"
241 // compile-command: "scons -u test"
242 // comment-column: 40
243 // End: