Search code examples
c#communicationpacketethernetpcap.net

How to build a Ethernet Packet without any protocole Pcap.net c#?


I follow this tutorial PcapDotNet/Pcap.Net. But I will try to build a packet like this picture Ethernet frame.

    private static Packet BuildEthernetPacket()
    {
        EthernetLayer ethernetLayer =
            new EthernetLayer
            {
                Destination = new MacAddress("00:00:00:00:00:00"),
                Source = new MacAddress("11:11:11:11:11:11"),
                EtherType = EthernetType.None, // I select NONE for no IPV4 or ARP and so one protocole ...
            };

        PayloadLayer payloadLayer =
            new PayloadLayer
            {
                Data = new Datagram(Encoding.ASCII.GetBytes("Hello stackoverflow")),
            };
        // The probleme it's here but the code build : 
        PacketBuilder builder = new PacketBuilder(ethernetLayer, payloadLayer);
        return builder.Build(DateTime.Now);
    }

Visual Studio Community detect an error " Can't determine ether type automatically from next layer (PcapDotNet.Packets.PayloadLayer)" & 'System.ArgumentException' in PcapDotNet.Packets.dll
But if I change EthernetType.None by EthernetType.IpV4 or IpV6 or ARP and so on .. .There are no problem, but I don't want to add other protocol.

Thanks in advance.


Solution

  • EtherType can't really be None in a packet. You should set it to some value. When it is set to None (or not set), Pcap.Net tries to calculate it automatically using the next layer. However, the next layer in your code is the PayloadLayer, which gives no information on the EtherType.

    Basically, the EtherType should say how is the Ethernet payload should be parsed. If you put some payload, there should be a way to parse it.