00001 // $Id: IPv4Packet.cc 1792 2011-06-09 15:49:59Z tho $ 00002 // 00003 // Copyright (C) 2006 00004 // Fraunhofer (FOKUS) 00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY 00006 // Stefan Bund <g0dil@berlios.de> 00007 // 00008 // This program is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation; either version 2 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the 00020 // Free Software Foundation, Inc., 00021 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 00026 #include "IPv4Packet.hh" 00027 //#include "IPv4Packet.ih" 00028 00029 // Custom includes 00030 #include <iomanip> 00031 #include <boost/io/ios_state.hpp> 00032 #include <senf/Utils/IpChecksum.hh> 00033 #include "EthernetPacket.hh" 00034 00035 #define prefix_ 00036 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00037 00038 namespace { 00039 SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, 0x0800, senf::IPv4Packet); 00040 SENF_PACKET_REGISTRY_REGISTER( senf::IpTypes, 4, senf::IPv4Packet); // IP-in-IP encapsulation 00041 } 00042 00043 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00044 // senf::IPv4PacketParser 00045 00046 prefix_ boost::uint16_t senf::IPv4PacketParser::calcChecksum() 00047 const 00048 { 00049 validate(bytes(*this)); 00050 IpChecksum summer; 00051 summer.feed( i(), i()+checksum_offset ); 00052 summer.feed( i()+checksum_offset+2, i()+bytes(*this) ); 00053 return summer.sum(); 00054 } 00055 00056 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00057 // senf::IPv4PacketType 00058 00059 prefix_ void senf::IPv4PacketType::dump(packet p, std::ostream & os) 00060 { 00061 boost::io::ios_all_saver ias(os); 00062 os << "Internet protocol Version 4:\n" 00063 << senf::fieldName("version") << p->version() << "\n" 00064 << senf::fieldName("ip header length") << p->ihl() << "\n" 00065 << senf::fieldName("tos") << unsigned(p->tos()) << "\n" 00066 << senf::fieldName("length") << p->length() << "\n" 00067 << senf::fieldName("identifier") << p->identifier() << "\n" 00068 << senf::fieldName("dont fragment") << p->df() << "\n" 00069 << senf::fieldName("more fragments") << p->mf() << "\n" 00070 << senf::fieldName("fragment") << p->frag() << "\n" 00071 << senf::fieldName("ttl") << unsigned(p->ttl()) << "\n" 00072 << senf::fieldName("protocol") << unsigned(p->protocol()) << "\n" 00073 << senf::fieldName("checksum") 00074 << "0x" << std::hex << std::setw(4) << std::setfill('0') << std::right << p->checksum() << std::dec << "\n" 00075 << senf::fieldName("source") << p->source() << "\n" 00076 << senf::fieldName("destination") << p->destination() << "\n"; 00077 } 00078 00079 prefix_ void senf::IPv4PacketType::finalize(packet p) 00080 { 00081 p->length() << p.size(); 00082 p->protocol() << key(p.next(nothrow)); 00083 p->checksum() << p->calcChecksum(); 00084 } 00085 00086 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00087 #undef prefix_ 00088 00089 00090 // Local Variables: 00091 // mode: c++ 00092 // fill-column: 100 00093 // c-file-style: "senf" 00094 // indent-tabs-mode: nil 00095 // ispell-local-dictionary: "american" 00096 // compile-command: "scons -u test" 00097 // comment-column: 40 00098 // End: