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