2012-10-20 53 views
2

我使用PCAP建立網絡TCP包:如何判斷多少剩餘空間可用於在TCP Pcap.Net分組數據

private static Packet BuildTcpPacket() 
    { 
     EthernetLayer ethernetLayer = 
      new EthernetLayer 
      { 
       Source = new MacAddress("01:01:01:01:01:01"), 
       Destination = new MacAddress("02:02:02:02:02:02"), 
       EtherType = EthernetType.None, // Will be filled automatically. 
      }; 

     IpV4Layer ipV4Layer = 
      new IpV4Layer 
      { 
       Source = new IpV4Address("1.2.3.4"), 
       CurrentDestination = new IpV4Address("11.22.33.44"), 
       Fragmentation = IpV4Fragmentation.None, 
       HeaderChecksum = null, // Will be filled automatically. 
       Identification = 123, 
       Options = IpV4Options.None, 
       Protocol = null, // Will be filled automatically. 
       Ttl = 100, 
       TypeOfService = 0, 
      }; 

     TcpLayer tcpLayer = 
      new TcpLayer 
      { 
       SourcePort = 4050, 
       DestinationPort = 25, 
       Checksum = null, // Will be filled automatically. 
       SequenceNumber = 100, 
       AcknowledgmentNumber = 50, 
       ControlBits = TcpControlBits.Acknowledgment, 
       Window = 100, 
       UrgentPointer = 0, 
       Options = TcpOptions.None, 
      }; 

     PayloadLayer payloadLayer = 
      new PayloadLayer 
      { 
       Data = new Datagram(Encoding.ASCII.GetBytes("hello world")), 
      }; 

     PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer); 

     return builder.Build(DateTime.Now); 
    } 

因爲包通過以太網發送,最大總包大小限於1500字節。有沒有什麼方法可以用來檢查pachage標題的大小,以便知道實際數據剩下多少空間?另外,如果我需要發送超過1500字節的數據,我只是將它分開,還是我需要遵守的其他規則?

+0

如果您需要發送比MTU更多的數據,您應該查看[IP碎片](http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly)。 – Nasreddine

+0

順便說一句,1500值是你的[MTU](http://en.wikipedia.org/wiki/MTU_(networking))。 – Nasreddine

回答

2

是的。

您可以使用ILayer.Length屬性來獲取每個圖層的長度。

您可以使用IP碎片或僅使用常規TCP(這是大多數應用程序所做的)來拆分數據包。