Use SharpPcap to construct and send UDP packet

SharpPcap is fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets from live and file based devices. Now we want to send UPD packet using SharpPcap.

public int SendUdp(byte[] dgram, int bytes, IPEndPoint endPoint)
{
    //construct ethernet packet
    var ethernet = new EthernetPacket(PhysicalAddress.Parse("112233445566"), PhysicalAddress.Parse("665544332211"), EthernetType.IPv4);
    //construct local IPV4 packet
    var ipv4 = new IPv4Packet(IPAddress.Parse("192.168.0.4"), endPoint.Address);
    ethernet.PayloadPacket = ipv4;
    //construct UDP packet
    var udp = new UdpPacket(12345, (ushort)endPoint.Port);
    //add data in
    udp.PayloadData = dgram;
    ipv4.PayloadPacket = udp;

    _device.SendPacket(ethernet);
    return bytes;
}

SharpPcap can be found https://github.com/chmorgan/sharppcap.