SIP Dialog 与 Transaction 详解

概述

在 SIP(会话发起协议)中,**Dialog(对话)和Transaction(事务)**是两个核心概念。理解它们的区别和关系对于开发、调试和维护 SIP 应用程序至关重要。

核心区别:

  • Transaction: SIP 通信的基本单位,短期存在
  • Dialog: SIP 会话的上下文环境,长期存在

Transaction (事务)

定义

事务是 SIP 协议中的基本通信单位,由一个请求和所有与之相关的响应组成。

核心特性

1. 唯一标识

每个事务都有唯一的标识符:

  • Via branch ID: Via 头中的 branch 参数
  • CSeq: 命令序列号
  • From tag + Call-ID: 组合标识

示例:

1
2
3
4
5
6
INVITE sip:bob@example.com SIP/2.0
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>
CSeq: 314159 INVITE

事务标识 = branch=z9hG4bK776asdhds + Call-ID + From tag

2. 事务类型

Invite 客户端事务 (INVITE Client Transaction)

1
2
3
4
5
状态机:
├─ "Calling" → 发送 INVITE
├─ "Proceeding" → 收到 1xx 临时响应
├─ "Completed" → 收到 200 OK (发送 ACK)
└─ "Terminated" → 收到 ACK 确认或超时

Invite 服务端事务 (INVITE Server Transaction)

1
2
3
4
5
状态机:
├─ "Proceeding" → 收到 INVITE, 发送 1xx
├─ "Completed" → 发送 200 OK (等待 ACK)
├─ "Confirmed" → 收到 ACK
└─ "Terminated" → Timer D 超时

非 Invite 客户端事务 (Non-INVITE Client Transaction)

1
2
3
4
5
状态机:
├─ "Trying" → 发送请求
├─ "Proceeding" → 收到 1xx
├─ "Completed" → 收到最终响应
└─ "Terminated" → Timer K 超时

非 Invite 服务端事务 (Non-INVITE Server Transaction)

1
2
3
4
5
状态机:
├─ "Trying" → 收到请求
├─ "Proceeding" → 发送 1xx
├─ "Completed" → 发送最终响应
└─ "Terminated" → Timer J 超时

3. 生命周期

短生命周期:

  • INVITE 事务: 从 INVITE 发送到收到 ACK (最长 64*T1 = 32秒)
  • 非 INVITE 事务: 从请求发送到收到最终响应 (最长 64*T1 = 32秒)

时间线:

1
2
3
4
5
6
7
8
t=0s     发送 INVITE
t=0.5s 未收到响应, 重传 (Timer A = T1)
t=1.0s 未收到响应, 重传 (Timer A = 2*T1)
t=2.0s 收到 100 Trying
t=2.5s 收到 180 Ringing
t=5.0s 收到 200 OK → 进入 Completed 状态
t=5.1s 发送 ACK → 进入 Confirmed 状态
t=5.1s Timer D 启动 → Terminated 状态

Dialog (对话)

定义

对话是两个 SIP 用户代理之间的点对点关系,由一系列相关的消息组成。

核心特性

1. 对话标识符 (Dialog ID)

对话由以下三部分唯一标识:

1
Dialog ID = Call-ID + From tag + To tag

示例:

1
2
3
4
5
6
7
8
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad

Dialog ID:
- Call-ID: a84b4c76e66710@pc33.example.com
- From tag: 1928301774
- To tag: as6f43ad

⚠️ 注意: To tag 是在响应中添加的,所以对话在 200 OK 响应 后才正式建立。

2. 对话类型

早期对话 (Early Dialog)

  • 收到 1xx 临时响应(包含 To tag)后创建
  • 可能升级为确认对话
  • 也可能被取消

确认对话 (Confirmed Dialog)

  • 收到 2xx 成功响应后创建
  • 可以承载多个事务

示例:

1
2
3
4
5
6
7
8
9
收到 180 Ringing (To tag: xyz)
→ 创建 Early Dialog #1

收到 183 Session Progress (To tag: abc)
→ 创建 Early Dialog #2

收到 200 OK (To tag: as6f43ad)
→ 升级为 Confirmed Dialog
→ Early Dialogs 终止

3. 对话状态

状态转换:

1
2
3
4
5
6
7
8
9
10
11
12
13
null
│
│ INVITE (无 To tag)
↓
"Early" (收到 1xx + To tag)
│
│ 2xx 成功响应
↓
"Confirmed" (收到 2xx + 发送 ACK)
│
│ BYE / 超时 / 错误
↓
"Terminated"

4. 长生命周期

对话持续整个会话期间:

  • 从 200 OK + ACK 开始
  • 到 BYE + 200 OK 结束
  • 可能持续数分钟、数小时甚至数天

Dialog 与 Transaction 的关系

层次关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Dialog (对话)
│
├─ Transaction #1: INVITE 事务
│ ├─ INVITE 请求
│ ├─ 100 Trying
│ ├─ 180 Ringing
│ ├─ 200 OK
│ └─ ACK
│
├─ Transaction #2: INFO 事务 (DTMF)
│ ├─ INFO
│ └─ 200 OK
│
├─ Transaction #3: UPDATE 事务
│ ├─ UPDATE (修改 SDP)
│ └─ 200 OK
│
└─ Transaction #4: BYE 事务
├─ BYE
└─ 200 OK

创建关系

INVITE 事务创建 Dialog:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Alice                           Bob
| |
|--- (INVITE 事务开始) ---------> |
| INVITE |
| |
|<------ 180 Ringing ----------- |
| (Early Dialog 创建) |
| |
|<------- 200 OK --------------- |
| (Dialog 确认) |
| |
|--- (INVITE 事务结束) ---------> |
| ACK |
| (Dialog 持续存在) |
| |
|<======= Dialog 存续期 =======> |
| |
|--- (新事务) ------------------> |
| INFO |
|<------ 200 OK ----------------- |
| |
|--- (新事务) ------------------> |
| BYE |
|<------ 200 OK ----------------- |
| (Dialog 终止) |

关键区别

特性 Transaction Dialog
标识符 branch ID + CSeq Call-ID + From tag + To tag
生命周期 短期(秒级) 长期(分钟到小时)
组成 1 个请求 + 多个响应 多个事务
状态机 4 种状态 (Calling/Proceeding/Completed/Terminated) 4 种状态 (null/Early/Confirmed/Terminated)
作用 可靠性机制(重传、超时) 会话上下文(路由、状态)
RFC 章节 RFC 3261 Section 17 RFC 3261 Section 12

实际示例

完整的 SIP 呼叫(多个事务)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
========== Transaction #1: INVITE (创建 Dialog) ==========

INVITE sip:bob@example.com SIP/2.0
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>
CSeq: 1 INVITE
Contact: <sip:alice@192.168.1.100:5060>
Content-Type: application/sdp

[SDP Offer]

---
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds
To: Bob <sip:bob@example.com>
From: Alice <sip:alice@example.com>;tag=1928301774
Call-ID: a84b4c76e66710@pc33.example.com
CSeq: 1 INVITE

---
SIP/2.0 180 Ringing
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds
To: Bob <sip:bob@example.com>;tag=as6f43ad ← To tag 首次出现
From: Alice <sip:alice@example.com>;tag=1928301774
Call-ID: a84b4c76e66710@pc33.example.com
CSeq: 1 INVITE
Contact: <sip:bob@192.168.1.200:5060>

⚠️ Early Dialog 创建: Call-ID + From tag + To tag

---
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds
To: Bob <sip:bob@example.com>;tag=as6f43ad
From: Alice <sip:alice@example.com>;tag=1928301774
Call-ID: a84b4c76e66710@pc33.example.com
CSeq: 1 INVITE
Contact: <sip:bob@192.168.1.200:5060>
Content-Type: application/sdp

[SDP Answer]

✅ Confirmed Dialog 创建

---
ACK sip:bob@192.168.1.200 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bK776asdhds2
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad
CSeq: 1 ACK

✅ Transaction #1 结束
✅ Dialog 持续存在


========== Transaction #2: INFO (在 Dialog 内) ==========

INFO sip:bob@192.168.1.200 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bKnew1
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad
CSeq: 2 INFO
Content-Type: application/dtmf-relay

Signal=1
Duration=160

---
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bKnew1
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad
CSeq: 2 INFO

✅ Transaction #2 结束
✅ Dialog 继续存在


========== Transaction #3: BYE (在 Dialog 内终止 Dialog) ==========

BYE sip:bob@192.168.1.200 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bKnew2
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad
CSeq: 3 BYE

---
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.100:5060;branch=z9hG4bKnew2
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad
CSeq: 3 BYE

✅ Transaction #3 结束
✅ Dialog 终止

Dialog 的路由机制

Record-Route 头

作用: 确保 Proxy 能够继续参与后续请求

INVITE 中的 Record-Route:

1
2
3
4
INVITE sip:bob@example.com SIP/2.0
Record-Route: <sip:proxy1.example.com;lr>
Record-Route: <sip:proxy2.example.com;lr>
...

后续请求的路由:

1
2
3
BYE sip:bob@192.168.1.200 SIP/2.0
Route: <sip:proxy2.example.com;lr>, <sip:proxy1.example.com;lr>
...

路由决策:

1
2
3
4
1. 如果 Dialog 中有 Route 头
→ 按照 Route 头路由(经过 Proxy)
2. 如果 Dialog 中无 Route 头
→ 直接发送到 Request-URI(P2P)

事务超时与重传

定时器参数

T1 (RTT 估算): 初始值 500ms

  • Timer A: INVITE 重传定时器 = T1
  • Timer B: INVITE 事务超时 = 64*T1 = 32秒
  • Timer E: 非 INVITE 重传定时器 = T1
  • Timer F: 非 INVITE 事务超时 = 64*T1 = 32秒
  • Timer G: 200 OK 重传定时器 = T2 或 RTT
  • Timer H: 200 OK 超时 = 64*T1 = 32秒
  • Timer D: ACK 等待超时 = 32秒
  • Timer J: 非 INVITE 确认超时 = 64*T1 = 32秒
  • Timer K: 非 INVITE 状态清除 = T4 = 5秒
  • Timer L: INVITE 确认超时 (等待 ACK) = 64*T1 = 32秒

T2: 最大重传间隔 = 2秒 T4: 网络延迟估算 = 5秒

重传示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
UAC                    网络                  UAS
| | |
|--- INVITE ---------->| (丢失) |
| | |
| [Timer A = 500ms 超时] |
|--- INVITE (重传1) -->| (丢失) |
| | |
| [Timer A = 1000ms 超时] |
|--- INVITE (重传2) ------------------------->|
| | |
| |<-- 100 Trying ------|
|<--- 100 Trying -------| |
| | |
| [Timer A = 2000ms 超时] |
|--- INVITE (重传3) ------------------------->|
| | |
| |<-- 180 Ringing -----|
|<--- 180 Ringing ------| |
| | |
| |<-- 200 OK ----------|
|<--- 200 OK ---------- | |
| | |
|--- ACK ------------------------------------>|
| | |
✅ Transaction 完成

Dialog 状态管理

UA 状态

UAC Dialog 状态:

1
2
3
4
5
6
7
null
↓ INVITE (无 To tag)
Early (收到 1xx + To tag)
↓ 2xx
Confirmed (收到 2xx + 发送 ACK)
↓ BYE / 超时
Terminated

UAS Dialog 状态:

1
2
3
4
5
6
7
null
↓ INVITE (无 To tag)
Early (发送 1xx + 生成 To tag)
↓ 收到 ACK
Confirmed (发送 2xx + 收到 ACK)
↓ BYE
Terminated

状态转换表

当前状态 事件 新状态 说明
null 收到 INVITE (无 To tag) Early 创建 Early Dialog
Early 收到 2xx Confirmed 升级为 Confirmed Dialog
Early 收到 CANCEL Terminated 对话取消
Confirmed 收到 BYE Terminated 对话终止
Confirmed Session 超时 Terminated 会话过期
Terminated - null 清理资源

Forking 请求与 Dialog

什么是 Forking

Proxy 将 INVITE 同时发送到多个 UAS,创建多个 Early Dialog。

