Utils: Implement IpChecksum helper class
[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         static void finalize(packet p);
136     };
137
138     /** \brief Ethernet packet typedef */
139     typedef EthernetPacketType::packet EthernetPacket;
140
141     /** \brief Parse an ethernet VLAN tag
142         
143         Parser interpreting the ethernet VLAN tag. Fields are
144
145         \see EthVLanPacketType
146      */
147     struct Parse_EthVLan : public PacketParserBase
148     {
149         typedef Parse_UIntField < 0,  3 > Parse_Priority;
150         typedef Parse_Flag          < 3 > Parse_CFI;
151         typedef Parse_UIntField < 4, 16 > Parse_VLanId;
152         typedef Parse_UInt16              Parse_Type;
153
154 #       ifndef DOXYGEN
155
156         SENF_PACKET_PARSER_INIT(Parse_EthVLan);
157
158         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
159             ((OverlayField)( priority, Parse_Priority ))
160             ((OverlayField)( cfi,      Parse_CFI      ))
161             ((Field       )( vlanId,   Parse_VLanId   ))
162             ((Field       )( type,     Parse_Type     )) );
163
164 #       else
165
166         Parse_Priority priority();
167         Parse_CFI cfi();
168         Parse_VLanId vlanId();
169         Parse_Type type();
170
171 #       endif
172     };
173
174     /** \brief Ethernet VLAN tag
175
176         \par Packet type (typedef):
177             \ref EthVLanPacket
178
179         \par Fields:
180             \ref Parse_EthVLan
181
182         \par Associated registries:
183             \ref EtherTypes
184
185         \ingroup protocolbundle_default
186      */
187     struct EthVLanPacketType
188         : public PacketTypeBase, 
189           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
190     {
191         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
192         typedef ConcretePacket<EthVLanPacketType> packet;
193         typedef Parse_EthVLan parser;
194
195         using mixin::nextPacketRange;
196         using mixin::nextPacketType;
197         using mixin::initSize;
198         using mixin::init;
199
200         /** \todo Add LLC/SNAP support -> only use the registry
201             for type() values >=1536, otherwise expect an LLC header */
202         static registry_key_t nextPacketKey(packet p) 
203             { return p->type(); }
204
205         static void dump(packet p, std::ostream & os);
206         static void finalize(packet p);
207     };
208
209     /** \brief Ethernet VLAN tag typedef */
210     typedef EthVLanPacketType::packet EthVLanPacket;
211 }
212
213
214 ///////////////////////////////hh.e////////////////////////////////////////
215 #endif
216 #ifndef SENF_PACKETS_DECL_ONLY
217 //#include "EthernetPacket.cci"
218 //#include "EthernetPacket.ct"
219 //#include "EthernetPacket.cti"
220 #endif
221
222 \f
223 // Local Variables:
224 // mode: c++
225 // fill-column: 100
226 // c-file-style: "senf"
227 // indent-tabs-mode: nil
228 // ispell-local-dictionary: "american"
229 // compile-command: "scons -u test"
230 // comment-column: 40
231 // End: