pcap.net - pcap.net ReceivePacket 无法正常工作
问题描述
接收数据包。
在我发送一个数据包之后,我还想接收来自前一个数据包目的地的数据包。
我尝试使用 ReceivePacket,但总是得到 packet12 的空值。
我还有什么需要做的吗?
设置过滤器。
我查了pcap.net的文档
我假设参数(“ip和icmp”)与我们可以在wireshark中使用的过滤器相同。
ip.src == 192.168.15.32 and icmp
但是这个参数有错误。这和wireshark的过滤器有区别吗?
代码:
using (PacketCommunicator inputCommunicator =
selectedInputDevice.Open(65536, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers
PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
1000)) // read timeout
{
using (PacketCommunicator outputCommunicator =
selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
// Check the MAC type
if (inputCommunicator.DataLink != outputCommunicator.DataLink)
{
Console.WriteLine("Warning: the datalink of the capture differs from the one of the selected interface.");
Console.WriteLine("Press a key to continue, or CTRL+C to stop.");
Console.ReadKey();
}
// Allocate a send buffer
using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
{
// Fill the buffer with the packets from the file
PcapDotNet.Packets.Packet tpacket;
while (inputCommunicator.ReceivePacket(out tpacket) ==
PacketCommunicatorReceiveResult.Ok)
{
sendBuffer.Enqueue(tpacket);
++numPackets;
}
// bool isSync = true;
// Transmit the queue
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
long startTimeMs = stopwatch.ElapsedMilliseconds;
Console.WriteLine("Start Time: " + DateTime.Now);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Sending a pcap file is in progress. Wait until it is finished");
Console.ResetColor();
outputCommunicator.Transmit(sendBuffer, isSync);
long endTimeMs = stopwatch.ElapsedMilliseconds;
Console.WriteLine("End Time: " + DateTime.Now);
long elapsedTimeMs = endTimeMs - startTimeMs;
Console.WriteLine("Elapsed Time: " + elapsedTimeMs);
Console.WriteLine("Elapsed time: " + elapsedTimeMs + " ms");
Console.WriteLine("Total packets generated = " + numPackets);
// Console.WriteLine("Average packets per second = " + averagePacketsPerSecond);
Console.WriteLine("============================================================");
}
inputCommunicator.SetFilter("ip and icmp");
Console.WriteLine("Listening on " + selectedInputDevice.Description + "...");
PcapDotNet.Packets.Packet packet12;
inputCommunicator.ReceivePacket(out packet12);
foreach (var options in packet12)
Console.WriteLine(options);
解决方案
1.
在某一时刻,您似乎使用inputCommunicator
to
// 用文件中的数据包填充缓冲区
但是在不同的地方,您使用相同inputCommunicator
的方法来获取从某个目的地发送的数据包。
似乎您需要使用两个不同的 Communicator 并使用不同的设备创建它们。
2.
如果您收到错误,请提供错误。
你从哪里得到的语法ip.src
?你试过src host
了吗?
推荐阅读
- react-native - 加载图像并将其作为图标传递给本机代码
- c# - Linq:在哪里+保持秩序
- c# - 如何循环 Observable Rx 直到满足条件?
- bots - Discord.js 在文本频道中加入/离开语音频道通知
- javascript - Can I use a Promise as the default value of a function's parameter?
- python - 从 DataFrame 列创建 PySpark 映射并应用于另一个 DataFrame
- javascript - 当我在手机中运行此代码时反应本机错误
- cypress - 在 cypress 中运行远程命令(SSH)
- c - MPI allReduce 迭代
- tfs - TFS - 自定义发布视图