Add new file-variable 'comment-column'
[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 #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("eth0"));
88         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
89
90         while (true) { // forever
91             std::string data (sock.read());
92             senf::EthernetPacket::ptr packet (
93                 senf::Packet::create<senf::EthernetPacket>(
94                     data.begin(), data.end()));
95             packet->dump(std::cout);
96             hexdump(packet->last()->begin(),
97                     packet->last()->end());
98             std::cout << "\n\n";
99         }
100     }
101     catch (std::exception const & ex) {
102         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
103     }
104     return 0;
105 }
106
107 class Sniffer
108 {
109     senf::PacketSocketHandle sock;
110
111 public:
112     Sniffer(std::string const & interface) 
113     {
114         sock.bind(senf::LLSocketAddress(interface)); 
115     }
116
117     void run() 
118     {
119         senf::Scheduler::instance().add(
120             sock, senf::membind(&Sniffer::dumpPacket, this));
121         senf::Scheduler::instance().process();
122     }
123          
124 private:
125     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
126     {
127         std::string data (sock.read());
128         senf::EthernetPacket::ptr packet (
129             senf::Packet::create<senf::EthernetPacket>(
130                 data.begin(), data.end()));
131         packet->dump(std::cout);
132         hexdump(packet->last()->begin(),
133                 packet->last()->end());
134         std::cout << "\n\n";
135     }
136 };
137
138 int scheduler_main(int argc, char const * argv[])
139 {
140     try {
141         Sniffer sniffer ("eth0");
142         sniffer.run();
143     }
144     catch (std::exception const & ex) {
145         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
146     }
147     return 0;
148 }
149
150 int main(int argc, char const * argv[])
151 {
152     if (argc >= 2)
153         if (std::string(argv[1]) == "loop")
154             return loop_main(argc,argv);
155         else if (std::string(argv[1]) == "scheduler")
156             return scheduler_main(argc,argv);
157
158     std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
159     return 1;
160 }
161
162 ///////////////////////////////cc.e////////////////////////////////////////
163 #undef prefix_
164 //#include "Sniffer.mpp"
165
166 \f
167 // Local Variables:
168 // mode: c++
169 // fill-column: 100
170 // c-file-style: "senf"
171 // indent-tabs-mode: nil
172 // ispell-local-dictionary: "american"
173 // compile-command: "scons -u test"
174 // comment-column: 40
175 // End: