switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / Examples / Sniffer / Sniffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28
29 // Definition of non-inline non-template functions
30
31 //#include "Sniffer.hh"
32 //#include "Sniffer.ih"
33
34 // Custom includes
35 #include <string>
36 #include <iostream>
37 #include <iomanip>
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>
43
44 //#include "Sniffer.mpp"
45 #define prefix_
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 int loop_main (int argc, char const * argv[])
49 {
50     try {
51         senf::PacketSocketHandle sock;
52         sock.bind(senf::LLSocketAddress(argv[2]));
53         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
54
55         while (true) { // forever
56             senf::EthernetPacket packet (senf::EthernetPacket::create(
57                                              senf::noinit));
58             sock.read(packet.data(),0);
59             packet.dump(std::cout);
60             senf::hexdump(
61                     packet.last().data().begin(),
62                     packet.last().data().end(),
63                     std::cout);
64             std::cout << "\n\n";
65         }
66     }
67     catch (std::exception const & ex) {
68         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
69     }
70     return 0;
71 }
72
73 class Sniffer
74 {
75     senf::PacketSocketHandle sock;
76     senf::scheduler::FdEvent event;
77
78 public:
79     Sniffer(std::string const & interface)
80         : event ("Sniffer", senf::membind(&Sniffer::dumpPacket, this),
81                  sock, senf::scheduler::FdEvent::EV_READ)
82     {
83         sock.bind(senf::LLSocketAddress(interface));
84     }
85
86     void run()
87     {
88         senf::scheduler::process();
89     }
90
91 private:
92     void dumpPacket(int event)
93     {
94         senf::EthernetPacket packet (
95             senf::EthernetPacket::create(senf::noinit));
96         sock.read(packet.data(),0);
97         packet.dump(std::cout);
98         senf::hexdump(
99                 packet.last().data().begin(),
100                 packet.last().data().end(),
101                 std::cout);
102         std::cout << "\n\n";
103     }
104 };
105
106 int scheduler_main(int argc, char const * argv[])
107 {
108     try {
109         Sniffer sniffer (argv[2]);
110         sniffer.run();
111     }
112     catch (std::exception const & ex) {
113         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
114     }
115     return 0;
116 }
117
118 int main(int argc, char const * argv[])
119 {
120     std::cout << "Registered packets:\n\n";
121     senf::dumpPacketRegistries(std::cout);
122
123     if (argc >= 3) {
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);
128     }
129
130     std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
131     return 1;
132 }
133
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 #undef prefix_
136 //#include "Sniffer.mpp"
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u"
146 // comment-column: 40
147 // End: