5ef86f3c0c59fbbc10923dc112fe2d7229df103e
[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 <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <boost/io/ios_state.hpp>
35 #include <senf/Utils/IpChecksum.hh>
36 #include "EthernetPacket.hh"
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42     SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, 0x0800, senf::IPv4Packet);
43     SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes,    4,      senf::IPv4Packet); // IP-in-IP encapsulation
44 }
45
46 ///////////////////////////////////////////////////////////////////////////
47 // senf::IPv4PacketParser
48
49 prefix_ boost::uint16_t senf::IPv4PacketParser::calcChecksum()
50     const
51 {
52     validate(bytes(*this));
53     IpChecksum summer;
54     summer.feed( i(),                   i()+checksum_offset );
55     summer.feed( i()+checksum_offset+2, i()+bytes(*this)    );
56     return summer.sum();
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60 // senf::IPv4PacketType
61
62 prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os)
63 {
64     boost::io::ios_all_saver ias(os);
65     os << "Internet protocol Version 4:\n"
66        << senf::fieldName("version")                   << p->version() << "\n"
67        << senf::fieldName("ip header length")          << p->ihl() << "\n"
68        << senf::fieldName("tos")                       << unsigned(p->tos()) << "\n"
69        << senf::fieldName("length")                    << p->length() << "\n"
70        << senf::fieldName("identifier")                << p->identifier() << "\n"
71        << senf::fieldName("dont fragment")             << p->df() << "\n"
72        << senf::fieldName("more fragments")            << p->mf() << "\n"
73        << senf::fieldName("fragment")                  << p->frag() << "\n"
74        << senf::fieldName("ttl")                       << unsigned(p->ttl()) << "\n"
75        << senf::fieldName("protocol")                  << unsigned(p->protocol()) << "\n"
76        << senf::fieldName("checksum")
77        << "0x" << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << std::dec << "\n"
78        << senf::fieldName("source")                    << p->source() << "\n"
79        << senf::fieldName("destination")               << p->destination() << "\n";
80 }
81
82 prefix_ void senf::IPv4PacketType::finalize(packet p)
83 {
84     p->length()   << p.size();
85     p->protocol() << key(p.next(nothrow));
86     p->checksum() << p->calcChecksum();
87 }
88
89 ///////////////////////////////cc.e////////////////////////////////////////
90 #undef prefix_
91
92 \f
93 // Local Variables:
94 // mode: c++
95 // fill-column: 100
96 // c-file-style: "senf"
97 // indent-tabs-mode: nil
98 // ispell-local-dictionary: "american"
99 // compile-command: "scons -u test"
100 // comment-column: 40
101 // End: