some very small fixes
[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     {
123         sock.bind(senf::LLSocketAddress(interface)); 
124     }
125
126     void run() 
127     {
128         senf::Scheduler::instance().add(
129             sock, senf::membind(&Sniffer::dumpPacket, this));
130         senf::Scheduler::instance().process();
131     }
132          
133 private:
134     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
135     {
136         std::string data (sock.read());
137         senf::EthernetPacket::ptr packet (
138             senf::Packet::create<senf::EthernetPacket>(
139                 data.begin(), data.end()));
140         packet->dump(std::cout);
141         hexdump(packet->last()->begin(),
142                 packet->last()->end());
143         std::cout << "\n\n";
144     }
145 };
146
147 int scheduler_main(int argc, char const * argv[])
148 {
149     try {
150         Sniffer sniffer ("eth0");
151         sniffer.run();
152     }
153     catch (std::exception const & ex) {
154         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
155     }
156     return 0;
157 }
158
159 int main(int argc, char const * argv[])
160 {
161     if (argc >= 2)
162         if (std::string(argv[1]) == "loop")
163             return loop_main(argc,argv);
164         else if (std::string(argv[1]) == "scheduler")
165             return scheduler_main(argc,argv);
166
167     std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
168     return 1;
169 }
170
171 ///////////////////////////////cc.e////////////////////////////////////////
172 #undef prefix_
173 //#include "Sniffer.mpp"
174
175 \f
176 // Local Variables:
177 // mode: c++
178 // fill-column: 100
179 // c-file-style: "senf"
180 // indent-tabs-mode: nil
181 // ispell-local-dictionary: "american"
182 // End: