Replace all relative includes with abolute ones
[senf.git] / senf / Packets / DefaultBundle / TCPPacket.cc
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 non-inline non-template implementation */
25
26 #include "TCPPacket.hh"
27 //#include "TCPPacket.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <boost/io/ios_state.hpp>
32 #include <senf/Packets/Packets.hh>
33 #include <senf/Utils/IpChecksum.hh>
34 #include "IPv4Packet.hh"
35 #include "IPv6Packet.hh"
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     senf::PacketRegistry<senf::IpTypes>::RegistrationProxy<senf::TCPPacket>
42         registerTCPPacket (6);
43 }
44
45 ///////////////////////////////////////////////////////////////////////////
46 // senf::TCPPacketParser
47
48 prefix_ boost::uint16_t senf::TCPPacketParser::calcChecksum()
49     const
50 {
51     IpChecksum summer;
52     // first on to the awkward part: the IP pseudo header
53     IPv4Packet ipv4 (packet().rfind<IPv4Packet>(nothrow));
54     if (ipv4) {
55         // Pseudo header defined in RFC793
56         // begin
57         summer.feed( ipv4->source().i(),
58                      ipv4->source().i() + IPv4Packet::Parser::source_t::fixed_bytes );
59         ///\fixme What about a hop-by-hop routing option? Which destination is used in IPv4 ?
60         summer.feed( ipv4->destination().i(),
61                      ipv4->destination().i() + IPv4PacketParser::destination_t::fixed_bytes );
62         // include zero byte
63         summer.feed( 0u );
64         ///\fixme May there be another header between the IPv4 header and TCP? if so, we
65         /// need to hack the correct protocol number here ...
66         summer.feed( 6u );
67
68         // feed 1st byte and 2nd byte from field tcp length
69         summer.feed(boost::uint16_t(data().size()) & 0xff00);
70         summer.feed(boost::uint16_t(data().size()) & 0x00ff);
71         // end pseudo header
72     }
73     else {
74     // Pseudo header defined in RFC2460
75         IPv6Packet ipv6 (packet().rfind<IPv6Packet>(nothrow));
76         if (ipv6) {
77             summer.feed( ipv6->source().i(),
78                          ipv6->source().i() + IPv6Packet::Parser::source_t::fixed_bytes );
79             ///\todo Implement routing header support
80             // The destination used here must be the *final* destination ...
81             summer.feed( ipv6->destination().i(),
82                          ipv6->destination().i() + IPv6PacketParser::destination_t::fixed_bytes );
83             // This is a simplification. The value is really 32bit to support UDP Jumbograms
84             // (RFC2147). However, skipping an even number of 0 bytes does not change the checksum
85             // RFC2460 specifies, that this must always be 6, not the value used in the ipv6
86             // header
87             // feed 1st byte and 2nd byte from field tcp length
88             summer.feed(boost::uint16_t(data().size()) & 0xff00);
89             summer.feed(boost::uint16_t(data().size()) & 0x00ff);
90             // include zero byte
91             summer.feed( 0u );
92             // add TCP protocol number
93             summer.feed( 6u );
94         }
95     }
96
97     // since header are 16 / even 32bit aligned we don't have to care for padding. since IpChecksum
98     // cares for padding at the final summing we don't have to care is the payload is 16nbit-aligned, too.
99
100     // iterate till checksum field and skip 2 bytes,
101     // iterate over rest of tcp header and tcp payload
102     summer.feed( i(), i()+16 );
103     summer.feed( i()+18, data().end() );
104
105     boost::uint16_t rv (summer.sum());
106     return rv ? rv : 0xffffu;
107
108 }
109
110 ///////////////////////////////////////////////////////////////////////////
111 // senf::TCPPacketType
112
113 prefix_ void senf::TCPPacketType::dump(packet p, std::ostream & os)
114 {
115     boost::io::ios_all_saver ias(os);
116     os << "TCP:\n"
117        <<     "  source port             : " << p->source() << "\n"
118        <<     "  destination port        : " << p->destination() << "\n"
119        <<     "  sequence number         : " << p->sequencenumber() << "\n"
120        <<     "  acknowledgment number   : " << p->acknowledgmentnumber() << "\n"
121        <<     "  data offset             : " << p->dataoffset() << "\n"
122        <<     "  urgent flag             : " << p->urgf() << "\n"
123        <<     "  ack flag                : " << p->ackf() << "\n"
124        <<     "  push flag               : " << p->pshf() << "\n"
125        <<     "  reset flag              : " << p->rstf() << "\n"
126        <<     "  syn flag                : " << p->synf() << "\n"
127        <<     "  fin flag                : " << p->finf() << "\n"
128        <<     "  window size             : " << p->window() << "\n"
129        <<     "  checksum                : "
130        << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << "\n"
131        <<     "  urgent pointer          : " << p->urgentpointer() << "\n";
132 }
133
134 prefix_ void senf::TCPPacketType::finalize(packet p)
135 {
136     p->dataoffset() << p.size();
137     p->checksum() << p->calcChecksum();
138 }
139
140 ///////////////////////////////cc.e////////////////////////////////////////
141 #undef prefix_
142
143 \f
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
147 // c-file-style: "senf"
148 // indent-tabs-mode: nil
149 // ispell-local-dictionary: "american"
150 // compile-command: "scons -u test"
151 // comment-column: 40
152 // End: