0

我正在尝试使用 TCP 协议进行一些原始套接字编程,但是我遇到了 PacketDotNet 和 TCP 校验和的问题。

我在 PacketDotNet.TCPPacket 中遇到空指针异常。我得到的例外如下:

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

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

第 131 行是tcpPacket.Checksum = (ushort)tcpPacket.CalculateTCPChecksum();

似乎它可能与HelpLinknull 有关,但我不是 100% 确定。

我试图自己做校验和,但到目前为止我还不能实现一个有效的校验和算法。


这是基本构建我的数据包的packetstuff方法。也许它有什么问题。

    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

4

1 回答 1

2

我看到你已经放弃了这个,但我会为其他寻找解决方案的人回答这个问题。

我在尝试做类似的事情时遇到了同样的问题。我发现您必须先将所有数据包链接在一起,然后才能调用 CalculateTCPChecksum()

因此,要修复问题中提出的示例,这应该可以工作(我尚未测试此代码,但它与我编写的工作代码非常相似):

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();

}
于 2013-04-01T16:57:36.787 回答