3da21e008c7c77e2b8e7e8f2009c4a3a88a8bb2f
[senf.git] / Packets / DefaultBundle / IPv4Packet.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@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 IPv4Packet public header */
25
26 #ifndef HH_IPv4Packet_
27 #define HH_IPv4Packet_ 1
28
29 // Custom includes
30 #include "../../Socket/Protocols/INet/INet4Address.hh"
31 #include "../../Packets/Packets.hh"
32
33 //#include "IPv4Packet.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38     /** \brief Parse in IPv4 address
39
40         \see INet4Address
41      */
42     struct INet4AddressParser : public PacketParserBase
43     {
44         INet4AddressParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
45
46         ///////////////////////////////////////////////////////////////////////////
47
48         typedef INet4Address value_type;
49         static const size_type fixed_bytes = 4u;
50
51         value_type value() const { return value_type::from_data(i()); }
52         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
53         operator value_type() { return value(); }
54         byte & operator[](size_type index) { return *boost::next(i(),index); }
55         INet4AddressParser const & operator= (value_type const & other) 
56             { value(other); return *this; }
57     };
58
59     /** \brief Parse an IPv4 packet
60
61         Parser implementing the IPv4 header.
62         
63         \see IPv4PacketType \n
64             <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
65
66         \todo Implement options
67      */
68     struct IPv4PacketParser : public PacketParserBase
69     {
70 #       include SENF_FIXED_PARSER()
71
72         SENF_PARSER_BITFIELD( version,   4, unsigned );
73         SENF_PARSER_BITFIELD( ihl,       4, unsigned );
74
75         SENF_PARSER_FIELD( tos,         UInt8Parser        );
76         SENF_PARSER_FIELD( length,      UInt16Parser       );
77         SENF_PARSER_FIELD( identifier,  UInt16Parser       );
78
79         SENF_PARSER_BITFIELD( reserved,  1, bool     );
80         SENF_PARSER_BITFIELD( df,        1, bool     );
81         SENF_PARSER_BITFIELD( mf,        1, bool     );
82         SENF_PARSER_BITFIELD( frag,     13, unsigned );
83
84         SENF_PARSER_FIELD( ttl,         UInt8Parser        );
85         SENF_PARSER_FIELD( protocol,    UInt8Parser        );
86         SENF_PARSER_FIELD( checksum,    UInt16Parser       );
87         SENF_PARSER_FIELD( source,      INet4AddressParser );
88         SENF_PARSER_FIELD( destination, INet4AddressParser );
89
90         SENF_PARSER_INIT() {
91             version() = 4;
92             // We don't support option headers at the moment ...
93             ihl() = 5;
94         }
95
96         SENF_PARSER_FINALIZE(IPv4PacketParser);
97         
98         boost::uint16_t calcChecksum() const;
99
100         bool validateChecksum() const {
101             return checksum() == calcChecksum();
102         }
103     };
104
105     /** \brief IP protocol number registry
106
107         This registeres packets with their IP protocol number.
108
109         \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
110             PacketRegistry
111      */
112     struct IpTypes {
113         typedef boost::uint16_t key_t;
114     };
115
116     /** \brief IPv4 packet
117
118         <table class="packet" cellpadding="5" cellspacing="1" border="1">
119           <tr>
120             <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
121             <th width="12%">12</th> <th width="3%">16</th>
122             <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
123             <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
124             <th style="text-align:right" width="6%">31</th>
125           </tr><tr>
126             <td>\ref IPv4PacketParser::version() "Version"</td>
127             <td>\ref IPv4PacketParser::ihl() "IHL"</td>
128             <td colspan="2">\ref IPv4PacketParser::tos() "TOS"</td>
129             <td colspan="8">\ref IPv4PacketParser::length() "Length"</td> 
130           </tr><tr>
131             <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</td>
132             <td>\ref IPv4PacketParser::reserved() "R"</td>
133             <td>\ref IPv4PacketParser::df() "DF"</td>
134             <td>\ref IPv4PacketParser::mf() "MF"</td>
135             <td colspan="5">\ref IPv4PacketParser::frag() "Fragment Offset"</td>
136           </tr><tr>
137             <td colspan="2">\ref IPv4PacketParser::ttl() "Time to Live (ttl)"</td>
138             <td colspan="2">\ref IPv4PacketParser::protocol() "Protocol"</td>
139             <td colspan="8">\ref IPv4PacketParser::checksum() "Header Checksum"</td>
140           </tr><tr>
141             <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
142           </tr><tr>
143             <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
144           </tr>
145         </table>
146         
147         \par Packet type (typedef):
148             \ref IPv4Packet
149
150         \par Fields:
151             \ref IPv4PacketParser
152
153         \par Associated registries:
154             \ref IpTypes
155
156         \par Finalize action:
157             Set \a length from payload size\n
158             Set \a protocol from type of next packet if found in \ref IpTypes\n
159             Calculate \a checksum
160
161         \ingroup protocolbundle_default
162      */
163     struct IPv4PacketType
164         : public PacketTypeBase,
165           public PacketTypeMixin<IPv4PacketType, IpTypes>
166     {
167 #ifndef DOXYGEN
168         typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
169         typedef ConcretePacket<IPv4PacketType> packet;
170         typedef IPv4PacketParser parser;
171 #endif
172         using mixin::nextPacketRange;
173         using mixin::nextPacketType;
174         using mixin::initSize;
175         using mixin::init;
176
177         static key_t nextPacketKey(packet p) 
178             { return p->protocol(); }
179
180         static void dump(packet p, std::ostream & os);
181         static void finalize(packet p);
182     };
183         
184     /** \brief IPv4 packet typedef */
185     typedef ConcretePacket<IPv4PacketType> IPv4Packet;
186 }
187
188
189 ///////////////////////////////hh.e////////////////////////////////////////
190 #endif
191 #ifndef SENF_PACKETS_DECL_ONLY
192 //#include IPv4Packet.cci"
193 //#include "IPv4Packet.ct"
194 //#include "IPv4Packet.cti"
195 #endif
196
197 \f
198 // Local Variables:
199 // mode: c++
200 // fill-column: 100
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // comment-column: 40
206 // End: