跳到主要内容

7. 数据结构

以下各节定义 NTP 状态机 (state machine). 状态变量 (state variable) 按其在数据包头部 (packet header), 对等方进程 (peer process) 和轮询进程 (poll process), 系统进程 (system process), 以及时钟规训进程 (clock discipline process) 中的功能划分为不同类别. 数据包变量 (packet variable) 表示已发送和已接收数据包中的 NTP 头部值. 对等方变量 (peer variable) 和轮询变量 (poll variable) 分别表示每个服务器的关联 (association) 内容. 系统变量 (system variable) 表示依赖该服务器的客户端所看到的服务器状态. 时钟规训变量 (clock discipline variable) 表示时钟规训算法 (clock discipline algorithm) 的内部工作状态. 附录 A 给出了一个示例.

7.1. 结构约定

为了区分名称相同但用于不同进程的变量, 本文采用图 5 汇总的命名约定. 接收数据包变量 v 是数据包结构 r 的成员, 其完全限定名为 r.v. 类似地, x.v 是发送数据包变量, p.v 是对等方变量, s.v 是系统变量, c.v 是时钟规训变量. 每个关联都有一组对等方变量; 系统变量和时钟变量则只有一组.

名称 (Name)描述 (Description)
r.接收数据包头部变量 (receive packet header variable)
x.发送数据包头部变量 (transmit packet header variable)
p.对等方/轮询变量 (peer/poll variable)
s.系统变量 (system variable)
c.时钟规训变量 (clock discipline variable)

图 5: 前缀约定

7.2. 全局参数

除变量类别外, 本文还定义了一些全局参数 (global parameter), 包括图 6 中列出并给出取值的参数.

名称 (Name)值 (Value)描述 (Description)
PORT123NTP 端口号 (NTP port number)
VERSION4NTP 版本号 (NTP version number)
TOLERANCE15e-6频率容差 PHI (frequency tolerance PHI, s/s)
MINPOLL4最小轮询指数 (minimum poll exponent, 16 s)
MAXPOLL17最大轮询指数 (maximum poll exponent, 36 h)
MAXDISP16最大离散度 (maximum dispersion, 16 s)
MINDISP.005最小离散度增量 (minimum dispersion increment, s)
MAXDIST1距离阈值 (distance threshold, 1 s)
MAXSTRAT16最大 stratum 编号 (maximum stratum number)

图 6: 全局参数

虽然这些是互操作性所需的唯一全局参数, 但任何实现都需要更多参数. 附录 A.1.1 列出了骨架程序 (skeleton) 在缓解算法 (mitigation algorithm), 时钟规训算法, 以及相关实现相关函数中使用的参数.

7.3. 数据包头部变量

从外部视角看, 最重要的状态变量是图 7 及下文描述的数据包头部变量. NTP 数据包头部由整数个 32-bit (4 octet) 字组成, 采用网络字节序. 数据包格式由三个组成部分构成: 头部本身, 一个或多个可选扩展字段 (extension field), 以及一个可选消息认证码 (message authentication code, MAC).

名称 (Name)公式 (Formula)描述 (Description)
leapleapleap indicator (LI), 闰秒指示符
versionversionversion number (VN), 版本号
modemode模式 (mode)
stratumstratumstratum, 层级
pollpoll轮询指数 (poll exponent)
precisionrho精度指数 (precision exponent)
rootdelaydelta_r根延迟 (root delay)
rootdispepsilon_r根离散度 (root dispersion)
refidrefid参考 ID (reference ID)
reftimereftime参考时间戳 (reference timestamp)
orgT1起源时间戳 (origin timestamp)
recT2接收时间戳 (receive timestamp)
xmtT3发送时间戳 (transmit timestamp)
dstT4目的时间戳 (destination timestamp)
keyidkeyid密钥 ID (key ID)
dgstdgst消息摘要 (message digest)

图 7: 数据包头部变量

NTP 数据包是一个 UDP 数据报 (datagram) [RFC0768]. 图 8 所示的 NTP 数据包头部有 12 个字, 后面跟随可选扩展字段, 最后是可选消息认证码 (MAC).

    0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode | Stratum | Poll | Precision |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Delay |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Dispersion |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reference ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Reference Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Origin Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Receive Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Transmit Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
