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