4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief IPv4Packet public header */
26 #ifndef HH_SENF_Packets_DefaultBundle_IPv4Packet_
27 #define HH_SENF_Packets_DefaultBundle_IPv4Packet_ 1
30 #include <senf/Socket/Protocols/INet/INet4Address.hh>
31 #include <senf/Packets/Packets.hh>
33 //#include "IPv4Packet.mpp"
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 /** \brief Parse in IPv4 address
42 struct INet4AddressParser
43 : public ValueParserBase<INet4AddressParser, INet4Address, 4u>
45 INet4AddressParser(data_iterator i, state_type s) : Base(i,s) {}
47 value_type value() const { return value_type::from_data(i()); }
48 void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
50 using Base::operator=;
53 /** \brief Parse an IPv4 packet
55 Parser implementing the IPv4 header.
57 \image html IPv4Packet.png
59 \see IPv4PacketType \n
60 <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
62 \todo Implement options
64 struct IPv4PacketParser : public PacketParserBase
66 # include SENF_FIXED_PARSER()
68 SENF_PARSER_BITFIELD( version, 4, unsigned );
69 SENF_PARSER_BITFIELD( ihl, 4, unsigned );
71 SENF_PARSER_FIELD( tos, UInt8Parser );
72 SENF_PARSER_FIELD( length, UInt16Parser );
73 SENF_PARSER_FIELD( identifier, UInt16Parser );
75 SENF_PARSER_PRIVATE_BITFIELD( reserved, 1, bool );
76 SENF_PARSER_BITFIELD ( df, 1, bool );
77 SENF_PARSER_BITFIELD ( mf, 1, bool );
78 SENF_PARSER_BITFIELD ( frag, 13, unsigned );
80 SENF_PARSER_FIELD( ttl, UInt8Parser );
81 SENF_PARSER_FIELD( protocol, UInt8Parser );
82 SENF_PARSER_FIELD( checksum, UInt16Parser );
83 SENF_PARSER_FIELD( source, INet4AddressParser );
84 SENF_PARSER_FIELD( destination, INet4AddressParser );
88 // We don't support option headers at the moment ...
92 SENF_PARSER_FINALIZE(IPv4PacketParser);
94 boost::uint16_t calcChecksum() const; ///< calculate header checksum
95 /**< calculate and return the checksum of the header
96 \see \ref senf::IpChecksum */
98 bool validateChecksum() const {
99 return checksum() == calcChecksum();
100 } ///< validate header checksum
101 /**< return \c true if the \ref checksum() "checksum"
102 field is equal to the \ref calcChecksum()
103 "calculated checksum" */
106 /** \brief IP protocol number registry
108 This registeres packets with their IP protocol number.
110 \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
114 typedef boost::uint8_t key_t;
117 /** \brief IPv4 packet
119 \par Packet type (typedef):
123 see \ref IPv4PacketParser
125 <table class="packet" cellpadding="5" cellspacing="1" border="1">
127 <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
128 <th width="12%">12</th> <th width="3%">16</th>
129 <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
130 <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
131 <th style="text-align:right" width="6%">31</th>
133 <td>\ref IPv4PacketParser::version() "Version"</td>
134 <td>\ref IPv4PacketParser::ihl() "IHL"</td>
135 <td colspan="2">\ref IPv4PacketParser::tos() "TOS"</td>
136 <td colspan="8">\ref IPv4PacketParser::length() "Length"</td>
138 <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</td>
140 <td>\ref IPv4PacketParser::df() "DF"</td>
141 <td>\ref IPv4PacketParser::mf() "MF"</td>
142 <td colspan="5">\ref IPv4PacketParser::frag() "Fragment Offset"</td>
144 <td colspan="2">\ref IPv4PacketParser::ttl() "Time to Live (ttl)"</td>
145 <td colspan="2">\ref IPv4PacketParser::protocol() "Protocol"</td>
146 <td colspan="8">\ref IPv4PacketParser::checksum() "Header Checksum"</td>
148 <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
150 <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
154 \par Associated registries:
157 \par Finalize action:
158 \copydetails finalize()
160 \ingroup protocolbundle_default
162 struct IPv4PacketType
163 : public PacketTypeBase,
164 public PacketTypeMixin<IPv4PacketType, IpTypes>
167 typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
169 typedef ConcretePacket<IPv4PacketType> packet; ///< IPv4 packet typedef
170 typedef IPv4PacketParser parser; ///< typedef to the parser of IPv4 packet
172 using mixin::nextPacketRange;
173 using mixin::nextPacketType;
174 using mixin::initSize;
177 static key_t nextPacketKey(packet p)
178 { return p->protocol(); }
180 /** \brief Dump given IPv4Packet in readable form to given output stream */
181 static void dump(packet p, std::ostream & os);
183 static void finalize(packet p); ///< Finalize packet.
184 /**< \li set \ref IPv4PacketParser::length() "length"
186 \li set \ref IPv4PacketParser::protocol() "protocol"
187 from type of next packet if found in \ref IpTypes
188 \li calculate and set
189 \ref IPv4PacketParser::checksum() "checksum" */
192 /** \brief IPv4 packet typedef
193 \ingroup protocolbundle_default
195 typedef ConcretePacket<IPv4PacketType> IPv4Packet;
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
200 //#include "IPv4Packet.cci"
201 //#include "IPv4Packet.ct"
202 //#include "IPv4Packet.cti"
209 // c-file-style: "senf"
210 // indent-tabs-mode: nil
211 // ispell-local-dictionary: "american"
212 // compile-command: "scons -u test"
213 // comment-column: 40