Merged revisions 570-572,574-575,578-579,581-595,598-611 via svnmerge from
[senf.git] / Examples / MCSniffer / MCSniffer.cc
index c4aded2..fbb5fd7 100644 (file)
@@ -1,8 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2007
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+// Fraunhofer Institute for Open Communication Systems (FOKUS) 
+// Competence Center NETwork research (NET), St. Augustin, GERMANY 
+//     Thorsten Horstmann <tho@berlios.de>
 //
 // 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
 #include <fstream>
 #include <string>
 #include <iomanip>
-#include "Socket/UDPSocketHandle.hh"
-#include "Scheduler/Scheduler.hh"
-#include "Packets/DefaultBundle/EthernetPacket.hh"
-#include "Utils/membind.hh"
+#include <senf/Socket/Protocols/INet.hh>
+#include <senf/Scheduler/Scheduler.hh>
+#include <senf/Packets/DefaultBundle/EthernetPacket.hh>
+#include <senf/Utils/membind.hh>
+#include <senf/Utils/hexdump.hh>
 
 
 //#include "MCSniffer.mpp"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-namespace {
-
-    static const unsigned BLOCK_SIZE = 16;
-
-    template <class Iterator>
-    void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
-    {
-        unsigned offset (0);
-        std::string ascii;
-        for (; i != i_end; ++i, ++offset) {
-            switch (offset % BLOCK_SIZE) {
-            case 0:
-                if (!ascii.empty()) {
-                    stream << "  " << ascii << "\n";
-                    ascii = "";
-                }
-                stream << "  "
-                          << std::hex << std::setw(4) << std::setfill('0')
-                          << offset << ' ';
-                break;
-            case BLOCK_SIZE/2:
-                stream << " ";
-                ascii += ' ';
-                break;
-            }
-            stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
-                      << unsigned(*i);
-            ascii += (*i >= ' ' && *i < 126) ? *i : '.';
-        }
-        if (!ascii.empty()) {
-            for (; (offset % BLOCK_SIZE) != 0; ++offset) {
-                if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
-                    stream << " ";
-                stream << "   ";
-            }
-            stream << "  " << ascii << "\n";
-        }
-        stream << std::dec;
-    }
-}
-
-
 class MCSniffer
 {
     senf::UDPv4ClientSocketHandle sock;
@@ -89,7 +49,7 @@ public:
     MCSniffer(senf::INet4Address addr, std::ostream& s)
         : stream(s)
     {
-        sock.protocol().bind(addr);
+        // sock.bind(addr);
         sock.protocol().mcLoop(true);
         sock.protocol().mcAddMembership(addr);
         senf::Scheduler::instance().add(
@@ -97,13 +57,13 @@ public:
     }
 
 private:
-    void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
+    void dumpPacket(senf::Scheduler::EventId event)
     {
         std::string data (sock.read());
         senf::EthernetPacket packet (
             senf::EthernetPacket::create(data));
         packet.dump(stream);
-        hexdump(packet.last().data().begin(),
+        senf::hexdump(packet.last().data().begin(),
                 packet.last().data().end(),
                 stream);
         stream << "\n\n";
@@ -118,9 +78,9 @@ int main(int argc, char const * argv[])
         std::ofstream f2 ("233.132.152.2.txt");
         
         MCSniffer sniffer1 (
-            senf::INet4Address::INet4Address("233.132.152.1:22344"), f1);
+            senf::INet4Address::from_string("233.132.152.1"), f1);
         MCSniffer sniffer2 (
-            senf::INet4Address::INet4Address("233.132.152.2:22344"), f2);
+            senf::INet4Address::from_string("233.132.152.2"), f2);
             
         senf::Scheduler::instance().process();
     }