2

我正在使用 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 字节的数据,我是否只需将其拆分,还是需要遵守任何其他规则?

4

1 回答 1

2

是的。

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

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

于 2012-10-21T22:38:44.990 回答