switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / IPv4Packet.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief IPv4Packet public header */
30
31 #ifndef HH_SENF_Packets_DefaultBundle_IPv4Packet_
32 #define HH_SENF_Packets_DefaultBundle_IPv4Packet_ 1
33
34 // Custom includes
35 #include <senf/Socket/Protocols/INet/INet4Address.hh>
36 #include <senf/Packets/Packets.hh>
37
38 //#include "IPv4Packet.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace senf {
42
43     /** \brief Parse in IPv4 address
44
45         \see INet4Address
46      */
47     struct INet4AddressParser
48         : public ValueParserBase<INet4AddressParser, INet4Address, 4u>
49     {
50         INet4AddressParser(data_iterator i, state_type s) : Base(i,s) {}
51
52         value_type value() const { return value_type::from_data(i()); }
53         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
54
55         using Base::operator=;
56     };
57
58     /** \brief Parse an IPv4 packet
59
60         Parser implementing the IPv4 header.
61
62         \image html IPv4Packet.png
63
64         \see IPv4PacketType \n
65             <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
66
67         \todo Implement options
68      */
69     struct IPv4PacketParser : public PacketParserBase
70     {
71 #       include SENF_FIXED_PARSER()
72
73         SENF_PARSER_BITFIELD( version,   4, unsigned );
74         SENF_PARSER_BITFIELD( ihl,       4, unsigned );
75
76         SENF_PARSER_FIELD( tos,         UInt8Parser        );
77         SENF_PARSER_FIELD( length,      UInt16Parser       );
78         SENF_PARSER_FIELD( identifier,  UInt16Parser       );
79
80         SENF_PARSER_PRIVATE_BITFIELD( reserved,  1, bool     );
81         SENF_PARSER_BITFIELD        ( df,        1, bool     );
82         SENF_PARSER_BITFIELD        ( mf,        1, bool     );
83         SENF_PARSER_BITFIELD        ( frag,     13, unsigned );
84
85         SENF_PARSER_FIELD( ttl,         UInt8Parser        );
86         SENF_PARSER_FIELD( protocol,    UInt8Parser        );
87         SENF_PARSER_FIELD( checksum,    UInt16Parser       );
88         SENF_PARSER_FIELD( source,      INet4AddressParser );
89         SENF_PARSER_FIELD( destination, INet4AddressParser );
90
91         SENF_PARSER_INIT() {
92             version() = 4;
93             // We don't support option headers at the moment ...
94             ihl() = 5;
95         }
96
97         SENF_PARSER_FINALIZE(IPv4PacketParser);
98
99         boost::uint16_t calcChecksum() const; ///< calculate header checksum
100                                               /**< calculate and return the checksum of the header
101                                                    \see \ref senf::IpChecksum */
102
103         bool validateChecksum() const {
104             return checksum() == calcChecksum();
105         }                               ///< validate header checksum
106                                         /**< return \c true if the \ref checksum() "checksum"
107                                              field is equal to the \ref calcChecksum()
108                                              "calculated checksum" */
109     };
110
111     /** \brief IP protocol number registry
112
113         This registeres packets with their IP protocol number.
114
115         \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
116             PacketRegistry
117      */
118     struct IpTypes {
119         typedef boost::uint8_t key_t;
120     };
121
122     /** \brief IPv4 packet
123
124         \par Packet type (typedef):
125             \ref IPv4Packet
126
127         \par Fields:
128             see \ref IPv4PacketParser
129
130         <table class="packet" cellpadding="5" cellspacing="1" border="1">
131           <tr>
132             <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
133             <th width="12%">12</th> <th width="3%">16</th>
134             <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
135             <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
136             <th style="text-align:right" width="6%">31</th>
137           </tr><tr>
138             <td>\ref IPv4PacketParser::version() "Version"</td>
139             <td>\ref IPv4PacketParser::ihl() "IHL"</td>
140             <td colspan="2">\ref IPv4PacketParser::tos() "TOS"</td>
141             <td colspan="8">\ref IPv4PacketParser::length() "Length"</td>
142           </tr><tr>
143             <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</td>
144             <td>R</td>
145             <td>\ref IPv4PacketParser::df() "DF"</td>
146             <td>\ref IPv4PacketParser::mf() "MF"</td>
147             <td colspan="5">\ref IPv4PacketParser::frag() "Fragment Offset"</td>
148           </tr><tr>
149             <td colspan="2">\ref IPv4PacketParser::ttl() "Time to Live (ttl)"</td>
150             <td colspan="2">\ref IPv4PacketParser::protocol() "Protocol"</td>
151             <td colspan="8">\ref IPv4PacketParser::checksum() "Header Checksum"</td>
152           </tr><tr>
153             <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
154           </tr><tr>
155             <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
156           </tr>
157         </table>
158
159         \par Associated registries:
160             \ref IpTypes
161
162         \par Finalize action:
163             \copydetails finalize()
164
165         \ingroup protocolbundle_default
166      */
167     struct IPv4PacketType
168         : public PacketTypeBase,
169           public PacketTypeMixin<IPv4PacketType, IpTypes>
170     {
171 #ifndef DOXYGEN
172         typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
173 #endif
174         typedef ConcretePacket<IPv4PacketType> packet;  ///< IPv4 packet typedef
175         typedef IPv4PacketParser parser;                ///< typedef to the parser of IPv4 packet
176
177         using mixin::nextPacketRange;
178         using mixin::nextPacketType;
179         using mixin::initSize;
180         using mixin::init;
181
182         static key_t nextPacketKey(packet p)
183             { return p->protocol(); }
184
185         /** \brief Dump given IPv4Packet in readable form to given output stream */
186         static void dump(packet p, std::ostream & os);
187
188         static void finalize(packet p); ///< Finalize packet.
189                                         /**< \li set \ref IPv4PacketParser::length() "length"
190                                                from payload size
191                                              \li set \ref IPv4PacketParser::protocol() "protocol"
192                                                from type of next packet if found in \ref IpTypes
193                                              \li calculate and set
194                                                \ref IPv4PacketParser::checksum() "checksum" */
195     };
196
197     /** \brief IPv4 packet typedef
198         \ingroup protocolbundle_default
199      */
200     typedef ConcretePacket<IPv4PacketType> IPv4Packet;
201 }
202
203
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 //#include "IPv4Packet.cci"
206 //#include "IPv4Packet.ct"
207 //#include "IPv4Packet.cti"
208 #endif
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // comment-column: 40
219 // End: