首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PacketDotNet问题与CalculateTCPChecksum,ValidTCPChecksum和ValidChecksum

PacketDotNet问题与CalculateTCPChecksum,ValidTCPChecksum和ValidChecksum
EN

Stack Overflow用户
提问于 2013-02-23 11:49:09
回答 1查看 1.2K关注 0票数 0

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

我在PacketDotNet.TCPPacket中获得了空指针异常。我得到的例外是:

代码语言:javascript
复制
ValidTCPChecksum = 'tcpPacket.ValidTCPChecksum' threw an exception of type 'System.NullReferenceException'

代码语言:javascript
复制
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();

这似乎与HelpLink为null有关,但我不能百分之百肯定。

我曾经尝试过自己做校验和,但是到目前为止我还没有能够实现一个工作的校验和算法。

下面是打包方法,它基本构建我的包。也许它有什么问题。

代码语言:javascript
复制
    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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-01 16:57:36

我看你已经放弃了,但我会回答其他人,寻找解决办法。

我在做类似的事情时遇到了同样的问题。我发现在调用CalculateTCPChecksum()之前,必须将所有数据包链接在一起。

因此,要修复问题中提出的示例,这应该是可行的(我还没有测试过这段代码,但它与我编写的代码非常相似):

代码语言:javascript
复制
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();

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15040207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档