4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 // Definition of non-inline non-template functions
31 //#include "Sniffer.hh"
32 //#include "Sniffer.ih"
38 #include <senf/Socket/Protocols/Raw.hh>
39 #include <senf/Utils/membind.hh>
40 #include <senf/Utils/hexdump.hh>
41 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
42 #include <senf/Scheduler/Scheduler.hh>
44 //#include "Sniffer.mpp"
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48 int loop_main (int argc, char const * argv[])
51 senf::PacketSocketHandle sock;
52 sock.bind(senf::LLSocketAddress(argv[2]));
53 // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
55 while (true) { // forever
56 senf::EthernetPacket packet (senf::EthernetPacket::create(
58 sock.read(packet.data(),0);
59 packet.dump(std::cout);
61 packet.last().data().begin(),
62 packet.last().data().end(),
67 catch (std::exception const & ex) {
68 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
75 senf::PacketSocketHandle sock;
76 senf::scheduler::FdEvent event;
79 Sniffer(std::string const & interface)
80 : event ("Sniffer", senf::membind(&Sniffer::dumpPacket, this),
81 sock, senf::scheduler::FdEvent::EV_READ)
83 sock.bind(senf::LLSocketAddress(interface));
88 senf::scheduler::process();
92 void dumpPacket(int event)
94 senf::EthernetPacket packet (
95 senf::EthernetPacket::create(senf::noinit));
96 sock.read(packet.data(),0);
97 packet.dump(std::cout);
99 packet.last().data().begin(),
100 packet.last().data().end(),
106 int scheduler_main(int argc, char const * argv[])
109 Sniffer sniffer (argv[2]);
112 catch (std::exception const & ex) {
113 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
118 int main(int argc, char const * argv[])
120 std::cout << "Registered packets:\n\n";
121 senf::dumpPacketRegistries(std::cout);
124 if (std::string(argv[1]) == "loop")
125 return loop_main(argc,argv);
126 else if (std::string(argv[1]) == "scheduler")
127 return scheduler_main(argc,argv);
130 std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
136 //#include "Sniffer.mpp"
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u"
146 // comment-column: 40