如何判断Pcap.Net TCP数据包中的数据剩余空间

时间:2012-10-20 15:28:23

标签: c# tcp ethernet pcap pcap.net

我正在使用Pcap构建网络tcp包:

    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字节的数据,我是将其拆分,还是需要遵守的其他规则?

1 个答案:

答案 0 :(得分:2)

您可以使用ILayer.Length属性来获取每个图层的长度。

您可以使用IP分段或仅使用常规TCP(这是大多数应用程序所做的)来拆分数据包。