使用Pcap.Net创建自己的TCP数据包 - 数据包已发送但从未到达目的地

时间:2014-12-02 23:13:51

标签: c# networking tcp pcap.net

 private Packet BuildTcpPacket(string SourceIP, string DestIP, int sourceport, int destport,
        string payload, bool syn, bool ack, bool fin, bool rst, bool urg, bool psh, bool cwr, bool enc, bool ns, bool none)
    {
        EthernetLayer ethernetLayer = new EthernetLayer
        {
            Source = new MacAddress(tbMacVs.Text),  //doublechecked its OK
            Destination = new MacAddress(tbMacVy.Text), //OK
            EtherType = EthernetType.None, // Will be filled automatically.
        };

        IpV4Layer ipV4Layer = new IpV4Layer
        {
            Source = new IpV4Address(SourceIP), //OK
            CurrentDestination = new IpV4Address(DestIP),  //OK
            Fragmentation = IpV4Fragmentation.None,
            HeaderChecksum = null, // Will be filled automatically.
            Identification = (ushort)Convert.ToInt16("1000"), //1000
            Options = IpV4Options.None,
            Protocol = null, // Will be filled automatically.
            Ttl = (byte)Convert.ToInt16("64"), //64
            TypeOfService = 0,
        };

        TcpLayer tcpLayer = new TcpLayer();
        tcpLayer.SourcePort = (ushort)sourceport; //OK
        tcpLayer.DestinationPort = (ushort)destport; //OK
        tcpLayer.Checksum = null;// Will be filled automatically.
        tcpLayer.SequenceNumber = Convert.ToUInt16("124");
        tcpLayer.AcknowledgmentNumber = (ushort)Convert.ToUInt16("0");
        if (syn)
            tcpLayer.ControlBits = TcpControlBits.Synchronize;
        if (ack)
            tcpLayer.ControlBits = TcpControlBits.Acknowledgment;
        //if (fin)
        //    tcpLayer.ControlBits = TcpControlBits.Fin;
        //if (rst)
        //    tcpLayer.ControlBits = TcpControlBits.Reset;
        //if (urg)
        //    tcpLayer.ControlBits = TcpControlBits.Urgent;
        //if (psh)
        //    tcpLayer.ControlBits = TcpControlBits.Push;
        //if (cwr)
        //    tcpLayer.ControlBits = TcpControlBits.CongestionWindowReduced;
        //if (enc)
        //    tcpLayer.ControlBits = TcpControlBits.ExplicitCongestionNotificationEcho;
        //if (ns)
        //    tcpLayer.ControlBits = TcpControlBits.NonceSum; // not a flag, rather a tcp header bit set
        //if (none)
        //    tcpLayer.ControlBits = TcpControlBits.None;


        tcpLayer.Window = (ushort)Convert.ToUInt16("0");
        tcpLayer.UrgentPointer = 0;
        tcpLayer.Options = TcpOptions.None;

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

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

        return builder.Build(DateTime.Now);
    }

您好我使用Pcap.Net库创建我自己的TCP数据包,如Syn / Fin / Ack,没有其他信息,我需要在没有建立TCP连接的情况下对此数据包进行处理。我只需要使用此数据包到达我的服务器端口。

但是使用这种TCP数据包构造它永远不会到达我的服务器。但我确定数据包已发送(由wireshark测试)。所以请帮我构建这些数据包,我不确定所有参数是否正确使用。

0 个答案:

没有答案