Fix SCons 1.2.0 build failure
[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         \image html IPv4Packet.png
64                 
65         \see IPv4PacketType \n
66             <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
67
68         \todo Implement options
69      */
70     struct IPv4PacketParser : public PacketParserBase
71     {
72 #       include SENF_FIXED_PARSER()
73
74         SENF_PARSER_BITFIELD( version,   4, unsigned );
75         SENF_PARSER_BITFIELD( ihl,       4, unsigned );
76
77         SENF_PARSER_FIELD( tos,         UInt8Parser        );
78         SENF_PARSER_FIELD( length,      UInt16Parser       );
79         SENF_PARSER_FIELD( identifier,  UInt16Parser       );
80
81         SENF_PARSER_PRIVATE_BITFIELD( reserved,  1, bool     );
82         SENF_PARSER_BITFIELD        ( df,        1, bool     );
83         SENF_PARSER_BITFIELD        ( mf,        1, bool     );
84         SENF_PARSER_BITFIELD        ( frag,     13, unsigned );
85
86         SENF_PARSER_FIELD( ttl,         UInt8Parser        );
87         SENF_PARSER_FIELD( protocol,    UInt8Parser        );
88         SENF_PARSER_FIELD( checksum,    UInt16Parser       );
89         SENF_PARSER_FIELD( source,      INet4AddressParser );
90         SENF_PARSER_FIELD( destination, INet4AddressParser );
91
92         SENF_PARSER_INIT() {
93             version() = 4;
94             // We don't support option headers at the moment ...
95             ihl() = 5;
96         }
97
98         SENF_PARSER_FINALIZE(IPv4PacketParser);
99         
100         boost::uint16_t calcChecksum() const; ///< calculate header checksum
101                                               /**< calculate and return the checksum of the header
102                                                    \see \ref senf::IpChecksum */
103
104         bool validateChecksum() const {
105             return checksum() == calcChecksum();
106         }                               ///< validate header checksum
107                                         /**< return \c true if the \ref checksum() "checksum" 
108                                              field is equal to the \ref calcChecksum() 
109                                              "calculated checksum" */
110     };
111
112     /** \brief IP protocol number registry
113
114         This registeres packets with their IP protocol number.
115
116         \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
117             PacketRegistry
118      */
119     struct IpTypes {
120         typedef boost::uint16_t key_t;
121     };
122
123     /** \brief IPv4 packet
124         
125         \par Packet type (typedef):
126             \ref IPv4Packet
127
128         \par Fields:
129             see \ref IPv4PacketParser
130
131         <table class="packet" cellpadding="5" cellspacing="1" border="1">
132           <tr>
133             <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
134             <th width="12%">12</th> <th width="3%">16</th>
135             <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
136             <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
137             <th style="text-align:right" width="6%">31</th>
138           </tr><tr>
139             <td>\ref IPv4PacketParser::version() "Version"</td>
140             <td>\ref IPv4PacketParser::ihl() "IHL"</td>
141             <td colspan="2">\ref IPv4PacketParser::tos() "TOS"</td>
142             <td colspan="8">\ref IPv4PacketParser::length() "Length"</td> 
143           </tr><tr>
144             <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</td>
145             <td>R</td>
146             <td>\ref IPv4PacketParser::df() "DF"</td>
147             <td>\ref IPv4PacketParser::mf() "MF"</td>
148             <td colspan="5">\ref IPv4PacketParser::frag() "Fragment Offset"</td>
149           </tr><tr>
150             <td colspan="2">\ref IPv4PacketParser::ttl() "Time to Live (ttl)"</td>
151             <td colspan="2">\ref IPv4PacketParser::protocol() "Protocol"</td>
152             <td colspan="8">\ref IPv4PacketParser::checksum() "Header Checksum"</td>
153           </tr><tr>
154             <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
155           </tr><tr>
156             <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
157           </tr>
158         </table>
159
160         \par Associated registries:
161             \ref IpTypes
162
163         \par Finalize action:
164             \copydetails finalize()
165  
166         \ingroup protocolbundle_default
167      */
168     struct IPv4PacketType
169         : public PacketTypeBase,
170           public PacketTypeMixin<IPv4PacketType, IpTypes>
171     {
172 #ifndef DOXYGEN
173         typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
174 #endif
175         typedef ConcretePacket<IPv4PacketType> packet;  ///< IPv4 packet typedef
176         typedef IPv4PacketParser parser;                ///< typedef to the parser of IPv4 packet
177
178         using mixin::nextPacketRange;
179         using mixin::nextPacketType;
180         using mixin::initSize;
181         using mixin::init;
182
183         static key_t nextPacketKey(packet p) 
184             { return p->protocol(); }
185         
186         /** \brief Dump given IPv4Packet in readable form to given output stream */
187         static void dump(packet p, std::ostream & os); 
188         
189         static void finalize(packet p); ///< Finalize packet.
190                                         /**< \li set \ref IPv4PacketParser::length() "length" 
191                                                from payload size
192                                              \li set \ref IPv4PacketParser::protocol() "protocol" 
193                                                from type of next packet if found in \ref IpTypes
194                                              \li calculate and set 
195                                                \ref IPv4PacketParser::checksum() "checksum" */
196     };
197
198     /** \brief IPv4 packet typedef */
199     typedef ConcretePacket<IPv4PacketType> IPv4Packet;
200 }
201
202
203 ///////////////////////////////hh.e////////////////////////////////////////
204 //#include "IPv4Packet.cci"
205 //#include "IPv4Packet.ct"
206 //#include "IPv4Packet.cti"
207 #endif
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
213 // c-file-style: "senf"
214 // indent-tabs-mode: nil
215 // ispell-local-dictionary: "american"
216 // compile-command: "scons -u test"
217 // comment-column: 40
218 // End: