From: g0dil Date: Fri, 25 Sep 2009 23:03:13 +0000 (+0000) Subject: Packets: Implement packet dump formating helpers X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=1c55e269cfd07c017fe592a04d1ec86fdc7f4b37;p=senf.git Packets: Implement packet dump formating helpers git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1448 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/DefaultBundle/ICMPv6Packet.test.cc b/senf/Packets/DefaultBundle/ICMPv6Packet.test.cc index 3fa5002..9424438 100644 --- a/senf/Packets/DefaultBundle/ICMPv6Packet.test.cc +++ b/senf/Packets/DefaultBundle/ICMPv6Packet.test.cc @@ -195,44 +195,44 @@ BOOST_AUTO_UNIT_TEST(ICMPv6Packet_create) ip.finalizeAll(); -// std::string dump ( -// "Internet protocol Version 6:\n" -// " version : 6\n" -// " traffic class : 0x00\n" -// " flow label : 0x00000\n" -// " payload length : 64\n" -// " next header : 58\n" -// " hop limit : 64\n" -// " source : ::1\n" -// " destination : ::1\n" -// "ICMPv6 protocol:\n" -// " type : 128\n" -// " code : 0\n" -// " checksum : 0xdae0\n" -// "ICMPv6 Echo Request:\n" -// " identifier : 40830\n" -// " sequence nr. : 9\n" -// "Payload data (56 bytes)\n" -// ); - -// { -// std::stringstream ss; -// ip.dump(ss); -// BOOST_CHECK_EQUAL( ss.str(), dump ); -// } - SENF_CHECK_NO_THROW (ip.dump( oss )); + std::string dump ( + "Internet protocol Version 6:\n" + " version : 6\n" + " traffic class : 0x00\n" + " flow label : 0x00000\n" + " payload length : 64\n" + " next header : 58\n" + " hop limit : 64\n" + " source : ::1\n" + " destination : ::1\n" + "ICMPv6 protocol:\n" + " type : 128\n" + " code : 0\n" + " checksum : 0xdae0\n" + "ICMPv6 Echo Request:\n" + " Identifier : 40830\n" + " SequenceNumber : 9\n" + "Payload data (56 bytes)\n" + ); + + { + std::stringstream ss; + ip.dump(ss); + BOOST_CHECK_EQUAL( ss.str(), dump ); + } + SENF_CHECK_EQUAL_COLLECTIONS( ip.data().begin(), ip.data().end(), ping, ping+sizeof(ping) ); -// senf::IPv6Packet orig (senf::IPv6Packet::create(ping)); -// -// { -// std::stringstream ss; -// orig.dump(ss); -// BOOST_CHECK_EQUAL( ss.str(), dump ); -// } + senf::IPv6Packet orig (senf::IPv6Packet::create(ping)); + + { + std::stringstream ss; + orig.dump(ss); + BOOST_CHECK_EQUAL( ss.str(), dump ); + } } // Local Variables: diff --git a/senf/Packets/DumpFormat.cc b/senf/Packets/DumpFormat.cc new file mode 100644 index 0000000..2ce3b3f --- /dev/null +++ b/senf/Packets/DumpFormat.cc @@ -0,0 +1,92 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief DumpFormat non-inline non-template implementation */ + +#include "Packets.hh" +#include "DumpFormat.ih" + +// Custom includes +#include +#include + +//#include "DumpFormat.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +#ifndef SENF_PACKET_DUMP_COLON_COLUMN +#define SENF_PACKET_DUMP_COLON_COLUMN 27 +#endif + +prefix_ std::string senf::fieldName(std::string const & s) +{ + std::string t (std::max(unsigned(SENF_PACKET_DUMP_COLON_COLUMN+1), s.size()+5), ' '); + std::copy(s.begin(), s.end(), t.begin()+2); + t[t.size()-2] = ':'; + return t; +} + +prefix_ std::string senf::detail::prettySignedNumber(long long v, unsigned bits) +{ + if (v<0) return prettyUnsignedNumber(-v,bits,-1); + else return prettyUnsignedNumber(v,bits,+1); +} + +prefix_ std::string senf::detail::prettyUnsignedNumber(unsigned long long v, unsigned bits, + int sign) +{ + int bytes ((bits+7)/8); + int digs (int(2.4*bytes)+1); + std::stringstream ss; + ss << (sign ? (sign<0 ? "-" : " ") : "") + << "0x" << std::setw(2*bytes) << std::setfill('0') << std::hex + << 1u*v + << " (" << std::setw(digs+(sign ? 1 : 0)) << std::setfill(' ') << std::dec; + if (sign) + ss << sign*static_cast(v); + else + ss << 1u*v; + ss << ") ("; + for (int i (bytes-1); i>=0; --i) { + char c ((v>>(8*i))&0xff); + ss << ((c>=32 && c<=127) ? c : '.'); + } + ss << ')'; + return ss.str(); +} + + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "DumpFormat.mpp" + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Packets/DumpFormat.ct b/senf/Packets/DumpFormat.ct new file mode 100644 index 0000000..93650c5 --- /dev/null +++ b/senf/Packets/DumpFormat.ct @@ -0,0 +1,60 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief DumpFormat non-inline template implementation */ + +#include "DumpFormat.ih" + +// Custom includes +#include + +#define prefix_ +///////////////////////////////ct.p//////////////////////////////////////// + +template +prefix_ std::string +senf::prettyNumber(T const & v, typename boost::enable_if >::type *) +{ + return detail::prettySignedNumber(v, std::numeric_limits::digits); +} + +template +prefix_ std::string +senf::prettyNumber(T const & v, typename boost::enable_if >::type *) +{ + return detail::prettyUnsignedNumber(v, std::numeric_limits::digits); +} + +///////////////////////////////ct.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Packets/DumpFormat.hh b/senf/Packets/DumpFormat.hh new file mode 100644 index 0000000..0d1cea2 --- /dev/null +++ b/senf/Packets/DumpFormat.hh @@ -0,0 +1,72 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief DumpFormat public header */ + +#ifndef HH_SENF_senf_Packets_DumpFormat_ +#define HH_SENF_senf_Packets_DumpFormat_ 1 + +#ifndef HH_SENF_Packets_Packets_ +#error "Don't include 'DumpFormat.hh' directly, include 'Packets.hh'" +#endif + +// Custom includes +#include +#include +#include + +//#include "DumpFormat.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + std::string fieldName(std::string const & s); + + template + std::string prettyNumber(T const & v, + typename boost::enable_if >::type * = 0); + + template + std::string prettyNumber(T const & v, + typename boost::enable_if >::type * = 0); +} + +///////////////////////////////hh.e//////////////////////////////////////// +#endif +#if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_DumpFormat_i_) +#define HH_SENF_Packets_DumpFormat_i_ +//#include "DumpFormat.cci" +#include "DumpFormat.ct" +//#include "DumpFormat.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Packets/DumpFormat.ih b/senf/Packets/DumpFormat.ih new file mode 100644 index 0000000..302cb8d --- /dev/null +++ b/senf/Packets/DumpFormat.ih @@ -0,0 +1,53 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief DumpFormat internal header */ + +#ifndef IH_SENF_senf_Packets_DumpFormat_ +#define IH_SENF_senf_Packets_DumpFormat_ 1 + +// Custom includes + +///////////////////////////////ih.p//////////////////////////////////////// + +namespace senf { +namespace detail { + + std::string prettySignedNumber(long long v, unsigned bits); + std::string prettyUnsignedNumber(unsigned long long v, unsigned bits, int sign=0); + +}} + +///////////////////////////////ih.e//////////////////////////////////////// +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Packets/DumpFormat.test.cc b/senf/Packets/DumpFormat.test.cc new file mode 100644 index 0000000..37ab50f --- /dev/null +++ b/senf/Packets/DumpFormat.test.cc @@ -0,0 +1,61 @@ +// $Id$ +// +// Copyright (C) 2009 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief DumpFormat.test unit tests */ + +//#include "DumpFormat.test.hh" +//#include "DumpFormat.test.ih" + +// Custom includes +#include "Packets.hh" + +#include +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(dumpFormat) +{ + BOOST_CHECK_EQUAL( senf::fieldName("test"), " test : " ); + BOOST_CHECK_EQUAL( senf::fieldName("xxxxxxxxxxxxxxxxxxxxxxx"), " xxxxxxxxxxxxxxxxxxxxxxx : " ); + BOOST_CHECK_EQUAL( senf::fieldName("xxxxxxxxxxxxxxxxxxxxxxxx"), " xxxxxxxxxxxxxxxxxxxxxxxx : " ); + + BOOST_CHECK_EQUAL( senf::prettyNumber(-1), "-0x0001 ( -1) (..)" ); + BOOST_CHECK_EQUAL( senf::prettyNumber(1), " 0x0001 ( 1) (..)" ); + BOOST_CHECK_EQUAL( senf::prettyNumber(1), "0x0001 ( 1) (..)" ); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/senf/Packets/PacketRegistry.ct b/senf/Packets/PacketRegistry.ct index df12669..cda493b 100644 --- a/senf/Packets/PacketRegistry.ct +++ b/senf/Packets/PacketRegistry.ct @@ -169,19 +169,7 @@ prefix_ void senf::detail::DumpKey::dump(KeyType const & v, template prefix_ void senf::detail::DumpKey::dump(KeyType const & v, std::ostream & os) { - int bytes ((std::numeric_limits::digits+7)/8); - int digs (int(2.4*bytes)+1); - - os << " 0x" << std::setw(2*bytes) << std::setfill('0') << std::hex - << typename senf::detail::CharToInt::type (v) - << " (" << std::setw(digs) << std::setfill(' ') << std::dec - << typename senf::detail::CharToInt::type (v) - << ") ("; - for (int i (bytes-1); i>=0; --i) { - char c ((v>>(8*i))&0xff); - os << ((c>=32 && c<=127) ? c : '.'); - } - os << ')'; + os << " " << senf::prettyNumber(v); } ///////////////////////////////ct.e//////////////////////////////////////// diff --git a/senf/Packets/PacketRegistry.ih b/senf/Packets/PacketRegistry.ih index 240773c..169d275 100644 --- a/senf/Packets/PacketRegistry.ih +++ b/senf/Packets/PacketRegistry.ih @@ -155,11 +155,6 @@ namespace detail { static void dump(KeyType const & v, std::ostream & os); }; - template struct CharToInt { typedef Type type; }; - template <> struct CharToInt { typedef int type; }; - template <> struct CharToInt { typedef int type; }; - template <> struct CharToInt { typedef unsigned type; }; - }} ///////////////////////////////ih.e//////////////////////////////////////// diff --git a/senf/Packets/Packets.hh b/senf/Packets/Packets.hh index 087d012..7b466bc 100644 --- a/senf/Packets/Packets.hh +++ b/senf/Packets/Packets.hh @@ -46,6 +46,7 @@ #include "VectorParser.hh" #include "ParseHelpers.hh" #include "DataPacket.hh" +#include "DumpFormat.hh" #undef HH_SENF_Packets_Packets__decls_ @@ -69,6 +70,7 @@ #include "VectorParser.hh" #include "ParseHelpers.hh" #include "DataPacket.hh" +#include "DumpFormat.hh" #endif diff --git a/senf/Packets/bundledump.cc b/senf/Packets/bundledump.cc index 19ec8d0..e05a46d 100644 --- a/senf/Packets/bundledump.cc +++ b/senf/Packets/bundledump.cc @@ -37,7 +37,7 @@ int main(int argc, char const ** argv) { // Link in logger library ... (void) senf::log::StreamRegistry::instance(); - for (unsigned i (1); i