《Kamailio实战》目录
第1章 Kamailio与SIP··························1
1.1 什么是 Kamailio ································· 1
1.2 背景 ······················································ 3
1.3 SIP ························································ 5
1.3.1 SIP 基础 ····································· 6
1.3.2 SIP 的基本概念和相关元素········ 7
1.3.3 SIP 的基本方法和头域 ··············· 9
1.3.4 SIP URI ······································ 9
1.3.5 SDP 和 SOA ····························· 10
1.3.6 SIP 承载 ··································· 14
1.3.7 事务、对话和会话···················· 14
1.3.8 Stateless 与 Stateful ·················· 17
1.3.9 严格路由和松散路由 ················ 18
1.3.10 Record-Route ·························· 19
1.4 Kamailio 基本架构 ··························· 19
第2章 理解Kamailio配置文件········23
2.1 基本配置文件···································· 23
2.2 原生脚本············································ 27
2.3 Lua 脚本············································· 32
2.4 Lua 脚本的其他写法························ 38
第3章 Kamailio基本概念和组件·····40
3.1 core 详解············································ 40
3.1.1 全局参数部分 ··························· 40
3.1.2 模块设置部分 ··························· 41
3.1.3 路由块部分······························· 41
3.1.4 通用元素 ·································· 42
3.1.5 核心关键字······························· 46
3.1.6 核心值 ······································· 48
3.1.7 核心参数 ·································· 49
3.1.8 DNS 相关参数 ·························· 60
3.1.9 TCP 相关参数或选项 ··············· 61
3.1.10 TLS 相关参数························· 66
3.1.11 SCTP 概述 ······························ 66
3.1.12 UDP 相关参数 ························ 66
3.1.13 核心函数································· 67
3.1.14 自定义全局参数 ····················· 73
3.1.15 脚本语句································· 73
3.1.16 脚本操作符 ····························· 75
3.2 其他概念和组件 ······························· 78
3.2.1 伪变量 ······································ 78
3.2.2 htable ········································ 79
3.2.3 AVP ·········································· 80
3.2.4 模块 ·········································· 81
第4章 KEMI详解·································83
4.1 KEMI Lua 入口································· 84
4.2 KEMI 函数 ········································ 85
4.2.1 函数整型返回值规则 ················ 85
4.2.2 函数返回 0 的情况···················· 86
4.2.3 模块函数 ·································· 86
4.3 在 C 函数中导出 KEMI 函数 ·········· 86
4.4 KEMI 和伪变量 ································ 89
4.4.1 伪变量静态名称限制················ 89
4.4.2 针对特定伪变量的函数 ············ 90
4.5 核心和 pv 模块中的函数················· 91
4.5.1 核心中的常用函数···················· 91
4.5.2 pv 模块相关函数 ······················ 94
4.5.3 KSR.hdr 子模块 ························ 96
4.5.4 特殊的 KEMI 函数 ··················· 99
4.6 原生脚本与 KEMI 对比··················100
4.6.1 函数名 ·····································100
4.6.2 函数的参数······························100
4.6.3 停止当前脚本执行···················101
4.7 其他 ···················································101
第5章 Kamailio运行环境与实例····104
5.1 运行 Kamailio ··································104
5.1.1 环境准备 ·································104
5.1.2 在命令行上运行 Kamailio ·······105
5.1.3 将配置文件保存到宿主机 ·······113
5.1.4 使用 Docker Compose 管理容器 ·········································113
5.2 将 SIP 呼叫转发到 FreeSWITCH ···115
5.3 从简单的路由脚本开始 ··················116
5.4 Kamailio 命令行工具 ······················117
5.4.1 kamctl ······································117
5.4.2 kamdbctl ··································120
5.4.3 kamcmd ···································121
5.4.4 kamcli ······································122
5.4.5 sipexer ·····································124
5.5 Web 管理界面 ··································127
5.6 调试与排错·······································130
5.6.1 使用 sipdump 模块跟踪 SIP 消息 ·········································130
5.6.2 其他 SIP 相关工具简介 ···········131
第6章 使用Kamailio做SIP路由转发 ·····································132
6.1 什么是路由·······································132
6.2 基本路由转发···································134
6.2.1 最简单、最安全的路由转发····134
6.2.2 无状态转发······························134
6.2.3 有状态转发······························135
6.2.4 并行转发 ·································135
6.2.5 串行转发 ·································138
6.3 使用 dispatcher 模块做路由转发 和负载均衡 ·······································140
6.3.1 基本用法 ·································140
6.3.2 dispatcher 模块 ························142
6.3.3 优先级路由及备用路由 ···········144
6.3.4 按权重路由······························145
6.3.5 特殊参数 ·································145
6.3.6 从数据库中加载 ······················146
6.4 呼叫从哪里来···································146
6.4.1 根据 IP 地址段判断 ·················147
6.4.2 使用 dispatcher 模块判断 ········148
6.4.3 使用 permissions 模块判断······149
6.4.4 使用 geoip2 模块判断··············150
6.5 API 路由 ···········································151
6.5.1 通过 HTTP 查询路由···············151
6.5.2 rtjson········································157
6.5.3 evapi ········································161
6.6 在 KEMI 脚本中调用原生脚本 中的路由块·······································168
第7章 数据库操作·································169
7.1 初始化数据库···································169
7.1.1 PostgreSQL······························169
7.1.2 MySQL ····································171
7.2 配置数据库连接 ······························172
7.3 在路由时进行 SQL 查询 ················172
7.4 其他函数和伪变量 ··························174
7.5 常用数据库表结构 ··························175
第8章 15个典型路由示例·············177
8.1 通过号码分析树进行路由 ··············177
8.2 号码翻译 ···········································179
8.3 低成本路由·······································181
8.4 前缀路由 ···········································184
8.5 动态路由 ···········································186
8.6 缩位拨号 ···········································188
8.7 通过别名数据库路由 ······················189
8.8 运营商路由·······································190
8.9 字冠域名翻译···································192
8.10 用户注册和查询 ····························193
8.11 向外注册 ·········································195
8.12 更多 AVP 示例·······························198
8.13 话单 ·················································200
8.14 SBC ·················································202
8.14.1 代理注册································202
8.14.2 NAT 穿透·······························206
8.14.3 代理媒体································209
8.14.4 使用FreeSWITCH做B2BUA模式 ·······································217
8.14.5 拓扑隐藏································218
8.15 WebRTC ···································219
第9章 性能··············································223
9.1 性能测试 ···········································223
9.1.1 早期的性能测试 ······················223
9.1.2 KEMI 性能测试·······················234
9.1.3 使用 VoIPPerf 进行性能测试 ·········································235
9.2 拆解 Kamailio 高性能信令服务设计 ···················································238
9.2.1 懒解析 ·····································238
9.2.2 内存管理 ·································240
9.2.3 并发和同步······························241
9.2.4 定时器和异步操作···················242
9.2.5 缓存 ·········································242
9.2.6 异步处理 ·································243
9.2.7 其他 ·········································243
第10章 安全············································244
10.1 基本安全手段和策略 ····················244
10.2 限呼 ·················································245
10.2.1 限制 User-Agent 头域··········245
10.2.2 限呼某些目的地 ··················246
10.2.3 限制高频呼叫 ······················247
10.2.4 限制太多的错误鉴权···········248
10.2.5 限制并发呼叫 ······················249
10.3 TLS ··················································250
10.3.1 理解 TLS 证书及密钥 ·········251
10.3.2 自签名证书··························252
10.3.3 在 Kamailio 中配置 TLS······252
10.3.4 TLS 连接测试······················253
10.3.5 自制 CA 根证书 ··················254
10.3.6 其他·····································255
10.4 iptables ············································256
10.5 其他安全建议和相关链接············257
附录A 安装Kamailio···························258
附录B FreeSWITCH快速入门 ········262
附录C Lua快速入门········267
附录D Docker简介及常用命令········275
附录E 模块索引表·································280
后记································································282
XIII
XIV
XV
XVI