1#!/usr/bin/python 2# 3# Copyright 2015 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import time 18from socket import * # pylint: disable=wildcard-import 19 20import net_test 21import multinetwork_base 22import packets 23 24# TCP states. See include/net/tcp_states.h. 25TCP_ESTABLISHED = 1 26TCP_SYN_SENT = 2 27TCP_SYN_RECV = 3 28TCP_FIN_WAIT1 = 4 29TCP_FIN_WAIT2 = 5 30TCP_TIME_WAIT = 6 31TCP_CLOSE = 7 32TCP_CLOSE_WAIT = 8 33TCP_LAST_ACK = 9 34TCP_LISTEN = 10 35TCP_CLOSING = 11 36TCP_NEW_SYN_RECV = 12 37 38TCP_NOT_YET_ACCEPTED = -1 39 40 41class TcpBaseTest(multinetwork_base.MultiNetworkBaseTest): 42 43 def tearDown(self): 44 if hasattr(self, "s"): 45 self.s.close() 46 super(TcpBaseTest, self).tearDown() 47 48 def OpenListenSocket(self, version, netid): 49 family = {4: AF_INET, 5: AF_INET6, 6: AF_INET6}[version] 50 address = {4: "0.0.0.0", 5: "::", 6: "::"}[version] 51 s = net_test.Socket(family, SOCK_STREAM, IPPROTO_TCP) 52 # We haven't configured inbound iptables marking, so bind explicitly. 53 self.SelectInterface(s, netid, "mark") 54 self.port = net_test.BindRandomPort(version, s) 55 return s 56 57 def _ReceiveAndExpectResponse(self, netid, packet, reply, msg): 58 pkt = super(TcpBaseTest, self)._ReceiveAndExpectResponse(netid, packet, 59 reply, msg) 60 self.last_packet = pkt 61 return pkt 62 63 def ReceivePacketOn(self, netid, packet): 64 super(TcpBaseTest, self).ReceivePacketOn(netid, packet) 65 self.last_packet = packet 66 67 def RstPacket(self): 68 return packets.RST(self.version, self.myaddr, self.remoteaddr, 69 self.last_packet) 70 71 def FinPacket(self): 72 return packets.FIN(self.version, self.myaddr, self.remoteaddr, 73 self.last_packet) 74 75 76 def IncomingConnection(self, version, end_state, netid): 77 self.s = self.OpenListenSocket(version, netid) 78 self.end_state = end_state 79 80 remoteaddr = self.remoteaddr = self.GetRemoteAddress(version) 81 myaddr = self.myaddr = self.MyAddress(version, netid) 82 83 if version == 5: version = 4 84 self.version = version 85 86 if end_state == TCP_LISTEN: 87 return 88 89 desc, syn = packets.SYN(self.port, version, remoteaddr, myaddr) 90 synack_desc, synack = packets.SYNACK(version, myaddr, remoteaddr, syn) 91 msg = "Received %s, expected to see reply %s" % (desc, synack_desc) 92 reply = self._ReceiveAndExpectResponse(netid, syn, synack, msg) 93 if end_state == TCP_SYN_RECV: 94 return 95 96 establishing_ack = packets.ACK(version, remoteaddr, myaddr, reply)[1] 97 self.ReceivePacketOn(netid, establishing_ack) 98 99 if end_state == TCP_NOT_YET_ACCEPTED: 100 return 101 102 self.accepted, _ = self.s.accept() 103 net_test.DisableFinWait(self.accepted) 104 105 if end_state == TCP_ESTABLISHED: 106 return 107 108 desc, data = packets.ACK(version, myaddr, remoteaddr, establishing_ack, 109 payload=net_test.UDP_PAYLOAD) 110 self.accepted.send(net_test.UDP_PAYLOAD) 111 self.ExpectPacketOn(netid, msg + ": expecting %s" % desc, data) 112 113 desc, fin = packets.FIN(version, remoteaddr, myaddr, data) 114 fin = packets._GetIpLayer(version)(str(fin)) 115 ack_desc, ack = packets.ACK(version, myaddr, remoteaddr, fin) 116 msg = "Received %s, expected to see reply %s" % (desc, ack_desc) 117 118 # TODO: Why can't we use this? 119 # self._ReceiveAndExpectResponse(netid, fin, ack, msg) 120 self.ReceivePacketOn(netid, fin) 121 time.sleep(0.1) 122 self.ExpectPacketOn(netid, msg + ": expecting %s" % ack_desc, ack) 123 if end_state == TCP_CLOSE_WAIT: 124 return 125 126 raise ValueError("Invalid TCP state %d specified" % end_state) 127