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    self.port = packets.RandomPort()
50    family = {4: AF_INET, 5: AF_INET6, 6: AF_INET6}[version]
51    address = {4: "0.0.0.0", 5: "::", 6: "::"}[version]
52    s = net_test.Socket(family, SOCK_STREAM, IPPROTO_TCP)
53    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
54    s.bind((address, self.port))
55    # We haven't configured inbound iptables marking, so bind explicitly.
56    self.SelectInterface(s, netid, "mark")
57    s.listen(100)
58    return s
59
60  def _ReceiveAndExpectResponse(self, netid, packet, reply, msg):
61    pkt = super(TcpBaseTest, self)._ReceiveAndExpectResponse(netid, packet,
62                                                             reply, msg)
63    self.last_packet = pkt
64    return pkt
65
66  def ReceivePacketOn(self, netid, packet):
67    super(TcpBaseTest, self).ReceivePacketOn(netid, packet)
68    self.last_packet = packet
69
70  def RstPacket(self):
71    return packets.RST(self.version, self.myaddr, self.remoteaddr,
72                       self.last_packet)
73
74  def IncomingConnection(self, version, end_state, netid):
75    self.s = self.OpenListenSocket(version, netid)
76    self.end_state = end_state
77
78    remoteaddr = self.remoteaddr = self.GetRemoteAddress(version)
79    myaddr = self.myaddr = self.MyAddress(version, netid)
80
81    if version == 5: version = 4
82    self.version = version
83
84    if end_state == TCP_LISTEN:
85      return
86
87    desc, syn = packets.SYN(self.port, version, remoteaddr, myaddr)
88    synack_desc, synack = packets.SYNACK(version, myaddr, remoteaddr, syn)
89    msg = "Received %s, expected to see reply %s" % (desc, synack_desc)
90    reply = self._ReceiveAndExpectResponse(netid, syn, synack, msg)
91    if end_state == TCP_SYN_RECV:
92      return
93
94    establishing_ack = packets.ACK(version, remoteaddr, myaddr, reply)[1]
95    self.ReceivePacketOn(netid, establishing_ack)
96
97    if end_state == TCP_NOT_YET_ACCEPTED:
98      return
99
100    self.accepted, _ = self.s.accept()
101    net_test.DisableLinger(self.accepted)
102
103    if end_state == TCP_ESTABLISHED:
104      return
105
106    desc, data = packets.ACK(version, myaddr, remoteaddr, establishing_ack,
107                             payload=net_test.UDP_PAYLOAD)
108    self.accepted.send(net_test.UDP_PAYLOAD)
109    self.ExpectPacketOn(netid, msg + ": expecting %s" % desc, data)
110
111    desc, fin = packets.FIN(version, remoteaddr, myaddr, data)
112    fin = packets._GetIpLayer(version)(str(fin))
113    ack_desc, ack = packets.ACK(version, myaddr, remoteaddr, fin)
114    msg = "Received %s, expected to see reply %s" % (desc, ack_desc)
115
116    # TODO: Why can't we use this?
117    #   self._ReceiveAndExpectResponse(netid, fin, ack, msg)
118    self.ReceivePacketOn(netid, fin)
119    time.sleep(0.1)
120    self.ExpectPacketOn(netid, msg + ": expecting %s" % ack_desc, ack)
121    if end_state == TCP_CLOSE_WAIT:
122      return
123
124    raise ValueError("Invalid TCP state %d specified" % end_state)
125