f3ebf2e19a60be1453148952d44b19af8b624b01
[senf.git] / senf / Packets / DefaultBundle / TCPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Dennis Goslar <dennis.goslar@inf.hochschule-bonn-rhein-sieg.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief TCPPacket public header */
25
26 #ifndef HH_SENF_Packets_DefaultBundle_TCPPacket_
27 #define HH_SENF_Packets_DefaultBundle_TCPPacket_ 1
28
29 // Custom includes
30 #include <senf/Packets/Packets.hh>
31
32 //#include "TCPPacket.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     /** \brief Parse a TCP packet
38
39         Parser implementing the TCP header. The fields implemented are:
40         \image html TCPPacket.png
41
42         \see TCPPacketType \n
43             <a href="http://tools.ietf.org/html/rfc793">RFC 793</a>
44      */
45     struct TCPPacketParser : public PacketParserBase
46     {
47 #       include SENF_PARSER()
48
49         SENF_PARSER_FIELD( source,                  UInt16Parser );
50         SENF_PARSER_FIELD( destination,             UInt16Parser );
51         SENF_PARSER_FIELD( sequencenumber,          UInt32Parser );
52         SENF_PARSER_FIELD( acknowledgmentnumber,    UInt32Parser );
53
54         SENF_PARSER_BITFIELD        ( dataoffset, 4, unsigned );
55         SENF_PARSER_PRIVATE_BITFIELD( reserved,   6, unsigned );
56         SENF_PARSER_BITFIELD        ( urgf,       1, bool     );
57         SENF_PARSER_BITFIELD        ( ackf,       1, bool     );
58         SENF_PARSER_BITFIELD        ( pshf,       1, bool     );
59         SENF_PARSER_BITFIELD        ( rstf,       1, bool     );
60         SENF_PARSER_BITFIELD        ( synf,       1, bool     );
61         SENF_PARSER_BITFIELD        ( finf,       1, bool     );
62
63         SENF_PARSER_FIELD( window,        UInt16Parser );
64         SENF_PARSER_FIELD( checksum,      UInt16Parser );
65         SENF_PARSER_FIELD( urgentpointer, UInt16Parser );
66
67         // skip option part in TCP-Header, dataoffset()*4 = TCP header length in byte
68         // for options see http://www.iana.org/assignments/tcp-parameters/
69         // if dataoffset() < 5 packet is invalid
70         SENF_PARSER_SKIP( (dataoffset() < 5 ? 0 : dataoffset()*4-20), 0);
71
72         SENF_PARSER_FINALIZE(TCPPacketParser);
73
74         SENF_PARSER_INIT() {
75             // Reserved: 6 bits, reserved for future use. Must be zero.
76             // see http://tools.ietf.org/html/rfc793
77             reserved_() = 0;
78             // dataoffset per default 5, 5*4 = 20 Byte Header Length
79             dataoffset() = 5;
80         }
81
82         boost::uint16_t calcChecksum() const; ///< calculate (pseudo-)header checksum
83                                               /**< calculate and return the checksum of the
84                                                    (pseudo-)header \see \ref senf::IpChecksum */
85
86         bool validateChecksum() const {
87             return checksum() == 0u || checksum() == calcChecksum();
88         }                               ///< validate header checksum
89                                         /**< return \c true if the \ref checksum() "checksum"
90                                              field is equal to the \ref calcChecksum() "calculated checksum" */
91     };
92
93     /** \brief TCP packet
94
95         \par Packet type (typedef):
96             \ref TCPPacket
97
98         \par Fields:
99             \ref TCPPacketParser
100
101         <table class="packet" cellpadding="5" cellspacing="1" border="1">
102         <tr>
103             <th width="12%">0</th>
104             <th width="20%">4</th>
105             <th width="3%">10</th>
106             <th width="3%">11</th>
107             <th width="3%">12</th>
108             <th width="3%">13</th>
109             <th width="3%">14</th>
110             <th width="3%">15</th>
111             <th width="25%">16</th>
112             <th style="text-align:right" width="25%">31</th>
113         </tr>
114         <tr>
115             <td colspan="8">\ref TCPPacketParser::source() "Source Port"</td>
116             <td colspan="2">\ref TCPPacketParser::destination() "Destination Port"</td>
117         </tr><tr>
118             <td colspan="10">\ref TCPPacketParser::sequencenumber() "Sequence Number"</td>
119         </tr><tr>
120             <td colspan="10">\ref TCPPacketParser::acknowledgmentnumber() "Acknowledgment Number"</td>
121         </tr><tr>
122             <td colspan="1">\ref TCPPacketParser::dataoffset() "DataOffset"</td>
123             <td colspan="1">Reserved</td>
124             <td colspan="1">\ref TCPPacketParser::urgf() "UR"</td>
125             <td colspan="1">\ref TCPPacketParser::ackf() "AC"</td>
126             <td colspan="1">\ref TCPPacketParser::pshf() "PS"</td>
127             <td colspan="1">\ref TCPPacketParser::rstf() "RS"</td>
128             <td colspan="1">\ref TCPPacketParser::synf() "SY"</td>
129             <td colspan="1">\ref TCPPacketParser::finf() "FI"</td>
130             <td colspan="2">\ref TCPPacketParser::window() "Window Size"</td>
131         </tr><tr>
132             <td colspan="8">\ref TCPPacketParser::checksum() "Checksum"</td>
133             <td colspan="2">\ref TCPPacketParser::urgentpointer() "Urgent Pointer"</td>
134         </tr>
135         </table>
136
137         \par Finalize action:
138             \copydetails finalize()
139
140         \ingroup protocolbundle_default
141      */
142     struct TCPPacketType
143         : public PacketTypeBase,
144           public PacketTypeMixin<TCPPacketType>
145     {
146 #ifndef DOXYGEN
147         typedef PacketTypeMixin<TCPPacketType> mixin;
148 #endif
149         typedef ConcretePacket<TCPPacketType> packet; ///< TCP packet typedef
150         typedef TCPPacketParser parser;               ///< typedef to the parser of TCP packet
151
152         using mixin::nextPacketRange;
153         using mixin::initSize;
154         using mixin::init;
155
156         /** \brief Dump given TCPPacket in readable form to given output stream */
157         static void dump(packet p, std::ostream & os);
158
159         static void finalize(packet p); ///< Finalize packet.
160                                         /**< \li set \ref TCPPacketParser::dataoffset() "Data Offset" from
161                                                header size
162                                              \li calculate and set \ref TCPPacketParser::checksum()
163                                                "checksum" */
164     };
165
166     /** \brief TCP packet typedef
167         \ingroup protocolbundle_default
168      */
169     typedef ConcretePacket<TCPPacketType> TCPPacket;
170 }
171
172
173 ///////////////////////////////hh.e////////////////////////////////////////
174 //#include "TCPPacket.cci"
175 //#include "TCPPacket.ct"
176 //#include "TCPPacket.cti"
177 #endif
178
179 \f
180 // Local Variables:
181 // mode: c++
182 // fill-column: 100
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // comment-column: 40
188 // End: