summaryrefslogtreecommitdiff
authormickeyl <mickeyl>2004-04-07 09:49:19 (UTC)
committer mickeyl <mickeyl>2004-04-07 09:49:19 (UTC)
commitab892edccddb983b844065e2a13b15ecaecefbbd (patch) (unidiff)
tree1fea3f8f6aee0246295d226168c49ec3193e6622
parent837069ed5f048c99774c88f2ad05a057a0b76ce8 (diff)
downloadopie-ab892edccddb983b844065e2a13b15ecaecefbbd.zip
opie-ab892edccddb983b844065e2a13b15ecaecefbbd.tar.gz
opie-ab892edccddb983b844065e2a13b15ecaecefbbd.tar.bz2
copy data handed over by libpcap because libpcap reuses the data area
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp39
1 files changed, 21 insertions, 18 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp
index 93176c0..067d6b7 100644
--- a/libopie2/opienet/opcap.cpp
+++ b/libopie2/opienet/opcap.cpp
@@ -1,220 +1,223 @@
1/* 1/*
2                 This file is part of the Opie Project 2                 This file is part of the Opie Project
3              Copyright (C) 2003 by Michael 'Mickey' Lauer <mickey@Vanille.de> 3              Copyright (C) 2003-2004 by Michael 'Mickey' Lauer <mickey@Vanille.de>
4 =. 4 =.
5 .=l. 5 .=l.
6           .>+-= 6           .>+-=
7 _;:,     .>    :=|. This program is free software; you can 7 _;:,     .>    :=|. This program is free software; you can
8.> <`_,   >  .   <= redistribute it and/or modify it under 8.> <`_,   >  .   <= redistribute it and/or modify it under
9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public 9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
10.="- .-=="i,     .._ License as published by the Free Software 10.="- .-=="i,     .._ License as published by the Free Software
11 - .   .-<_>     .<> Foundation; either version 2 of the License, 11 - .   .-<_>     .<> Foundation; either version 2 of the License,
12     ._= =}       : or (at your option) any later version. 12     ._= =}       : or (at your option) any later version.
13    .%`+i>       _;_. 13    .%`+i>       _;_.
14    .i_,=:_.      -<s. This program is distributed in the hope that 14    .i_,=:_.      -<s. This program is distributed in the hope that
15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY; 15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
16    : ..    .:,     . . . without even the implied warranty of 16    : ..    .:,     . . . without even the implied warranty of
17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A 17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU 18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.=       =       ; Library General Public License for more 19..}^=.=       =       ; Library General Public License for more
20++=   -.     .`     .: details. 20++=   -.     .`     .: details.
21 :     =  ...= . :.=- 21 :     =  ...= . :.=-
22 -.   .:....=;==+<; You should have received a copy of the GNU 22 -.   .:....=;==+<; You should have received a copy of the GNU
23  -_. . .   )=.  = Library General Public License along with 23  -_. . .   )=.  = Library General Public License along with
24    --        :-=` this library; see the file COPYING.LIB. 24    --        :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation, 25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330, 26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. 27 Boston, MA 02111-1307, USA.
28 28
29*/ 29*/
30 30
31#include "udp_ports.h"
32#include "opcap.h"
33
31/* OPIE */ 34/* OPIE */
32#include <opie2/opcap.h>
33#include <opie2/odebug.h> 35#include <opie2/odebug.h>
36using namespace Opie::Core;
34 37
35/* QT */ 38/* QT */
36#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) 39#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects)
37#include <qsocketnotifier.h> 40#include <qsocketnotifier.h>
38#include <qobjectlist.h> 41#include <qobjectlist.h>
39 42
40/* SYSTEM */ 43/* STD */
41#include <sys/time.h> 44#include <sys/time.h>
42#include <sys/types.h> 45#include <sys/types.h>
46#include <assert.h>
43#include <unistd.h> 47#include <unistd.h>
44 48#include <string.h>
45/* LOCAL */
46#include "udp_ports.h"
47
48using namespace Opie::Core;
49 49
50namespace Opie { 50namespace Opie {
51namespace Net { 51namespace Net {
52 52
53/*====================================================================================== 53/*======================================================================================
54 * OPacket 54 * OPacket
55 *======================================================================================*/ 55 *======================================================================================*/
56 56
57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) 57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent )
58 :QObject( parent, "Generic" ), _hdr( header ), _data( data ) 58 :QObject( parent, "Generic" ), _hdr( header ), _data( 0 )
59{ 59{
60 //FIXME: Copy the data structure here, because it isn't persistent. Check that.
61
62 qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen );
63 60
64 _end = (unsigned char*) data + header.len; 61 _data = new unsigned char[sizeof data];
65 //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); 62 assert( _data );
63 memcpy( const_cast<unsigned char*>(_data), data, sizeof data );
64 // We have to copy the data structure here, because the 'data' pointer handed by libpcap
65 // points to an internal region which is reused by lipcap.
66 odebug << "OPacket: Length = " << header.len << ", Caplen = " << header.caplen << oendl;
67 _end = (unsigned char*) _data + header.len;
66 68
67 switch ( datalink ) 69 switch ( datalink )
68 { 70 {
69 case DLT_EN10MB: 71 case DLT_EN10MB:
70 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; 72 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl;
71 new OEthernetPacket( _end, (const struct ether_header*) data, this ); 73 new OEthernetPacket( _end, (const struct ether_header*) _data, this );
72 break; 74 break;
73 75
74 case DLT_IEEE802_11: 76 case DLT_IEEE802_11:
75 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; 77 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl;
76 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); 78 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) _data, this );
77 break; 79 break;
78 80
79 case DLT_PRISM_HEADER: 81 case DLT_PRISM_HEADER:
80 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; 82 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl;
81 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); 83 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) _data, this );
82 break; 84 break;
83 85
84 default: 86 default:
85 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; 87 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl;
86 } 88 }
87} 89}
88 90
89 91
90OPacket::~OPacket() 92OPacket::~OPacket()
91{ 93{
92 qDebug( "OPacket::~OPacket( %s )", name() ); 94 odebug << "~OPacket( " << name() << " )" << oendl;
95 delete _data;
93} 96}
94 97
95 98
96timevalstruct OPacket::timeval() const 99timevalstruct OPacket::timeval() const
97{ 100{
98 return _hdr.ts; 101 return _hdr.ts;
99} 102}
100 103
101 104
102int OPacket::caplen() const 105int OPacket::caplen() const
103{ 106{
104 return _hdr.caplen; 107 return _hdr.caplen;
105} 108}
106 109
107 110
108void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) 111void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l )
109{ 112{
110 if (!l) return; 113 if (!l) return;
111 QObject* o = l->first(); 114 QObject* o = l->first();
112 while ( o ) 115 while ( o )
113 { 116 {
114 stats[o->name()]++; 117 stats[o->name()]++;
115 updateStats( stats, const_cast<QObjectList*>( o->children() ) ); 118 updateStats( stats, const_cast<QObjectList*>( o->children() ) );
116 o = l->next(); 119 o = l->next();
117 } 120 }
118} 121}
119 122
120 123
121QString OPacket::dumpStructure() const 124QString OPacket::dumpStructure() const
122{ 125{
123 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; 126 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]";
124} 127}
125 128
126 129
127QString OPacket::_dumpStructure( QObjectList* l ) const 130QString OPacket::_dumpStructure( QObjectList* l ) const
128{ 131{
129 if (!l) return QString::null; 132 if (!l) return QString::null;
130 QObject* o = l->first(); 133 QObject* o = l->first();
131 QString str(" "); 134 QString str(" ");
132 135
133 while ( o ) 136 while ( o )
134 { 137 {
135 str.append( o->name() ); 138 str.append( o->name() );
136 str.append( " |" ); 139 str.append( " |" );
137 str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); 140 str += _dumpStructure( const_cast<QObjectList*>( o->children() ) );
138 o = l->next(); 141 o = l->next();
139 } 142 }
140 return str; 143 return str;
141} 144}
142 145
143QString OPacket::dump( int bpl ) const 146QString OPacket::dump( int bpl ) const
144{ 147{
145 static int index = 0; 148 static int index = 0;
146 index++; 149 index++;
147 int len = _hdr.caplen; 150 int len = _hdr.caplen;
148 QString str; 151 QString str;
149 str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); 152 str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len );
150 str.append( "0000: " ); 153 str.append( "0000: " );
151 QString tmp; 154 QString tmp;
152 QString bytes; 155 QString bytes;
153 QString chars; 156 QString chars;
154 157
155 for ( int i = 0; i < len; ++i ) 158 for ( int i = 0; i < len; ++i )
156 { 159 {
157 tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); 160 tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp );
158 if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); 161 if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] );
159 else chars.append( '.' ); 162 else chars.append( '.' );
160 163
161 if ( !((i+1) % bpl) ) 164 if ( !((i+1) % bpl) )
162 { 165 {
163 str.append( bytes ); 166 str.append( bytes );
164 str.append( ' ' ); 167 str.append( ' ' );
165 str.append( chars ); 168 str.append( chars );
166 str.append( '\n' ); 169 str.append( '\n' );
167 tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); 170 tmp.sprintf( "%04X: ", i+1 ); str.append( tmp );
168 bytes = ""; 171 bytes = "";
169 chars = ""; 172 chars = "";
170 } 173 }
171 174
172 } 175 }
173 if ( (len % bpl) ) 176 if ( (len % bpl) )
174 { 177 {
175 str.append( bytes.leftJustify( 1 + 3*bpl ) ); 178 str.append( bytes.leftJustify( 1 + 3*bpl ) );
176 str.append( chars ); 179 str.append( chars );
177 } 180 }
178 str.append( '\n' ); 181 str.append( '\n' );
179 return str; 182 return str;
180} 183}
181 184
182 185
183int OPacket::len() const 186int OPacket::len() const
184{ 187{
185 return _hdr.len; 188 return _hdr.len;
186} 189}
187 190
188 191
189QTextStream& operator<<( QTextStream& s, const OPacket& p ) 192QTextStream& operator<<( QTextStream& s, const OPacket& p )
190{ 193{
191 s << p.dumpStructure(); 194 s << p.dumpStructure();
192} 195}
193 196
194 197
195/*====================================================================================== 198/*======================================================================================
196 * OEthernetPacket 199 * OEthernetPacket
197 *======================================================================================*/ 200 *======================================================================================*/
198 201
199OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) 202OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent )
200 :QObject( parent, "Ethernet" ), _ether( data ) 203 :QObject( parent, "Ethernet" ), _ether( data )
201{ 204{
202 205
203 odebug << "Source = " << sourceAddress().toString(); 206 odebug << "Source = " << sourceAddress().toString();
204 odebug << "Destination = " << destinationAddress().toString(); 207 odebug << "Destination = " << destinationAddress().toString();
205 208
206 if ( sourceAddress() == OMacAddress::broadcast ) 209 if ( sourceAddress() == OMacAddress::broadcast )
207 odebug << "Source is broadcast address" << oendl; 210 odebug << "Source is broadcast address" << oendl;
208 if ( destinationAddress() == OMacAddress::broadcast ) 211 if ( destinationAddress() == OMacAddress::broadcast )
209 odebug << "Destination is broadcast address" << oendl; 212 odebug << "Destination is broadcast address" << oendl;
210 213
211 switch ( type() ) 214 switch ( type() )
212 { 215 {
213 case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; 216 case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break;
214 case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; 217 case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break;
215 case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; } 218 case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; }
216 default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl; 219 default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl;
217 } 220 }
218 221
219} 222}
220 223