zhuang@linux:~/blog/common-network-protocols-format/$ cat

Common Network Protocols Format

$ grep tags common-network-protocols-format.md

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.

text
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 + trailer

Multi-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

http
METHOD request-target HTTP-version\r\n
Header-Name: value\r\n
Header-Name: value\r\n
\r\n
optional message body

Example:

http
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html

The request line contains:

  • Method: the requested operation, such as GET, POST, PUT, or DELETE.
  • Request target: the path or resource being requested.
  • HTTP version: commonly HTTP/1.1.

Response Format

http
HTTP-version status-code reason-phrase\r\n
Header-Name: value\r\n
Header-Name: value\r\n
\r\n
optional message body

Example:

http
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 5

Hello

The 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

plaintext
                 Email
                   |
          +--------+--------+
          |                 |
       Sending           Receiving
          |                 |
       SMTP            IMAP / POP3
          |
    +-----+-----+
    |     |     |
   25    587   465
 relay   submission
          |      |
      STARTTLS implicit TLS

SMTP transfers email using text commands and three-digit replies over TCP. Commands and replies end with \r\n.

Command Format

text
COMMAND optional-arguments\r\n

Common 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

text
status-code text\r\n

Reply 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

text
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 accepted

The 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.

text
  0               15 16              31
 +------------------+------------------+
 |   Source Port    | Destination Port |
 +------------------+------------------+
 |      Length      |     Checksum     |
 +------------------+------------------+
 |                Data ...             |
 +-------------------------------------+
FieldSizeDescription
Source Port16 bitsSender’s port; may be zero in IPv4 when unused.
Destination Port16 bitsReceiver’s port.
Length16 bitsTotal size of the UDP header and payload; minimum value is 8.
Checksum16 bitsError 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.

text
  0               15 16              31
 +------------------+------------------+
 |   Source Port    | Destination Port |
 +------------------+------------------+
 |            Sequence Number          |
 +-------------------------------------+
 |         Acknowledgment Number        |
 +----+---+---------+------------------+
 |DOFF|Res|  Flags  |   Window Size    |
 +----+---+---------+------------------+
 |     Checksum     |  Urgent Pointer  |
 +------------------+------------------+
 |       Options and Padding ...        |
 +-------------------------------------+
 |                Data ...              |
 +-------------------------------------+
FieldSizeDescription
Source Port16 bitsSender’s port.
Destination Port16 bitsReceiver’s port.
Sequence Number32 bitsPosition of the first data byte in this segment; during connection setup it carries the initial sequence number.
Acknowledgment Number32 bitsNext sequence number expected when ACK is set.
Data Offset (DOFF)4 bitsTCP header length in 32-bit words; minimum value is 5, or 20 bytes.
Reserved3 bitsReserved for future use and normally zero.
Flags9 bitsNS, CWR, ECE, URG, ACK, PSH, RST, SYN, and FIN.
Window Size16 bitsReceive window used for flow control.
Checksum16 bitsError detection over an IP pseudo-header, the TCP header, and the data.
Urgent Pointer16 bitsEnd of urgent data when URG is set.
OptionsVariableParameters 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

ProtocolLayerFormatTypical transportMain property
HTTP/1.1ApplicationText start line, headers, blank line, optional bodyTCPWeb request-response messaging
SMTPApplicationText commands, replies, and message dataTCPEmail transfer
UDPTransport8-byte binary header and datagram payloadIPLow overhead with no delivery guarantee
TCPTransport20-60-byte binary header and byte-stream dataIPReliable, ordered byte stream

zhuang@linux:~/blog/common-network-protocols-format/$ comments