4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.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 <senf/Socket/Protocols/Raw.hh>
33 #include <senf/Scheduler/Scheduler.hh>
34 #include <senf/Utils/membind.hh>
35 #include <senf/Utils/hexdump.hh>
36 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
38 //#include "Sniffer.mpp"
40 ///////////////////////////////cc.p////////////////////////////////////////
42 int loop_main (int argc, char const * argv[])
45 senf::PacketSocketHandle sock;
46 sock.bind(senf::LLSocketAddress(argv[2]));
47 // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
49 while (true) { // forever
50 senf::EthernetPacket packet (senf::EthernetPacket::create(
51 senf::EthernetPacket::noinit));
52 sock.read(packet.data(),0);
53 packet.dump(std::cout);
55 packet.last().data().begin(),
56 packet.last().data().end(),
61 catch (std::exception const & ex) {
62 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
69 senf::PacketSocketHandle sock;
72 Sniffer(std::string const & interface)
74 sock.bind(senf::LLSocketAddress(interface));
79 senf::Scheduler::instance().add(
80 sock, senf::membind(&Sniffer::dumpPacket, this));
81 senf::Scheduler::instance().process();
85 void dumpPacket(senf::Scheduler::EventId event)
87 senf::EthernetPacket packet (
88 senf::EthernetPacket::create(senf::EthernetPacket::noinit));
89 sock.read(packet.data(),0);
90 packet.dump(std::cout);
92 packet.last().data().begin(),
93 packet.last().data().end(),
99 int scheduler_main(int argc, char const * argv[])
102 Sniffer sniffer (argv[2]);
105 catch (std::exception const & ex) {
106 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
111 int main(int argc, char const * argv[])
114 if (std::string(argv[1]) == "loop")
115 return loop_main(argc,argv);
116 else if (std::string(argv[1]) == "scheduler")
117 return scheduler_main(argc,argv);
119 std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
123 ///////////////////////////////cc.e////////////////////////////////////////
125 //#include "Sniffer.mpp"
131 // c-file-style: "senf"
132 // indent-tabs-mode: nil
133 // ispell-local-dictionary: "american"
134 // compile-command: "scons -u"
135 // comment-column: 40