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