2013-02-23 81 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