SendPacket() Exception

May 27, 2014 at 9:45 AM
Hi! I have some problem with sending packets via Pcap.NET.
I try to send Ethernet-ipv4-udp packets with random data as payload. When I send 1472 random bytes as payload - everything is OK. But when I'm trying to send 1473 bytes and more via SendPacket() I have an exception: System.InvalidOperationException: Failed writing to device. WinPcap Error: send error: PacketSendPacket failed.
I have exactlly the same exception when trying to use packet queue and Transmit() method.

Here is my code:
namespace PackLengthTest
{
    public partial class Form1 : Form
    {
        public IList<LivePacketDevice> allDevices;
        public LivePacketDevice activeDevice;
        public PacketCommunicator communicator;
        public MacAddress senderMac;


        public Form1()
        {
            InitializeComponent();

            allDevices = LivePacketDevice.AllLocalMachine;
            activeDevice = allDevices[allDevices.Count - 1];
            PacketDeviceOpenAttributes opAttr = PacketDeviceOpenAttributes.Promiscuous;
            senderMac = PcapDotNet.Core.Extensions.LivePacketDeviceExtensions.GetMacAddress(activeDevice);

            try
            {
                communicator = activeDevice.Open(65536, opAttr, 10000);
            }
            catch (System.Exception ex)
            {
                listBox1.Items.Add("Exception in activeDevice.Open(): " + ex.Message.ToString());
                return;
            }

            listBox1.Items.Add("ActiveDevice successfully Opened");

           
        }




        private static PcapDotNet.Packets.Packet pBuilder(MacAddress mA)
        {
            EthernetLayer etherLayer = null;
            PcapDotNet.Packets.IpV4.IpV4Layer ip4Layer = null;
            UdpLayer udpLayer = null;
            PayloadLayer payloadLayer = null;


            etherLayer = new EthernetLayer
            {
                Source = mA,
                Destination = new MacAddress("FF:FF:FF:FF:FF:FF"),
                EtherType = PcapDotNet.Packets.Ethernet.EthernetType.IpV4,
            };

            IpV4Fragmentation frag = IpV4Fragmentation.None;
            IpV4Options opt = IpV4Options.None;

            ip4Layer = new PcapDotNet.Packets.IpV4.IpV4Layer
            {
                Source = new IpV4Address("192.168.1.5"),
                CurrentDestination = new IpV4Address("192.168.1.10"),
                Fragmentation = frag,
                HeaderChecksum = null,
                Identification = (ushort)444,
                Options = opt,
                Protocol = IpV4Protocol.Udp,
                Ttl = 64,
                TypeOfService = 0,
            };


            udpLayer = new UdpLayer
            {
                SourcePort = (ushort)50000,
                DestinationPort = (ushort)50050,
                Checksum = null,
                CalculateChecksumValue = true,
            };

            byte[] byteData = new byte[1500];
            Random rand = new Random(System.Environment.TickCount);
            for (int i = 0; i < byteData.Length; i++)
            {
                byteData[i] = (byte)rand.Next(0, 255);

            }

            payloadLayer = new PayloadLayer
            {
                Data = new Datagram(byteData),
            };



            PacketBuilder pckBuilder = new PacketBuilder(etherLayer, ip4Layer, udpLayer, payloadLayer);

            
            return pckBuilder.Build(DateTime.Now);

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            PcapDotNet.Packets.Packet pack = null;

            try
            {
                pack = pBuilder(senderMac);
            }
            catch (System.Exception ex)
            {
                listBox1.Items.Add("Exception in pBuilder():" + ex.Message.ToString());
                return;
            }

            listBox1.Items.Add("packet build successfull");

            for (int i = 0; i < 10; i++)
            {   
                
                try
                {
                    communicator.SendPacket(pack);
                }
                catch (System.Exception ex)
                {
                    listBox1.Items.Add("Exception in #"+i+" SendPacket():" + ex.Message.ToString());
                    return;
                }

                listBox1.Items.Add("Packet #" + i + "send");

            }
        }
    }
}
Coordinator
Jun 27, 2014 at 9:36 PM
Usually (depending your device), Ethernet payload cannot exceed 1500 bytes.
http://en.wikipedia.org/wiki/Ethernet_frame#Payload

IPv4 header (minimum): 20 bytes.
UDP header: 8 bytes.
1472 + 20 + 8 = 1500.
More than that is likely to fail.

I hope this helps,

Boaz.
Aug 21, 2014 at 2:17 PM
Yes, you are completely rigth!
I have solved my problem, before I saw your answer. It is my fault, I should RTFM!