Socket: Introduce sub-directory structure for concrete protocols
[senf.git] / Examples / Sniffer / Sniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 "Sniffer.hh"
26 //#include "Sniffer.ih"
27
28 // Custom includes
29 #include <string>
30 #include <iostream>
31 #include <iomanip>
32 #include "Socket/Protocols/PacketSocketHandle.hh"
33 #include "Scheduler/Scheduler.hh"
34 #include "Utils/membind.hh"
35 #include "Packets/DefaultBundle/EthernetPacket.hh"
36
37 //#include "Sniffer.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)
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                     std::cout << "  " << ascii << "\n";
55                     ascii = "";
56                 }
57                 std::cout << "  "
58                           << std::hex << std::setw(4) << std::setfill('0')
59                           << offset << ' ';
60                 break;
61             case BLOCK_SIZE/2:
62                 std::cout << " ";
63                 ascii += ' ';
64                 break;
65             }
66             std::cout << ' ' << 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                     std::cout << " ";
74                 std::cout << "   ";
75             }
76             std::cout << "  " << ascii << "\n";
77         }
78         std::cout << std::dec;
79     }
80
81 }
82
83 int loop_main (int argc, char const * argv[])
84 {
85     try {
86         senf::PacketSocketHandle sock;
87         sock.bind(senf::LLSocketAddress("eth1"));
88         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
89
90         while (true) { // forever
91             senf::EthernetPacket packet (senf::EthernetPacket::create(
92                                              senf::EthernetPacket::noinit));
93             sock.read(packet.data(),0);
94             packet.dump(std::cout);
95             hexdump(packet.last().data().begin(),
96                     packet.last().data().end());
97             std::cout << "\n\n";
98         }
99     }
100     catch (std::exception const & ex) {
101         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
102     }
103     return 0;
104 }
105
106 class Sniffer
107 {
108     senf::PacketSocketHandle sock;
109
110 public:
111     Sniffer(std::string const & interface) 
112     {
113         sock.bind(senf::LLSocketAddress(interface)); 
114     }
115
116     void run() 
117     {
118         senf::Scheduler::instance().add(
119             sock, senf::membind(&Sniffer::dumpPacket, this));
120         senf::Scheduler::instance().process();
121     }
122          
123 private:
124     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
125     {
126         senf::EthernetPacket packet (
127             senf::EthernetPacket::create(senf::EthernetPacket::noinit));
128         sock.read(packet.data(),0);
129         packet.dump(std::cout);
130         hexdump(packet.last().data().begin(),
131                 packet.last().data().end());
132         std::cout << "\n\n";
133     }
134 };
135
136 int scheduler_main(int argc, char const * argv[])
137 {
138     try {
139         Sniffer sniffer ("eth1");
140         sniffer.run();
141     }
142     catch (std::exception const & ex) {
143         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
144     }
145     return 0;
146 }
147
148 int main(int argc, char const * argv[])
149 {
150     if (argc >= 2)
151         if (std::string(argv[1]) == "loop")
152             return loop_main(argc,argv);
153         else if (std::string(argv[1]) == "scheduler")
154             return scheduler_main(argc,argv);
155
156     std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
157     return 1;
158 }
159
160 ///////////////////////////////cc.e////////////////////////////////////////
161 #undef prefix_
162 //#include "Sniffer.mpp"
163
164 \f
165 // Local Variables:
166 // mode: c++
167 // fill-column: 100
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u"
172 // comment-column: 40
173 // End: