172fea335d84be61df18ea6c8676aba1a7d7ce5d
[senf.git] / Examples / MCSniffer / MCSniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 class MCSniffer
44 {
45     senf::UDPv4ClientSocketHandle sock;
46     std::ostream& stream;
47     senf::scheduler::FdEvent event;
48
49 public:
50     MCSniffer(senf::INet4Address addr, std::ostream& s)
51         : stream(s), event("MCSniffer", senf::membind(&MCSniffer::dumpPacket, this),
52                            sock, senf::scheduler::FdEvent::EV_READ)
53     {
54         // sock.bind(addr);
55         sock.protocol().mcLoop(true);
56         sock.protocol().mcAddMembership(addr);
57     }
58
59 private:
60     void dumpPacket(int 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::INet4Address::from_string("233.132.152.1"), f1);
82         MCSniffer sniffer2 (
83             senf::INet4Address::from_string("233.132.152.2"), f2);
84
85         senf::scheduler::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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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: