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