author | mickeyl <mickeyl> | 2003-05-06 23:01:10 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2003-05-06 23:01:10 (UTC) |
commit | de62ac94791a969d950fc471b465d4f03267ae01 (patch) (unidiff) | |
tree | a43fa025de468c92d0705657fc950cfcf8151518 | |
parent | 9490e91d3876a8280f3f9a3839fddf95b957b879 (diff) | |
download | opie-de62ac94791a969d950fc471b465d4f03267ae01.zip opie-de62ac94791a969d950fc471b465d4f03267ae01.tar.gz opie-de62ac94791a969d950fc471b465d4f03267ae01.tar.bz2 |
add two minor functions in the pcap interface
-rw-r--r-- | libopie2/opienet/opcap.cpp | 19 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 20 |
2 files changed, 37 insertions, 2 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 523be3e..bef9182 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -962,96 +962,115 @@ bool OPacketCapturer::open( const QString& name, const QString& filename ) | |||
962 | qWarning( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); | 962 | qWarning( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); |
963 | return false; | 963 | return false; |
964 | } | 964 | } |
965 | 965 | ||
966 | qDebug( "OPacketCapturer::open(): libpcap [%s] opened successfully.", (const char*) name ); | 966 | qDebug( "OPacketCapturer::open(): libpcap [%s] opened successfully.", (const char*) name ); |
967 | _pch = handle; | 967 | _pch = handle; |
968 | _open = true; | 968 | _open = true; |
969 | _stats.clear(); | 969 | _stats.clear(); |
970 | 970 | ||
971 | // in case we have an application object, create a socket notifier | 971 | // in case we have an application object, create a socket notifier |
972 | if ( qApp ) //TODO: I don't like this here... | 972 | if ( qApp ) //TODO: I don't like this here... |
973 | { | 973 | { |
974 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 974 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
975 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 975 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
976 | } | 976 | } |
977 | 977 | ||
978 | // if requested, open a dump | 978 | // if requested, open a dump |
979 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); | 979 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); |
980 | if ( !dump ) | 980 | if ( !dump ) |
981 | { | 981 | { |
982 | qWarning( "OPacketCapturer::open(): can't open dump with '%s': %s", (const char*) filename, _errbuf ); | 982 | qWarning( "OPacketCapturer::open(): can't open dump with '%s': %s", (const char*) filename, _errbuf ); |
983 | return false; | 983 | return false; |
984 | } | 984 | } |
985 | qDebug( "OPacketCapturer::open(): dump [%s] opened successfully.", (const char*) filename ); | 985 | qDebug( "OPacketCapturer::open(): dump [%s] opened successfully.", (const char*) filename ); |
986 | _pcd = dump; | 986 | _pcd = dump; |
987 | 987 | ||
988 | return true; | 988 | return true; |
989 | } | 989 | } |
990 | 990 | ||
991 | 991 | ||
992 | bool OPacketCapturer::open( const QFile& file ) | 992 | bool OPacketCapturer::open( const QFile& file ) |
993 | { | 993 | { |
994 | QString name = file.name(); | 994 | QString name = file.name(); |
995 | 995 | ||
996 | if ( _open ) | 996 | if ( _open ) |
997 | { | 997 | { |
998 | close(); | 998 | close(); |
999 | if ( name == _name ) // ignore opening an already openend device | 999 | if ( name == _name ) // ignore opening an already openend device |
1000 | { | 1000 | { |
1001 | return true; | 1001 | return true; |
1002 | } | 1002 | } |
1003 | else // close the last opened device | 1003 | else // close the last opened device |
1004 | { | 1004 | { |
1005 | close(); | 1005 | close(); |
1006 | } | 1006 | } |
1007 | } | 1007 | } |
1008 | 1008 | ||
1009 | _name = name; | 1009 | _name = name; |
1010 | 1010 | ||
1011 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); | 1011 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); |
1012 | 1012 | ||
1013 | if ( handle ) | 1013 | if ( handle ) |
1014 | { | 1014 | { |
1015 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); | 1015 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); |
1016 | _pch = handle; | 1016 | _pch = handle; |
1017 | _open = true; | 1017 | _open = true; |
1018 | 1018 | ||
1019 | // in case we have an application object, create a socket notifier | 1019 | // in case we have an application object, create a socket notifier |
1020 | if ( qApp ) | 1020 | if ( qApp ) |
1021 | { | 1021 | { |
1022 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 1022 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
1023 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1023 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1024 | } | 1024 | } |
1025 | 1025 | ||
1026 | return true; | 1026 | return true; |
1027 | } | 1027 | } |
1028 | else | 1028 | else |
1029 | { | 1029 | { |
1030 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); | 1030 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); |
1031 | return false; | 1031 | return false; |
1032 | } | 1032 | } |
1033 | 1033 | ||
1034 | } | 1034 | } |
1035 | 1035 | ||
1036 | 1036 | ||
1037 | bool OPacketCapturer::isOpen() const | 1037 | bool OPacketCapturer::isOpen() const |
1038 | { | 1038 | { |
1039 | return _open; | 1039 | return _open; |
1040 | } | 1040 | } |
1041 | 1041 | ||
1042 | 1042 | ||
1043 | void OPacketCapturer::readyToReceive() | 1043 | void OPacketCapturer::readyToReceive() |
1044 | { | 1044 | { |
1045 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" ); | 1045 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" ); |
1046 | OPacket* p = next(); | 1046 | OPacket* p = next(); |
1047 | emit receivedPacket( p ); | 1047 | emit receivedPacket( p ); |
1048 | // emit is synchronous - packet has been dealt with, now it's safe to delete | 1048 | // emit is synchronous - packet has been dealt with, now it's safe to delete |
1049 | delete p; | 1049 | delete p; |
1050 | } | 1050 | } |
1051 | 1051 | ||
1052 | 1052 | ||
1053 | const QMap<QString,int>& OPacketCapturer::statistics() const | 1053 | const QMap<QString,int>& OPacketCapturer::statistics() const |
1054 | { | 1054 | { |
1055 | return _stats; | 1055 | return _stats; |
1056 | } | 1056 | } |
1057 | 1057 | ||
1058 | |||
1059 | int OPacketCapturer::snapShot() const | ||
1060 | { | ||
1061 | return pcap_snapshot( _pch ); | ||
1062 | } | ||
1063 | |||
1064 | |||
1065 | bool OPacketCapturer::swapped() const | ||
1066 | { | ||
1067 | return pcap_is_swapped( _pch ); | ||
1068 | } | ||
1069 | |||
1070 | |||
1071 | QString OPacketCapturer::version() const | ||
1072 | { | ||
1073 | return QString().sprintf( "%s.%s", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); | ||
1074 | } | ||
1075 | |||
1076 | |||
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index 5a50d9b..ad5b07c 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h | |||
@@ -449,142 +449,158 @@ class OARPPacket : public QObject | |||
449 | OARPPacket( const unsigned char*, const struct myarphdr*, QObject* parent = 0 ); | 449 | OARPPacket( const unsigned char*, const struct myarphdr*, QObject* parent = 0 ); |
450 | virtual ~OARPPacket(); | 450 | virtual ~OARPPacket(); |
451 | 451 | ||
452 | QHostAddress senderIPV4Address() const; | 452 | QHostAddress senderIPV4Address() const; |
453 | OMacAddress senderMacAddress() const; | 453 | OMacAddress senderMacAddress() const; |
454 | QHostAddress targetIPV4Address() const; | 454 | QHostAddress targetIPV4Address() const; |
455 | OMacAddress targetMacAddress() const; | 455 | OMacAddress targetMacAddress() const; |
456 | 456 | ||
457 | //int type() const; | 457 | //int type() const; |
458 | QString type() const; | 458 | QString type() const; |
459 | 459 | ||
460 | private: | 460 | private: |
461 | const struct myarphdr* _arphdr; | 461 | const struct myarphdr* _arphdr; |
462 | }; | 462 | }; |
463 | 463 | ||
464 | /*====================================================================================== | 464 | /*====================================================================================== |
465 | * OUDPPacket | 465 | * OUDPPacket |
466 | *======================================================================================*/ | 466 | *======================================================================================*/ |
467 | 467 | ||
468 | class OUDPPacket : public QObject | 468 | class OUDPPacket : public QObject |
469 | { | 469 | { |
470 | Q_OBJECT | 470 | Q_OBJECT |
471 | 471 | ||
472 | public: | 472 | public: |
473 | OUDPPacket( const unsigned char*, const struct udphdr*, QObject* parent = 0 ); | 473 | OUDPPacket( const unsigned char*, const struct udphdr*, QObject* parent = 0 ); |
474 | virtual ~OUDPPacket(); | 474 | virtual ~OUDPPacket(); |
475 | 475 | ||
476 | int fromPort() const; | 476 | int fromPort() const; |
477 | int toPort() const; | 477 | int toPort() const; |
478 | 478 | ||
479 | private: | 479 | private: |
480 | const struct udphdr* _udphdr; | 480 | const struct udphdr* _udphdr; |
481 | }; | 481 | }; |
482 | 482 | ||
483 | /*====================================================================================== | 483 | /*====================================================================================== |
484 | * OTCPPacket | 484 | * OTCPPacket |
485 | *======================================================================================*/ | 485 | *======================================================================================*/ |
486 | 486 | ||
487 | class OTCPPacket : public QObject | 487 | class OTCPPacket : public QObject |
488 | { | 488 | { |
489 | Q_OBJECT | 489 | Q_OBJECT |
490 | 490 | ||
491 | public: | 491 | public: |
492 | OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 ); | 492 | OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 ); |
493 | virtual ~OTCPPacket(); | 493 | virtual ~OTCPPacket(); |
494 | 494 | ||
495 | int fromPort() const; | 495 | int fromPort() const; |
496 | int toPort() const; | 496 | int toPort() const; |
497 | 497 | ||
498 | private: | 498 | private: |
499 | const struct tcphdr* _tcphdr; | 499 | const struct tcphdr* _tcphdr; |
500 | }; | 500 | }; |
501 | 501 | ||
502 | 502 | ||
503 | /*====================================================================================== | 503 | /*====================================================================================== |
504 | * OPacketCapturer | 504 | * OPacketCapturer |
505 | *======================================================================================*/ | 505 | *======================================================================================*/ |
506 | 506 | ||
507 | /** | 507 | /** |
508 | * @brief A class based wrapper for network packet capturing. | 508 | * @brief A class based wrapper for network packet capturing. |
509 | * | 509 | * |
510 | * This class is the base of a high-level interface to the well known packet capturing | 510 | * This class is the base of a high-level interface to the well known packet capturing |
511 | * library libpcap. ... | 511 | * library libpcap. ... |
512 | */ | 512 | */ |
513 | class OPacketCapturer : public QObject | 513 | class OPacketCapturer : public QObject |
514 | { | 514 | { |
515 | Q_OBJECT | 515 | Q_OBJECT |
516 | 516 | ||
517 | public: | 517 | public: |
518 | /** | 518 | /** |
519 | * Constructor. | 519 | * Constructor. |
520 | */ | 520 | */ |
521 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); | 521 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); |
522 | /** | 522 | /** |
523 | * Destructor. | 523 | * Destructor. |
524 | */ | 524 | */ |
525 | ~OPacketCapturer(); | 525 | ~OPacketCapturer(); |
526 | /** | 526 | /** |
527 | * Setting the packet capturer to use blocking IO calls can be useful when | 527 | * Setting the packet capturer to use blocking IO calls can be useful when |
528 | * not using the socket notifier, e.g. without an application object. | 528 | * not using the socket notifier, e.g. without an application object. |
529 | */ | 529 | */ |
530 | void setBlocking( bool ); | 530 | void setBlocking( bool ); |
531 | /** | 531 | /** |
532 | * @returns true if the packet capturer uses blocking IO calls. | 532 | * @returns true if the packet capturer uses blocking IO calls. |
533 | */ | 533 | */ |
534 | bool blocking() const; | 534 | bool blocking() const; |
535 | /** | 535 | /** |
536 | * Closes the packet capturer. This is automatically done in the destructor. | 536 | * Closes the packet capturer. This is automatically done in the destructor. |
537 | */ | 537 | */ |
538 | void close(); | 538 | void close(); |
539 | /** | 539 | /** |
540 | * @returns the data link type. | 540 | * @returns the data link type. |
541 | * @see <pcap.h> for possible values. | 541 | * @see <pcap.h> for possible values. |
542 | */ | 542 | */ |
543 | int dataLink() const; | 543 | int dataLink() const; |
544 | /** | 544 | /** |
545 | * @returns the filedescriptor of the packet capturer. This is only useful, if | 545 | * @returns the file descriptor of the packet capturer. This is only useful, if |
546 | * not using the socket notifier, e.g. without an application object. | 546 | * not using the socket notifier, e.g. without an application object. |
547 | */ | 547 | */ |
548 | int fileno() const; | 548 | int fileno() const; |
549 | /** | 549 | /** |
550 | * @returns the next @ref OPacket from the packet capturer. | 550 | * @returns the next @ref OPacket from the packet capturer. |
551 | * @note If blocking mode is true then this call might block. | 551 | * @note If blocking mode is true then this call might block. |
552 | */ | 552 | */ |
553 | OPacket* next(); | 553 | OPacket* next(); |
554 | /** | 554 | /** |
555 | * Open the packet capturer to capture packets in live-mode from @a interface. | 555 | * Open the packet capturer to capture packets in live-mode from @a interface. |
556 | * If a @a filename is given, all captured packets are output to a tcpdump-compatible capture file. | 556 | * If a @a filename is given, all captured packets are output to a tcpdump-compatible capture file. |
557 | */ | 557 | */ |
558 | bool open( const QString& interface, const QString& filename = QString::null ); | 558 | bool open( const QString& interface, const QString& filename = QString::null ); |
559 | /** | 559 | /** |
560 | * Open the packet capturer to capture packets in offline-mode from @a file. | 560 | * Open the packet capturer to capture packets in offline-mode from @a file. |
561 | */ | 561 | */ |
562 | bool open( const QFile& file ); | 562 | bool open( const QFile& file ); |
563 | /** | 563 | /** |
564 | * @returns true if the packet capturer is open | 564 | * @returns true if the packet capturer is open |
565 | */ | 565 | */ |
566 | bool isOpen() const; | 566 | bool isOpen() const; |
567 | 567 | /** | |
568 | * @returns the snapshot length of this packet capturer | ||
569 | */ | ||
570 | int snapShot() const; | ||
571 | /** | ||
572 | * @returns true if the input capture file has a different byte-order | ||
573 | * than the byte-order of the running system. | ||
574 | */ | ||
575 | bool swapped() const; | ||
576 | /** | ||
577 | * @returns the libpcap version string used to write the input capture file. | ||
578 | */ | ||
579 | QString version() const; | ||
580 | /** | ||
581 | * @returns the packet statistic database. | ||
582 | * @see QMap | ||
583 | */ | ||
568 | const QMap<QString,int>& statistics() const; | 584 | const QMap<QString,int>& statistics() const; |
569 | 585 | ||
570 | signals: | 586 | signals: |
571 | /** | 587 | /** |
572 | * This signal is emitted, when a packet has been received. | 588 | * This signal is emitted, when a packet has been received. |
573 | */ | 589 | */ |
574 | void receivedPacket( OPacket* ); | 590 | void receivedPacket( OPacket* ); |
575 | 591 | ||
576 | protected slots: | 592 | protected slots: |
577 | void readyToReceive(); | 593 | void readyToReceive(); |
578 | 594 | ||
579 | protected: | 595 | protected: |
580 | QString _name; // devicename | 596 | QString _name; // devicename |
581 | bool _open; // check this before doing pcap calls | 597 | bool _open; // check this before doing pcap calls |
582 | pcap_t* _pch; // pcap library handle | 598 | pcap_t* _pch; // pcap library handle |
583 | pcap_dumper_t* _pcd; // pcap dumper handle | 599 | pcap_dumper_t* _pcd; // pcap dumper handle |
584 | QSocketNotifier* _sn; // socket notifier for main loop | 600 | QSocketNotifier* _sn; // socket notifier for main loop |
585 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap | 601 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap |
586 | QMap<QString, int> _stats; // statistics; | 602 | QMap<QString, int> _stats; // statistics; |
587 | }; | 603 | }; |
588 | 604 | ||
589 | #endif // OPCAP_H | 605 | #endif // OPCAP_H |
590 | 606 | ||