4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
23 // Definition of non-inline non-template functions
25 //#include "Sniffer.hh"
26 //#include "Sniffer.ih"
32 #include "Socket/Protocols/Raw/PacketSocketHandle.hh"
33 #include "Scheduler/Scheduler.hh"
34 #include "Utils/membind.hh"
35 #include "Packets/DefaultBundle/EthernetPacket.hh"
37 //#include "Sniffer.mpp"
39 ///////////////////////////////cc.p////////////////////////////////////////
43 static const unsigned BLOCK_SIZE = 16;
45 template <class Iterator>
46 void hexdump(Iterator i, Iterator const & i_end)
50 for (; i != i_end; ++i, ++offset) {
51 switch (offset % BLOCK_SIZE) {
54 std::cout << " " << ascii << "\n";
58 << std::hex << std::setw(4) << std::setfill('0')
66 std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0')
68 ascii += (*i >= ' ' && *i < 126) ? *i : '.';
71 for (; (offset % BLOCK_SIZE) != 0; ++offset) {
72 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
76 std::cout << " " << ascii << "\n";
78 std::cout << std::dec;
83 int loop_main (int argc, char const * argv[])
86 senf::PacketSocketHandle sock;
87 sock.bind(senf::LLSocketAddress("eth1"));
88 // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
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());
100 catch (std::exception const & ex) {
101 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
108 senf::PacketSocketHandle sock;
111 Sniffer(std::string const & interface)
113 sock.bind(senf::LLSocketAddress(interface));
118 senf::Scheduler::instance().add(
119 sock, senf::membind(&Sniffer::dumpPacket, this));
120 senf::Scheduler::instance().process();
124 void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
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());
136 int scheduler_main(int argc, char const * argv[])
139 Sniffer sniffer ("eth1");
142 catch (std::exception const & ex) {
143 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
148 int main(int argc, char const * argv[])
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);
156 std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
160 ///////////////////////////////cc.e////////////////////////////////////////
162 //#include "Sniffer.mpp"
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u"
172 // comment-column: 40