TCP - UDP

課程影片

第 8A 講 TCP 與網路阻塞偵測與控制技術 L08 1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

UDP 簡介

UDP 是一個 L4 中的不可靠傳輸服務。因為是在 L4,所以更明確地說,UDP 提供的服務是「一個主機上的行程」到「另一個主機上的行程」的不可靠傳輸。

而現在已經有 L3 中主機到主機傳輸的 IP 協定當基礎了,所以 UDP 要做的一件事情,就是要有辦法將 IP 上送來的封包「分發」給主機上執行的不同行程。換句話說,UDP 至少要在 IP 之上,加上一層 demultiplexing 的機制。而這個機制反映在 UDP 的 header 當中:

UPD Header

                  0      7 8     15 16    23 24    31  
                 +--------+--------+--------+--------+ 
                 |     Source      |   Destination   | 
                 |      Port       |      Port       | 
                 +--------+--------+--------+--------+ 
                 |                 |                 | 
                 |     Length      |    Checksum     | 
                 +--------+--------+--------+--------+ 
                 |                                     
                 |          data octets ...            
                 +---------------- ...                 

                      User Datagram Header Format

在這個 header 中,最重要的是 SrcPortDstPort 兩個欄位。其中:

  1. SrcPort 欄位紀錄的是這個封包「從發送方主機上的哪個 port」來的。
  2. DstPort 指得是這個封包要「送給接收方主機上的哪個 port」。

要注意的一件事情是:上述的兩個欄位只包含了 port,不包含各自主機的 IP 位址。這是因為接收方與發送方的 IP 位址已經記載在 L3 的 IP header 中了,所以這邊不需要重複紀錄。

而 Linux 核心中的資料結構則是在 include/uapi/linux/udp.h 中定義的 struct udphdr

struct udphdr {
	__be16	source;
	__be16	dest;
	__be16	len;
	__sum16	check;
};

Demultiplexing

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →