Transport Layer – TCP IP Ports , Segment Header and Connections
In the last post we have seen various basic terms related to the TCP protocol in detail. In this post, we will mainly concentrate on the TCP segment header and connections involved in TCP . Without wasting any time let’s start the discussion .
Every byte in a TCP connection has its own 32-bit sequence number. These numbers are used for both acknowledgments and for window mechanism. Let us first understand some basic terms :
1. Segments : The segment of a TCP consists of a 20- to 60-byte header, followed by data from the application program. The sending and receiving TCP entities will exchange data in the form of segments. A segment will consist of a fixed 20 byte header (plus and optional part) followed by zero or more data bytes.(Segment header explained later in the post)
2. Segment Size : Each segment including the TCP header, must fit in the 65535 byte IP payload. The segment size is decided by the TCP software .
3. Fragmentation : If a segment is too large, then it should be broken into small segments. Using fragmentation by a router. Each new segment gets a new IP header.
TCP IP PORT (Process to Process communication)
As we have seen in UDP, TCP also provides a process to process communication via ports . Let us see some well-known port numbers that has been extensively used by TCP protocol.
|7||Echo||Echoes received datagram back to receiver|
|9||Discard||Discards any received datagram|
|13||Daytime||Returns date and time|
|17||Quote||Returns Quote of the day|
|19||Chargen||Returns string of characters|
|20||FTP data||FTP Data connection|
|21||FTP Control||FTP Control Connection|
|25||SMTP||Simple Mail Transfer
|53||DNS||Domain Name Server|
|80||HTTP||HyperText Transfer Protocol|
|111||RPC||Remote Procedure Call|
The TCP Segment Header :
Every segment begins with a 20 byte fixed format header. The first 20 bytes correspond to the IP header and the next 20 correspond to the TCP header. The TCP segment without data is used for sending the acknowledgments and control messages .
Let us now understand each of the terms present in the header :
1. Source Port : This serves the same purpose as the source port address in the UDP header. A 16-bit number will identify the application the TCP segment originated from within the sending host. The port numbers are divided into three ranges i.e. the well-known ports (0 through 1023), registered ports (1024 through 49151) and private ports (49152 through 65535).
2. Destination Port : This serves the same purpose as the destination port address in the UDP header. A 16-bit number identifying the application the TCP segment is destined for a receiving host. Destination ports use the same port number assignments as those set aside for source ports.
3. Sequence Number : TCP is a stream transport protocol. In order to ensure connectivity, each byte to be transmitted is numbered. A 32-bit number will identify the current position of the first data byte in the segment within the entire byte stream for the TCP connection. After reaching the end, this number will wrap around to 0.
4. Acknowledgement Number : A 32-bit number identifying the next data byte the sender expects from the receive, the number will be one greater than the most recently received data byte. This field is only used when the ACK control bit is turned on.
5. Header Length or Offset : A 4-bit field that specifies the total TCP header length in 32-bit words (or in multiples of 4 bytes if you prefer). The length of the header will be between 20 and 60 bytes. Without options, a TCP header wiil always be 20 bytes in length.
6. Reserved: A 6-bit field currently unused and reserved for future use.
7. Control Bits (flags) :
7.1 Urgent pointer (URG) : If this bit field is set, the receiving tcp should interpret the urgent pointer field.
7.2 Acknowledgement (ACK) : If this bit field is set, the acknowledgment field described earlier is valid.
7.3 Push function (PSH) : If this bit field is set, the receiver should deliver this segment to the receiving application as soon as possible.
7.4 Reset the connection (RST) : If this bit is present, it signals the receiver that the sender’ is aborting the connection .
7.5 Synchronize (SYN) : When present, this field Signifies that the sender is attempting to “synchronize” sequence numbers.
7.6 No more data from sender (FIN) : If set, this bit field tells the receiver that the sender has reached the end of its byte stream for the current TCP connection.
8. Window : A 16-bit integer used by TCP for flow control in the form of a data transmission window size. This number tells the sender about the quantity of data, the receiver is willing to accept. The maximum value of this field will limit the window size to 65,535 bytes.
9. Checksum : A TCP sender computes a value based on the contents of the TCP header and data fields. This 16-bit value will be compared with the value that the receiver generates using the same computation. The inclusion of the checksum in the UDP datagram is optional, whereas the inclusion of the checksum in TCP is mandatory.
10. Urgent Pointer : In certain circumstances, it may be necessary for a TCP sender to notify the receiver of urgent data that should be processed by the receiving application as soon as possible. It defines the number that must be added to the sequence number in order to obtain the number of the last urgent byte in the data section of the segment.
11. Options : In order to provide additional functionality, several optional parameters may be used between a TCP sender and receiver. Depending on the option(s) used, the length of the field will vary in size, but it cannot be larger than 40 bytes due to the size of the header length field (4 bits).
12. Padding : Because options may vary in size, it may be necessary to “pad” the TCP header with zeros so that the segment ends on a 32-bit word boundary as defined by the standard.
13. Data : Although not used in some circumstances (e.g. acknowledgment segments with no data in the reverse direction), this variable-length field carries the application data from TCP sender to receiver.
TCP Connection Establishment :
To make the transport services reliable, TCP hosts must establish a connection-oriented session with one another. All the segments belonging to a message are sent over the virtual path.
Using a single virtual pathway for the entire message facilitates the acknowledgment process as well as retransmission of damaged or lost frames.Connection establishment is performed by using a three-way handshake mechanism.
A three-way handshake synchronizes both the ends of a connection by allowing both sides to agree upon initial sequence numbers. This also guarantees that both sides are ready to transmit data and know that the other side is ready to transmit as well.
This is necessary so that packets are not transmitted or re-transmitted during session establishment or after session termination. Each host randomly chooses a sequence number that is used to track bytes within the stream it is Sending and receiving.
TCP Connection Termination Protocol (Connection Release)
Any of the two parties (sender and receiver) involved in exchanging data (client or server) can close the connection, although it is usually initiated by the client.
Most implementations today allow two options for connection termination i.e a three-way handshaking and four-way handshaking with a half-close option.
While it takes three segments in order to establish a connection. It takes four to terminate a connection. Since a TCP connection is full-duplex (that is, data flows in each direction independently of the other direction), the connection should be terminated in both the directions independently.
The termination procedure in each direction is shown below. The rule is that either side can send a FIN when it has finished sending data (FIN indicates finished). When a TCP program on a host receives a FIN, it informs the application that the other end has terminated the data flow.
The receipt of a FIN can only mean there will be no more data flowing. in that direction. A TCP can still able to send data after receiving a FIN.
TCP Connection Release
A TCP connection is a full-duplex connection but to understand the connection release we will assume that it is a pair of simplex connections. Releasing a TCP connection is identical on both ends. Each side can send a TCP segment with the FIN bit set, which means it has no more data to send.
After receiving a FIN, the acknowledge (ACK) signal is sent and that direction is shut down, but data may continue to flow indefinitely in the other direction. If the sender of FIN does not receive the ACK within 2 maximum packet lifetimes, it releases (terminate) the connection. The receiver will then eventually notice that it receives no more data.
Finally we reach the end of this post . Stay tuned for more interesting stuff in the next post.
Aric is a tech enthusiast , who love to write about the tech related products and ‘How To’ blogs . IT Engineer by profession , right now working in the Automation field in a Software product company . The other hobbies includes singing , trekking and writing blogs .