예제 #1
0
 def ReceiveRstPacketOn(self, netid):
     # self.last_packet is the last packet we received. Invert direction twice.
     _, ack = packets.ACK(self.version, self.myaddr, self.remoteaddr,
                          self.last_packet)
     desc, rst = packets.RST(self.version, self.remoteaddr, self.myaddr,
                             ack)
     super(TcpBaseTest, self).ReceivePacketOn(netid, rst)
 def SendRSTOnClosedSocket(self, version, netid, expect_rst):
     self.IncomingConnection(version, tcp_test.TCP_ESTABLISHED, netid)
     self.accepted.setsockopt(net_test.SOL_TCP, net_test.TCP_LINGER2, -1)
     net_test.EnableFinWait(self.accepted)
     self.accepted.shutdown(SHUT_WR)
     desc, fin = self.FinPacket()
     self.ExpectPacketOn(netid, "Closing FIN_WAIT1 socket", fin)
     finversion = 4 if version == 5 else version
     desc, finack = packets.ACK(finversion, self.remoteaddr, self.myaddr,
                                fin)
     self.ReceivePacketOn(netid, finack)
     try:
         self.ExpectPacketOn(netid, "Closing FIN_WAIT1 socket", fin)
     except AssertionError:
         pass
     self.accepted.close()
     desc, rst = packets.RST(version, self.myaddr, self.remoteaddr,
                             self.last_packet)
     if expect_rst:
         msg = "closing socket with linger2, expecting %s: " % desc
         self.ExpectPacketOn(netid, msg, rst)
     else:
         msg = "closing socket with linger2, expecting no packets"
         self.ExpectNoPacketsOn(netid, msg)
예제 #3
0
 def RstPacket(self):
     return packets.RST(self.version, self.myaddr, self.remoteaddr,
                        self.last_packet)
예제 #4
0
    def CheckConnectOption(self, version):
        ip_layer = {4: scapy.IP, 6: scapy.IPv6}[version]
        netid = self.RandomNetid()
        s = self.TFOClientSocket(version, netid)

        self.clearTcpMetrics(version, netid)

        # Connect the first time.
        remoteaddr = self.GetRemoteAddress(version)
        with self.assertRaisesErrno(EINPROGRESS):
            s.connect((remoteaddr, 53))
        self.assertSocketNotConnected(s)

        # Expect a SYN handshake with an empty TFO option.
        myaddr = self.MyAddress(version, netid)
        port = s.getsockname()[1]
        self.assertNotEqual(0, port)
        desc, syn = packets.SYN(53,
                                version,
                                myaddr,
                                remoteaddr,
                                port,
                                seq=None)
        syn.getlayer("TCP").options = [(TCPOPT_FASTOPEN, "")]
        msg = "Fastopen connect: expected %s" % desc
        syn = self.ExpectPacketOn(netid, msg, syn)
        syn = ip_layer(str(syn))

        # Receive a SYN+ACK with a TFO cookie and expect the connection to proceed
        # as normal.
        desc, synack = packets.SYNACK(version, remoteaddr, myaddr, syn)
        synack.getlayer("TCP").options = [(TCPOPT_FASTOPEN, "helloT"),
                                          ("NOP", None), ("NOP", None)]
        self.ReceivePacketOn(netid, synack)
        synack = ip_layer(str(synack))
        desc, ack = packets.ACK(version, myaddr, remoteaddr, synack)
        msg = "First connect: got SYN+ACK, expected %s" % desc
        self.ExpectPacketOn(netid, msg, ack)
        self.assertSocketConnected(s)
        s.close()
        desc, rst = packets.RST(version, myaddr, remoteaddr, synack)
        msg = "Closing client socket, expecting %s" % desc
        self.ExpectPacketOn(netid, msg, rst)

        # Connect to the same destination again. Expect the connect to succeed
        # without sending a SYN packet.
        s = self.TFOClientSocket(version, netid)
        s.connect((remoteaddr, 53))
        self.assertSocketNotConnected(s)
        self.ExpectNoPacketsOn(netid,
                               "Second TFO connect, expected no packets")

        # Issue a write and expect a SYN with data.
        port = s.getsockname()[1]
        s.send(net_test.UDP_PAYLOAD)
        desc, syn = packets.SYN(53,
                                version,
                                myaddr,
                                remoteaddr,
                                port,
                                seq=None)
        t = syn.getlayer(scapy.TCP)
        t.options = [(TCPOPT_FASTOPEN, "helloT"), ("NOP", None), ("NOP", None)]
        t.payload = scapy.Raw(net_test.UDP_PAYLOAD)
        msg = "TFO write, expected %s" % desc
        self.ExpectPacketOn(netid, msg, syn)