fd288d2edf7ac3fcac7a5f4875c8bde56fd8a420
[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 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 // Definition of non-inline non-template functions
23
24 //#include "MCSniffer.hh"
25 //#include "MCSniffer.ih"
26
27 // Custom includes
28 #include <fstream>
29 #include <string>
30 #include <iomanip>
31 #include "Socket/Protocols/INet/UDPSocketHandle.hh"
32 #include "Scheduler/Scheduler.hh"
33 #include "Packets/DefaultBundle/EthernetPacket.hh"
34 #include "Utils/membind.hh"
35 #include "Utils/hexdump.hh"
36
37
38 //#include "MCSniffer.mpp"
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 class MCSniffer
43 {
44     senf::UDPv4ClientSocketHandle sock;
45     std::ostream& stream;
46
47 public:
48     MCSniffer(senf::INet4SocketAddress addr, std::ostream& s)
49         : stream(s)
50     {
51         sock.protocol().bind(addr);
52         sock.protocol().mcLoop(true);
53         sock.protocol().mcAddMembership(addr);
54         senf::Scheduler::instance().add(
55             sock, senf::membind(&MCSniffer::dumpPacket, this));
56     }
57
58 private:
59     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
60     {
61         std::string data (sock.read());
62         senf::EthernetPacket packet (
63             senf::EthernetPacket::create(data));
64         packet.dump(stream);
65         senf::hexdump(packet.last().data().begin(),
66                 packet.last().data().end(),
67                 stream);
68         stream << "\n\n";
69      }
70 };
71
72
73 int main(int argc, char const * argv[])
74 {       
75     try {
76         std::ofstream f1 ("233.132.152.1.txt");
77         std::ofstream f2 ("233.132.152.2.txt");
78         
79         MCSniffer sniffer1 (
80             senf::INet4SocketAddress("233.132.152.1:22344"), f1);
81         MCSniffer sniffer2 (
82             senf::INet4SocketAddress("233.132.152.2:22344"), f2);
83             
84         senf::Scheduler::instance().process();
85     }
86     catch (std::exception const & ex) {
87         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
88     }
89     return 0;
90 }
91
92  
93 ///////////////////////////////cc.e////////////////////////////////////////
94 #undef prefix_
95 //#include "MCSniffer.mpp"
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // c-file-style: "senf"
102 // indent-tabs-mode: nil
103 // ispell-local-dictionary: "american"
104 // compile-command: "scons -u"
105 // comment-column: 40
106 // End: