patch #8333
[mldonkey.git] / tools / pandora / tcpprintcomponent.cc
blob59e6d1b1ffbeace2ab766282cf2459b6b82497b1
1 /* Copyright (C) 1999, 2000, 2001, 2002, 2003 Simon Patarin, INRIA
3 This file is part of Pandora, the Flexible Monitoring Platform.
5 Pandora is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 Pandora is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pandora; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
21 #include <libpandora/global.h>
23 #include "tcpprintcomponent.h"
24 #include <pandora_components/ippacket.h>
25 #include <pandora_components/tcppacket.h>
26 #include <pandora_components/udppacket.h>
28 component_export(TCPPrintComponent, TCPPacket+|UDPPacket+,);
30 int TCPPrintComponent::id = 0;
32 bool TCPPrintComponent::add(Packet *pkt)
34 int sport = 0, dport = 0;
36 locatePacket0(TCPPacket, tcpp, pkt);
37 locatePacket0(UDPPacket, udpp, pkt);
39 if (tcpp != NULL) {
40 sport = tcpp->sport;
41 dport = tcpp->dport;
42 } else if (udpp != NULL) {
43 sport = udpp->sport;
44 dport = udpp->dport;
47 locatePacket(IPPacket, ipp, pkt);
49 if (ipp == NULL) {
50 cleanPacket(pkt);
51 return false;
54 int len = ipp->dlength();
56 if (len == 0) {
57 discard(pkt);
58 return false;
61 cout << "new_packet ";
63 char *data = ipp->data();
64 if (tcpp != NULL) {
65 cout << "TCP " << cnx;
66 } else {
67 cout << "UDP " << cnx;
70 cout << " \"" << ipp->src << "\" " << ntohs(sport) << " \""
71 << ipp->dst << "\" " << ntohs(dport) << "\n";
73 cout << "\"";
74 for (int i = 0; i < len; ++i) {
75 int c = (int) (unsigned char)data[i];
76 if (c == '\\') cout << "\\\\";
77 else
78 if (c == '"') cout << "\\\"";
79 else
80 if (c == '\n' || (c > 31 && c < 127))
81 cout << data[i];
82 else
83 printf("\\%03d", c);
85 printf( "\";\n");
87 cout << "\n(*----------------------------------------------------------------------*)\n";
89 discard(pkt);
90 return false;