. Extension Field 1 (variable) .
. .
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Key Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| dgst (128) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

图 8: 数据包头部格式 (Packet Header Format)

字段解释

LI (Leap Indicator, leap): 2-bit integer, 用于警告即将发生的闰秒 (leap second).

值 (Value)含义 (Meaning)
0no warning, 无警告
1last minute of the day has 61 seconds, 当天最后一分钟有 61 秒
2last minute of the day has 59 seconds, 当天最后一分钟有 59 秒
3unknown (clock unsynchronized), 未知 (时钟未同步)

图 9: Leap Indicator

VN (Version Number, version): 3-bit integer, 表示 NTP 版本号, 当前为 4.

Mode (mode): 3-bit integer, 表示模式.

值 (Value)含义 (Meaning)
0reserved, 保留
1symmetric active, 对称主动
2symmetric passive, 对称被动
3客户端 (client)
4服务器 (server)
5broadcast, 广播
6NTP control message, NTP 控制消息
7reserved for private use, 保留供私有使用

图 10: Association Mode

Stratum (stratum): 8-bit integer, 表示 stratum.

值 (Value)含义 (Meaning)
0unspecified or invalid, 未指定或无效
1主服务器 (primary server, 例如配备 GPS 接收器)
2-15辅助服务器 (secondary server, 经由 NTP)
16unsynchronized, 未同步
17-255reserved, 保留

图 11: 数据包 Stratum (Packet Stratum)

Poll: 8-bit signed integer, 表示连续消息之间的最大间隔, 单位为 log2 秒.

Precision: 8-bit signed integer, 表示系统时钟的精度, 单位为 log2 秒.

Root Delay (rootdelay): 到参考时钟 (reference clock) 的总往返延迟 (round-trip delay), 采用 NTP 短格式.

Root Dispersion (rootdisp): 到参考时钟的总离散度, 采用 NTP 短格式.

Reference ID (refid): 32-bit code, 用于标识特定服务器或参考时钟.

Reference Timestamp (reftime): 系统时钟最近一次被设置或校正的时间, 采用 NTP 时间戳格式.

Origin Timestamp (org, T1): 请求发往服务器时客户端处的时间, 采用 NTP 时间戳格式.

Receive Timestamp (rec, T2): 请求从客户端到达服务器时服务器处的时间, 采用 NTP 时间戳格式.

Transmit Timestamp (xmt, T3): 响应发往客户端时服务器处的时间, 采用 NTP 时间戳格式.

Destination Timestamp (dst, T4): 应答从服务器到达客户端时客户端处的时间, 采用 NTP 时间戳格式.

7.4. Kiss-o'-Death 数据包

如果 Stratum 字段为 0, 表示未指定或无效, 则 Reference Identifier 字段可用于传递对状态报告和访问控制有用的消息. 这类数据包称为 Kiss-o'-Death (KoD) 数据包, 它们承载的 ASCII 消息称为 kiss code.

Kiss code 的接收者必须检查这些 code, 并在以下情况下采取相应操作:

a. 对于 kiss code DENY 和 RSTR, 客户端必须解除动员 (demobilize) 与该服务器的任何关联, 并停止向该服务器发送数据包;

b. 对于 kiss code RATE, 客户端必须立即降低到该服务器的轮询间隔, 并在每次收到 RATE kiss code 时继续降低该间隔.

c. 以 ASCII 字符 "X" 开头的 kiss code 用于未注册的实验和开发, 如果无法识别, 必须忽略.

d. 除上述情况外, KoD 数据包没有协议意义, 在检查后即丢弃.

7.5. NTP Extension Field 格式

在 NTPv4 中, 可以在头部之后, MAC 之前插入一个或多个扩展字段. 扩展字段包含请求消息或响应消息, 其格式如图 14 所示.

    0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Field Type | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
. .
. Value .
. .
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Padding (as needed) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

图 14: Extension Field 格式 (Extension Field Format)

所有扩展字段都以零填充到字 (四个 octet) 边界. Length 字段是一个 16-bit unsigned integer, 表示整个扩展字段的长度, 单位为 octet, 包括 Padding 字段.