Add 'include/senf' directory
[senf.git] / Examples / Sniffer / Sniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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/Scheduler/Scheduler.hh>
34 #include <senf/Utils/membind.hh>
35 #include <senf/Utils/hexdump.hh>
36 #include <senf/Packets/DefaultBundle/EthernetPacket.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::EthernetPacket::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
71 public:
72     Sniffer(std::string const & interface) 
73     {
74         sock.bind(senf::LLSocketAddress(interface)); 
75     }
76
77     void run() 
78     {
79         senf::Scheduler::instance().add(
80             sock, senf::membind(&Sniffer::dumpPacket, this));
81         senf::Scheduler::instance().process();
82     }
83          
84 private:
85     void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
86     {
87         senf::EthernetPacket packet (
88             senf::EthernetPacket::create(senf::EthernetPacket::noinit));
89         sock.read(packet.data(),0);
90         packet.dump(std::cout);
91         senf::hexdump(
92                 packet.last().data().begin(),
93                 packet.last().data().end(),
94                 std::cout);
95         std::cout << "\n\n";
96     }
97 };
98
99 int scheduler_main(int argc, char const * argv[])
100 {
101     try {
102         Sniffer sniffer (argv[2]);
103         sniffer.run();
104     }
105     catch (std::exception const & ex) {
106         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
107     }
108     return 0;
109 }
110
111 int main(int argc, char const * argv[])
112 {
113     if (argc >= 3)
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);
118
119     std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
120     return 1;
121 }
122
123 ///////////////////////////////cc.e////////////////////////////////////////
124 #undef prefix_
125 //#include "Sniffer.mpp"
126
127 \f
128 // Local Variables:
129 // mode: c++
130 // fill-column: 100
131 // c-file-style: "senf"
132 // indent-tabs-mode: nil
133 // ispell-local-dictionary: "american"
134 // compile-command: "scons -u"
135 // comment-column: 40
136 // End: