switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / TCPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26
27 /** \file
28     \brief TCPPacket public header */
29
30 #ifndef HH_SENF_Packets_DefaultBundle_TCPPacket_
31 #define HH_SENF_Packets_DefaultBundle_TCPPacket_ 1
32
33 // Custom includes
34 #include <senf/Packets/Packets.hh>
35
36 //#include "TCPPacket.mpp"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace senf {
40
41     /** \brief Parse a TCP packet
42
43         Parser implementing the TCP header. The fields implemented are:
44         \image html TCPPacket.png
45
46         \see TCPPacketType \n
47             <a href="http://tools.ietf.org/html/rfc793">RFC 793</a>
48      */
49     struct TCPPacketParser : public PacketParserBase
50     {
51 #       include SENF_PARSER()
52
53         SENF_PARSER_FIELD( source,                  UInt16Parser );
54         SENF_PARSER_FIELD( destination,             UInt16Parser );
55         SENF_PARSER_FIELD( sequencenumber,          UInt32Parser );
56         SENF_PARSER_FIELD( acknowledgmentnumber,    UInt32Parser );
57
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     );
66
67         SENF_PARSER_FIELD( window,        UInt16Parser );
68         SENF_PARSER_FIELD( checksum,      UInt16Parser );
69         SENF_PARSER_FIELD( urgentpointer, UInt16Parser );
70
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);
75
76         SENF_PARSER_FINALIZE(TCPPacketParser);
77
78         SENF_PARSER_INIT() {
79             // Reserved: 6 bits, reserved for future use. Must be zero.
80             // see http://tools.ietf.org/html/rfc793
81             reserved_() = 0;
82             // dataoffset per default 5, 5*4 = 20 Byte Header Length
83             dataoffset() = 5;
84         }
85
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 */
89
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" */
95     };
96
97     /** \brief TCP packet
98
99         \par Packet type (typedef):
100             \ref TCPPacket
101
102         \par Fields:
103             \ref TCPPacketParser
104
105         <table class="packet" cellpadding="5" cellspacing="1" border="1">
106         <tr>
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>
117         </tr>
118         <tr>
119             <td colspan="8">\ref TCPPacketParser::source() "Source Port"</td>
120             <td colspan="2">\ref TCPPacketParser::destination() "Destination Port"</td>
121         </tr><tr>
122             <td colspan="10">\ref TCPPacketParser::sequencenumber() "Sequence Number"</td>
123         </tr><tr>
124             <td colspan="10">\ref TCPPacketParser::acknowledgmentnumber() "Acknowledgment Number"</td>
125         </tr><tr>
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>
135         </tr><tr>
136             <td colspan="8">\ref TCPPacketParser::checksum() "Checksum"</td>
137             <td colspan="2">\ref TCPPacketParser::urgentpointer() "Urgent Pointer"</td>
138         </tr>
139         </table>
140
141         \par Finalize action:
142             \copydetails finalize()
143
144         \ingroup protocolbundle_default
145      */
146     struct TCPPacketType
147         : public PacketTypeBase,
148           public PacketTypeMixin<TCPPacketType>
149     {
150 #ifndef DOXYGEN
151         typedef PacketTypeMixin<TCPPacketType> mixin;
152 #endif
153         typedef ConcretePacket<TCPPacketType> packet; ///< TCP packet typedef
154         typedef TCPPacketParser parser;               ///< typedef to the parser of TCP packet
155
156         using mixin::nextPacketRange;
157         using mixin::initSize;
158         using mixin::init;
159
160         /** \brief Dump given TCPPacket in readable form to given output stream */
161         static void dump(packet p, std::ostream & os);
162
163         static void finalize(packet p); ///< Finalize packet.
164                                         /**< \li set \ref TCPPacketParser::dataoffset() "Data Offset" from
165                                                header size
166                                              \li calculate and set \ref TCPPacketParser::checksum()
167                                                "checksum" */
168     };
169
170     /** \brief TCP packet typedef
171         \ingroup protocolbundle_default
172      */
173     typedef ConcretePacket<TCPPacketType> TCPPacket;
174 }
175
176
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
178 //#include "TCPPacket.cci"
179 //#include "TCPPacket.ct"
180 //#include "TCPPacket.cti"
181 #endif
182
183 \f
184 // Local Variables:
185 // mode: c++
186 // fill-column: 100
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
192 // End: