var devices = CaptureDeviceList.Instance;instance报红
//IP = ipAddress;
int count = devices.Count;count也报红
if (count < 1)
{
Console.WriteLine("No device found on this machine");
return;
}
//开始抓包
for (int i = 0; i < count; ++i)
{
for (int j = 0; j < ports.Count; ++j)
{
CaptureFlowRecv(IP, ports[j], i);
CaptureFlowSend(IP, ports[j], i);
}
}
}
#region 获取IP地址
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIPAdrress()
{
List<IPAddress> PhysicalIP = new List<IPAddress>();//物理网卡IP地址(包括无线,有线,不包括虚拟网卡)
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();//获取本机所有网卡对象
foreach (NetworkInterface adapter in adapters)
{
if (!adapter.Description.Contains("Virtual") && !adapter.Description.Contains("VMnet"))//枚举条件:描述中包含"Virtual"
{
IPInterfaceProperties ipProperties = adapter.GetIPProperties();//获取IP配置
UnicastIPAddressInformationCollection ipCollection = ipProperties.UnicastAddresses;//获取单播地址集
foreach (UnicastIPAddressInformation ip in ipCollection)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)//只要ipv4的
PhysicalIP.Add(ip.Address);
}
}
}
//获取所有IP地址,包括有线,无线,虚拟网卡
String LocalhostName = Dns.GetHostName();
IPHostEntry host = Dns.GetHostEntry(LocalhostName);
List<IPAddress> addresses = new List<IPAddress>();
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork && PhysicalIP.Contains(ip))
{
addresses.Add(ip);
}
}
if (addresses.Count == 0)
return "";
return addresses.ToArray()[0].ToString();
}
#endregion
public void CaptureFlowSend(string IP, int portID, int deviceID)
{
ICaptureDevice device = (ICaptureDevice)CaptureDeviceList.New()[deviceID];new()也报红
device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrivalSend);
int readTimeoutMilliseconds = 1000;
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
string filter = "src host " + IP + " and src port " + portID;
device.Filter = filter;
device.StartCapture();
ProcInfo.dev.Add(device);
}
|