Sniffer.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 #include <string>
15 #include <iostream>
16 #include <iomanip>
18 #include <senf/Utils/membind.hh>
19 #include <senf/Utils/hexdump.hh>
22 
23 //#include "Sniffer.mpp"
24 #define prefix_
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 int loop_main (int argc, char const * argv[])
28 {
29  try {
31  sock.bind(senf::LLSocketAddress(argv[2]));
32  // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
33 
34  while (true) { // forever
35  senf::EthernetPacket packet (senf::EthernetPacket::create(
36  senf::noinit));
37  sock.read(packet.data(),0);
38  packet.dump(std::cout);
39  senf::hexdump(
40  packet.last().data().begin(),
41  packet.last().data().end(),
42  std::cout);
43  std::cout << "\n\n";
44  }
45  }
46  catch (std::exception const & ex) {
47  std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
48  }
49  return 0;
50 }
51 
52 class Sniffer
53 {
56 
57 public:
58  Sniffer(std::string const & interface)
59  : event ("Sniffer", senf::membind(&Sniffer::dumpPacket, this),
60  sock, senf::scheduler::FdEvent::EV_READ)
61  {
62  sock.bind(senf::LLSocketAddress(interface));
63  }
64 
65  void run()
66  {
67  senf::scheduler::process();
68  }
69 
70 private:
71  void dumpPacket(int event)
72  {
73  senf::EthernetPacket packet (
74  senf::EthernetPacket::create(senf::noinit));
75  sock.read(packet.data(),0);
76  packet.dump(std::cout);
77  senf::hexdump(
78  packet.last().data().begin(),
79  packet.last().data().end(),
80  std::cout);
81  std::cout << "\n\n";
82  }
83 };
84 
85 int scheduler_main(int argc, char const * argv[])
86 {
87  try {
88  Sniffer sniffer (argv[2]);
89  sniffer.run();
90  }
91  catch (std::exception const & ex) {
92  std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
93  }
94  return 0;
95 }
96 
97 int main(int argc, char const * argv[])
98 {
99  std::cout << "Registered packets:\n\n";
100  senf::dumpPacketRegistries(std::cout);
101 
102  if (argc >= 3) {
103  if (std::string(argv[1]) == "loop")
104  return loop_main(argc,argv);
105  else if (std::string(argv[1]) == "scheduler")
106  return scheduler_main(argc,argv);
107  }
108 
109  std::cerr << "Usage: sniffer { loop | scheduler } [interface]" << std::endl;
110  return 1;
111 }
112 
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 #undef prefix_
115 //#include "Sniffer.mpp"
116 
117 
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u"
125 // comment-column: 40
126 // End:
int loop_main(int argc, char const *argv[])
Definition: Sniffer.cc:27
void run()
Definition: Sniffer.cc:65
boost::function< R(Args)> membind(R(T::*fn)(Args), T *ob)
noinit
int main(int argc, char const *argv[])
Definition: Sniffer.cc:97
Sniffer(std::string const &interface)
Definition: Sniffer.cc:58
ProtocolClientSocketHandle< PacketSocketProtocol > PacketSocketHandle
int scheduler_main(int argc, char const *argv[])
Definition: Sniffer.cc:85
ConcretePacket< EthernetPacketType > EthernetPacket