zhuang@linux:~/blog/common-network-protocols-format/$ cat
Common Network Protocols Format
Network protocols define how data is formatted and exchanged. This article summarizes the message or packet formats of HTTP, SMTP, UDP, and TCP, from application-layer text messages to transport-layer binary headers.
Protocol Layers
HTTP and SMTP are application-layer protocols. They usually send their messages over TCP, which in turn is carried by IP. UDP is an alternative transport-layer protocol.
Application data: HTTP message / SMTP message
Transport segment: TCP header + application data
IP packet: IP header + transport segment
Link frame: link-layer header + IP packet + trailerMulti-byte integers in TCP and UDP headers use network byte order, which is big-endian. Header sizes are measured in bytes unless stated otherwise.
HTTP
HTTP uses a request-response model. HTTP/1.1 messages are text-based, while HTTP/2 and HTTP/3 use binary framing. The following format describes HTTP/1.1.
Request Format
METHOD request-target HTTP-version\r\n
Header-Name: value\r\n
Header-Name: value\r\n
\r\n
optional message bodyExample:
GET /index.html HTTP/1.1
Host: example.com
Accept: text/htmlThe request line contains:
- Method: the requested operation, such as
GET,POST,PUT, orDELETE. - Request target: the path or resource being requested.
- HTTP version: commonly
HTTP/1.1.
Response Format
HTTP-version status-code reason-phrase\r\n
Header-Name: value\r\n
Header-Name: value\r\n
\r\n
optional message bodyExample:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 5
HelloThe blank line separates the header section from the body. A body may be delimited by Content-Length, chunked transfer encoding, or the end of the connection, depending on the message.
SMTP
Email
|
+--------+--------+
| |
Sending Receiving
| |
SMTP IMAP / POP3
|
+-----+-----+
| | |
25 587 465
relay submission
| |
STARTTLS implicit TLSSMTP transfers email using text commands and three-digit replies over TCP. Commands and replies end with \r\n.
Command Format
COMMAND optional-arguments\r\nCommon commands include:
EHLO domain: introduces the client and requests server capabilities.MAIL FROM:<address>: specifies the envelope sender.RCPT TO:<address>: specifies an envelope recipient.DATA: begins the message content.QUIT: closes the SMTP session.
Reply Format
status-code text\r\nReply codes beginning with 2 indicate success, 3 request more input, 4 indicate temporary failure, and 5 indicate permanent failure. A hyphen after the code begins a multiline reply; the final line uses a space.
Example Exchange
S: 220 mail.example.com ESMTP ready
C: EHLO client.example.com
S: 250-mail.example.com
S: 250 SIZE 10485760
C: MAIL FROM:<[email protected]>
S: 250 OK
C: RCPT TO:<[email protected]>
S: 250 OK
C: DATA
S: 354 End data with <CRLF>.<CRLF>
C: From: [email protected]
C: To: [email protected]
C: Subject: Hello
C:
C: Message body.
C: .
S: 250 Message acceptedThe SMTP envelope (MAIL FROM and RCPT TO) is distinct from the headers inside the email message. During DATA, a line containing only . ends the message. A body line that begins with . is escaped by adding another dot.
UDP
UDP is a connectionless, message-oriented transport protocol. Each datagram has a fixed eight-byte header followed by its payload.
0 15 16 31
+------------------+------------------+
| Source Port | Destination Port |
+------------------+------------------+
| Length | Checksum |
+------------------+------------------+
| Data ... |
+-------------------------------------+| Field | Size | Description |
|---|---|---|
| Source Port | 16 bits | Sender’s port; may be zero in IPv4 when unused. |
| Destination Port | 16 bits | Receiver’s port. |
| Length | 16 bits | Total size of the UDP header and payload; minimum value is 8. |
| Checksum | 16 bits | Error detection over a pseudo-header, the UDP header, and the payload. |
UDP preserves datagram boundaries but does not guarantee delivery, ordering, duplicate protection, or congestion control. The checksum is optional in IPv4 when set to zero but mandatory in IPv6.
TCP
TCP is a connection-oriented byte-stream protocol. It provides reliable, ordered delivery using sequence numbers, acknowledgments, retransmission, flow control, and congestion control.
0 15 16 31
+------------------+------------------+
| Source Port | Destination Port |
+------------------+------------------+
| Sequence Number |
+-------------------------------------+
| Acknowledgment Number |
+----+---+---------+------------------+
|DOFF|Res| Flags | Window Size |
+----+---+---------+------------------+
| Checksum | Urgent Pointer |
+------------------+------------------+
| Options and Padding ... |
+-------------------------------------+
| Data ... |
+-------------------------------------+| Field | Size | Description |
|---|---|---|
| Source Port | 16 bits | Sender’s port. |
| Destination Port | 16 bits | Receiver’s port. |
| Sequence Number | 32 bits | Position of the first data byte in this segment; during connection setup it carries the initial sequence number. |
| Acknowledgment Number | 32 bits | Next sequence number expected when ACK is set. |
Data Offset (DOFF) | 4 bits | TCP header length in 32-bit words; minimum value is 5, or 20 bytes. |
| Reserved | 3 bits | Reserved for future use and normally zero. |
| Flags | 9 bits | NS, CWR, ECE, URG, ACK, PSH, RST, SYN, and FIN. |
| Window Size | 16 bits | Receive window used for flow control. |
| Checksum | 16 bits | Error detection over an IP pseudo-header, the TCP header, and the data. |
| Urgent Pointer | 16 bits | End of urgent data when URG is set. |
| Options | Variable | Parameters such as maximum segment size, window scaling, selective acknowledgments, and timestamps. |
A TCP header is 20 to 60 bytes long. TCP exposes an ordered stream of bytes, not a sequence of application messages, so protocols such as HTTP and SMTP must define their own message boundaries.
Summary
| Protocol | Layer | Format | Typical transport | Main property |
|---|---|---|---|---|
| HTTP/1.1 | Application | Text start line, headers, blank line, optional body | TCP | Web request-response messaging |
| SMTP | Application | Text commands, replies, and message data | TCP | Email transfer |
| UDP | Transport | 8-byte binary header and datagram payload | IP | Low overhead with no delivery guarantee |
| TCP | Transport | 20-60-byte binary header and byte-stream data | IP | Reliable, ordered byte stream |
zhuang@linux:~/blog/common-network-protocols-format/$ comments