The TCP handshake · Lesson 4 of 8 · 3 min read
Reading the handshake in packet captures
tcpdump flag notation
tcpdump -i any -nn 'host 10.0.2.15 and port 5432'
tcpdump -i any -nn 'tcp[tcpflags] & tcp-syn != 0' # SYN and SYN-ACK only
tcpdump -i any -nn 'tcp[tcpflags] & (tcp-rst) != 0' # resets
tcpdump -i eth0 -nn -w /tmp/cap.pcap port 443 # save for Wireshark
| tcpdump shows | Flags |
|---|---|
[S] |
SYN |
[S.] |
SYN-ACK (. = ACK) |
[.] |
ACK |
[P.] |
PSH + ACK (data) |
[F.] |
FIN + ACK |
[R] / [R.] |
RST |
A healthy connection:
10:01:02.100 IP 10.0.1.20.51544 > 10.0.2.15.5432: Flags [S], seq 3812, win 64240, options [mss 8961,sackOK,TS,nop,wscale 7]
10:01:02.101 IP 10.0.2.15.5432 > 10.0.1.20.51544: Flags [S.], seq 9921, ack 3813, win 62643, options [mss 8961,...]
10:01:02.101 IP 10.0.1.20.51544 > 10.0.2.15.5432: Flags [.], ack 1, win 502
10:01:02.102 IP 10.0.1.20.51544 > 10.0.2.15.5432: Flags [P.], seq 1:42, ack 1, length 41
The time between [S] and [S.] is the network round-trip time to that server.
Failure patterns in captures:
| Capture | Meaning |
|---|---|
[S], then retransmitted [S] after 1 s, 3 s, 7 s... no reply |
SYNs dropped — firewall/SG/NACL, routing, wrong IP, host down (client sees timeout) |
[S] answered by [R.] |
Nothing listening on that port (client sees connection refused) |
Server sends [S.] repeatedly, client never ACKs |
Return path broken: NACL outbound, asymmetric routing, client-side firewall |
Data flows, then [R] from a middle IP or after idle time |
Load balancer/NAT/firewall idle timeout killed the flow |
| Handshake OK, small packets OK, large packets retransmitted forever | MTU / PMTUD black hole |
In Wireshark, the handshake is the first three packets (SYN, SYN-ACK, ACK); useful filters: tcp.flags.syn==1, tcp.flags.reset==1, tcp.analysis.retransmission, tcp.analysis.zero_window, and Statistics → Conversations for an overview.
Proving where packets die: capture on both ends at once. If SYNs leave the client but never reach the server, something in between drops them (SG, NACL, firewall, routing). If SYNs arrive and SYN-ACKs leave but never reach the client, suspect the return path.
Measuring handshake cost with curl
curl -o /dev/null -s -w 'dns:%{time_namelookup} connect:%{time_connect} tls:%{time_appconnect} ttfb:%{time_starttransfer} total:%{time_total}\n' https://shop.example.com
- TCP handshake time ≈
time_connect − time_namelookup(one RTT). - TLS handshake ≈
time_appconnect − time_connect. - Server processing ≈
time_starttransfer − time_appconnect(time to first byte).
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.