4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief IPv4Packet public header */
31 #ifndef HH_SENF_Packets_DefaultBundle_IPv4Packet_
32 #define HH_SENF_Packets_DefaultBundle_IPv4Packet_ 1
35 #include <senf/Socket/Protocols/INet/INet4Address.hh>
36 #include <senf/Packets/Packets.hh>
38 //#include "IPv4Packet.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 /** \brief Parse in IPv4 address
47 struct INet4AddressParser
48 : public ValueParserBase<INet4AddressParser, INet4Address, 4u>
50 INet4AddressParser(data_iterator i, state_type s) : Base(i,s) {}
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()); }
55 using Base::operator=;
58 /** \brief Parse an IPv4 packet
60 Parser implementing the IPv4 header.
62 \image html IPv4Packet.png
64 \see IPv4PacketType \n
65 <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
67 \todo Implement options
69 struct IPv4PacketParser : public PacketParserBase
71 # include SENF_FIXED_PARSER()
73 SENF_PARSER_BITFIELD( version, 4, unsigned );
74 SENF_PARSER_BITFIELD( ihl, 4, unsigned );
76 SENF_PARSER_FIELD( tos, UInt8Parser );
77 SENF_PARSER_FIELD( length, UInt16Parser );
78 SENF_PARSER_FIELD( identifier, UInt16Parser );
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 );
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 );
93 // We don't support option headers at the moment ...
97 SENF_PARSER_FINALIZE(IPv4PacketParser);
99 boost::uint16_t calcChecksum() const; ///< calculate header checksum
100 /**< calculate and return the checksum of the header
101 \see \ref senf::IpChecksum */
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" */
111 /** \brief IP protocol number registry
113 This registeres packets with their IP protocol number.
115 \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
119 typedef boost::uint8_t key_t;
122 /** \brief IPv4 packet
124 \par Packet type (typedef):
128 see \ref IPv4PacketParser
130 <table class="packet" cellpadding="5" cellspacing="1" border="1">
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>
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>
143 <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</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>
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>
153 <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
155 <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
159 \par Associated registries:
162 \par Finalize action:
163 \copydetails finalize()
165 \ingroup protocolbundle_default
167 struct IPv4PacketType
168 : public PacketTypeBase,
169 public PacketTypeMixin<IPv4PacketType, IpTypes>
172 typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
174 typedef ConcretePacket<IPv4PacketType> packet; ///< IPv4 packet typedef
175 typedef IPv4PacketParser parser; ///< typedef to the parser of IPv4 packet
177 using mixin::nextPacketRange;
178 using mixin::nextPacketType;
179 using mixin::initSize;
182 static key_t nextPacketKey(packet p)
183 { return p->protocol(); }
185 /** \brief Dump given IPv4Packet in readable form to given output stream */
186 static void dump(packet p, std::ostream & os);
188 static void finalize(packet p); ///< Finalize packet.
189 /**< \li set \ref IPv4PacketParser::length() "length"
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" */
197 /** \brief IPv4 packet typedef
198 \ingroup protocolbundle_default
200 typedef ConcretePacket<IPv4PacketType> IPv4Packet;
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 //#include "IPv4Packet.cci"
206 //#include "IPv4Packet.ct"
207 //#include "IPv4Packet.cti"
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