Search code examples
c#network-programmingsniffingsharppcap

Missing extract method for CaptureEventArgs.Packet


So, I am trying to sniff packets using Sharppcap, and closely following the developer's tutorial for Sharppcap at codeproject, I try to call the extract() method on CaptureEventArgs.Packet. Seems like, there's no implementation for the extract() method. Code:

using PacketDotNet;
using SharpPcap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SnifferGUI
{
    class ProjectBehaviour
    {
        public void Initsniff()
        {
            CaptureDeviceList captureDeviceList = CaptureDeviceList.Instance;

            if(captureDeviceList.Count < 1)
            {
                throw new InsufficientExecutionStackException();
            }
            ICaptureDevice device = captureDeviceList[1]; //todo
            device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival);

            device.Open(DeviceMode.Promiscuous, 0);

            device.StartCapture();
        }

        private void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var tcp = (TcpPacket)e.Packet.Extract(typeof(TcpPacket)); //According to the tutorial, this is a valid expression.
        }
    }

}

My goal with this is to parse the packet I received into a TcpPacket for further progression. I want to store and display source/destination ip and ports, timestamp and so on. So, am I missing something?


Solution

  • So, yes... After contacting the dev via issue on the official github repository, we came to the conclusion that the tutorial was outdated. Up-to-date examples are provided here: click!