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 "../../Socket/Protocols/INet/INet4Address.hh"
31 #include "../../Packets/Packets.hh"
33 //#include "IPv4Packet.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
38 /** \brief Parse in IPv4 address
42 struct INet4AddressParser : public PacketParserBase
44 INet4AddressParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
46 ///////////////////////////////////////////////////////////////////////////
48 typedef INet4Address value_type;
49 static const size_type fixed_bytes = 4u;
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; }
59 /** \brief Parse an IPv4 packet
61 Parser implementing the IPv4 header.
63 \image html IPv4Packet.png
65 \see IPv4PacketType \n
66 <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
68 \todo Implement options
70 struct IPv4PacketParser : public PacketParserBase
72 # include SENF_FIXED_PARSER()
74 SENF_PARSER_BITFIELD( version, 4, unsigned );
75 SENF_PARSER_BITFIELD( ihl, 4, unsigned );
77 SENF_PARSER_FIELD( tos, UInt8Parser );
78 SENF_PARSER_FIELD( length, UInt16Parser );
79 SENF_PARSER_FIELD( identifier, UInt16Parser );
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 );
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 );
94 // We don't support option headers at the moment ...
98 SENF_PARSER_FINALIZE(IPv4PacketParser);
100 boost::uint16_t calcChecksum() const; ///< calculate header checksum
101 /**< calculate and return the checksum of the header
102 \see \ref senf::IpChecksum */
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" */
112 /** \brief IP protocol number registry
114 This registeres packets with their IP protocol number.
116 \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
120 typedef boost::uint16_t key_t;
123 /** \brief IPv4 packet
125 \par Packet type (typedef):
129 see \ref IPv4PacketParser
131 <table class="packet" cellpadding="5" cellspacing="1" border="1">
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>
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>
144 <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</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>
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>
154 <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
156 <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
160 \par Associated registries:
163 \par Finalize action:
164 \copydetails finalize()
166 \ingroup protocolbundle_default
168 struct IPv4PacketType
169 : public PacketTypeBase,
170 public PacketTypeMixin<IPv4PacketType, IpTypes>
173 typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
175 typedef ConcretePacket<IPv4PacketType> packet; ///< IPv4 packet typedef
176 typedef IPv4PacketParser parser; ///< typedef to the parser of IPv4 packet
178 using mixin::nextPacketRange;
179 using mixin::nextPacketType;
180 using mixin::initSize;
183 static key_t nextPacketKey(packet p)
184 { return p->protocol(); }
186 /** \brief Dump given IPv4Packet in readable form to given output stream */
187 static void dump(packet p, std::ostream & os);
189 static void finalize(packet p); ///< Finalize packet.
190 /**< \li set \ref IPv4PacketParser::length() "length"
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" */
198 /** \brief IPv4 packet typedef */
199 typedef ConcretePacket<IPv4PacketType> IPv4Packet;
203 ///////////////////////////////hh.e////////////////////////////////////////
205 #ifndef SENF_PACKETS_DECL_ONLY
206 //#include "IPv4Packet.cci"
207 //#include "IPv4Packet.ct"
208 //#include "IPv4Packet.cti"
215 // c-file-style: "senf"
216 // indent-tabs-mode: nil
217 // ispell-local-dictionary: "american"
218 // compile-command: "scons -u test"
219 // comment-column: 40