The TCP/IP Model
The TCP/IP model is the core protocol architecture of the Internet. It divides network communication into four layers. This page covers the structure of the four-layer model, the function of each layer, and how it compares with the OSI model.
1. Background
The TCP/IP (Transmission Control Protocol/Internet Protocol) model is the protocol architecture the Internet is built on. It was developed by ARPA, the Advanced Research Projects Agency of the US Department of Defense, during the 1970s.
2. Characteristics
- Highly practical: applied directly on the Internet
- Simple structure: only four layers
- Mature protocols: validated by long-term practical use
- Wide adoption: the standard protocols of the Internet
3. The Four Layers
1. Network Access Layer
Description
The network access layer carries data over the physical medium. It covers the functions of both the physical layer and the data link layer.
Main Functions
- Physical transmission: carries bit streams over the transmission medium
- Framing: organises the bit stream into frames
- Address resolution: resolves an IP address to a physical address
- Error detection: detects errors introduced during transmission
- Media access control: controls access to a shared medium
Protocol Example
// Network access layer protocol structure
typedef struct {
uint8_t interface_type; // Interface type (Ethernet, WiFi, etc.)
uint8_t mac_address[6]; // MAC address
uint32_t mtu; // Maximum transmission unit
uint8_t link_status; // Link status
} NetworkInterfaceProtocol;
// Network access layer interface
typedef struct {
int (*send_frame)(const uint8_t* data, uint16_t length, const uint8_t* dest_mac);
int (*receive_frame)(uint8_t* buffer, uint16_t max_length, uint8_t* src_mac);
int (*get_mac_address)(uint8_t* mac_addr);
int (*set_mac_address)(const uint8_t* mac_addr);
int (*get_link_status)(void);
} NetworkInterface;
// Network access layer implementation
typedef struct {
NetworkInterfaceProtocol protocol;
NetworkInterface interface;
char interface_name[64];
uint32_t tx_packets;
uint32_t rx_packets;
uint32_t tx_errors;
uint32_t rx_errors;
} NetworkInterfaceImpl;
Typical Protocols
- Ethernet: the standard for local area networks
- WiFi (IEEE 802.11): the standard for wireless local area networks
- PPP (Point-to-Point Protocol): point-to-point protocol
- ARP (Address Resolution Protocol): address resolution protocol
2. Internet Layer
Description
The internet layer transfers packets between different networks and provides route selection and packet forwarding.
Main Functions
- IP addressing: assigns IP addresses to the devices on a network
- Routing: selects the best path for a packet
- Packet forwarding: forwards packets from the source towards the destination
- Congestion control: keeps the network from becoming congested
- Error handling: handles errors that arise during transmission
Protocol Example
// IP protocol structure
typedef struct {
uint8_t version; // Version number (4 or 6)
uint8_t header_length; // Header length
uint8_t tos; // Type of service
uint16_t total_length; // Total length
uint16_t identification; // Identification
uint16_t flags_offset; // Flags and fragment offset
uint8_t ttl; // Time to live
uint8_t protocol; // Upper layer protocol
uint16_t checksum; // Checksum
uint32_t src_ip; // Source IP address
uint32_t dest_ip; // Destination IP address
uint8_t options[40]; // Options
uint8_t data[65535]; // Data part
} IPPacket;
// Internet layer protocol
typedef struct {
uint8_t protocol_type; // Protocol type
uint32_t routing_table[256]; // Routing table
uint8_t fragmentation; // Fragmentation support
uint8_t qos_support; // QoS support
} InternetProtocol;
// Internet layer interface
typedef struct {
int (*send_packet)(const IPPacket* packet);
int (*receive_packet)(IPPacket* packet);
int (*route_packet)(const IPPacket* packet);
int (*add_route)(uint32_t network, uint32_t mask, uint32_t next_hop);
int (*remove_route)(uint32_t network, uint32_t mask);
} InternetInterface;
Typical Protocols
- IP (Internet Protocol): the internet layer protocol
- ICMP (Internet Control Message Protocol): internet control message protocol
- IGMP (Internet Group Management Protocol): internet group management protocol
- OSPF (Open Shortest Path First): open shortest path first routing protocol
3. Transport Layer
Description
The transport layer provides reliable data transfer between end systems and guarantees the integrity and the order of the data.
Main Functions
- End-to-end transfer: moves data between the source and the destination
- Reliable transfer: ensures data is neither lost, duplicated, nor reordered
- Flow control: controls the sending rate of the sender
- Congestion control: keeps the network from becoming congested
- Multiplexing: lets several applications use the network at the same time
Protocol Example
// TCP segment structure
typedef struct {
uint16_t src_port; // Source port
uint16_t dest_port; // Destination port
uint32_t sequence_num; // Sequence number
uint32_t ack_num; // Acknowledgement number
uint8_t header_length; // Header length
uint8_t flags; // Flags
uint16_t window_size; // Window size
uint16_t checksum; // Checksum
uint16_t urgent_ptr; // Urgent pointer
uint8_t options[40]; // Options
uint8_t data[65535]; // Data part
} TCPSegment;
// UDP datagram structure
typedef struct {
uint16_t src_port; // Source port
uint16_t dest_port; // Destination port
uint16_t length; // Length
uint16_t checksum; // Checksum
uint8_t data[65507]; // Data part
} UDPDatagram;
// Transport layer protocol
typedef struct {
uint8_t protocol_type; // Protocol type (TCP/UDP)
uint32_t max_segment_size; // Maximum segment size
uint8_t flow_control; // Flow control
uint8_t congestion_control; // Congestion control
} TransportProtocol;
// Transport layer interface
typedef struct {
int (*send_segment)(const TCPSegment* segment);
int (*receive_segment)(TCPSegment* segment);
int (*send_datagram)(const UDPDatagram* datagram);
int (*receive_datagram)(UDPDatagram* datagram);
int (*open_connection)(uint32_t dest_ip, uint16_t dest_port);
int (*close_connection)(int connection_id);
} TransportInterface;
Typical Protocols
- TCP (Transmission Control Protocol): transmission control protocol
- UDP (User Datagram Protocol): user datagram protocol
4. Application Layer
Description
The application layer provides network application services to users. It is the interface through which users interact with the network.
Main Functions
- File transfer: provides a file transfer service
- Electronic mail: provides an electronic mail service
- Remote login: provides a remote login service
- Network management: provides network management functions
- Name resolution: provides a domain name resolution service
Protocol Example
// HTTP request structure
typedef struct {
char method[16]; // Request method
char url[256]; // Request URL
char version[16]; // HTTP version
char headers[1024]; // Request headers
char body[4096]; // Request body
} HTTPRequest;
// HTTP response structure
typedef struct {
char version[16]; // HTTP version
uint16_t status_code; // Status code
char status_text[64]; // Status text
char headers[1024]; // Response headers
char body[4096]; // Response body
} HTTPResponse;
// Application layer protocol
typedef struct {
uint8_t protocol_type; // Protocol type
uint16_t port_number; // Port number
uint8_t data_format; // Data format
uint8_t security_level; // Security level
} ApplicationProtocol;
// Application layer interface
typedef struct {
int (*send_request)(const void* request, size_t length);
int (*receive_response)(void* response, size_t max_length);
int (*establish_connection)(const char* server, int port);
int (*close_connection)(void);
} ApplicationInterface;
Typical Protocols
- HTTP (Hypertext Transfer Protocol): hypertext transfer protocol
- FTP (File Transfer Protocol): file transfer protocol
- SMTP (Simple Mail Transfer Protocol): simple mail transfer protocol
- DNS (Domain Name System): domain name system
- SNMP (Simple Network Management Protocol): simple network management protocol
1. Protocol Stack Structure
// TCP/IP protocol stack
typedef struct {
ApplicationInterface app_interface;
TransportInterface transport_interface;
InternetInterface internet_interface;
NetworkInterface network_interface;
} TCPIPStack;
// Initialise the protocol stack
TCPIPStack* init_tcpip_stack() {
TCPIPStack* stack = malloc(sizeof(TCPIPStack));
if (!stack) return NULL;
// Initialise the interface of each layer
init_application_interface(&stack->app_interface);
init_transport_interface(&stack->transport_interface);
init_internet_interface(&stack->internet_interface);
init_network_interface(&stack->network_interface);
return stack;
}
2. Encapsulation
// Data encapsulation
typedef struct {
void* application_data;
void* transport_header;
void* internet_header;
void* network_header;
} EncapsulatedData;
// Encapsulate data on the sending side
EncapsulatedData* encapsulate_send_data(const void* app_data, size_t length,
uint32_t dest_ip, uint16_t dest_port) {
EncapsulatedData* data = malloc(sizeof(EncapsulatedData));
if (!data) return NULL;
// Application layer data
data->application_data = malloc(length);
memcpy(data->application_data, app_data, length);
// Add the transport layer header
TCPSegment* tcp_seg = create_tcp_segment(dest_port, length);
data->transport_header = tcp_seg;
// Add the internet layer header
IPPacket* ip_pkt = create_ip_packet(dest_ip, IP_PROTO_TCP);
data->internet_header = ip_pkt;
// Add the network access layer header
EthernetFrame* eth_frame = create_ethernet_frame(dest_ip);
data->network_header = eth_frame;
return data;
}
3. Decapsulation
// Decapsulate data on the receiving side
void* decapsulate_receive_data(const EncapsulatedData* data) {
void* result = data->application_data;
// Remove the network access layer header
remove_ethernet_header(data->network_header);
// Remove the internet layer header
remove_ip_header(data->internet_header);
// Remove the transport layer header
remove_tcp_header(data->transport_header);
return result;
}
1. Layer Correspondence
| TCP/IP Model | OSI Model | Description |
|---|---|---|
| Application Layer | Application, Presentation, Session | Provides network application services to users |
| Transport Layer | Transport Layer | Reliable end-to-end transfer |
| Internet Layer | Network Layer | Route selection and packet forwarding |
| Network Access Layer | Data Link, Physical Layer | Physical transmission and framing |
2. Main Differences
Number of Layers
- OSI model: seven layers, divided in fine detail
- TCP/IP model: four layers, divided simply
Theoretical Rigour
- OSI model: stronger on theory, highly standardised
- TCP/IP model: stronger on practice, widely deployed
Protocol Support
- OSI model: supports many network protocols
- TCP/IP model: mainly supports the TCP/IP protocol family
3. Pros and Cons
Advantages of the OSI Model
- Complete theory, clear layering
- Highly standardised
- Easy to understand and to learn
Disadvantages of the OSI Model
- Too complex, hard to implement
- Not very practical
- Rarely used as such
Advantages of the TCP/IP Model
- Simple structure, easy to implement
- Highly practical, widely deployed
- Mature and well-validated protocols
Disadvantages of the TCP/IP Model
- Weaker on theory
- Layers are not divided as finely
- Less standardised overall
1. Core Protocols
IP
- Function: provides connectionless packet transfer
- Characteristics: unreliable, connectionless
- Application: the foundation protocol of the Internet
TCP
- Function: provides reliable connection-oriented data transfer
- Characteristics: reliable, connection-oriented, with flow control
- Application: HTTP, FTP, SMTP, and so on
UDP
- Function: provides unreliable connectionless data transfer
- Characteristics: unreliable, connectionless, low overhead
- Application: DNS, DHCP, streaming media, and so on
2. Supporting Protocols
ICMP
- Function: carries network control messages
- Application: ping, traceroute, and so on
ARP
- Function: resolves an IP address to a MAC address
- Application: local area network communication
DNS
- Function: resolves a domain name to an IP address
- Application: domain name resolution services
| Term | Abbreviation | Explanation |
|---|---|---|
| Transmission Control Protocol/Internet Protocol | TCP/IP | The protocol family of the Internet |
| Network Interface Layer | - | First layer of TCP/IP |
| Internet Layer | - | Second layer of TCP/IP |
| Transport Layer | - | Third layer of TCP/IP |
| Application Layer | - | Fourth layer of TCP/IP |
| Internet Protocol | IP | Core protocol of the internet layer |
| Transmission Control Protocol | TCP | Connection-oriented transport protocol |
| User Datagram Protocol | UDP | Connectionless transport protocol |
| Address Resolution Protocol | ARP | Resolves IP to MAC |
| Internet Control Message Protocol | ICMP | Error and control messages |
| Hypertext Transfer Protocol | HTTP | Web page transfer |
| File Transfer Protocol | FTP | File transfer |
| Simple Mail Transfer Protocol | SMTP | Mail delivery |
| Domain Name System | DNS | Domain name resolution |
| Dynamic Host Configuration Protocol | DHCP | Automatic IP address assignment |
| Simple Network Management Protocol | SNMP | Network management |
| Internet Engineering Task Force | IETF | Organisation that defines the TCP/IP standards |
| Request for Comments | RFC | Document series that carries Internet standards |
