moved Sniffer into new Examples directory
[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/PacketSocketHandle.hh"
33 #include "Scheduler/Scheduler.hh"
34 #include "Utils/membind.hh"
35
36 #include "Packets/EthernetPacket.hh"
37 #include "Packets/IpV4Packet.hh"
38 #include "Packets/UDPPacket.hh"
39
40 //#include "Sniffer.mpp"
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 namespace {
45
46     static const unsigned BLOCK_SIZE = 16;
47
48     template <class Iterator>
49     void hexdump(Iterator i, Iterator const & i_end)
50     {
51         unsigned offset (0);
52         std::string ascii;
53         for (; i != i_end; ++i, ++offset) {
54             switch (offset % BLOCK_SIZE) {
55             case 0:
56                 if (!ascii.empty()) {
57                     std::cout << "  " << ascii << "\n";
58                     ascii = "";
59                 }
60                 std::cout << "  "
61                           << std::hex << std::setw(4) << std::setfill('0')
62                           << offset << ' ';
63                 break;
64             case BLOCK_SIZE/2:
65                 std::cout << " ";
66                 ascii += ' ';
67                 break;
68             }
69             std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0')
70                       << unsigned(*i);
71             ascii += (*i >= ' ' && *i < 126) ? *i : '.';
72         }
73         if (!ascii.empty()) {
74             for (; (offset % BLOCK_SIZE) != 0; ++offset) {
75                 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
76                     std::cout << " ";
77                 std::cout << "   ";
78             }
79             std::cout << "  " << ascii << "\n";
80         }
81         std::cout << std::dec;
82     }
83
84     void use_em()
85     {
86         // Pull in symbols from the wanted packets ...
87         senf::Packet::create<senf::IpV4Packet>(0,0);
88         senf::Packet::create<senf::UDPPacket>(0,0);
89     }
90 }
91
92 int loop_main (int argc, char const * argv[])
93 {
94     try {
95         senf::PacketSocketHandle sock;
96         sock.bind(senf::LLSocketAddress("eth0"));
97         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
98
99         while (true) { // forever
100             std::string data (sock.read());
101             senf::EthernetPacket::ptr packet (
102                 senf::Packet::create<senf::EthernetPacket>(
103                     data.begin(), data.end()));
104             packet->dump(std::cout);
105             hexdump(packet->last()->begin(),
106                     packet->last()->end());
107             std::cout << "\n\n";
108         }
109     }
110     catch (std::exception const & ex) {
111         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
112     }
113     return 0;
114 }
115
116 class Sniffer
117 {
118     senf::PacketSocketHandle sock;
119
120 public:
121     Sniffer(std::string const & interface)
122         { sock.bind(senf::LLSocketAddress(interface)); }
123
124     void run()
125         {
126             senf::Scheduler::instance().add(sock, senf::membind(&Sniffer::dumpPacket, this));
127             senf::Scheduler::instance().process();
128         }
129          
130 private:
131     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
132         {
133             std::string data (sock.read());
134             senf::EthernetPacket::ptr packet (
135                 senf::Packet::create<senf::EthernetPacket>(
136                     data.begin(), data.end()));
137             packet->dump(std::cout);
138             hexdump(packet->last()->begin(),
139                     packet->last()->end());
140             std::cout << "\n\n";
141         }
142 };
143
144 int scheduler_main(int argc, char const * argv[])
145 {
146     try {
147         Sniffer sniffer ("eth0");
148         sniffer.run();
149     }
150     catch (std::exception const & ex) {
151         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
152     }
153     return 0;
154 }
155
156 int main(int argc, char const * argv[])
157 {
158     if (argc >= 2)
159         if (std::string(argv[1]) == "loop")
160             return loop_main(argc,argv);
161         else if (std::string(argv[1]) == "scheduler")
162             return scheduler_main(argc,argv);
163
164     std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
165     return 1;
166 }
167
168 ///////////////////////////////cc.e////////////////////////////////////////
169 #undef prefix_
170 //#include "Sniffer.mpp"
171
172 \f
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // c-file-style: "senf"
177 // indent-tabs-mode: nil
178 // ispell-local-dictionary: "american"
179 // End: