0

Say two hosts, A and B, both attempt to start a connection with each other, but the SYN from A gets to B before B's SYN arrives at A.

My answer is that A would sent SYN ACK and would ignore the SYN from B. Am I correct?

7
  • 1
    No. Two connections get established and it's up to your app to detect this and decide if one needs to get dropped.
    – Sven
    Jan 1, 2015 at 15:15
  • @Sven So there isn't some sort of protocol stated by RFC?
    – orange
    Jan 1, 2015 at 15:16
  • 5
    What? Think about it: Host A tries to SSH to Host B and vice versa. Why should one of the connections not be established? They are completely unrelated.
    – Sven
    Jan 1, 2015 at 15:17
  • SvW is correct, the connections are independent. As long as there's nothing preventing the connections from being establish, both would be. The order is indeterminate given the information provided. The order is also largely irrelevant unless the applications initiating and receiving the connections at each end interact in some way that would place some relevance on the connection order.
    – Chris S
    Jan 1, 2015 at 17:46
  • 1
    @psusi - no, well sort of no. So I can establish an SSH session from comp A to comp B and then one from comp B to comp A and both can be over port 22. Keep in mind that port 22 is the DESTINATION port. The SOURCE ports on either side will differ - so they will still be two autonomous connections using the same destination ports and there will be no conflicts. I can even establish two SSH connections from comp A to comp B, both using port 22 as the DESTINATION port and it will still work fine, but both connections will still have different SOURCE ports.
    – MaQleod
    Jan 2, 2015 at 19:03

3 Answers 3

1

Two connections would be made. TCP is not state aware in that fashion - neither connection would have any concept of the other connection.

For instance:

Connection 1:
192.168.1.5 sends SYN to 192.168.1.6 on port 80.

Connection 2:
192.168.1.6 sends SYN to 192.168.1.5 on port 80.

For this to continue, both would need a listening service on port 80, so each would have something listening on TCP for port 80 and that service would receive the SYN and respond with a SYN-ACK:

Connection 1:
192.167.1.6 responds with SYN-ACK to 192.168.1.5 on port 80

Connection 2:
192.167.1.5 responds with SYN-ACK to 192.168.1.6 on port 80

Keep in mind, these listening services are on opposite machines - no way to know that the other has also received a SYN, so no reason they shouldn't send an SYN-ACK.

As the TCP protocol dictates, once the originating side receives the SYN-ACK, it will respond:

Connection 1:
192.168.1.5 sends ACK to 192.168.1.6 on port 80.

Connection 2:
192.168.1.6 sends ACK to 192.168.1.5 on port 80.

You now have two independent connections with completed TCP handshakes. As mentioned in the comments by SvW: if this is a bad thing, it would be up to whatever application initiated the connections to determine that this state was present and to figure out which connection to break down - that part isn't TCP's job.

2

Remember that a TCP socket is a quadruplet srcHost:srcPort:dstHost:dstPort and that for a connection to be established one host must listen on a specific port.

So for the first connection, A:portA:B:portB socket will be (B listens on portB) :

  • in SYN_SENT state on A
  • in SYN_RECEIVED state on B

And for the second connection, A:portA':B:portB' (A listens on portA')

  • in SYN_SENT on B
  • in LISTEN state on A
2
  • Do you mind posting the full answer to my question please. As I understand there will 2 connections right? In what order do the messages arrive? Thanks! :)
    – orange
    Jan 1, 2015 at 16:24
  • 1
    This is actually the full answer to your question. The two connections are unrelated because if you send a SYN packet then the other hand has to be in LISTEN state for the connection to be established. So ports are necessarily different. Jan 1, 2015 at 16:46
1

Assuming that both hosts use the same pair of ports, then a connection would be established. In practice this does not happen, both because when hosts establish a connection, they use a random local port and a well known remote port, so both hosts would not use the same port pair. Then even if they did, the timing would have to be just right such that the SYNs pass each other in flight. If one host gets the SYN before sending its own, then it would respond with an RST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.