5c81ddafe82ff4672da82598f9ebdca89c0b043f
[senf.git] / Examples / Sniffer / Sniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 <senf/Socket/Protocols/Raw.hh>
33 #include <senf/Utils/membind.hh>
34 #include <senf/Utils/hexdump.hh>
35 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
36 #include <senf/Scheduler/Scheduler.hh>
37
38 //#include "Sniffer.mpp"
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 int loop_main (int argc, char const * argv[])
43 {
44     try {
45         senf::PacketSocketHandle sock;
46         sock.bind(senf::LLSocketAddress(argv[2]));
47         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
48
49         while (true) { // forever
50             senf::EthernetPacket packet (senf::EthernetPacket::create(
51                                              senf::noinit));
52             sock.read(packet.data(),0);
53             packet.dump(std::cout);
54             senf::hexdump(
55                     packet.last().data().begin(),
56                     packet.last().data().end(),
57                     std::cout);
58             std::cout << "\n\n";
59         }
60     }
61     catch (std::exception const & ex) {
62         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
63     }
64     return 0;
65 }
66
67 class Sniffer
68 {
69     senf::PacketSocketHandle sock;
70     senf::scheduler::FdEvent event;
71
72 public:
73     Sniffer(std::string const & interface)
74         : event ("Sniffer", senf::membind(&Sniffer::dumpPacket, this),
75                  sock, senf::scheduler::FdEvent::EV_READ)
76     {
77         sock.bind(senf::LLSocketAddress(interface));
78     }
79
80     void run()
81     {
82         senf::scheduler::process();
83     }
84
85 private:
86     void dumpPacket(int event)
87     {
88         senf::EthernetPacket packet (
89             senf::EthernetPacket::create(senf::noinit));
90         sock.read(packet.data(),0);
91         packet.dump(std::cout);
92         senf::hexdump(
93                 packet.last().data().begin(),
94                 packet.last().data().end(),
95                 std::cout);
96         std::cout << "\n\n";
97     }
98 };
99
100 int scheduler_main(int argc, char const * argv[])
101 {
102     try {
103         Sniffer sniffer (argv[2]);
104         sniffer.run();
105     }
106     catch (std::exception const & ex) {
107         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
108     }
109     return 0;
110 }
111
112 int main(int argc, char const * argv[])
113 {
114     std::cout << "Registered packets:\n\n";
115     senf::dumpPacketRegistries(std::cout);
116
117     if (argc >= 3) {
118         if (std::string(argv[1]) == "loop")
119             return loop_main(argc,argv);
120         else if (std::string(argv[1]) == "scheduler")
121             return scheduler_main(argc,argv);
122     }
123
124     std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
125     return 1;
126 }
127
128 ///////////////////////////////cc.e////////////////////////////////////////
129 #undef prefix_
130 //#include "Sniffer.mpp"
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u"
140 // comment-column: 40
141 // End: