530bcb564fad82e9b27008598b453e9c3988f9ba
[senf.git] / 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 "Socket/PacketSocketHandle.hh"
33 #include "Packets/EthernetPacket.hh"
34 #include "Packets/IpV4Packet.hh"
35 #include "Packets/UDPPacket.hh"
36
37 //#include "Sniffer.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42
43     static const unsigned BLOCK_SIZE = 16;
44
45     template <class Iterator>
46     void hexdump(Iterator i, Iterator const & i_end)
47     {
48         unsigned offset (0);
49         std::string ascii;
50         for (; i != i_end; ++i, ++offset) {
51             switch (offset % BLOCK_SIZE) {
52             case 0:
53                 if (!ascii.empty()) {
54                     std::cout << "  " << ascii << "\n";
55                     ascii = "";
56                 }
57                 std::cout << "  " 
58                           << std::hex << std::setw(4) << std::setfill('0') 
59                           << offset << ' ';
60                 break;
61             case BLOCK_SIZE/2:
62                 std::cout << " ";
63                 ascii += ' ';
64                 break;
65             }
66             std::cout << ' ' << std::hex << std::setw(2) << std::setfill('0') 
67                       << unsigned(*i);
68             ascii += (*i >= ' ' && *i < 126) ? *i : '.';
69         }
70         if (!ascii.empty()) {
71             for (; (offset % BLOCK_SIZE) != 0; ++offset) {
72                 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
73                     std::cout << " ";
74                 std::cout << "   ";
75             }
76             std::cout << "  " << ascii << "\n";
77         }
78         std::cout << std::dec;
79     }
80
81     void use_em()
82     {
83         // Pull in symbols from the wanted packets ...
84         senf::Packet::create<senf::IpV4Packet>(0,0);
85         senf::Packet::create<senf::UDPPacket>(0,0);
86     }
87 }
88
89 int main (int argc, char const * argv[])
90 {
91     try {
92         senf::PacketSocketHandle sock;
93         sock.bind(senf::LLSocketAddress("eth0"));
94         // sock.protocol().promisc("eth0",senf::PacketProtocol::Promiscuous);
95         
96         while (true) { // forever
97             std::string data (sock.read());
98             senf::EthernetPacket::ptr packet (
99                 senf::Packet::create<senf::EthernetPacket>(
100                     data.begin(), data.end()));
101             packet->dump(std::cout);
102             hexdump(packet->last()->begin(),
103                     packet->last()->end());
104             std::cout << "\n\n";
105         }
106     }
107     catch (std::exception const & ex) {
108         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
109     }
110 }
111
112 ///////////////////////////////cc.e////////////////////////////////////////
113 #undef prefix_
114 //#include "Sniffer.mpp"
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // c-file-style: "senf"
120 // End: