Packets: Add packet diagrams
[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_SENF_Packets_DefaultBundle_IPv4Packet_
27 #define HH_SENF_Packets_DefaultBundle_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() const { return value(); }
54         byte & operator[](size_type index) const { 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         \image html IPv4Packet.png
162
163         \ingroup protocolbundle_default
164      */
165     struct IPv4PacketType
166         : public PacketTypeBase,
167           public PacketTypeMixin<IPv4PacketType, IpTypes>
168     {
169 #ifndef DOXYGEN
170         typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
171         typedef ConcretePacket<IPv4PacketType> packet;
172         typedef IPv4PacketParser parser;
173 #endif
174         using mixin::nextPacketRange;
175         using mixin::nextPacketType;
176         using mixin::initSize;
177         using mixin::init;
178
179         static key_t nextPacketKey(packet p) 
180             { return p->protocol(); }
181
182         static void dump(packet p, std::ostream & os);
183         static void finalize(packet p);
184     };
185         
186     /** \brief IPv4 packet typedef */
187     typedef ConcretePacket<IPv4PacketType> IPv4Packet;
188 }
189
190
191 ///////////////////////////////hh.e////////////////////////////////////////
192 #endif
193 #ifndef SENF_PACKETS_DECL_ONLY
194 //#include IPv4Packet.cci"
195 //#include "IPv4Packet.ct"
196 //#include "IPv4Packet.cti"
197 #endif
198
199 \f
200 // Local Variables:
201 // mode: c++
202 // fill-column: 100
203 // c-file-style: "senf"
204 // indent-tabs-mode: nil
205 // ispell-local-dictionary: "american"
206 // compile-command: "scons -u test"
207 // comment-column: 40
208 // End: