4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
23 // Definition of non-inline non-template functions
25 //#include "Sniffer.hh"
26 //#include "Sniffer.ih"
32 #include "Socket/PacketSocketHandle.hh"
33 #include "Scheduler/Scheduler.hh"
34 #include "Utils/membind.hh"
36 #include "Packets/EthernetPacket.hh"
37 #include "Packets/IpV4Packet.hh"
38 #include "Packets/UDPPacket.hh"
40 //#include "Sniffer.mpp"
42 ///////////////////////////////cc.p////////////////////////////////////////
46 static const unsigned BLOCK_SIZE = 16;
48 template <class Iterator>
49 void hexdump(Iterator i, Iterator const & i_end)
53 for (; i != i_end; ++i, ++offset) {
54 switch (offset % BLOCK_SIZE) {
57 std::cout << " " << ascii << "\n";
61 << std::hex << std::setw(4) << std::setfill('0')
69 std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0')
71 ascii += (*i >= ' ' && *i < 126) ? *i : '.';
74 for (; (offset % BLOCK_SIZE) != 0; ++offset) {
75 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
79 std::cout << " " << ascii << "\n";
81 std::cout << std::dec;
86 // Pull in symbols from the wanted packets ...
87 senf::Packet::create<senf::IpV4Packet>(0,0);
88 senf::Packet::create<senf::UDPPacket>(0,0);
92 int loop_main (int argc, char const * argv[])
95 senf::PacketSocketHandle sock;
96 sock.bind(senf::LLSocketAddress("eth0"));
97 // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
99 while (true) { // forever
100 std::string data (sock.read());
101 senf::EthernetPacket::ptr packet (
102 senf::Packet::create<senf::EthernetPacket>(
103 data.begin(), data.end()));
104 packet->dump(std::cout);
105 hexdump(packet->last()->begin(),
106 packet->last()->end());
110 catch (std::exception const & ex) {
111 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
118 senf::PacketSocketHandle sock;
121 Sniffer(std::string const & interface)
123 sock.bind(senf::LLSocketAddress(interface));
128 senf::Scheduler::instance().add(
129 sock, senf::membind(&Sniffer::dumpPacket, this));
130 senf::Scheduler::instance().process();
134 void dumpPacket(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
136 std::string data (sock.read());
137 senf::EthernetPacket::ptr packet (
138 senf::Packet::create<senf::EthernetPacket>(
139 data.begin(), data.end()));
140 packet->dump(std::cout);
141 hexdump(packet->last()->begin(),
142 packet->last()->end());
147 int scheduler_main(int argc, char const * argv[])
150 Sniffer sniffer ("eth0");
153 catch (std::exception const & ex) {
154 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
159 int main(int argc, char const * argv[])
162 if (std::string(argv[1]) == "loop")
163 return loop_main(argc,argv);
164 else if (std::string(argv[1]) == "scheduler")
165 return scheduler_main(argc,argv);
167 std::cerr << "Usage: sniffer { loop | scheduler }" << std::endl;
171 ///////////////////////////////cc.e////////////////////////////////////////
173 //#include "Sniffer.mpp"
179 // c-file-style: "senf"
180 // indent-tabs-mode: nil
181 // ispell-local-dictionary: "american"