Packets/DefaultBundle: Documentation
[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. The fields implemented are
80
81         <table class="senf">
82         <tr> <th>Field name</th>  <th>Parser type</th>     </tr>
83         <tr> <td>destination</td> <td>\ref Parse_MAC</td>  </tr>
84         <tr> <td>source</td>      <td>\ref Parse_MAC</td>  </tr>
85         <tr> <td>type</td>        <td>\ref Parse_Type</td> </tr>
86         </table>
87
88         \see EthernetPacketType
89      */
90     struct Parse_Ethernet : public PacketParserBase
91     {
92         SENF_PACKET_PARSER_INIT(Parse_Ethernet);
93
94         ///////////////////////////////////////////////////////////////////////////
95
96         typedef Parse_UInt16                      Parse_Type;
97
98         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
99             ((Field)( destination, Parse_MAC  ))
100             ((Field)( source,      Parse_MAC  ))
101             ((Field)( type,        Parse_Type )) );
102     };
103
104     /** \brief EtherType registry
105
106         This registry registers packet types with their EtherType number.
107         
108         \see <a href="http://www.iana.org/assignments/ethernet-numbers">Ethernet numbers</a>
109          \ref PacketRegistry
110      */
111     struct EtherTypes {
112         // See 
113         typedef boost::uint16_t key_t;
114     };
115
116     /** \brief Ethernet packet
117
118         \par Packet type (typedef):
119             \ref EthernetPacket
120
121         \par Fields:
122             \ref Parse_Ethernet
123
124         \par Associated registries:
125             \ref EtherTypes
126
127         \ingroup protocolbundle_default
128      */
129     struct EthernetPacketType
130         : public PacketTypeBase,
131           public PacketTypeMixin<EthernetPacketType, EtherTypes>
132     {
133         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
134         typedef ConcretePacket<EthernetPacketType> packet;
135         typedef Parse_Ethernet parser;
136
137         using mixin::nextPacketRange;
138         using mixin::nextPacketType;
139         using mixin::initSize;
140         using mixin::init;
141
142         /** \todo Add LLC/SNAP support -> only use the registry
143             for type() values >=1536, otherwise expect an LLC header */
144         static registry_key_t nextPacketKey(packet p) 
145             { return p->type(); }
146
147         static void dump(packet p, std::ostream & os);
148     };
149
150     /** \brief Ethernet packet typedef */
151     typedef EthernetPacketType::packet EthernetPacket;
152
153     /** \brief Parse an ethernet VLAN tag
154         
155         Parser interpreting the ethernet VLAN tag. Fields are
156
157         <table class="senf">
158         <tr> <th>Field name</th><th>Parser type</th></tr>
159         <tr><td>priority</td><td>\ref Parse_Priority</td></tr>
160         <tr><td>cfi</td><td>\ref Parse_CFI</td></tr>
161         <tr><td>vlanId</td><td>\ref Parse_VLanId</td></tr>
162         <tr><td>type</td><td>\ref Parse_Type</td></tr>
163         </table>
164
165         \see EthVLanPacketType
166      */
167     struct Parse_EthVLan : public PacketParserBase
168     {
169         SENF_PACKET_PARSER_INIT(Parse_EthVLan);
170
171         ///////////////////////////////////////////////////////////////////////////
172
173         typedef Parse_UIntField < 0,  3 > Parse_Priority;
174         typedef Parse_Flag          < 3 > Parse_CFI;
175         typedef Parse_UIntField < 4, 16 > Parse_VLanId;
176         typedef Parse_UInt16              Parse_Type;
177
178         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
179             ((OverlayField)( priority, Parse_Priority ))
180             ((OverlayField)( cfi,      Parse_CFI      ))
181             ((Field       )( vlanId,   Parse_VLanId   ))
182             ((Field       )( type,     Parse_Type     )) );
183     };
184
185     /** \brief Ethernet VLAN tag
186
187         \par Packet type (typedef):
188             \ref EthVLanPacket
189
190         \par Fields:
191             \ref Parse_EthVLan
192
193         \par Associated registries:
194             \ref EtherTypes
195
196         \ingroup protocolbundle_default
197      */
198     struct EthVLanPacketType
199         : public PacketTypeBase, 
200           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
201     {
202         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
203         typedef ConcretePacket<EthVLanPacketType> packet;
204         typedef Parse_EthVLan parser;
205
206         using mixin::nextPacketRange;
207         using mixin::nextPacketType;
208         using mixin::initSize;
209         using mixin::init;
210
211         /** \todo Add LLC/SNAP support -> only use the registry
212             for type() values >=1536, otherwise expect an LLC header */
213         static registry_key_t nextPacketKey(packet p) 
214             { return p->type(); }
215
216         static void dump(packet p, std::ostream & os);
217     };
218
219     /** \brief Ethernet VLAN tag typedef */
220     typedef EthVLanPacketType::packet EthVLanPacket;
221 }
222
223
224 ///////////////////////////////hh.e////////////////////////////////////////
225 #endif
226 #ifndef SENF_PACKETS_DECL_ONLY
227 //#include "EthernetPacket.cci"
228 #include "EthernetPacket.ct"
229 //#include "EthernetPacket.cti"
230 #endif
231
232 \f
233 // Local Variables:
234 // mode: c++
235 // fill-column: 100
236 // c-file-style: "senf"
237 // indent-tabs-mode: nil
238 // ispell-local-dictionary: "american"
239 // compile-command: "scons -u test"
240 // comment-column: 40
241 // End: