3ddffb90400605604abb1f884f3f1851803424cc
[senf.git] / Packets / DefaultBundle / IPv4Packet.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 "../../Utils/IpChecksum.hh"
36 #include "EthernetPacket.hh"
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42     senf::PacketRegistry<senf::EtherTypes>::RegistrationProxy<senf::IPv4Packet>
43         registerIPv4Packet (0x0800);
44
45     senf::PacketRegistry<senf::IpTypes>::RegistrationProxy<senf::IPv4Packet>
46         regsiterIPv4Packet2 (4); // IP-in-IP encapsulation
47 }
48
49 ///////////////////////////////////////////////////////////////////////////
50 // senf::IPv4PacketParser
51
52 prefix_ boost::uint16_t senf::IPv4PacketParser::calcChecksum()
53     const
54 {
55     validate(bytes(*this));
56     IpChecksum summer;
57     summer.feed( i(),                   i()+checksum_offset );
58     // Not needed since the number of 0-bytes is even
59     // summer.feed( 0u );
60     // summer.feed( 0u );
61     summer.feed( i()+checksum_offset+2, i()+bytes(*this)    );
62     return summer.sum();
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 // senf::IPv4PacketType
67
68 prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os)
69 {
70     boost::io::ios_all_saver ias(os);
71     os << "Internet protocol Version 4:\n"
72        << "  version       : " << p->version() << "\n"
73        << "  IHL           : " << p->ihl() << "\n"
74        << "  TOS           : " << unsigned(p->tos()) << "\n"
75        << "  length        : " << p->length() << "\n"
76        << "  identifier    : " << p->identifier() << "\n"
77        << "  DF            : " << p->df() << "\n"
78        << "  MF            : " << p->mf() << "\n"
79        << "  fragment      : " << p->frag() << "\n"
80        << "  TTL           : " << unsigned(p->ttl()) << "\n"
81        << "  protocol      : " << unsigned(p->protocol()) << "\n"
82        << "  checksum      : 0x" 
83          << std::hex << std::setw(4) << std::setfill('0') << p->checksum() << std::dec << "\n"
84        << "  source        : " << p->source() << "\n"
85        << "  destination   : " << p->destination() << "\n";
86 }
87
88 prefix_ void senf::IPv4PacketType::finalize(packet p)
89 {
90     p->length()   << p.size();
91     p->protocol() << key(p.next());
92     p->checksum() << p->calcChecksum();
93 }
94
95 ///////////////////////////////cc.e////////////////////////////////////////
96 #undef prefix_
97
98 \f
99 // Local Variables:
100 // mode: c++
101 // fill-column: 100
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // comment-column: 40
107 // End: