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