added new Example MCSniffer
[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 "Sniffer.hh"
25 //#include "Sniffer.ih"
26
27 // Custom includes
28 #include <string>
29 #include <fstream>
30
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <sys/ioctl.h>
35 #include <linux/sockios.h> //
36
37 #include <string>
38 #include <iostream>
39 #include <iomanip>
40 #include "Socket/UDPSocketHandle.hh"
41 #include "Scheduler/Scheduler.hh"
42 #include "Utils/membind.hh"
43
44 #include "Packets/EthernetPacket.hh"
45
46
47
48
49 //#include "Sniffer.mpp"
50 #define prefix_
51 ///////////////////////////////cc.p////////////////////////////////////////
52
53 namespace {
54
55     static const unsigned BLOCK_SIZE = 16;
56
57     template <class Iterator>
58     void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
59     {
60         unsigned offset (0);
61         std::string ascii;
62         for (; i != i_end; ++i, ++offset) {
63             switch (offset % BLOCK_SIZE) {
64             case 0:
65                 if (!ascii.empty()) {
66                     stream << "  " << ascii << "\n";
67                     ascii = "";
68                 }
69                 stream << "  "
70                           << std::hex << std::setw(4) << std::setfill('0')
71                           << offset << ' ';
72                 break;
73             case BLOCK_SIZE/2:
74                 stream << " ";
75                 ascii += ' ';
76                 break;
77             }
78             stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
79                       << unsigned(*i);
80             ascii += (*i >= ' ' && *i < 126) ? *i : '.';
81         }
82         if (!ascii.empty()) {
83             for (; (offset % BLOCK_SIZE) != 0; ++offset) {
84                 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
85                     stream << " ";
86                 stream << "   ";
87             }
88             stream << "  " << ascii << "\n";
89         }
90         stream << std::dec;
91     }
92 }
93
94
95 class MSniffer
96 {
97     senf::UDPv4ClientSocketHandle sock;
98     std::ostream& stream;
99
100 public:
101     MSniffer(senf::INet4Address addr, std::ostream& s)
102         : stream(s)
103     {
104         sock.protocol().bind(addr);
105         sock.protocol().mcLoop(true);
106         sock.protocol().mcAddMembership(addr);
107         senf::Scheduler::instance().add(
108             sock, senf::membind(&MSniffer::dumpPacket, this));
109     }
110
111 private:
112     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
113     {
114         std::string data (sock.read());
115         senf::EthernetPacket::ptr packet (
116             senf::Packet::create<senf::EthernetPacket>(
117                 data.begin(), data.end()));
118         packet->dump(stream);
119         hexdump(packet->last()->begin(),
120                 packet->last()->end(),
121                 stream);
122         stream << "\n\n";
123      }
124 };
125
126
127 int main(int argc, char const * argv[])
128 {       
129     try {
130         std::ofstream f1 ("233.132.152.1.txt");
131         std::ofstream f2 ("233.132.152.2.txt");
132         
133         MSniffer sniffer1 (
134             senf::INet4Address::INet4Address("233.132.152.1:22344"), f1);
135         MSniffer sniffer2 (
136             senf::INet4Address::INet4Address("233.132.152.2:22344"), f2);
137             
138         senf::Scheduler::instance().process();
139     }
140     catch (std::exception const & ex) {
141         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
142     }
143     return 0;
144 }
145
146  
147 ///////////////////////////////cc.e////////////////////////////////////////
148 #undef prefix_
149 //#include "Sniffer.mpp"
150
151 \f
152 // Local Variables:
153 // mode: c++
154 // fill-column: 100
155 // c-file-style: "senf"
156 // indent-tabs-mode: nil
157 // ispell-local-dictionary: "american"
158 // End: