Packets/80211Bundle: fix problems with dynamic padding due to byte alignment in Radio...
[senf.git] / Packets / 80211Bundle / RadiotapPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Christian Niephaus <cni@berlios.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 Radiotap header \n
25         <b>Radiotap uses least significant bit byte order</b>
26     */
27
28 #ifndef HH_SENF_Packets_80211Bundle_RadiotapPacket_
29 #define HH_SENF_Packets_80211Bundle_RadiotapPacket_ 1
30
31 #include "../../Packets/Packets.hh"
32
33 namespace senf
34 {
35
36     /** \brief Parse Flag field in Radiotap header
37      * <b>Re-ordering of bits due to LSB byte order</b>
38      * (see http://www.radiotap.org/)
39
40      */
41     struct RadiotapPacketParser_Flags : public senf::PacketParserBase
42     {
43     #   include SENF_FIXED_PARSER()
44
45         SENF_PARSER_BITFIELD ( shortGI,        1, bool );
46         SENF_PARSER_BITFIELD ( badFCS,         1, bool );
47         SENF_PARSER_BITFIELD ( padding,        1, bool );
48         SENF_PARSER_BITFIELD ( fcsPresent,     1, bool );
49         SENF_PARSER_BITFIELD ( fragmentation,  1, bool );
50         SENF_PARSER_BITFIELD ( wep,            1, bool );
51         SENF_PARSER_BITFIELD ( shortPreamble,  1, bool );
52         SENF_PARSER_BITFIELD ( cfp,            1, bool );
53
54         SENF_PARSER_FINALIZE ( RadiotapPacketParser_Flags );
55     };
56
57     /** \brief Parse in Radiotap Header channel frequency and flag field
58
59         <b>Re-ordering of bits due to LSB byte order</b>
60      */
61     struct RadiotapPacketParser_ChannelOptions : public senf::PacketParserBase
62     {
63     #   include SENF_FIXED_PARSER()
64
65         SENF_PARSER_FIELD     ( freq,          UInt16LSBParser );
66
67         SENF_PARSER_BITFIELD  ( flag2ghz,             1, bool  );
68         SENF_PARSER_BITFIELD  ( ofdm,                 1, bool  );
69         SENF_PARSER_BITFIELD  ( cck,                  1, bool  );
70         SENF_PARSER_BITFIELD  ( turbo,                1, bool  );
71         SENF_PARSER_SKIP_BITS ( 4                              ); //currently unused in radiotap
72         SENF_PARSER_BITFIELD  ( quarterRateChannel,   1, bool  );
73         SENF_PARSER_BITFIELD  ( halfRateChannel,      1, bool  );
74         SENF_PARSER_BITFIELD  ( gsm,                  1, bool  );
75         SENF_PARSER_BITFIELD  ( staticTurbo,          1, bool  );
76         SENF_PARSER_BITFIELD  ( gfsk,                 1, bool  );
77         SENF_PARSER_BITFIELD  ( cckOfdm,              1, bool  );
78         SENF_PARSER_BITFIELD  ( passive,              1, bool  );
79         SENF_PARSER_BITFIELD  ( flag5ghz,             1, bool  );
80
81         SENF_PARSER_FINALIZE ( RadiotapPacketParser_ChannelOptions );
82     };
83
84     /** \brief Parse an Radiotap header
85
86         Parser implementing the Radiotap header
87
88         Radiotap requires that all fields in the radiotap header
89         are aligned to natural boundaries. For radiotap,
90         that means all 8-, 16-, 32-, and 64-bit fields
91         must begin on 8-, 16-, 32-, and 64-bit boundaries, respectively.
92         In this way, generators and parsers can avoid unaligned
93         accesses to radiotap capture fields. Radiotap-compliant
94         generators must insert padding before a capture field
95         to ensure its natural alignment.
96
97         \see <a href="http://www.radiotap.org">Radiotap.org</a>
98
99         \todo extended present field (bit 31 of present field is set)
100     */
101     struct RadiotapPacketParser : public senf::PacketParserBase
102     {
103     #   include SENF_PARSER()
104
105         /*
106          * mandatory fields
107          */
108         SENF_PARSER_FIELD ( version, UInt8Parser     );
109         //padding bits, currently unused, it simply aligns the fields onto natural word boundaries.
110         SENF_PARSER_SKIP  ( 1,1                      );
111         SENF_PARSER_FIELD ( length,  UInt16LSBParser );
112
113         /*
114          * present flags
115          * indicate which data field are contained in the packet
116          */
117         SENF_PARSER_BITFIELD_RO ( lockQualityPresent,      1, bool );
118         SENF_PARSER_BITFIELD_RO ( dbmAntennaNoisePresent,  1, bool );
119         SENF_PARSER_BITFIELD_RO ( dbmAntennaSignalPresent, 1, bool );
120         SENF_PARSER_BITFIELD_RO ( fhssPresent,             1, bool );
121         SENF_PARSER_BITFIELD_RO ( channelOptionsPresent,   1, bool );
122         SENF_PARSER_BITFIELD_RO ( ratePresent,             1, bool );
123         SENF_PARSER_BITFIELD_RO ( flagsPresent,            1, bool );
124         SENF_PARSER_BITFIELD_RO ( tsftPresent,             1, bool );
125         SENF_PARSER_SKIP_BITS   ( 1                                ); //currently unused bits
126         SENF_PARSER_BITFIELD_RO ( fcsPresent,              1, bool );
127         SENF_PARSER_BITFIELD_RO ( dbAntennaNoisePresent,   1, bool );
128         SENF_PARSER_BITFIELD_RO ( dbAntennaSignalPresent,  1, bool );
129         SENF_PARSER_BITFIELD_RO ( antennaPresent,          1, bool );
130         SENF_PARSER_BITFIELD_RO ( dbmTxAttenuationPresent, 1, bool );
131         SENF_PARSER_BITFIELD_RO ( dbTxAttenuationPresent,  1, bool );
132         SENF_PARSER_BITFIELD_RO ( txAttenuationPresent,    1, bool );
133         SENF_PARSER_SKIP_BITS   ( 8                                ); //currently unused bits
134         //if bit is set,another 32 bit present flag is attached (not implemented yet)
135         SENF_PARSER_BITFIELD    ( extendedBitmaskPresent,  1, bool );
136         SENF_PARSER_SKIP_BITS   ( 7                                ); //currently unused bits
137
138         /*
139          * Radiotap data
140          * parsing data according to present flags
141          *
142          * PARSER_SKIP required to skip correct length of padding bits
143          */
144
145         /* macro to create required variant parser */
146         #define OPTIONAL_FIELD(name, parser) SENF_PARSER_VARIANT \
147             ( name##_, name##Present, \
148               ( novalue( disable_##name, VoidPacketParser )) \
149               (      id( name,           parser           )) );
150
151         /*  */
152         #define SKIP_OPTIONAL_PADDING(cond, parser, size) \
153             SENF_PARSER_SKIP( \
154                     (cond ? (size - (parser##__offset() + \
155                             senf::bytes(parser##_())) % size) % size : 0) , 0  );
156
157         OPTIONAL_FIELD ( tsft,             UInt64LSBParser                     );
158         OPTIONAL_FIELD ( flags,            RadiotapPacketParser_Flags          );
159         OPTIONAL_FIELD ( rate,             UInt8Parser                         );
160         SKIP_OPTIONAL_PADDING(channelOptionsPresent(), rate, 2);
161         OPTIONAL_FIELD ( channelOptions,   RadiotapPacketParser_ChannelOptions ) ;
162         SKIP_OPTIONAL_PADDING(fhssPresent(), channelOptions, 2);
163         OPTIONAL_FIELD ( fhss,             UInt16LSBParser                     );
164         OPTIONAL_FIELD ( dbmAntennaSignal, Int8Parser                          );
165         OPTIONAL_FIELD ( dbmAntennaNoise,  Int8Parser                          );
166         SKIP_OPTIONAL_PADDING(lockQualityPresent(), dbmAntennaNoise, 2);
167         OPTIONAL_FIELD ( lockQuality,             UInt16LSBParser                     );
168         SKIP_OPTIONAL_PADDING(txAttenuationPresent(), lockQuality, 2);
169         OPTIONAL_FIELD ( txAttenuation,    UInt16LSBParser                     );
170         SKIP_OPTIONAL_PADDING(dbTxAttenuationPresent(), txAttenuation, 2);
171         OPTIONAL_FIELD ( dbTxAttenuation,  UInt16LSBParser                     );
172         OPTIONAL_FIELD ( dbmTxAttenuation, Int8Parser                     );
173         OPTIONAL_FIELD ( antenna,          UInt8Parser                         );
174         OPTIONAL_FIELD ( dbAntennaSignal,  UInt8Parser                         );
175         OPTIONAL_FIELD ( dbAntennaNoise,   UInt8Parser                         );
176         SKIP_OPTIONAL_PADDING(fcsPresent(), dbAntennaNoise, 4)
177         OPTIONAL_FIELD ( fcs,              UInt32Parser                        );
178
179         SENF_PARSER_INIT() {
180             version() = 0;
181         }
182
183         SENF_PARSER_FINALIZE(RadiotapPacketParser);
184     };
185
186     /** \brief Radiotap packet
187
188         \par Packet type (typedef):
189             \ref RadiotapPacket
190
191         \par Fields:
192             \ref RadiotapPacketParser
193
194         \ingroup protocolbundle_80211
195      */
196     struct RadiotapPacketType
197         : public senf::PacketTypeBase,
198           public senf::PacketTypeMixin<RadiotapPacketType>
199     {
200         typedef senf::PacketTypeMixin<RadiotapPacketType> mixin;
201         typedef senf::ConcretePacket<RadiotapPacketType> packet;
202         typedef senf::RadiotapPacketParser parser;
203
204         using mixin::nextPacketRange;
205         using mixin::init;
206         using mixin::initSize;
207
208         static void dump(packet p, std::ostream &os);
209         static void finalize(packet p);
210         static factory_t nextPacketType(packet p);
211
212     };
213
214     typedef senf::ConcretePacket<RadiotapPacketType> RadiotapPacket;
215 }
216
217 #endif