Packets/80211Bundle: Read-only radiotap reimplementation
[senf.git] / senf / Packets / 80211Bundle / RadiotapPacket.cc
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 // Definition of RadiotapPacket non-inline non-template functions
24
25 #include "RadiotapPacket.hh"
26 //#include "RadiotapPacket.ih"
27
28 // Custom includes
29 #include "WLANPacket.hh"
30 #include <boost/io/ios_state.hpp>
31
32 extern "C" {
33 #   include "radiotap/radiotap_iter.h"
34 }
35
36 #define prefix_
37 ///////////////////////////////cc.p//////////////////////////////////////
38
39 prefix_ void senf::RadiotapPacketParser::fillOffsetTable(boost::uint8_t * data, int maxLength,
40                                                          OffsetTable & table)
41 {
42     struct ieee80211_radiotap_iterator iter;
43     ieee80211_radiotap_iterator_init(&iter,
44                                      (struct ieee80211_radiotap_header *)data,
45                                      maxLength,
46                                      0);
47     while (ieee80211_radiotap_iterator_next(&iter)==0) {
48         if (iter.is_radiotap_ns &&
49             iter.this_arg_index <= int(senf::RadiotapPacketParser::MAX_INDEX)) {
50             table[iter.this_arg_index] = iter.this_arg - data;
51             std::cerr << ">> " << iter.this_arg_index << " " << table[iter.this_arg_index] << "\n";
52         }
53     }
54     table[MAX_INDEX+1] = iter.this_arg - data + iter.this_arg_size;
55     std::cerr << ">> size " << table[MAX_INDEX+1] << "\n";
56 }
57
58 prefix_ senf::RadiotapPacketParser::OffsetTable const &
59 senf::RadiotapPacketParser::offsetTable(boost::uint32_t presentFlags)
60 {
61     typedef std::map<boost::uint32_t, OffsetTable> OffsetMap;
62     static OffsetMap offsetMap;
63
64     OffsetMap::iterator i (offsetMap.find(presentFlags));
65     if (i == offsetMap.end()) {
66         OffsetTable table;
67         fillOffsetTable(&(*data().begin()), data().size(), table);
68         i = offsetMap.insert(std::make_pair(presentFlags, table)).first;
69     }
70     return i->second;
71 }
72
73
74 #define DUMP_OPTIONAL_FIELD(name, sign, desc)                           \
75     if (p->name ## Present())                                           \
76         os << senf::fieldName(desc) << sign(p->name())                  \
77            << std::endl;
78
79 prefix_ void senf::RadiotapPacketType::dump(packet p, std::ostream &os)
80 {
81     boost::io::ios_all_saver ias(os);
82     os << "Radiotap:\n"
83        << senf::fieldName("version") << unsigned(p->version()) << "\n"
84        << senf::fieldName("length")  << unsigned(p->length()) << "\n";
85     // TODO: flags, channelOptions
86     DUMP_OPTIONAL_FIELD( tsft,              unsigned, "MAC timestamp"        );
87     DUMP_OPTIONAL_FIELD( rate,              unsigned, "rate"                 );
88     DUMP_OPTIONAL_FIELD( fhss,              unsigned, "FHSS"                 );
89     DUMP_OPTIONAL_FIELD( dbmAntennaSignal,  signed,   "antenna signal (dBm)" );
90     DUMP_OPTIONAL_FIELD( dbmAntennaNoise,   signed,   "antenna noise (dBm)"  );
91     DUMP_OPTIONAL_FIELD( lockQuality,       unsigned, "lock quality"         );
92     DUMP_OPTIONAL_FIELD( txAttenuation,     unsigned, "tx attenuation"       );
93     DUMP_OPTIONAL_FIELD( dbTxAttenuation,   unsigned, "tx attenuation (dB)"  );
94     DUMP_OPTIONAL_FIELD( dbmTxAttenuation,  signed,   "tx attenuation (dBm)" );
95     DUMP_OPTIONAL_FIELD( antenna,           unsigned, "antenna"              );
96     DUMP_OPTIONAL_FIELD( dbAntennaSignal,   unsigned, "antenna signal (dB)"  );
97     DUMP_OPTIONAL_FIELD( dbAntennaNoise,    unsigned, "antenna noise (dB)"   );
98     DUMP_OPTIONAL_FIELD( headerFcs,         unsigned, "FCS (in header)"      );
99     if (p->flagsPresent() && p->flags().fcsAtEnd())
100         os << senf::fieldName("FCS (at end)") << unsigned(p->fcs()) << "\n";
101 }
102
103 #undef DUMP_OPTIONAL_FIELD
104
105 prefix_ void senf::RadiotapPacketType::finalize(packet p)
106 {
107     p->length() << p->calculateSize();
108 }
109
110 prefix_ senf::PacketInterpreterBase::factory_t senf::RadiotapPacketType::nextPacketType(packet p)
111 {
112     static factory_t frameTypeFactory[] = { WLANPacket_MgtFrame::factory(),
113                                             WLANPacket_CtrlFrame::factory(),
114                                             WLANPacket_DataFrame::factory(),
115                                             no_factory() };
116     return frameTypeFactory[p->frameType()];
117 }
118
119 prefix_ senf::RadiotapPacketType::optional_range
120 senf::RadiotapPacketType::nextPacketRange(packet p)
121 {
122     size_type h (senf::bytes(p.parser()));
123     size_type t (p->flagsPresent() && p->flags().fcsAtEnd() ? 4 : 0);
124     return p.size() < h+t
125         ? no_range()
126         : optional_range( range(p.data().begin() + h, p.data().end() - t) );
127 }
128
129 ///////////////////////////////cc.e////////////////////////////////////////
130 #undef prefix_
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // comment-column: 40
141 // End: