-rw-r--r-- | libopie2/opienet/opcap.h | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index f5dc5c0..b873b49 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h | |||
@@ -10,74 +10,76 @@ | |||
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 | #ifndef OPCAP_H | 31 | #ifndef OPCAP_H |
32 | #define OPCAP_H | 32 | #define OPCAP_H |
33 | 33 | ||
34 | /* LINUX */ | 34 | /* OPIE */ |
35 | extern "C" // work around a bpf/pcap conflict in recent headers | 35 | #include <opie2/onetutils.h> |
36 | { | ||
37 | #include <pcap.h> | ||
38 | } | ||
39 | #include <netinet/ether.h> | ||
40 | #include <netinet/ip.h> | ||
41 | #include <netinet/udp.h> | ||
42 | #include <netinet/tcp.h> | ||
43 | #include <time.h> | ||
44 | 36 | ||
45 | /* QT */ | 37 | /* QT */ |
46 | #include <qevent.h> | 38 | #include <qevent.h> |
47 | #include <qfile.h> | 39 | #include <qfile.h> |
48 | #include <qhostaddress.h> | 40 | #include <qhostaddress.h> |
49 | #include <qobject.h> | 41 | #include <qobject.h> |
50 | #include <qstring.h> | 42 | #include <qstring.h> |
43 | #include <qtextstream.h> | ||
51 | #include <qmap.h> | 44 | #include <qmap.h> |
52 | 45 | ||
53 | /* OPIE */ | 46 | /* STD */ |
54 | #include <opie2/onetutils.h> | 47 | extern "C" // work around a bpf/pcap conflict in recent headers |
48 | { | ||
49 | #include <pcap.h> | ||
50 | } | ||
51 | #include <netinet/ether.h> | ||
52 | #include <netinet/ip.h> | ||
53 | #include <netinet/udp.h> | ||
54 | #include <netinet/tcp.h> | ||
55 | #include <time.h> | ||
55 | 56 | ||
56 | /* Custom Network Includes */ | 57 | /* Custom Network Includes (must go here, don't reorder!) */ |
57 | #include "802_11_user.h" | 58 | #include "802_11_user.h" |
58 | #include "dhcp.h" | 59 | #include "dhcp.h" |
59 | 60 | ||
61 | |||
60 | /* TYPEDEFS */ | 62 | /* TYPEDEFS */ |
61 | typedef struct timeval timevalstruct; | 63 | typedef struct timeval timevalstruct; |
62 | typedef struct pcap_pkthdr packetheaderstruct; | 64 | typedef struct pcap_pkthdr packetheaderstruct; |
63 | 65 | ||
64 | /* FORWARDS */ | 66 | /* FORWARDS */ |
65 | class OPacketCapturer; | 67 | class OPacketCapturer; |
66 | class QSocketNotifier; | 68 | class QSocketNotifier; |
67 | 69 | ||
68 | /*====================================================================================== | 70 | /*====================================================================================== |
69 | * OPacket - A frame on the wire | 71 | * OPacket - A frame on the wire |
70 | *======================================================================================*/ | 72 | *======================================================================================*/ |
71 | 73 | ||
72 | /** @brief A class representing a data frame on the wire. | 74 | /** @brief A class representing a data frame on the wire. |
73 | * | 75 | * |
74 | * The whole family of the packet classes are used when capturing frames from a network. | 76 | * The whole family of the packet classes are used when capturing frames from a network. |
75 | * Most standard network protocols in use share a common architecture, which mostly is | 77 | * Most standard network protocols in use share a common architecture, which mostly is |
76 | * a packet header and then the packet payload. In layered architectures, each lower layer | 78 | * a packet header and then the packet payload. In layered architectures, each lower layer |
77 | * encapsulates data from its upper layer - that is it | 79 | * encapsulates data from its upper layer - that is it |
78 | * treats the data from its upper layer as payload and prepends an own header to the packet, | 80 | * treats the data from its upper layer as payload and prepends an own header to the packet, |
79 | * which - again - is treated as the payload for the layer below. The figure below is an | 81 | * which - again - is treated as the payload for the layer below. The figure below is an |
80 | * example for how such a data frame is composed out of packets, e.g. when sending a mail. | 82 | * example for how such a data frame is composed out of packets, e.g. when sending a mail. |
81 | * | 83 | * |
82 | * <pre> | 84 | * <pre> |
83 | * | User Data | == Mail Data | 85 | * | User Data | == Mail Data |
@@ -95,72 +97,75 @@ class QSocketNotifier; | |||
95 | * encapsulation level varies and can't be deduced without actually looking into the packets. | 97 | * encapsulation level varies and can't be deduced without actually looking into the packets. |
96 | * | 98 | * |
97 | * For actually working with captured frames, it's useful to identify the packets via names and | 99 | * For actually working with captured frames, it's useful to identify the packets via names and |
98 | * insert them into a parent/child - relationship based on the encapsulation. This is why | 100 | * insert them into a parent/child - relationship based on the encapsulation. This is why |
99 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is | 101 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is |
100 | * not a problem in this case, because we're talking about a theoratical maximum of about | 102 | * not a problem in this case, because we're talking about a theoratical maximum of about |
101 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the | 103 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the |
102 | * QObject also cares about destroying the sub-, (child-) packets. | 104 | * QObject also cares about destroying the sub-, (child-) packets. |
103 | * | 105 | * |
104 | * This enables us to perform a simple look for packets of a certain type: | 106 | * This enables us to perform a simple look for packets of a certain type: |
105 | * @code | 107 | * @code |
106 | * OPacketCapturer* pcap = new OPacketCapturer(); | 108 | * OPacketCapturer* pcap = new OPacketCapturer(); |
107 | * pcap->open( "eth0" ); | 109 | * pcap->open( "eth0" ); |
108 | * OPacket* p = pcap->next(); | 110 | * OPacket* p = pcap->next(); |
109 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists | 111 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists |
110 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; | 112 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; |
111 | * | 113 | * |
112 | */ | 114 | */ |
113 | 115 | ||
114 | class OPacket : public QObject | 116 | class OPacket : public QObject |
115 | { | 117 | { |
116 | Q_OBJECT | 118 | Q_OBJECT |
117 | 119 | ||
118 | friend class OPacketCapturer; | 120 | friend class OPacketCapturer; |
121 | friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); | ||
119 | 122 | ||
120 | public: | 123 | public: |
121 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); | 124 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); |
122 | virtual ~OPacket(); | 125 | virtual ~OPacket(); |
123 | 126 | ||
124 | timevalstruct timeval() const; | 127 | timevalstruct timeval() const; |
125 | 128 | ||
126 | int caplen() const; | 129 | int caplen() const; |
127 | int len() const; | 130 | int len() const; |
128 | QString dump( int = 32 ) const; | 131 | QString dump( int = 32 ) const; |
129 | 132 | ||
130 | void updateStats( QMap<QString,int>&, QObjectList* ); | 133 | void updateStats( QMap<QString,int>&, QObjectList* ); |
131 | 134 | ||
132 | private: | 135 | private: |
133 | 136 | ||
134 | void dumpStructure( QObjectList* ); | 137 | QString dumpStructure() const; |
135 | QString _dumpStructure( QObjectList* ); | 138 | QString _dumpStructure( QObjectList* ) const; |
136 | 139 | ||
137 | private: | 140 | private: |
138 | const packetheaderstruct _hdr; // pcap packet header | 141 | const packetheaderstruct _hdr; // pcap packet header |
139 | const unsigned char* _data; // pcap packet data | 142 | const unsigned char* _data; // pcap packet data |
140 | const unsigned char* _end; // end of pcap packet data | 143 | const unsigned char* _end; // end of pcap packet data |
141 | }; | 144 | }; |
142 | 145 | ||
146 | QTextStream& operator<<( QTextStream& s, const OPacket& p ); | ||
147 | |||
143 | /*====================================================================================== | 148 | /*====================================================================================== |
144 | * OEthernetPacket - DLT_EN10MB frame | 149 | * OEthernetPacket - DLT_EN10MB frame |
145 | *======================================================================================*/ | 150 | *======================================================================================*/ |
146 | 151 | ||
147 | class OEthernetPacket : public QObject | 152 | class OEthernetPacket : public QObject |
148 | { | 153 | { |
149 | Q_OBJECT | 154 | Q_OBJECT |
150 | 155 | ||
151 | public: | 156 | public: |
152 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); | 157 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); |
153 | virtual ~OEthernetPacket(); | 158 | virtual ~OEthernetPacket(); |
154 | 159 | ||
155 | OMacAddress sourceAddress() const; | 160 | OMacAddress sourceAddress() const; |
156 | OMacAddress destinationAddress() const; | 161 | OMacAddress destinationAddress() const; |
157 | int type() const; | 162 | int type() const; |
158 | 163 | ||
159 | private: | 164 | private: |
160 | const struct ether_header* _ether; | 165 | const struct ether_header* _ether; |
161 | }; | 166 | }; |
162 | 167 | ||
163 | /*====================================================================================== | 168 | /*====================================================================================== |
164 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame | 169 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame |
165 | *======================================================================================*/ | 170 | *======================================================================================*/ |
166 | 171 | ||