-rw-r--r-- | libopie2/opienet/opcap.cpp | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 8ec4f47..8184f21 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -23,276 +23,274 @@ | |||
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" | 31 | #include "udp_ports.h" |
32 | #include "opcap.h" | 32 | #include "opcap.h" |
33 | 33 | ||
34 | /* OPIE */ | 34 | /* OPIE */ |
35 | #include <opie2/odebug.h> | 35 | #include <opie2/odebug.h> |
36 | using namespace Opie::Core; | 36 | using namespace Opie::Core; |
37 | 37 | ||
38 | /* QT */ | 38 | /* QT */ |
39 | #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) |
40 | #include <qsocketnotifier.h> | 40 | #include <qsocketnotifier.h> |
41 | #include <qobjectlist.h> | 41 | #include <qobjectlist.h> |
42 | 42 | ||
43 | /* STD */ | 43 | /* STD */ |
44 | #include <sys/time.h> | 44 | #include <sys/time.h> |
45 | #include <sys/types.h> | 45 | #include <sys/types.h> |
46 | #include <assert.h> | 46 | #include <assert.h> |
47 | #include <unistd.h> | 47 | #include <unistd.h> |
48 | #include <string.h> | 48 | #include <string.h> |
49 | 49 | ||
50 | namespace Opie { | 50 | namespace Opie { |
51 | namespace Net { | 51 | namespace Net { |
52 | 52 | ||
53 | /*====================================================================================== | 53 | /*====================================================================================== |
54 | * OPacket | 54 | * OPacket |
55 | *======================================================================================*/ | 55 | *======================================================================================*/ |
56 | 56 | ||
57 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) | 57 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) |
58 | :QObject( parent, "Generic" ), _hdr( header ), _data( 0 ) | 58 | :QObject( parent, "Generic" ), _hdr( header ), _data( 0 ) |
59 | { | 59 | { |
60 | 60 | ||
61 | _data = new unsigned char[ header.len ]; | 61 | _data = new unsigned char[ header.len ]; |
62 | assert( _data ); | 62 | assert( _data ); |
63 | memcpy( const_cast<unsigned char*>(_data), data, header.len ); | 63 | memcpy( const_cast<unsigned char*>(_data), data, header.len ); |
64 | // We have to copy the data structure here, because the 'data' pointer handed by libpcap | 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. | 65 | // points to an internal region which is reused by lipcap. |
66 | odebug << "OPacket: Length = " << header.len << ", Caplen = " << header.caplen << oendl; | 66 | odebug << "OPacket: Length = " << header.len << ", Caplen = " << header.caplen << oendl; |
67 | _end = (unsigned char*) _data + header.len; | 67 | _end = (unsigned char*) _data + header.len; |
68 | 68 | ||
69 | switch ( datalink ) | 69 | switch ( datalink ) |
70 | { | 70 | { |
71 | case DLT_EN10MB: | 71 | case DLT_EN10MB: |
72 | odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; | 72 | odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; |
73 | new OEthernetPacket( _end, (const struct ether_header*) _data, this ); | 73 | new OEthernetPacket( _end, (const struct ether_header*) _data, this ); |
74 | break; | 74 | break; |
75 | 75 | ||
76 | case DLT_IEEE802_11: | 76 | case DLT_IEEE802_11: |
77 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; | 77 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; |
78 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) _data, this ); | 78 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) _data, this ); |
79 | break; | 79 | break; |
80 | 80 | ||
81 | case DLT_PRISM_HEADER: | 81 | case DLT_PRISM_HEADER: |
82 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; | 82 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; |
83 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) _data, this ); | 83 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) _data, this ); |
84 | break; | 84 | break; |
85 | 85 | ||
86 | default: | 86 | default: |
87 | owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; | 87 | owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; |
88 | } | 88 | } |
89 | } | 89 | } |
90 | 90 | ||
91 | 91 | ||
92 | OPacket::~OPacket() | 92 | OPacket::~OPacket() |
93 | { | 93 | { |
94 | odebug << "~OPacket( " << name() << " )" << oendl; | 94 | odebug << "~OPacket( " << name() << " )" << oendl; |
95 | delete _data; | 95 | delete _data; |
96 | } | 96 | } |
97 | 97 | ||
98 | 98 | ||
99 | timevalstruct OPacket::timeval() const | 99 | timevalstruct OPacket::timeval() const |
100 | { | 100 | { |
101 | return _hdr.ts; | 101 | return _hdr.ts; |
102 | } | 102 | } |
103 | 103 | ||
104 | 104 | ||
105 | int OPacket::caplen() const | 105 | int OPacket::caplen() const |
106 | { | 106 | { |
107 | return _hdr.caplen; | 107 | return _hdr.caplen; |
108 | } | 108 | } |
109 | 109 | ||
110 | 110 | ||
111 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) | 111 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) |
112 | { | 112 | { |
113 | if (!l) return; | 113 | if (!l) return; |
114 | QObject* o = l->first(); | 114 | QObject* o = l->first(); |
115 | while ( o ) | 115 | while ( o ) |
116 | { | 116 | { |
117 | stats[o->name()]++; | 117 | stats[o->name()]++; |
118 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); | 118 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); |
119 | o = l->next(); | 119 | o = l->next(); |
120 | } | 120 | } |
121 | } | 121 | } |
122 | 122 | ||
123 | 123 | ||
124 | QString OPacket::dumpStructure() const | 124 | QString OPacket::dumpStructure() const |
125 | { | 125 | { |
126 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; | 126 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; |
127 | } | 127 | } |
128 | 128 | ||
129 | 129 | ||
130 | QString OPacket::_dumpStructure( QObjectList* l ) const | 130 | QString OPacket::_dumpStructure( QObjectList* l ) const |
131 | { | 131 | { |
132 | if (!l) return QString::null; | 132 | if (!l) return QString::null; |
133 | QObject* o = l->first(); | 133 | QObject* o = l->first(); |
134 | QString str(" "); | 134 | QString str(" "); |
135 | 135 | ||
136 | while ( o ) | 136 | while ( o ) |
137 | { | 137 | { |
138 | str.append( o->name() ); | 138 | str.append( o->name() ); |
139 | str.append( " |" ); | 139 | str.append( " |" ); |
140 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); | 140 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); |
141 | o = l->next(); | 141 | o = l->next(); |
142 | } | 142 | } |
143 | return str; | 143 | return str; |
144 | } | 144 | } |
145 | 145 | ||
146 | QString OPacket::dump( int bpl ) const | 146 | QString OPacket::dump( int bpl ) const |
147 | { | 147 | { |
148 | static int index = 0; | 148 | static int index = 0; |
149 | index++; | 149 | index++; |
150 | int len = _hdr.caplen; | 150 | int len = _hdr.caplen; |
151 | QString str; | 151 | QString str( "000:" ); |
152 | str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); | ||
153 | str.append( "0000: " ); | ||
154 | QString tmp; | 152 | QString tmp; |
155 | QString bytes; | 153 | QString bytes; |
156 | QString chars; | 154 | QString chars; |
157 | 155 | ||
158 | for ( int i = 0; i < len; ++i ) | 156 | for ( int i = 0; i < len; ++i ) |
159 | { | 157 | { |
160 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); | 158 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); |
161 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); | 159 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); |
162 | else chars.append( '.' ); | 160 | else chars.append( '.' ); |
163 | 161 | ||
164 | if ( !((i+1) % bpl) ) | 162 | if ( !((i+1) % bpl) ) |
165 | { | 163 | { |
166 | str.append( bytes ); | 164 | str.append( bytes ); |
167 | str.append( ' ' ); | 165 | str.append( ' ' ); |
168 | str.append( chars ); | 166 | str.append( chars ); |
169 | str.append( '\n' ); | 167 | str.append( '\n' ); |
170 | tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); | 168 | tmp.sprintf( "%03X:", i+1 ); str.append( tmp ); |
171 | bytes = ""; | 169 | bytes = ""; |
172 | chars = ""; | 170 | chars = ""; |
173 | } | 171 | } |
174 | 172 | ||
175 | } | 173 | } |
176 | if ( (len % bpl) ) | 174 | if ( (len % bpl) ) |
177 | { | 175 | { |
178 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); | 176 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); |
179 | str.append( chars ); | 177 | str.append( chars ); |
180 | } | 178 | } |
181 | str.append( '\n' ); | 179 | str.append( '\n' ); |
182 | return str; | 180 | return str; |
183 | } | 181 | } |
184 | 182 | ||
185 | 183 | ||
186 | int OPacket::len() const | 184 | int OPacket::len() const |
187 | { | 185 | { |
188 | return _hdr.len; | 186 | return _hdr.len; |
189 | } | 187 | } |
190 | 188 | ||
191 | 189 | ||
192 | QTextStream& operator<<( QTextStream& s, const OPacket& p ) | 190 | QTextStream& operator<<( QTextStream& s, const OPacket& p ) |
193 | { | 191 | { |
194 | s << p.dumpStructure(); | 192 | s << p.dumpStructure(); |
195 | } | 193 | } |
196 | 194 | ||
197 | 195 | ||
198 | /*====================================================================================== | 196 | /*====================================================================================== |
199 | * OEthernetPacket | 197 | * OEthernetPacket |
200 | *======================================================================================*/ | 198 | *======================================================================================*/ |
201 | 199 | ||
202 | OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) | 200 | OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) |
203 | :QObject( parent, "Ethernet" ), _ether( data ) | 201 | :QObject( parent, "Ethernet" ), _ether( data ) |
204 | { | 202 | { |
205 | 203 | ||
206 | odebug << "Source = " << sourceAddress().toString() << oendl; | 204 | odebug << "Source = " << sourceAddress().toString() << oendl; |
207 | odebug << "Destination = " << destinationAddress().toString() << oendl; | 205 | odebug << "Destination = " << destinationAddress().toString() << oendl; |
208 | 206 | ||
209 | if ( sourceAddress() == OMacAddress::broadcast ) | 207 | if ( sourceAddress() == OMacAddress::broadcast ) |
210 | odebug << "Source is broadcast address" << oendl; | 208 | odebug << "Source is broadcast address" << oendl; |
211 | if ( destinationAddress() == OMacAddress::broadcast ) | 209 | if ( destinationAddress() == OMacAddress::broadcast ) |
212 | odebug << "Destination is broadcast address" << oendl; | 210 | odebug << "Destination is broadcast address" << oendl; |
213 | 211 | ||
214 | switch ( type() ) | 212 | switch ( type() ) |
215 | { | 213 | { |
216 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; | 214 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; |
217 | case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; | 215 | case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; |
218 | case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; } | 216 | case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; } |
219 | default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl; | 217 | default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl; |
220 | } | 218 | } |
221 | 219 | ||
222 | } | 220 | } |
223 | 221 | ||
224 | 222 | ||
225 | OEthernetPacket::~OEthernetPacket() | 223 | OEthernetPacket::~OEthernetPacket() |
226 | { | 224 | { |
227 | } | 225 | } |
228 | 226 | ||
229 | 227 | ||
230 | OMacAddress OEthernetPacket::sourceAddress() const | 228 | OMacAddress OEthernetPacket::sourceAddress() const |
231 | { | 229 | { |
232 | return OMacAddress( _ether->ether_shost ); | 230 | return OMacAddress( _ether->ether_shost ); |
233 | } | 231 | } |
234 | 232 | ||
235 | 233 | ||
236 | OMacAddress OEthernetPacket::destinationAddress() const | 234 | OMacAddress OEthernetPacket::destinationAddress() const |
237 | { | 235 | { |
238 | return OMacAddress( _ether->ether_dhost ); | 236 | return OMacAddress( _ether->ether_dhost ); |
239 | } | 237 | } |
240 | 238 | ||
241 | int OEthernetPacket::type() const | 239 | int OEthernetPacket::type() const |
242 | { | 240 | { |
243 | return ntohs( _ether->ether_type ); | 241 | return ntohs( _ether->ether_type ); |
244 | } | 242 | } |
245 | 243 | ||
246 | 244 | ||
247 | /*====================================================================================== | 245 | /*====================================================================================== |
248 | * OIPPacket | 246 | * OIPPacket |
249 | *======================================================================================*/ | 247 | *======================================================================================*/ |
250 | 248 | ||
251 | 249 | ||
252 | OIPPacket::OIPPacket( const unsigned char* end, const struct iphdr* data, QObject* parent ) | 250 | OIPPacket::OIPPacket( const unsigned char* end, const struct iphdr* data, QObject* parent ) |
253 | :QObject( parent, "IP" ), _iphdr( data ) | 251 | :QObject( parent, "IP" ), _iphdr( data ) |
254 | { | 252 | { |
255 | odebug << "OIPPacket::OIPPacket(): decoding IP header..." << oendl; | 253 | odebug << "OIPPacket::OIPPacket(): decoding IP header..." << oendl; |
256 | 254 | ||
257 | odebug << "FromAddress = " << fromIPAddress().toString() << oendl; | 255 | odebug << "FromAddress = " << fromIPAddress().toString() << oendl; |
258 | odebug << " toAddress = " << toIPAddress().toString() << oendl; | 256 | odebug << " toAddress = " << toIPAddress().toString() << oendl; |
259 | 257 | ||
260 | switch ( protocol() ) | 258 | switch ( protocol() ) |
261 | { | 259 | { |
262 | case IPPROTO_UDP: new OUDPPacket( end, (const struct udphdr*) (data+1), this ); break; | 260 | case IPPROTO_UDP: new OUDPPacket( end, (const struct udphdr*) (data+1), this ); break; |
263 | case IPPROTO_TCP: new OTCPPacket( end, (const struct tcphdr*) (data+1), this ); break; | 261 | case IPPROTO_TCP: new OTCPPacket( end, (const struct tcphdr*) (data+1), this ); break; |
264 | default: odebug << "OIPPacket::OIPPacket(): unknown IP protocol, type = " << protocol() << oendl; | 262 | default: odebug << "OIPPacket::OIPPacket(): unknown IP protocol, type = " << protocol() << oendl; |
265 | } | 263 | } |
266 | 264 | ||
267 | } | 265 | } |
268 | 266 | ||
269 | OIPPacket::~OIPPacket() | 267 | OIPPacket::~OIPPacket() |
270 | { | 268 | { |
271 | } | 269 | } |
272 | 270 | ||
273 | 271 | ||
274 | QHostAddress OIPPacket::fromIPAddress() const | 272 | QHostAddress OIPPacket::fromIPAddress() const |
275 | { | 273 | { |
276 | return EXTRACT_32BITS( &_iphdr->saddr ); | 274 | return EXTRACT_32BITS( &_iphdr->saddr ); |
277 | } | 275 | } |
278 | 276 | ||
279 | 277 | ||
280 | QHostAddress OIPPacket::toIPAddress() const | 278 | QHostAddress OIPPacket::toIPAddress() const |
281 | { | 279 | { |
282 | return EXTRACT_32BITS( &_iphdr->saddr ); | 280 | return EXTRACT_32BITS( &_iphdr->saddr ); |
283 | } | 281 | } |
284 | 282 | ||
285 | 283 | ||
286 | int OIPPacket::tos() const | 284 | int OIPPacket::tos() const |
287 | { | 285 | { |
288 | return _iphdr->tos; | 286 | return _iphdr->tos; |
289 | } | 287 | } |
290 | 288 | ||
291 | 289 | ||
292 | int OIPPacket::len() const | 290 | int OIPPacket::len() const |
293 | { | 291 | { |
294 | return EXTRACT_16BITS( &_iphdr->tot_len ); | 292 | return EXTRACT_16BITS( &_iphdr->tot_len ); |
295 | } | 293 | } |
296 | 294 | ||
297 | 295 | ||
298 | int OIPPacket::id() const | 296 | int OIPPacket::id() const |