several fixes for clang/llvm
[senf.git] / senf / Packets / 80211Bundle / WLANPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Christian Niephaus <cni@berlios.de>
27
28 /** \file
29     \brief 802.11 WLANPacket public header */
30
31 #ifndef HH_SENF_Packets_80211Bundle_WLANPacket_
32 #define HH_SENF_Packets_80211Bundle_WLANPacket_ 1
33
34 #include <senf/Packets/Packets.hh>
35 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
36 #include <senf/Packets/DefaultBundle/LlcSnapPacket.hh>
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace senf
41 {
42
43     /** \brief 802.11 Frame parser
44         (see IEEE 802.11-2007 standard - Chapter 7 Frame formats)
45         <b>Re-ordering of bits due to host byte order</b>
46
47         Frame base class.
48      */
49     struct WLANPacketParser : public PacketParserBase
50     {
51     #   include SENF_PARSER()
52
53         /*
54          * Frame control field
55          * re-ordering of fields due to the byte order
56          */
57         SENF_PARSER_BITFIELD_RO ( subtype,        4,  unsigned );
58         SENF_PARSER_BITFIELD_RO ( type,           2,  unsigned );
59         SENF_PARSER_BITFIELD    ( version,        2,  unsigned );
60         SENF_PARSER_BITFIELD    ( order,          1,  bool     );
61         SENF_PARSER_BITFIELD    ( protectedFrame, 1,  bool     );
62         SENF_PARSER_BITFIELD    ( moreData,       1,  bool     );
63         SENF_PARSER_BITFIELD    ( pwrMgt,         1,  bool     );
64         SENF_PARSER_BITFIELD    ( retry,          1,  bool     );
65         SENF_PARSER_BITFIELD    ( moreFrag,       1,  bool     );
66         SENF_PARSER_BITFIELD    ( fromDS,         1,  bool     );
67         SENF_PARSER_BITFIELD    ( toDS,           1,  bool     );
68
69         SENF_PARSER_FIELD       ( duration,       UInt16LSBParser );
70
71         SENF_PARSER_FINALIZE(WLANPacketParser);
72     };
73
74     //-////////////////////////////////////////////////////////////////////////
75
76     /** \brief Management frame parser
77         <b>Re-ordering of bits due to LSB byte order</b>
78      */
79     struct WLANPacket_MgtFrameParser : public WLANPacketParser
80     {
81     #   include SENF_PARSER()
82
83         SENF_PARSER_INHERIT(WLANPacketParser);
84
85         SENF_PARSER_FIELD            ( destinationAddress, MACAddressParser );
86         SENF_PARSER_FIELD            ( sourceAddress,      MACAddressParser );
87         SENF_PARSER_FIELD            ( bssid,              MACAddressParser );
88
89         //workaround since bitfield LSB parsers are not available
90         SENF_PARSER_PRIVATE_BITFIELD ( seqNumber_1,    4, unsigned );
91         SENF_PARSER_BITFIELD         ( fragmentNumber, 4, unsigned );
92         SENF_PARSER_PRIVATE_FIELD    ( seqNumber_2,    UInt8Parser );
93
94         SENF_PARSER_INIT() { type_() = 0; }
95
96         SENF_PARSER_FINALIZE(WLANPacket_MgtFrameParser);
97
98         boost::uint16_t sequenceNumber() const {
99             return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
100         };
101
102         void sequenceNumber(boost::uint16_t sn);
103     };
104
105     /** \brief WLAN Management frame packet
106
107         \par Packet type (typedef):
108             \ref WLANPacket_MgtFrame
109
110         \par Fields:
111             \ref WLANPacket_MgtFrameParser
112             \image html WLANPacket_MgtFrame.png
113
114         \ingroup protocolbundle_80211
115      */
116     struct WLANPacket_MgtFrameType
117         : public PacketTypeBase,
118           public PacketTypeMixin<WLANPacket_MgtFrameType>
119     {
120         typedef PacketTypeMixin<WLANPacket_MgtFrameType> mixin;
121         typedef ConcretePacket<WLANPacket_MgtFrameType> packet;
122         typedef WLANPacket_MgtFrameParser parser;
123
124         using mixin::init;
125         using mixin::initSize;
126         using PacketTypeBase::nextPacketRange;
127
128         static void dump(packet p, std::ostream & os);
129     };
130
131     /** \brief WLAN Management frame packet typedef
132         \ingroup protocolbundle_80211
133      */
134     typedef WLANPacket_MgtFrameType::packet WLANPacket_MgtFrame;
135
136     //-////////////////////////////////////////////////////////////////////////
137
138     /** \brief Control frame parser
139         <b>Re-ordering of bits due to LSB byte order</b>
140
141         currently only CTS, RTS and ACK control frames are supported
142      */
143     struct WLANPacket_CtrlFrameParser : public WLANPacketParser
144     {
145     #   include SENF_PARSER()
146
147         SENF_PARSER_INHERIT(WLANPacketParser);
148
149         SENF_PARSER_FIELD   ( receiverAddress, MACAddressParser );
150
151         //only RTS frame contains a source address field
152         //variant is also needed to set correct subtype value
153         SENF_PARSER_VARIANT ( subtype__, subtype,
154                 ( ids( na,            is_cts, set_cts, key(12, VoidPacketParser)) )
155                 ( ids( na,            is_ack, set_ack, key(13, VoidPacketParser)) )
156                 ( ids( sourceAddress, is_rts, set_rts, key(11, MACAddressParser)) ) );
157
158         SENF_PARSER_INIT() { type_() = 1; }
159
160         SENF_PARSER_FINALIZE(WLANPacket_CtrlFrameParser);
161     };
162
163     /** \brief WLAN Control frame packet
164
165         \par Packet type (typedef):
166             \ref WLANPacket_CtrlFrame
167
168         \par Fields:
169             \ref WLANPacket_CtrlFrameParser
170             \image html WLANPacket_CtrlFrame.png
171
172         \ingroup protocolbundle_80211
173      */
174     struct WLANPacket_CtrlFrameType
175         : public PacketTypeBase,
176           public PacketTypeMixin<WLANPacket_CtrlFrameType>
177     {
178         typedef PacketTypeMixin<WLANPacket_CtrlFrameType> mixin;
179         typedef ConcretePacket<WLANPacket_CtrlFrameType> packet;
180         typedef WLANPacket_CtrlFrameParser parser;
181
182         using mixin::init;
183         using mixin::initSize;
184         using PacketTypeBase::nextPacketRange;
185
186         static void dump(packet p, std::ostream & os);
187     };
188
189     /** \brief WLAN Control frame packet typedef
190         \ingroup protocolbundle_80211
191      */
192     typedef WLANPacket_CtrlFrameType::packet WLANPacket_CtrlFrame;
193
194     //-////////////////////////////////////////////////////////////////////////
195
196     /** \brief Data frame parser
197         <b>Re-ordering of bits due to LSB byte order</b>
198      */
199     struct WLANPacket_DataFrameParser : public WLANPacketParser
200     {
201     #   include SENF_PARSER()
202
203         SENF_PARSER_INHERIT(WLANPacketParser);
204
205     protected:
206         typedef UIntFieldParser<6, 6+2> dsBits_t;
207         dsBits_t::value_type dsBits() const { return parse<dsBits_t>( 1); }
208
209         MACAddressParser addr1() const { return parse<MACAddressParser>(  4); }
210         MACAddressParser addr2() const { return parse<MACAddressParser>( 10); }
211         MACAddressParser addr3() const { return parse<MACAddressParser>( 16); }
212
213         //sequence Number and fragment number
214         //shift bits manually due to LSB
215
216         typedef UIntFieldParser<0, 0+4> seqNumber_1_t;
217         seqNumber_1_t seqNumber_1() const { return parse<seqNumber_1_t>( 22); }
218
219     public:
220         typedef UIntFieldParser<4, 4+4> fragmentNumber_t;
221         fragmentNumber_t fragmentNumber() const { return parse<fragmentNumber_t>( 22); }
222
223     protected:
224         UInt8Parser seqNumber_2() const { return parse<UInt8Parser>( 23); }
225
226     public:
227         boost::uint16_t sequenceNumber() const {
228             return (uint16_t)(seqNumber_2()) << 4 | seqNumber_1();
229         };
230
231         void sequenceNumber(boost::uint16_t sn);
232
233         SENF_PARSER_GOTO_OFFSET( 24, 24);
234
235         // TODO fourth address field in case of WDS
236         // SENF_PARSER_PRIVATE_VARIANT (wds_, dsBits,
237         //     ( novalue ( disable_addr4,               VoidPacketParser ))
238         //     ( id      ( addr4,  key (3,              MACAddressParser ))) );
239
240         //QoS Field
241         SENF_PARSER_VARIANT ( qosField_, subtype,
242                 ( ids( na,       na,           set_data,        key(0, VoidPacketParser)) )
243                 ( ids( na,       na,           set_nullData,    key(4, VoidPacketParser)) )
244                 ( ids( qosField, has_qosField, set_qosData,     key(8, UInt16LSBParser )) )
245                 //we cannot parse qos Null (data) frames at the moment
246                 ( ids( na,       na,           set_qosNullData, key(12, UInt16LSBParser)) ) );
247
248         SENF_PARSER_INIT() { type_() = 2; }
249
250         SENF_PARSER_FINALIZE(WLANPacket_DataFrameParser);
251
252         MACAddressParser receiverAddress() const    { return addr1(); }; //ra is always addr1
253         MACAddressParser transmitterAddress() const { return addr2(); }; //ta is always addr2
254         MACAddressParser sourceAddress() const;
255         MACAddressParser destinationAddress() const;
256         MACAddressParser bssid() const;
257
258         friend struct WLANPacket_DataFrameType;
259     };
260
261     /** \brief WLAN Data frame packet
262
263         \par Packet type (typedef):
264             \ref WLANPacket_DataFrame
265
266         \par Fields:
267             \ref WLANPacket_DataFrameParser
268             \image html WLANPacket_DataFrame.png
269
270         \ingroup protocolbundle_80211
271      */
272     struct WLANPacket_DataFrameType
273         : public PacketTypeBase,
274           public PacketTypeMixin<WLANPacket_DataFrameType>
275     {
276         typedef PacketTypeMixin<WLANPacket_DataFrameType> mixin;
277         typedef ConcretePacket<WLANPacket_DataFrameType> packet;
278         typedef WLANPacket_DataFrameParser parser;
279
280         using mixin::init;
281         using mixin::initSize;
282         using mixin::nextPacketRange;
283
284         static factory_t nextPacketType(packet p) {
285             return p->subtype() == 0 || p->subtype() == 8
286                 ? LlcSnapPacket::factory()
287                 : no_factory();
288         }
289
290         static void dump(packet p, std::ostream & os);
291     };
292
293     /** \brief WLAN Data frame packet typedef
294         \ingroup protocolbundle_80211
295      */
296     typedef WLANPacket_DataFrameType::packet WLANPacket_DataFrame;
297 }
298
299 //-/////////////////////////////////////////////////////////////////////////////////////////////////
300 //#include "WLANPacket.cci"
301 //#include "WLANPacket.ct"
302 //#include "WLANPacket.cti"
303 #endif
304
305 \f
306 // Local Variables:
307 // mode: c++
308 // fill-column: 100
309 // c-file-style: "senf"
310 // indent-tabs-mode: nil
311 // ispell-local-dictionary: "american"
312 // compile-command: "scons -u test"
313 // comment-column: 40
314 // End: