-rw-r--r-- | libopie2/opienet/opcap.cpp | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 04b1bb1..e362883 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -735,206 +735,207 @@ OWaveLanControlPacket::~OWaveLanControlPacket() | |||
735 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) | 735 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) |
736 | :QObject( parent, name ), _name( QString::null ), _open( false ), | 736 | :QObject( parent, name ), _name( QString::null ), _open( false ), |
737 | _pch( 0 ), _pcd( 0 ), _sn( 0 ) | 737 | _pch( 0 ), _pcd( 0 ), _sn( 0 ) |
738 | { | 738 | { |
739 | } | 739 | } |
740 | 740 | ||
741 | 741 | ||
742 | OPacketCapturer::~OPacketCapturer() | 742 | OPacketCapturer::~OPacketCapturer() |
743 | { | 743 | { |
744 | if ( _open ) | 744 | if ( _open ) |
745 | { | 745 | { |
746 | qDebug( "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." ); | 746 | qDebug( "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." ); |
747 | close(); | 747 | close(); |
748 | } | 748 | } |
749 | } | 749 | } |
750 | 750 | ||
751 | 751 | ||
752 | void OPacketCapturer::setBlocking( bool b ) | 752 | void OPacketCapturer::setBlocking( bool b ) |
753 | { | 753 | { |
754 | if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) | 754 | if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) |
755 | { | 755 | { |
756 | qDebug( "OPacketCapturer::setBlocking(): blocking mode changed successfully." ); | 756 | qDebug( "OPacketCapturer::setBlocking(): blocking mode changed successfully." ); |
757 | } | 757 | } |
758 | else | 758 | else |
759 | { | 759 | { |
760 | qDebug( "OPacketCapturer::setBlocking(): can't change blocking mode: %s", _errbuf ); | 760 | qDebug( "OPacketCapturer::setBlocking(): can't change blocking mode: %s", _errbuf ); |
761 | } | 761 | } |
762 | } | 762 | } |
763 | 763 | ||
764 | 764 | ||
765 | bool OPacketCapturer::blocking() const | 765 | bool OPacketCapturer::blocking() const |
766 | { | 766 | { |
767 | int b = pcap_getnonblock( _pch, _errbuf ); | 767 | int b = pcap_getnonblock( _pch, _errbuf ); |
768 | if ( b == -1 ) | 768 | if ( b == -1 ) |
769 | { | 769 | { |
770 | qDebug( "OPacketCapturer::blocking(): can't get blocking mode: %s", _errbuf ); | 770 | qDebug( "OPacketCapturer::blocking(): can't get blocking mode: %s", _errbuf ); |
771 | return -1; | 771 | return -1; |
772 | } | 772 | } |
773 | return !b; | 773 | return !b; |
774 | } | 774 | } |
775 | 775 | ||
776 | 776 | ||
777 | void OPacketCapturer::close() | 777 | void OPacketCapturer::close() |
778 | { | 778 | { |
779 | if ( _open ) | 779 | if ( _open ) |
780 | { | 780 | { |
781 | if ( _sn ) | 781 | if ( _sn ) |
782 | { | 782 | { |
783 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 783 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
784 | delete _sn; | 784 | delete _sn; |
785 | } | 785 | } |
786 | if ( _pcd ) | 786 | if ( _pcd ) |
787 | { | 787 | { |
788 | pcap_dump_close( _pcd ); | 788 | pcap_dump_close( _pcd ); |
789 | _pcd = 0; | 789 | _pcd = 0; |
790 | } | 790 | } |
791 | pcap_close( _pch ); | 791 | pcap_close( _pch ); |
792 | _open = false; | 792 | _open = false; |
793 | } | 793 | } |
794 | 794 | ||
795 | qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); | 795 | qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); |
796 | qDebug( "--------------------------------------------------" ); | 796 | qDebug( "--------------------------------------------------" ); |
797 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) | 797 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) |
798 | qDebug( "%s : %d", (const char*) it.key(), it.data() ); | 798 | qDebug( "%s : %d", (const char*) it.key(), it.data() ); |
799 | qDebug( "--------------------------------------------------" ); | 799 | qDebug( "--------------------------------------------------" ); |
800 | 800 | ||
801 | } | 801 | } |
802 | 802 | ||
803 | 803 | ||
804 | int OPacketCapturer::dataLink() const | 804 | int OPacketCapturer::dataLink() const |
805 | { | 805 | { |
806 | return pcap_datalink( _pch ); | 806 | return pcap_datalink( _pch ); |
807 | } | 807 | } |
808 | 808 | ||
809 | 809 | ||
810 | int OPacketCapturer::fileno() const | 810 | int OPacketCapturer::fileno() const |
811 | { | 811 | { |
812 | if ( _open ) | 812 | if ( _open ) |
813 | { | 813 | { |
814 | return pcap_fileno( _pch ); | 814 | return pcap_fileno( _pch ); |
815 | } | 815 | } |
816 | else | 816 | else |
817 | { | 817 | { |
818 | return -1; | 818 | return -1; |
819 | } | 819 | } |
820 | } | 820 | } |
821 | 821 | ||
822 | OPacket* OPacketCapturer::next() | 822 | OPacket* OPacketCapturer::next() |
823 | { | 823 | { |
824 | packetheaderstruct header; | 824 | packetheaderstruct header; |
825 | qDebug( "==> OPacketCapturer::next()" ); | 825 | qDebug( "==> OPacketCapturer::next()" ); |
826 | const unsigned char* pdata = pcap_next( _pch, &header ); | 826 | const unsigned char* pdata = pcap_next( _pch, &header ); |
827 | qDebug( "<== OPacketCapturer::next()" ); | 827 | qDebug( "<== OPacketCapturer::next()" ); |
828 | if ( _pcd ) | 828 | if ( _pcd ) |
829 | pcap_dump( (u_char*) _pcd, &header, pdata ); | 829 | pcap_dump( (u_char*) _pcd, &header, pdata ); |
830 | 830 | ||
831 | if ( header.len ) | 831 | if ( pdata && header.len ) |
832 | { | 832 | { |
833 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); | 833 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); |
834 | // packets shouldn't be inserted in the QObject child-parent hierarchy, | 834 | // packets shouldn't be inserted in the QObject child-parent hierarchy, |
835 | // because due to memory constraints they will be deleted as soon | 835 | // because due to memory constraints they will be deleted as soon |
836 | // as possible - that is right after they have been processed | 836 | // as possible - that is right after they have been processed |
837 | // by emit() [ see below ] | 837 | // by emit() [ see below ] |
838 | //TODO: make gathering statistics optional, because it takes time | 838 | //TODO: make gathering statistics optional, because it takes time |
839 | p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); | 839 | p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); |
840 | 840 | ||
841 | return p; | 841 | return p; |
842 | } | 842 | } |
843 | else | 843 | else |
844 | { | 844 | { |
845 | qWarning( "OPacketCapturer::next() - no packet received!" ); | ||
845 | return 0; | 846 | return 0; |
846 | } | 847 | } |
847 | } | 848 | } |
848 | 849 | ||
849 | 850 | ||
850 | bool OPacketCapturer::open( const QString& name, const QString& filename ) | 851 | bool OPacketCapturer::open( const QString& name, const QString& filename ) |
851 | { | 852 | { |
852 | if ( _open ) | 853 | if ( _open ) |
853 | { | 854 | { |
854 | if ( name == _name ) // ignore opening an already openend device | 855 | if ( name == _name ) // ignore opening an already openend device |
855 | { | 856 | { |
856 | return true; | 857 | return true; |
857 | } | 858 | } |
858 | else // close the last opened device | 859 | else // close the last opened device |
859 | { | 860 | { |
860 | close(); | 861 | close(); |
861 | } | 862 | } |
862 | } | 863 | } |
863 | 864 | ||
864 | _name = name; | 865 | _name = name; |
865 | 866 | ||
866 | // open libpcap | 867 | // open libpcap |
867 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); | 868 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); |
868 | 869 | ||
869 | if ( !handle ) | 870 | if ( !handle ) |
870 | { | 871 | { |
871 | qWarning( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); | 872 | qWarning( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); |
872 | return false; | 873 | return false; |
873 | } | 874 | } |
874 | 875 | ||
875 | qDebug( "OPacketCapturer::open(): libpcap [%s] opened successfully.", (const char*) name ); | 876 | qDebug( "OPacketCapturer::open(): libpcap [%s] opened successfully.", (const char*) name ); |
876 | _pch = handle; | 877 | _pch = handle; |
877 | _open = true; | 878 | _open = true; |
878 | _stats.clear(); | 879 | _stats.clear(); |
879 | 880 | ||
880 | // in case we have an application object, create a socket notifier | 881 | // in case we have an application object, create a socket notifier |
881 | if ( qApp ) //TODO: I don't like this here... | 882 | if ( qApp ) //TODO: I don't like this here... |
882 | { | 883 | { |
883 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 884 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
884 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 885 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
885 | } | 886 | } |
886 | 887 | ||
887 | // if requested, open a dump | 888 | // if requested, open a dump |
888 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); | 889 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); |
889 | if ( !dump ) | 890 | if ( !dump ) |
890 | { | 891 | { |
891 | qWarning( "OPacketCapturer::open(): can't open dump with '%s': %s", (const char*) filename, _errbuf ); | 892 | qWarning( "OPacketCapturer::open(): can't open dump with '%s': %s", (const char*) filename, _errbuf ); |
892 | return false; | 893 | return false; |
893 | } | 894 | } |
894 | qDebug( "OPacketCapturer::open(): dump [%s] opened successfully.", (const char*) filename ); | 895 | qDebug( "OPacketCapturer::open(): dump [%s] opened successfully.", (const char*) filename ); |
895 | _pcd = dump; | 896 | _pcd = dump; |
896 | 897 | ||
897 | return true; | 898 | return true; |
898 | } | 899 | } |
899 | 900 | ||
900 | 901 | ||
901 | bool OPacketCapturer::open( const QFile& file ) | 902 | bool OPacketCapturer::open( const QFile& file ) |
902 | { | 903 | { |
903 | QString name = file.name(); | 904 | QString name = file.name(); |
904 | 905 | ||
905 | if ( _open ) | 906 | if ( _open ) |
906 | { | 907 | { |
907 | close(); | 908 | close(); |
908 | if ( name == _name ) // ignore opening an already openend device | 909 | if ( name == _name ) // ignore opening an already openend device |
909 | { | 910 | { |
910 | return true; | 911 | return true; |
911 | } | 912 | } |
912 | else // close the last opened device | 913 | else // close the last opened device |
913 | { | 914 | { |
914 | close(); | 915 | close(); |
915 | } | 916 | } |
916 | } | 917 | } |
917 | 918 | ||
918 | _name = name; | 919 | _name = name; |
919 | 920 | ||
920 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); | 921 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); |
921 | 922 | ||
922 | if ( handle ) | 923 | if ( handle ) |
923 | { | 924 | { |
924 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); | 925 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); |
925 | _pch = handle; | 926 | _pch = handle; |
926 | _open = true; | 927 | _open = true; |
927 | 928 | ||
928 | // in case we have an application object, create a socket notifier | 929 | // in case we have an application object, create a socket notifier |
929 | if ( qApp ) | 930 | if ( qApp ) |
930 | { | 931 | { |
931 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 932 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
932 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 933 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
933 | } | 934 | } |
934 | 935 | ||
935 | return true; | 936 | return true; |
936 | } | 937 | } |
937 | else | 938 | else |
938 | { | 939 | { |
939 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); | 940 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); |
940 | return false; | 941 | return false; |