【问题标题】:How to tell how much space is left for data in a Pcap.Net TCP packet如何判断 Pcap.Net TCP 数据包中的数据剩余多少空间
【发布时间】:2012-10-10 23:47:32
【问题描述】:

我正在使用 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 字节。有什么方法可以用来检查包头的大小,以便知道实际数据还剩下多少空间?

另外,如果我需要发送超过 1500 字节的数据,我是直接拆分,还是需要遵守其他规则?

【问题讨论】:

  • 如果您需要发送的数据超出 MTU 允许的范围,您应该查看 IP fragmentation
  • 顺便说一句,1500 值是您的MTU

标签: c# tcp ethernet pcap pcap.net


【解决方案1】:

是的。

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

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

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 2018-05-19
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多