Пример #1
0
        private void Capture_task()
        {
            try
            {
                IntPtr adhandle = packet_capture.InitCapture();

                int count = 0;
                int temp_sec = 0, temp_usec = 0;

                while (iscapturing)
                {
                    Packet_Info temp = packet_capture.CapturePacket(adhandle);
                    if (temp == null)
                    {
                        continue;
                    }
                    if (count == 0)
                    {
                        temp_sec  = temp.tv_sec;
                        temp_usec = temp.tv_usec;
                    }
                    temp.Time   = temp.tv_sec - temp_sec + (float)(temp.tv_usec - temp_usec) / 1000000;
                    temp.Number = ++count;
                    Dispatcher.BeginInvoke(new InsertPacket(packets.Add), temp);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #2
0
        public static Packet_Info CapturePacket(IntPtr adhandle)
        {
            int    res;
            IntPtr pkt_data = new IntPtr();
            IntPtr header   = new IntPtr();

            res = pcap_next_ex(adhandle, ref header, ref pkt_data);
            if (res == 0)
            {
                return(null);
            }
            if (res == -1 || res == -2)
            {
                StringBuilder errbuf = pcap_geterr(adhandle);
                throw new InvalidOperationException(errbuf.ToString());
            }

            pcap_pkthdr header_s = Marshal.PtrToStructure <pcap_pkthdr>(header);

            byte[] packet = new byte[header_s.len];

            Marshal.Copy(pkt_data, packet, 0, header_s.len);

            Packet_Info temp = new Packet_Info(packet, header_s.tv_sec, header_s.tv_usec);

            return(temp);
        }