2013-02-23 83 views
0

我想用TCP协议做一些原始套接字编程,但是我遇到了PacketDotNet和TCP校验和的问题。PacketDotNet问题与CalculateTCPChecksum,ValidTCPChecksum和ValidChecksum

我得到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();

看来,因为它可能有一些做HelpLink是空的,但我不是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

+0

唉,我赞成[PcapDotNet(http://pcapdotnet.codeplex.com/),我开始工作,现在被遗弃packetDotNet。 – Automatico 2013-02-27 17:39:14

回答

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

} 
+0

我自己没有测试过,因此我无法确定它是否工作。我拥有的代码早已不复存在。我接受,因为看起来你已经有了这个工作,而且你需要把东西结合在一起是有道理的。 – Automatico 2013-04-02 12:08:42

+0

我停止了与图书馆的工作,并忘记了这个细微差别。你知道为什么在协议/库的工作方式方面,首先需要将你的随机TCP数据包附加到另一个TCP数据包上?理论上你可以(不是简单的或者是一个好主意)只是通过网络进行TCP操作,并跳过以太网和IP。 – 2014-06-19 05:25:00