Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

Sniffer.cc

Go to the documentation of this file.
00001 // $Id: Sniffer.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2006
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 // Definition of non-inline non-template functions
00024 
00025 //#include "Sniffer.hh"
00026 //#include "Sniffer.ih"
00027 
00028 // Custom includes
00029 #include <string>
00030 #include <iostream>
00031 #include <iomanip>
00032 #include <senf/Socket/Protocols/Raw.hh>
00033 #include <senf/Utils/membind.hh>
00034 #include <senf/Utils/hexdump.hh>
00035 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
00036 #include <senf/Scheduler/Scheduler.hh>
00037 
00038 //#include "Sniffer.mpp"
00039 #define prefix_
00040 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00041 
00042 int loop_main (int argc, char const * argv[])
00043 {
00044     try {
00045         senf::PacketSocketHandle sock;
00046         sock.bind(senf::LLSocketAddress(argv[2]));
00047         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
00048 
00049         while (true) { // forever
00050             senf::EthernetPacket packet (senf::EthernetPacket::create(
00051                                              senf::noinit));
00052             sock.read(packet.data(),0);
00053             packet.dump(std::cout);
00054             senf::hexdump(
00055                     packet.last().data().begin(),
00056                     packet.last().data().end(),
00057                     std::cout);
00058             std::cout << "\n\n";
00059         }
00060     }
00061     catch (std::exception const & ex) {
00062         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
00063     }
00064     return 0;
00065 }
00066 
00067 class Sniffer
00068 {
00069     senf::PacketSocketHandle sock;
00070     senf::scheduler::FdEvent event;
00071 
00072 public:
00073     Sniffer(std::string const & interface)
00074         : event ("Sniffer", senf::membind(&Sniffer::dumpPacket, this),
00075                  sock, senf::scheduler::FdEvent::EV_READ)
00076     {
00077         sock.bind(senf::LLSocketAddress(interface));
00078     }
00079 
00080     void run()
00081     {
00082         senf::scheduler::process();
00083     }
00084 
00085 private:
00086     void dumpPacket(int event)
00087     {
00088         senf::EthernetPacket packet (
00089             senf::EthernetPacket::create(senf::noinit));
00090         sock.read(packet.data(),0);
00091         packet.dump(std::cout);
00092         senf::hexdump(
00093                 packet.last().data().begin(),
00094                 packet.last().data().end(),
00095                 std::cout);
00096         std::cout << "\n\n";
00097     }
00098 };
00099 
00100 int scheduler_main(int argc, char const * argv[])
00101 {
00102     try {
00103         Sniffer sniffer (argv[2]);
00104         sniffer.run();
00105     }
00106     catch (std::exception const & ex) {
00107         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
00108     }
00109     return 0;
00110 }
00111 
00112 int main(int argc, char const * argv[])
00113 {
00114     std::cout << "Registered packets:\n\n";
00115     senf::dumpPacketRegistries(std::cout);
00116 
00117     if (argc >= 3) {
00118         if (std::string(argv[1]) == "loop")
00119             return loop_main(argc,argv);
00120         else if (std::string(argv[1]) == "scheduler")
00121             return scheduler_main(argc,argv);
00122     }
00123 
00124     std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
00125     return 1;
00126 }
00127 
00128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00129 #undef prefix_
00130 //#include "Sniffer.mpp"
00131 
00132 
00133 // Local Variables:
00134 // mode: c++
00135 // fill-column: 100
00136 // c-file-style: "senf"
00137 // indent-tabs-mode: nil
00138 // ispell-local-dictionary: "american"
00139 // compile-command: "scons -u"
00140 // comment-column: 40
00141 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research