d9aa5635c71bde4f7ad563e6b18287ef2f6db15f
[senf.git] / senf / Packets / DefaultBundle / IPv4Packet.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 IPv4Packet non-inline non-template implementation */
25
26 #include "IPv4Packet.hh"
27 //#include "IPv4Packet.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <boost/io/ios_state.hpp>
32 #include <senf/Utils/IpChecksum.hh>
33 #include "EthernetPacket.hh"
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace {
39     SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, 0x0800, senf::IPv4Packet);
40     SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes,    4,      senf::IPv4Packet); // IP-in-IP encapsulation
41 }
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::IPv4PacketParser
45
46 prefix_ boost::uint16_t senf::IPv4PacketParser::calcChecksum()
47     const
48 {
49     validate(bytes(*this));
50     IpChecksum summer;
51     summer.feed( i(),                   i()+checksum_offset );
52     summer.feed( i()+checksum_offset+2, i()+bytes(*this)    );
53     return summer.sum();
54 }
55
56 //-/////////////////////////////////////////////////////////////////////////////////////////////////
57 // senf::IPv4PacketType
58
59 prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os)
60 {
61     boost::io::ios_all_saver ias(os);
62     os << "Internet protocol Version 4:\n"
63        << senf::fieldName("version")                   << p->version() << "\n"
64        << senf::fieldName("ip header length")          << p->ihl() << "\n"
65        << senf::fieldName("tos")                       << unsigned(p->tos()) << "\n"
66        << senf::fieldName("length")                    << p->length() << "\n"
67        << senf::fieldName("identifier")                << p->identifier() << "\n"
68        << senf::fieldName("dont fragment")             << p->df() << "\n"
69        << senf::fieldName("more fragments")            << p->mf() << "\n"
70        << senf::fieldName("fragment")                  << p->frag() << "\n"
71        << senf::fieldName("ttl")                       << unsigned(p->ttl()) << "\n"
72        << senf::fieldName("protocol")                  << unsigned(p->protocol()) << "\n"
73        << senf::fieldName("checksum")
74        << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << std::dec << "\n"
75        << senf::fieldName("source")                    << p->source() << "\n"
76        << senf::fieldName("destination")               << p->destination() << "\n";
77 }
78
79 prefix_ void senf::IPv4PacketType::finalize(packet p)
80 {
81     p->length()   << p.size();
82     p->protocol() << key(p.next(nothrow));
83     p->checksum() << p->calcChecksum();
84 }
85
86 //-/////////////////////////////////////////////////////////////////////////////////////////////////
87 #undef prefix_
88
89 \f
90 // Local Variables:
91 // mode: c++
92 // fill-column: 100
93 // c-file-style: "senf"
94 // indent-tabs-mode: nil
95 // ispell-local-dictionary: "american"
96 // compile-command: "scons -u test"
97 // comment-column: 40
98 // End: