Can't determine protocol automatically from next layer exception

Nov 10, 2014 at 6:18 PM
Edited Nov 10, 2014 at 6:22 PM
Hello,

When i'm trying to send a packet by using a version af BuildEthernetPacket() from the sending packets example, where i have added an IpV4Layer (code below). But i get an "Can't determine protocol automatically from next layer" exception:
System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Can't determine protocol automatically from next layer (PcapDotNet.Packets.PayloadLayer)
  Source=PcapDotNet.Packets
  StackTrace:
       at PcapDotNet.Packets.IpV4.IpV4Layer.Write(Byte[] buffer, Int32 offset, Int32 payloadLength, ILayer previousLayer, ILayer nextLayer)
       at PcapDotNet.Packets.PacketBuilder.WriteLayers(Int32[] layersLength, Byte[] buffer, Int32 length)
       at PcapDotNet.Packets.PacketBuilder.Build(DateTime timestamp)
       at Packets.Program.BuildEthernetPacket() i C:\Users\xx\Documents\Visual Studio 2010\Projects\Packets\Packets\Program.cs:linje 155
       at Packets.Program.Main(String[] args) i C:\Users\xx\Documents\Visual Studio 2010\Projects\Packets\Packets\Program.cs:linje 119
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Modified BuildEthernetPacket() code:
private static Packet BuildEthernetPacket()
{
    EthernetLayer ethernetLayer =
        new EthernetLayer
        {
            Source = new MacAddress("01:01:01:01:01:01"),
            Destination = new MacAddress("02:02:02:02:02:02"),
            EtherType = EthernetType.IpV4,
        };

    // IPv4 Layer
    IpV4Layer ipV4Layer = new IpV4Layer
    {
        Source = new IpV4Address("192.168.10.3"),
        Ttl = 128,
        // The rest of the important parameters will be set for each packet
    };

    PayloadLayer payloadLayer =
        new PayloadLayer
        {
            Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
        };
    
    PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
    return builder.Build(DateTime.Now);
}
I can't figure out what i'm doing wrong. I hope you can help me.

Thanks.
Nov 10, 2014 at 7:35 PM
Hi larsser,

you're not filling in all the necessary information for IPv4 header. That's why you're getting this error. Fill in IpV4Layer like:
IpV4Layer ipV4Layer =
                new IpV4Layer
                    {
                        Source = new IpV4Address("1.2.3.4"),
                        CurrentDestination = new IpV4Address("11.22.33.44"),
                        Fragmentation = IpV4Fragmentation.None,
                        HeaderChecksum = null, // Will be filled automatically.
                        Identification = 123,
                        Options = IpV4Options.None,
                        Protocol = IpV4Protocol.Udp,
                        Ttl = 100,
                        TypeOfService = 0,
                    };
I didn't check which parameters need to be set for PacketBuilder to work correctly, but this is answer to your problem.

Regards,
Anderam
Marked as answer by larsser on 11/10/2014 at 12:43 PM
Nov 10, 2014 at 7:43 PM
Hello anderam,

I didn't know about the other needed infomations. Thank you for your example code.

Regrars
larsser