4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
25 // Stefan Bund <g0dil@berlios.de>
28 /** \mainpage Simple packet sniffer reading and dumping raw network packets
30 \dontinclude Sniffer.cc
32 The Sniffer application is a simple command line network sniffer like \c tcpdump or \c
33 tethereal. The application uses a packet socket to read Ethernet packets from the \c eth0
34 interface and dumps the parsed packets out to the standard output.
36 To try out the example application, check out the library, go to the \c %Sniffer
42 < Hit Ctrl-C when you've seen enough >
44 < Hit Ctrl-C when you've seen enough >
47 We will now look at the code which is found in \c Sniffer.cc in the <tt>Examples/%Sniffer</tt>
48 directory. The code starts out by including the necessary headers
50 \skip // Custom includes
51 \until #include <senf/Scheduler/Scheduler.hh>
53 The example includes two implementations, one using blocking calls and a while loop, the other
54 using the senf::Scheduler for asynchronous event notification. They are implemented in
55 \c loop_main() and \c scheduler_main(). They will be documented below. For now, we skip these
56 implementations and go straight to the \c main() function
62 This routine simply interprets the first command line argument and dispatches to the required
65 Now lets go back and study each implementation in detail.
67 \dontinclude Sniffer.cc
69 \section example_loop A Blocking Implementation
71 This implementation is found in the \c loop_main function.
76 We catch all exceptions in a \c try block. This is good for a deliverable binary. When debugging
77 the application, it might be better to let the exception \c abort the execution so you can get a
78 backtrace of the exception origin in the debugger.
80 We create a packet socket and bind it to the interface given as second command line argument. A
81 packet socket is a linux specific type of socket which returns ethernet packets directly from
82 the network wire. By uncommenting the last line, you may switch the interface into promiscuous
87 We will now read packets from the socket forever, that is until the user hits Ctrl-C
92 The next step is, to parse the data read from the socket as an Ethernet packet
96 There are several ways to read and parse a packet with different tradeoffs between efficiency
97 and simplicity. The Version we use here is already quite efficient.
99 We begin by pre-declaring an uninitialized senf::EthernetPacket instance. By uninitialized we
100 mean, that the instance is not parseable and has a length of 0 bytes. This differs from a
101 default-constructed packet instance which may have initial content and \e is parseable.
103 We then tell the socket to read as much data as is available into the packet. The second arg to
104 read specifies the maximum number of bytes to read or 0 to read as much as possible. We pass
105 <tt>packet.data()</tt> to <tt>socket.read</tt> which is an STL compatible container holding the
106 data bytes of our previously created senf::EthernetPacket instance (which is currently empty).
108 The next step is to write out the packet to the standard output
112 The \c dump call will write out a complete representation of the parsed packet data. The Packet
113 library will \e not try to interpret payload data as long as no exact indication of the payload
114 type is available (example: A UDP Payload is not parsed further unless you explicitly tell the
115 library, how to parse it). Tools like \c tethereal guess the payload type by checking port
116 numbers and the payload data, however this is not advisable for a general purpose packet
119 The next line, \c hexdump, will write out the \e last packet component. Packets are managed as a
120 chain of headers. The last header is normally a \c DataPacket holding the payload data.
122 That's it. We finish of by catching the exception and giving as much detail as possible if an
129 The \c prettyName function from the \c Utils library is used, to get a nice, printable
130 representation of the \e dynamic type of the exception instance. It is an interface to the g++
131 de-mangler. This is necessary since the \c name member of the C++ \c type_info instance is a
132 mangled name in \c g++.
134 That's it for the simple blocking implementation.
136 \section example_scheduler Using the Scheduler
138 However, we have another one which uses the Scheduler.
142 The class constructor binds the socket defined as a data member to the correct interface. To
143 tell the scheduler to call us back whenever data is available on the socket, we add a
144 senf::scheduler::FdEvent instance to out class.
146 The senf::scheduler::FdEvent constructor takes several arguments:
147 \li a string describing the event.
148 \li the callback to call whenever the event occurs. The callback is specified as a <a
149 href="http://www.boost.org/doc/libs/release/doc/html/function.html">Boost.Function</a>
150 object. We use the \c senf::membind helper from the Utils library to build such a
151 function object. This helper takes an arbitrary class member and binds it to a specific
153 \li the handle or file descriptor to monitor.
154 \li and the events to watch for.
158 The public \c run() member is called to run the sniffer. Here we just forward the call to the
159 scheduler. Calling the Schedulers \c process() method will start the event loop. This call does
160 not return (ok, that's a lie. It does return when \c senf::scheduler::terminate() is called
161 which does not apply here).
165 The \c dumpPacket() member is called by the scheduler whenever an event on the socket is
166 encountered. The scheduler calls this function with a mask of the events which triggered the
171 The body is absolutely identical to the body of the \c while loop of the blocking
172 implementation. However, the scheduler guarantees, that a read on the socket will not block if
173 the socket is triggered to be readable (even if the socket is not set to non-blocking mode).
175 What's left is the \c scheduler_main() function to utilize this code
180 This function is straight forward. The exception handling is the same as in \c loop_main().
182 \see \ref senf_components \n
184 <a href="../../../../senf/Socket/doc/html/index.html"><b>libSocket API reference</b></a> \n
185 <a href="../../../../senf/Packets/doc/html/index.html"><b>libPackets API reference</b></a> \n
186 <a href="../../../../senf/Utils/doc/html/index.html"><b>libUtils API reference</b></a>
193 // comment-column: 40
194 // c-file-style: "senf"
195 // indent-tabs-mode: nil
196 // ispell-local-dictionary: "american"
197 // compile-command: "scons -u doc"