-rw-r--r-- | libopie2/opienet/opcap.cpp | 82 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 10 |
2 files changed, 92 insertions, 0 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index f1f2b4b..7a6f61b 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp @@ -348,136 +348,218 @@ OMacAddress OARPPacket::targetMacAddress() const /*====================================================================================== * OUDPPacket *======================================================================================*/ OUDPPacket::OUDPPacket( const unsigned char* end, const struct udphdr* data, QObject* parent ) :QObject( parent, "UDP" ), _udphdr( data ) { qDebug( "OUDPPacket::OUDPPacket(): decoding UDP header..." ); qDebug( "fromPort = %d", fromPort() ); qDebug( " toPort = %d", toPort() ); // TODO: Make this a case or a hash if we know more udp protocols if ( fromPort() == UDP_PORT_BOOTPS || fromPort() == UDP_PORT_BOOTPC || toPort() == UDP_PORT_BOOTPS || toPort() == UDP_PORT_BOOTPC ) { qDebug( "seems to be part of a DHCP conversation => creating DHCP packet." ); new ODHCPPacket( end, (const struct dhcp_packet*) (data+1), this ); } } OUDPPacket::~OUDPPacket() { } int OUDPPacket::fromPort() const { return EXTRACT_16BITS( &_udphdr->source ); } int OUDPPacket::toPort() const { return EXTRACT_16BITS( &_udphdr->dest ); } int OUDPPacket::length() const { return EXTRACT_16BITS( &_udphdr->len ); } int OUDPPacket::checksum() const { return EXTRACT_16BITS( &_udphdr->check ); } /*====================================================================================== * ODHCPPacket *======================================================================================*/ ODHCPPacket::ODHCPPacket( const unsigned char* end, const struct dhcp_packet* data, QObject* parent ) :QObject( parent, "DHCP" ), _dhcphdr( data ) { qDebug( "ODHCPPacket::ODHCPPacket(): decoding DHCP information..." ); + qDebug( "DHCP opcode seems to be %02d - '%s'", _dhcphdr->op, isRequest() ? "REQUEST" : "REPLY" ); + qDebug( "clientAddress: %s", (const char*) clientAddress().toString() ); + qDebug( " yourAddress: %s", (const char*) yourAddress().toString() ); + qDebug( "serverAddress: %s", (const char*) serverAddress().toString() ); + qDebug( " relayAddress: %s", (const char*) relayAddress().toString() ); + qDebug( "parsing DHCP options..." ); + + _type = 0; + + const unsigned char* option = &_dhcphdr->options[4]; + char tag = -1; + char len = -1; + + while ( ( tag = *option++ ) != -1 /* end of option field */ ) + { + len = *option++; + qDebug( "recognized DHCP option #%d, length %d", tag, len ); + + if ( tag == DHO_DHCP_MESSAGE_TYPE ) + _type = *option; + + option += len; + if ( option >= end ) + { + qWarning( "DHCP parsing ERROR: sanity check says the packet is at its end!" ); + break; + } + } + + qDebug( "DHCP type seems to be '%s'", (const char*) type() ); } ODHCPPacket::~ODHCPPacket() { } +bool ODHCPPacket::isRequest() const +{ + return ( _dhcphdr->op == 01 ); +} + + +bool ODHCPPacket::isReply() const +{ + return ( _dhcphdr->op == 02 ); +} + + +QString ODHCPPacket::type() const +{ + switch ( _type ) + { + case 1: return "DISCOVER"; + case 2: return "OFFER"; + case 3: return "REQUEST"; + case 4: return "DECLINE"; + case 5: return "ACK"; + case 6: return "NAK"; + case 7: return "RELEASE"; + case 8: return "INFORM"; + default: qWarning( "ODHCPPacket::type(): invalid DHCP type (%d) !", _dhcphdr->op ); return "<unknown>"; + } +} + + +QHostAddress ODHCPPacket::clientAddress() const +{ + return EXTRACT_32BITS( &_dhcphdr->ciaddr ); +} + + +QHostAddress ODHCPPacket::yourAddress() const +{ + return EXTRACT_32BITS( &_dhcphdr->yiaddr ); +} + + +QHostAddress ODHCPPacket::serverAddress() const +{ + return EXTRACT_32BITS( &_dhcphdr->siaddr ); +} + + +QHostAddress ODHCPPacket::relayAddress() const +{ + return EXTRACT_32BITS( &_dhcphdr->giaddr ); +} + /*====================================================================================== * OTCPPacket *======================================================================================*/ OTCPPacket::OTCPPacket( const unsigned char* end, const struct tcphdr* data, QObject* parent ) :QObject( parent, "TCP" ), _tcphdr( data ) { qDebug( "OTCPPacket::OTCPPacket(): decoding TCP header..." ); } OTCPPacket::~OTCPPacket() { } int OTCPPacket::fromPort() const { return EXTRACT_16BITS( &_tcphdr->source ); } int OTCPPacket::toPort() const { return EXTRACT_16BITS( &_tcphdr->dest ); } int OTCPPacket::seq() const { return EXTRACT_16BITS( &_tcphdr->seq ); } int OTCPPacket::ack() const { return EXTRACT_16BITS( &_tcphdr->ack_seq ); } int OTCPPacket::window() const { return EXTRACT_16BITS( &_tcphdr->window ); } int OTCPPacket::checksum() const { return EXTRACT_16BITS( &_tcphdr->check ); } /*====================================================================================== * OPrismHeaderPacket *======================================================================================*/ OPrismHeaderPacket::OPrismHeaderPacket( const unsigned char* end, const struct prism_hdr* data, QObject* parent ) :QObject( parent, "Prism" ), _header( data ) { qDebug( "OPrismHeaderPacket::OPrismHeaderPacket(): decoding PRISM header..." ); diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index 0c9e7da..a031dd1 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h @@ -438,130 +438,140 @@ class OIPPacket : public QObject int protocol() const; int checksum() const; private: const struct iphdr* _iphdr; }; /*====================================================================================== * OARPPacket *======================================================================================*/ class OARPPacket : public QObject { Q_OBJECT public: OARPPacket( const unsigned char*, const struct myarphdr*, QObject* parent = 0 ); virtual ~OARPPacket(); QHostAddress senderIPV4Address() const; OMacAddress senderMacAddress() const; QHostAddress targetIPV4Address() const; OMacAddress targetMacAddress() const; //int type() const; QString type() const; private: const struct myarphdr* _arphdr; }; /*====================================================================================== * OUDPPacket *======================================================================================*/ class OUDPPacket : public QObject { Q_OBJECT public: OUDPPacket( const unsigned char*, const struct udphdr*, QObject* parent = 0 ); virtual ~OUDPPacket(); int fromPort() const; int toPort() const; int length() const; int checksum() const; private: const struct udphdr* _udphdr; }; /*====================================================================================== * ODHCPPacket *======================================================================================*/ class ODHCPPacket : public QObject { Q_OBJECT public: ODHCPPacket( const unsigned char*, const struct dhcp_packet*, QObject* parent = 0 ); virtual ~ODHCPPacket(); + QHostAddress clientAddress() const; + QHostAddress yourAddress() const; + QHostAddress serverAddress() const; + QHostAddress relayAddress() const; + + bool isRequest() const; + bool isReply() const; + QString type() const; + private: const struct dhcp_packet* _dhcphdr; + unsigned char _type; }; /*====================================================================================== * OTCPPacket *======================================================================================*/ class OTCPPacket : public QObject { Q_OBJECT public: OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 ); virtual ~OTCPPacket(); int fromPort() const; int toPort() const; int seq() const; int ack() const; int window() const; int checksum() const; private: const struct tcphdr* _tcphdr; }; /*====================================================================================== * OPacketCapturer *======================================================================================*/ /** * @brief A class based wrapper for network packet capturing. * * This class is the base of a high-level interface to the well known packet capturing * library libpcap. * @see http://tcpdump.org */ class OPacketCapturer : public QObject { Q_OBJECT public: /** * Constructor. */ OPacketCapturer( QObject* parent = 0, const char* name = 0 ); /** * Destructor. */ ~OPacketCapturer(); /** * Set the packet capturer to use blocking or non-blocking IO. This can be useful when * not using the socket notifier, e.g. without an application object. */ void setBlocking( bool ); /** * @returns true if the packet capturer uses blocking IO calls. */ bool blocking() const; /** * Close the packet capturer. This is automatically done in the destructor. */ void close(); /** |