Question

I am trying to do some raw socket programming with TCP protocol, however I am running into an issue with PacketDotNet and TCP checksum.

I am getting nullpointer exceptions within the PacketDotNet.TCPPacket. The exception I get is the following:

ValidTCPChecksum = 'tcpPacket.ValidTCPChecksum' threw an exception of type 'System.NullReferenceException'

And

System.NullReferenceException: Object reference not set to an instance of an object.
at PacketDotNet.TransportPacket.CalculateChecksum(TransportChecksumOption option)
at PacketDotNet.TcpPacket.CalculateTCPChecksum()
at ProjectServer.MainWindow.packetstuff(String toIp, String fromIp, Byte[] payload) in  c:\\Users\\MyUser\\Documents\\Visual Studio 2012\\Projects\\ProjectServer\\ProjectServer\\MainWindow.xaml.cs:line 131
at ProjectServer.MainWindow.Project_Click(Object sender, RoutedEventArgs e) in c:\\Users\\MyUser\\Documents\\Visual Studio 2012\\Projects\\ProjectServer\\ProjectServer\\MainWindow.xaml.cs:line 68

Line 131 is tcpPacket.Checksum = (ushort)tcpPacket.CalculateTCPChecksum();

It seems as it might have something to do with HelpLink being null, but I am not 100% sure.

I have tried to do the checksum myself, but I have thus far not been able to implement a working checksum algorithm.


Here is the packetstuff method which basicly builds my packet. Maybe there is something wrong with it.

    public void packetstuff(string toIp, string fromIp, byte[] payload)
    {
        ushort tcpSourcePort = 123;
        ushort tcpDestinationPort = 321;
        var tcpPacket = new TcpPacket(tcpSourcePort, tcpDestinationPort);

        var ipSourceAddress = System.Net.IPAddress.Parse(fromIp);
        var ipDestinationAddress = System.Net.IPAddress.Parse(toIp);
        var ipPacket = new IPv4Packet(ipSourceAddress, ipDestinationAddress);

        var sourceHwAddress = "MY-MA-CA-DD-RE-SS";//?actually a bit unsure what this should be
        var ethernetSourceHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(sourceHwAddress);
        var destinationHwAddress = "MY-MA-CA-DD-RE-SS";
        var ethernetDestinationHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(destinationHwAddress);

        var ethernetPacket = new EthernetPacket(ethernetSourceHwAddress,
            ethernetDestinationHwAddress,
            EthernetPacketType.None);

        if (tcpPacket != null)
        {
            tcpPacket.Checksum = 0;

            tcpPacket.Checksum = (ushort)tcpPacket.CalculateTCPChecksum();  //This is where the error occurs.
        }

        ipPacket.PayloadPacket = tcpPacket;
        ipPacket.UpdateIPChecksum();

        ethernetPacket.PayloadPacket = ipPacket;

        ethernetPacket.UpdateCalculatedValues();

        packetBytes = ethernetPacket.Bytes;
        Thread producer = new Thread(new ThreadStart(ThreadRun));
        device.Open();
        producer.Start();

    }

Windows7, VS2012

Was it helpful?

Solution

I see you have abandoned this, but I'll answer it for anyone else looking for the solution.

I ran into the same problem when trying to do a similar thing. I discovered that you have to link all your packets together before you can call CalculateTCPChecksum()

So to fix the example asked in the question, this should work (I haven't tested this code but it is very similar to the code I wrote that I got working):

public void packetstuff(string toIp, string fromIp, byte[] payload)
{
    ushort tcpSourcePort = 123;
    ushort tcpDestinationPort = 321;
    var tcpPacket = new TcpPacket(tcpSourcePort, tcpDestinationPort);

    var ipSourceAddress = System.Net.IPAddress.Parse(fromIp);
    var ipDestinationAddress = System.Net.IPAddress.Parse(toIp);
    var ipPacket = new IPv4Packet(ipSourceAddress, ipDestinationAddress);

    var sourceHwAddress = "MY-MA-CA-DD-RE-SS";
    var ethernetSourceHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(sourceHwAddress);
    var destinationHwAddress = "MY-MA-CA-DD-RE-SS";
    var ethernetDestinationHwAddress = System.Net.NetworkInformation.PhysicalAddress.Parse(destinationHwAddress);

    var ethernetPacket = new EthernetPacket(ethernetSourceHwAddress,
        ethernetDestinationHwAddress,
        EthernetPacketType.None);

    ethernetPacket.PayloadPacket = ipPacket;
    ipPacket.ParentPacket = ethernetPacket;

    if (tcpPacket != null)
    {
        ipPacket.PayloadPacket = tcpPacket;
        tcpPacket.ParentPacket = ip;
        ipPacket.UpdateIPChecksum();

        tcpPacket.Checksum = (ushort)tcpPacket.CalculateTCPChecksum();
    }
    else
        ipPacket.UpdateIPChecksum();

    ethernetPacket.UpdateCalculatedValues();

    packetBytes = ethernetPacket.Bytes;
    Thread producer = new Thread(new ThreadStart(ThreadRun));
    device.Open();
    producer.Start();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top