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_IPv4Packet_
27 #define HH_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() { return value(); }
54 byte & operator[](size_type index) { 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 \see IPv4PacketType \n
64 <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
66 \todo Implement options
68 struct IPv4PacketParser : public PacketParserBase
70 # include SENF_FIXED_PARSER()
72 SENF_PARSER_BITFIELD( version, 4, unsigned );
73 SENF_PARSER_BITFIELD( ihl, 4, unsigned );
75 SENF_PARSER_FIELD( tos, UInt8Parser );
76 SENF_PARSER_FIELD( length, UInt16Parser );
77 SENF_PARSER_FIELD( identifier, UInt16Parser );
79 SENF_PARSER_BITFIELD( reserved, 1, bool );
80 SENF_PARSER_BITFIELD( df, 1, bool );
81 SENF_PARSER_BITFIELD( mf, 1, bool );
82 SENF_PARSER_BITFIELD( frag, 13, unsigned );
84 SENF_PARSER_FIELD( ttl, UInt8Parser );
85 SENF_PARSER_FIELD( protocol, UInt8Parser );
86 SENF_PARSER_FIELD( checksum, UInt16Parser );
87 SENF_PARSER_FIELD( source, INet4AddressParser );
88 SENF_PARSER_FIELD( destination, INet4AddressParser );
92 // We don't support option headers at the moment ...
96 SENF_PARSER_FINALIZE(IPv4PacketParser);
98 boost::uint16_t calcChecksum() const;
100 bool validateChecksum() const {
101 return checksum() == calcChecksum();
105 /** \brief IP protocol number registry
107 This registeres packets with their IP protocol number.
109 \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
113 typedef boost::uint16_t key_t;
116 /** \brief IPv4 packet
118 <table class="packet" cellpadding="5" cellspacing="1" border="1">
120 <th width="12%">0</th> <th width="12%">4</th> <th width="12%">8</th>
121 <th width="12%">12</th> <th width="3%">16</th>
122 <th width="3%"></th> <th width="3%"></th> <th width="3%"></th>
123 <th width="12%">20</th> <th width="12%">24</th> <th width="6%">28</th>
124 <th style="text-align:right" width="6%">31</th>
126 <td>\ref IPv4PacketParser::version() "Version"</td>
127 <td>\ref IPv4PacketParser::ihl() "IHL"</td>
128 <td colspan="2">\ref IPv4PacketParser::tos() "TOS"</td>
129 <td colspan="8">\ref IPv4PacketParser::length() "Length"</td>
131 <td colspan="4">\ref IPv4PacketParser::identifier() "Identifier"</td>
132 <td>\ref IPv4PacketParser::reserved() "R"</td>
133 <td>\ref IPv4PacketParser::df() "DF"</td>
134 <td>\ref IPv4PacketParser::mf() "MF"</td>
135 <td colspan="5">\ref IPv4PacketParser::frag() "Fragment Offset"</td>
137 <td colspan="2">\ref IPv4PacketParser::ttl() "Time to Live (ttl)"</td>
138 <td colspan="2">\ref IPv4PacketParser::protocol() "Protocol"</td>
139 <td colspan="8">\ref IPv4PacketParser::checksum() "Header Checksum"</td>
141 <td colspan="12">\ref IPv4PacketParser::source() "Source Address"</td>
143 <td colspan="12">\ref IPv4PacketParser::destination() "Destination Address"</td>
147 \par Packet type (typedef):
151 \ref IPv4PacketParser
153 \par Associated registries:
156 \par Finalize action:
157 Set \a length from payload size\n
158 Set \a protocol from type of next packet if found in \ref IpTypes\n
159 Calculate \a checksum
161 \ingroup protocolbundle_default
163 struct IPv4PacketType
164 : public PacketTypeBase,
165 public PacketTypeMixin<IPv4PacketType, IpTypes>
168 typedef PacketTypeMixin<IPv4PacketType, IpTypes> mixin;
169 typedef ConcretePacket<IPv4PacketType> packet;
170 typedef IPv4PacketParser parser;
172 using mixin::nextPacketRange;
173 using mixin::nextPacketType;
174 using mixin::initSize;
177 static registry_key_t nextPacketKey(packet p)
178 { return p->protocol(); }
180 static void dump(packet p, std::ostream & os);
181 static void finalize(packet p);
184 /** \brief IPv4 packet typedef */
185 typedef ConcretePacket<IPv4PacketType> IPv4Packet;
189 ///////////////////////////////hh.e////////////////////////////////////////
191 #ifndef SENF_PACKETS_DECL_ONLY
192 //#include IPv4Packet.cci"
193 //#include "IPv4Packet.ct"
194 //#include "IPv4Packet.cti"
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // comment-column: 40