252734d772751326ed052f1e4ecd2413bd6c4aed
[senf.git] / Examples / MCSniffer / MCSniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Thorsten Horstmann <thorsten.horstmann@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 // Definition of non-inline non-template functions
24
25 //#include "MCSniffer.hh"
26 //#include "MCSniffer.ih"
27
28 // Custom includes
29 #include <fstream>
30 #include <string>
31 #include <iomanip>
32 #include <senf/Socket/Protocols/INet.hh>
33 #include <senf/Scheduler/Scheduler.hh>
34 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
35 #include <senf/Utils/membind.hh>
36 #include <senf/Utils/hexdump.hh>
37
38
39 //#include "MCSniffer.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 class MCSniffer
44 {
45     senf::UDPv4ClientSocketHandle sock;
46     std::ostream& stream;
47
48 public:
49     MCSniffer(senf::INet4SocketAddress addr, std::ostream& s)
50         : stream(s)
51     {
52         sock.protocol().bind(addr);
53         sock.protocol().mcLoop(true);
54         sock.protocol().mcAddMembership(addr);
55         senf::Scheduler::instance().add(
56             sock, senf::membind(&MCSniffer::dumpPacket, this));
57     }
58
59 private:
60     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
61     {
62         std::string data (sock.read());
63         senf::EthernetPacket packet (
64             senf::EthernetPacket::create(data));
65         packet.dump(stream);
66         senf::hexdump(packet.last().data().begin(),
67                 packet.last().data().end(),
68                 stream);
69         stream << "\n\n";
70      }
71 };
72
73
74 int main(int argc, char const * argv[])
75 {       
76     try {
77         std::ofstream f1 ("233.132.152.1.txt");
78         std::ofstream f2 ("233.132.152.2.txt");
79         
80         MCSniffer sniffer1 (
81             senf::INet4SocketAddress("233.132.152.1:22344"), f1);
82         MCSniffer sniffer2 (
83             senf::INet4SocketAddress("233.132.152.2:22344"), f2);
84             
85         senf::Scheduler::instance().process();
86     }
87     catch (std::exception const & ex) {
88         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
89     }
90     return 0;
91 }
92
93  
94 ///////////////////////////////cc.e////////////////////////////////////////
95 #undef prefix_
96 //#include "MCSniffer.mpp"
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"
106 // comment-column: 40
107 // End: