pcap.net ReceivePacket не работает должным образом - PullRequest
0 голосов
/ 20 мая 2019
  1. получение пакета.

    После того, как я отправил пакет, я также хочу получить пакет, который пришел от места назначения предыдущего пакета.

    Я пыталсяиспользовать ReceivePacket, но всегда я получаю нулевое значение для packet12.

    Есть ли что-нибудь, что мне нужно сделать больше?

  2. установить фильтр.

    Я просмотрел документы pcap.net

    Я предположил, что параметр ("ip and 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 Ответ

0 голосов
/ 21 июня 2019

1.

В какой-то момент кажется, что вы используете inputCommunicator до

// Заполнить буфер пакетами из файла

Но в другом месте вы используете тот же inputCommunicator, чтобы получить пакет, отправленный из некоторого места назначения.

Похоже, вам нужно использовать два разных коммуникатора и создавать их с использованием разных устройств.

2

Если вы получили ошибку, укажите ее.

Где вы взяли синтаксис ip.src? Вы пробовали src host вместо этого?

...