7c6d84a5c0528680cd4b9ba77b7554cde41dcaea
[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     memset(&table, 0, sizeof(table));
43     struct ieee80211_radiotap_iterator iter;
44     ieee80211_radiotap_iterator_init(&iter,
45                                      (struct ieee80211_radiotap_header *)data,
46                                      maxLength,
47                                      0);
48     while (ieee80211_radiotap_iterator_next(&iter)==0) {
49         if (iter.is_radiotap_ns &&
50             iter.this_arg_index <= int(senf::RadiotapPacketParser::MAX_INDEX))
51             table[iter.this_arg_index] = iter.this_arg - data;
52     }
53     table[MAX_INDEX+1] = iter.this_arg - data + iter.this_arg_size;
54 }
55
56 prefix_ senf::RadiotapPacketParser::OffsetTable const &
57 senf::RadiotapPacketParser::offsetTable(boost::uint32_t presentFlags)
58 {
59     typedef std::map<boost::uint32_t, OffsetTable> OffsetMap;
60     static OffsetMap offsetMap;
61
62     OffsetMap::iterator i (offsetMap.find(presentFlags));
63     if (i == offsetMap.end()) {
64         OffsetTable table;
65         fillOffsetTable(&(*data().begin()), data().size(), table);
66         i = offsetMap.insert(std::make_pair(presentFlags, table)).first;
67     }
68     return i->second;
69 }
70
71
72 prefix_ void senf::RadiotapPacketType::dump(packet p, std::ostream &os)
73 {
74     boost::io::ios_all_saver ias(os);
75     os << "Radiotap:\n"
76        << senf::fieldName("version") << unsigned(p->version()) << '\n'
77        << senf::fieldName("length")  << unsigned(p->length()) << '\n';
78
79 #   define DUMP_OPTIONAL_FIELD(name, sign, desc)                        \
80         if (p->name ## Present())                                       \
81             os << senf::fieldName(desc) << sign(p->name()) << '\n';
82
83     DUMP_OPTIONAL_FIELD( tsft, boost::uint64_t, "MAC timestamp" );
84
85     if (p->flagsPresent()) {
86         os << senf::fieldName("flags");
87
88 #       define DUMP_FLAG(name,desc) if (p->flags().name()) os << desc " "
89         DUMP_FLAG(shortGI,            "ShortGI");
90         DUMP_FLAG(badFCS,             "BadFCS");
91         DUMP_FLAG(fcsAtEnd,           "FCSatEnd");
92         DUMP_FLAG(fragmentation,      "Frag");
93         DUMP_FLAG(wep,                "WEP");
94         DUMP_FLAG(shortPreamble,      "ShortPreamble");
95         DUMP_FLAG(cfp,                "CFP");
96 #       undef DUMP_FLAG
97
98         os << '\n';
99     }
100
101     DUMP_OPTIONAL_FIELD( rate, unsigned, "rate" );
102
103     if (p->channelOptionsPresent()) {
104         os << senf::fieldName("channel frequency")
105            << unsigned(p->channelOptions().freq()) << '\n'
106            << senf::fieldName("channel flags");
107
108 #       define DUMP_FLAG(name,desc) if (p->channelOptions().name()) os << desc " "
109         DUMP_FLAG(flag2ghz,           "2GHz");
110         DUMP_FLAG(ofdm,               "OFDM");
111         DUMP_FLAG(cck,                "CCK");
112         DUMP_FLAG(turbo,              "Turbo");
113         DUMP_FLAG(quarterRateChannel, "Rate/4");
114         DUMP_FLAG(halfRateChannel,    "Rate/2");
115         DUMP_FLAG(gsm,                "GSM");
116         DUMP_FLAG(staticTurbo,        "StaticTurbo");
117         DUMP_FLAG(gfsk,               "GFSK");
118         DUMP_FLAG(cckOfdm,            "CCKOFDM");
119         DUMP_FLAG(passive,            "Passive");
120         DUMP_FLAG(flag5ghz,           "5GHz");
121 #       undef DUMP_FLAG
122
123         os << '\n';
124     }
125
126     DUMP_OPTIONAL_FIELD( fhss,              unsigned, "FHSS"                 );
127     DUMP_OPTIONAL_FIELD( dbmAntennaSignal,  signed,   "antenna signal (dBm)" );
128     DUMP_OPTIONAL_FIELD( dbmAntennaNoise,   signed,   "antenna noise (dBm)"  );
129     DUMP_OPTIONAL_FIELD( lockQuality,       unsigned, "lock quality"         );
130     DUMP_OPTIONAL_FIELD( txAttenuation,     unsigned, "tx attenuation"       );
131     DUMP_OPTIONAL_FIELD( dbTxAttenuation,   unsigned, "tx attenuation (dB)"  );
132     DUMP_OPTIONAL_FIELD( dbmTxAttenuation,  signed,   "tx attenuation (dBm)" );
133     DUMP_OPTIONAL_FIELD( antenna,           unsigned, "antenna"              );
134     DUMP_OPTIONAL_FIELD( dbAntennaSignal,   unsigned, "antenna signal (dB)"  );
135     DUMP_OPTIONAL_FIELD( dbAntennaNoise,    unsigned, "antenna noise (dB)"   );
136     DUMP_OPTIONAL_FIELD( headerFcs,         unsigned, "FCS (in header)"      );
137
138     if (p->flagsPresent() && p->flags().fcsAtEnd())
139         os << senf::fieldName("FCS (at end)") << unsigned(p->fcs()) << '\n';
140
141 #   undef DUMP_OPTIONAL_FIELD
142 }
143
144
145 prefix_ void senf::RadiotapPacketType::finalize(packet p)
146 {
147     ///\fixme Is this really correct ? shouldn't I use nextPacket.begin() - begin() here ?
148     p->length() << p->calculateSize();
149 }
150
151 prefix_ senf::PacketInterpreterBase::factory_t senf::RadiotapPacketType::nextPacketType(packet p)
152 {
153     static factory_t frameTypeFactory[] = { WLANPacket_MgtFrame::factory(),
154                                             WLANPacket_CtrlFrame::factory(),
155                                             WLANPacket_DataFrame::factory(),
156                                             no_factory() };
157     return frameTypeFactory[p->frameType()];
158 }
159
160 prefix_ senf::RadiotapPacketType::optional_range
161 senf::RadiotapPacketType::nextPacketRange(packet p)
162 {
163     size_type h (senf::bytes(p.parser()));
164     size_type t (p->flagsPresent() && p->flags().fcsAtEnd() ? 4 : 0);
165     return p.size() < h+t
166         ? no_range()
167         : optional_range( range(p.data().begin() + h, p.data().end() - t) );
168 }
169
170 ///////////////////////////////cc.e////////////////////////////////////////
171 #undef prefix_
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // comment-column: 40
182 // End: