我一直在遵循http://www.codeproject.com/KB/IP/sharppcap.aspx上的指南来实现一个简单的数据包嗅探器来为我自动进行身份验证,我已经设法进入过滤部分,并且到目前为止,必须对教程代码进行一些调整才能使其正常工作,但我现在很难过。
我收到的错误是;
'PacketDotNet.TcpPacket.GetEncapsulated(PacketDotNet.Packet)' 的最佳重载方法匹配有一些无效参数
参数 1:无法从 'SharpPcap.RawCapture' 转换为 'PacketDotNet.Packet'
但是我还没有对我自己的 PacketDotNet 进行任何引用(到目前为止,一切都是 SharpPcap)。
我到目前为止的整个代码都包括在内,问题出在 device_OnPacketArrival() 函数中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PacketDotNet;
using SharpPcap;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string ver = SharpPcap.Version.VersionString;
Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);
// Retrieve the device list
CaptureDeviceList devices = CaptureDeviceList.Instance;
// If no devices were found print an error
if (devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return;
}
// Extract a device from the list
ICaptureDevice device = devices[0];
// Register our handler function to the
// 'packet arrival' event
device.OnPacketArrival +=
new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival);
// Open the device for capturing
int readTimeoutMilliseconds = 1000;
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
// tcpdump filter to capture only TCP/IP packets
string filter = "ip and tcp";
device.Filter = filter;
Console.WriteLine();
Console.WriteLine("-- The following tcpdump filter will be applied: \"{0}\"",
filter);
Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
device.Description);
// Start capturing packets indefinitely
device.Capture();
// Close the pcap device
// (Note: this line will never be called since
// we're capturing indefinitely
device.Close();
}
private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
var tcp = TcpPacket.GetEncapsulated(e.Packet);
}
}
}