Forking 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Alice                    Proxy                Bob1    Bob2    Bob3
| | | | |
|--- INVITE ----------->|--- INVITE -------->| | |
| |--- INVITE -------->| | |
| |--- INVITE -------->| | |
| | | | |
|<-- 100 Trying --------|<-- 100 Trying -----| | |
| | | | |
|<-- 180 Ringing -------|<-- 180 Ringing ----| | |
| (To tag: abc) | | | |
| | | | |
|<-- 180 Ringing -------| |<-- 180 Ringing |
| (To tag: def) | | | |
| | | | |
|<-- 200 OK -------------|<-- 200 OK ---------| | |
| (To tag: abc) | | | |
| | | | |
|--- ACK -------------->|--- ACK ------------>| | |
| (Dialog #1 确认) | | | |
| |--- CANCEL -------->| | |
| |--- CANCEL ---------------->| |
| |--- CANCEL ------------------------>|

结果:

  • 创建 3 个 Early Dialogs
  • 只有 Bob1 接听,确认 Dialog #1
  • Bob2 和 Bob3 的 Dialog 被取消

常见问题

Q1: 为什么 ACK 是 INVITE 事务的一部分,但使用不同的 CSeq 方法?

答:

  • ACK 的 CSeq 号码与 INVITE 相同,但方法改为 ACK
  • 这是为了标识 ACK 属于哪个 INVITE 事务
  • ACK 不创建新事务,是 INVITE 事务的最后一部分

Q2: Dialog 中的 To tag 什么时候添加?

答:

  • UAS 在响应中添加 To tag(1xx 或 2xx)
  • UAC 收到带 To tag 的响应后,Dialog 标识符完整
  • 早期对话在 1xx 响应后创建
  • 确认对话在 2xx 响应后创建

Q3: 如果收到多个 2xx 响应怎么办?

答:

  • 对于 INVITE 事务,可能收到多个 2xx(Forking)
  • 每个 2xx 创建独立的 Dialog
  • UAC 可以接受或拒绝多余的 2xx(发送 BYE)

Q4: Transaction 失败会影响 Dialog 吗?

答:

  • 如果 INVITE 事务失败,Dialog 不会建立
  • 如果 Dialog 内的 INFO/UPDATE 事务失败,Dialog 继续存在
  • 如果 BYE 事务失败,Dialog 可能超时终止

Q5: 如何判断消息属于哪个 Dialog?

答: 检查以下三个字段:

1
2
3
Call-ID: a84b4c76e66710@pc33.example.com
From: Alice <sip:alice@example.com>;tag=1928301774
To: Bob <sip:bob@example.com>;tag=as6f43ad

三者完全匹配 → 同一个 Dialog


调试技巧

查看事务

Wireshark 过滤:

1
sip.Method == "INVITE" && sip.Call-ID == "a84b4c76e66710@pc33.example.com"

查看 Via branch:

1
tcpdump -n -A 'udp port 5060' | grep "branch="

查看 Dialog

提取 Dialog ID:

1
grep -A 10 "INVITE" sip.log | grep -E "(Call-ID|tag)"

Wireshark 过滤:

1
2
3
sip.Call-ID == "a84b4c76e66710@pc33.example.com"
&& sip.from.tag == "1928301774"
&& sip.to.tag == "as6f43ad"

追踪对话状态

使用 siptrace:

1
2
siptrace -i eth0 -f "port 5060" -w sip.pcap
wireshark sip.pcap

Telephony → SIP → Dialogs 查看所有对话


相关 RFC


参考文档


快速参考

Transaction 状态速查

INVITE 客户端事务:

  • Calling → Proceeding → Completed → Terminated

INVITE 服务端事务:

  • Proceeding → Completed → Confirmed → Terminated

非 INVITE 客户端事务:

  • Trying → Proceeding → Completed → Terminated

非 INVITE 服务端事务:

  • Trying → Proceeding → Completed → Terminated

Dialog 标识符提取

1
2
3
4
5
def get_dialog_id(message):
call_id = message.headers['Call-ID']
from_tag = message.headers['From'].tag
to_tag = message.headers['To'].tag
return f"{call_id}|{from_tag}|{to_tag}"

CSeq 规则

  • 同一事务: CSeq 号码相同,方法可能不同(INVITE → ACK)
  • 新事务: CSeq 号码递增,方法改变(INVITE → INFO)
  • 错误响应的 ACK: CSeq 号码相同,方法为 ACK(不属于 INVITE 事务)

Pasted image 20260128170154


© 2018–2026 小周周 · 使用 Stellar 构建

站内搜索

没有找到内容!