I wrote a UDP port scanner. I send a packet to another device in the same network but over here is a little problem, why wireshark receives only one packet if I send 10 packets on different ports(each one port is closed). I should receive 10 ICMP packects with type: 3. What should I change in my code to get these?
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
using System;
using System.Collections.Generic;
namespace SendingASinglePacketWithSendPacket
{
class Program
{
static void Main(string[] args)
{
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
PacketDevice selectedDevice = allDevices[2];
for (int i = 1; i < 10; i++)
{
new Sender().SendUDPandGetStatus(selectedDevice, (ushort)i);
}
System.Console.ReadKey();
}
}
class Sender
{
public void SendUDPandGetStatus(PacketDevice selectedDevice, ushort port)
{
using (PacketCommunicator communicator = selectedDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, -1))
{
communicator.SendPacket(BuildUdpPacket(port));
}
}
private static Packet BuildUdpPacket(ushort destinationPort)
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Destination = new MacAddress("14:cc:20:2c:7e:36"),
Source = new MacAddress("9C:4E:36:17:86:48"),
EtherType = EthernetType.None,
};
IpV4Layer ipV4Layer =new IpV4Layer
{
Source = new IpV4Address("192.168.0.104"),
CurrentDestination = new IpV4Address("192.168.0.105"),
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null,
Identification = 123,
Options = IpV4Options.None,
Ttl = 30,
TypeOfService = 0,
};
UdpLayer udpLayer = new UdpLayer
{
SourcePort = 4050,
DestinationPort = destinationPort,
Checksum = null,
CalculateChecksumValue = true,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer);
return builder.Build(DateTime.Now);
}
}
}
I would only add if I put breakpoint at line:
new Sender().SendUDPandGetStatus(selectedDevice, (ushort)i);
and push F5 button(5s interval), get correct result.