Send packet with SharpPCAP on Loopback device

100 Views Asked by At

first of all sorry for my english. I tried to send and receive packet on loopback device with SharpPCAP and an error occured. I show you my programm. First, I find loopback device. Second, I open device and create packet. Finally, I send with SendPacket method

LibPcapLiveDevice senderDevice = null;
LibPcapLiveDeviceList devices = LibPcapLiveDeviceList.Instance;

foreach(LibPcapLiveDevice dev in devices)
{
    if (dev.Addresses.Count>0 && 
        IPAddress.Loopback.Equals(dev.Addresses?[1].Addr.ipAddress))
    {
        senderDevice = dev; 
        break;
    }
}

senderDevice.Open();
EthernetPacket packet = PacketCreate(File.ReadAllBytes(".txt"));
senderDevice.SendPacket(packet) // also with packet.Bytes

where PacketCreate function is

EthernetPacket PacketCreate(byte[] bytesToSend)
{
    PhysicalAddress macAdd = GetMACAddress(); //simple function to obtain mac address

    ushort tcpSourcePort = 123, tcpDestinationPort = 45678;
    TcpPacket tcpPacket = new TcpPacket(tcpSourcePort, tcpDestinationPort);
    tcpPacket.PayloadData = bytesToSend;

    var ipPacket = new IPv4Packet(IPAddress.Loopback, IPAddress.Loopback);
    var ethernetPacket = new EthernetPacket(macAdd, macAdd, EthernetType.None);

    // Now stitch all of the packets together
    ipPacket.PayloadPacket = tcpPacket;
    ethernetPacket.PayloadPacket = ipPacket;

    return ethernetPacket;

}

If I throw application in debug mode, i will have this error on SendPacket function

SharpPcap.PcapException: 'Can't send packet: send error: PacketSendPacket failed: The request is not supported. (50)'

Why? What I don't understand?.

0

There are 0 best solutions below