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.
28 \brief TCPPacket public header */
30 #ifndef HH_SENF_Packets_DefaultBundle_TCPPacket_
31 #define HH_SENF_Packets_DefaultBundle_TCPPacket_ 1
34 #include <senf/Packets/Packets.hh>
36 //#include "TCPPacket.mpp"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 /** \brief Parse a TCP packet
43 Parser implementing the TCP header. The fields implemented are:
44 \image html TCPPacket.png
47 <a href="http://tools.ietf.org/html/rfc793">RFC 793</a>
49 struct TCPPacketParser : public PacketParserBase
51 # include SENF_PARSER()
53 SENF_PARSER_FIELD( source, UInt16Parser );
54 SENF_PARSER_FIELD( destination, UInt16Parser );
55 SENF_PARSER_FIELD( sequencenumber, UInt32Parser );
56 SENF_PARSER_FIELD( acknowledgmentnumber, UInt32Parser );
58 SENF_PARSER_BITFIELD ( dataoffset, 4, unsigned );
59 SENF_PARSER_PRIVATE_BITFIELD( reserved, 6, unsigned );
60 SENF_PARSER_BITFIELD ( urgf, 1, bool );
61 SENF_PARSER_BITFIELD ( ackf, 1, bool );
62 SENF_PARSER_BITFIELD ( pshf, 1, bool );
63 SENF_PARSER_BITFIELD ( rstf, 1, bool );
64 SENF_PARSER_BITFIELD ( synf, 1, bool );
65 SENF_PARSER_BITFIELD ( finf, 1, bool );
67 SENF_PARSER_FIELD( window, UInt16Parser );
68 SENF_PARSER_FIELD( checksum, UInt16Parser );
69 SENF_PARSER_FIELD( urgentpointer, UInt16Parser );
71 // skip option part in TCP-Header, dataoffset()*4 = TCP header length in byte
72 // for options see http://www.iana.org/assignments/tcp-parameters/
73 // if dataoffset() < 5 packet is invalid
74 SENF_PARSER_SKIP( (dataoffset() < 5 ? 0 : dataoffset()*4-20), 0);
76 SENF_PARSER_FINALIZE(TCPPacketParser);
79 // Reserved: 6 bits, reserved for future use. Must be zero.
80 // see http://tools.ietf.org/html/rfc793
82 // dataoffset per default 5, 5*4 = 20 Byte Header Length
86 boost::uint16_t calcChecksum() const; ///< calculate (pseudo-)header checksum
87 /**< calculate and return the checksum of the
88 (pseudo-)header \see \ref senf::IpChecksum */
90 bool validateChecksum() const {
91 return checksum() == 0u || checksum() == calcChecksum();
92 } ///< validate header checksum
93 /**< return \c true if the \ref checksum() "checksum"
94 field is equal to the \ref calcChecksum() "calculated checksum" */
99 \par Packet type (typedef):
105 <table class="packet" cellpadding="5" cellspacing="1" border="1">
107 <th width="12%">0</th>
108 <th width="20%">4</th>
109 <th width="3%">10</th>
110 <th width="3%">11</th>
111 <th width="3%">12</th>
112 <th width="3%">13</th>
113 <th width="3%">14</th>
114 <th width="3%">15</th>
115 <th width="25%">16</th>
116 <th style="text-align:right" width="25%">31</th>
119 <td colspan="8">\ref TCPPacketParser::source() "Source Port"</td>
120 <td colspan="2">\ref TCPPacketParser::destination() "Destination Port"</td>
122 <td colspan="10">\ref TCPPacketParser::sequencenumber() "Sequence Number"</td>
124 <td colspan="10">\ref TCPPacketParser::acknowledgmentnumber() "Acknowledgment Number"</td>
126 <td colspan="1">\ref TCPPacketParser::dataoffset() "DataOffset"</td>
127 <td colspan="1">Reserved</td>
128 <td colspan="1">\ref TCPPacketParser::urgf() "UR"</td>
129 <td colspan="1">\ref TCPPacketParser::ackf() "AC"</td>
130 <td colspan="1">\ref TCPPacketParser::pshf() "PS"</td>
131 <td colspan="1">\ref TCPPacketParser::rstf() "RS"</td>
132 <td colspan="1">\ref TCPPacketParser::synf() "SY"</td>
133 <td colspan="1">\ref TCPPacketParser::finf() "FI"</td>
134 <td colspan="2">\ref TCPPacketParser::window() "Window Size"</td>
136 <td colspan="8">\ref TCPPacketParser::checksum() "Checksum"</td>
137 <td colspan="2">\ref TCPPacketParser::urgentpointer() "Urgent Pointer"</td>
141 \par Finalize action:
142 \copydetails finalize()
144 \ingroup protocolbundle_default
147 : public PacketTypeBase,
148 public PacketTypeMixin<TCPPacketType>
151 typedef PacketTypeMixin<TCPPacketType> mixin;
153 typedef ConcretePacket<TCPPacketType> packet; ///< TCP packet typedef
154 typedef TCPPacketParser parser; ///< typedef to the parser of TCP packet
156 using mixin::nextPacketRange;
157 using mixin::initSize;
160 /** \brief Dump given TCPPacket in readable form to given output stream */
161 static void dump(packet p, std::ostream & os);
163 static void finalize(packet p); ///< Finalize packet.
164 /**< \li set \ref TCPPacketParser::dataoffset() "Data Offset" from
166 \li calculate and set \ref TCPPacketParser::checksum()
170 /** \brief TCP packet typedef
171 \ingroup protocolbundle_default
173 typedef ConcretePacket<TCPPacketType> TCPPacket;
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 //#include "TCPPacket.cci"
179 //#include "TCPPacket.ct"
180 //#include "TCPPacket.cti"
187 // c-file-style: "senf"
188 // indent-tabs-mode: nil
189 // ispell-local-dictionary: "american"
190 // compile-command: "scons -u test"
191 // comment-column: 40