简体   繁体   English

如何解码SIP数据包?

[英]How to decode SIP packets?

A SIP (Session Initiation Protocol) packet's format is something along the lines of: SIP(会话发起协议)数据包的格式大致如下:

INVITE sip:999999@192.168.202.90:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.100.86:5060;rport;branch=z9hG4bKPj3f79189a-581d-4f58-a4dd-bc64e050421c
Max-Forwards: 70
From: <sip:192.168.100.86>;tag=60f4c35a-6cdb-4b19-b580-f3d3ad5c1abf
To: <sip:999999@192.168.202.90>
Contact: <sip:192.168.101.86:5060;ob>
Call-ID: 247ee45e-957a-481a-b97b-82f2429a999d
CSeq: 17491 INVITE
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Supported: replaces, 100rel, timer, norefersub
Session-Expires: 1800
Min-SE: 90
User-Agent: PJSUA v2.3 Linux-3.2.0.4/x86_64/glibc-2.13
Content-Type: application/sdp
Content-Length:   479

I need to decode this packet and extract some of its fields, like From , To , Call-ID , etc. The packet is in ASCII format and each filed is encoded in a separate line. 我需要对该数据包进行解码,并提取其某些字段,例如FromToCall-ID等。该数据包采用ASCII格式,并且每个字段都以单独的一行编码。 Also, the order of fields is not determined and they may be in any order. 另外,字段的顺序不确定,可以是任何顺序。 What's the best method to extract the value of the desired fields from such packet? 从此类数据包中提取所需字段的值的最佳方法是什么? I need to be able to set and get each field separately. 我需要能够分别设置和获取每个字段。 The simplest idea that comes to my mind is to read the packet line by line and check if each line starts with any of my desired values. 我想到的最简单的想法是逐行读取数据包,并检查每行是否以我想要的任何值开头。 I really appreciate any advice on showing me an efficient way to do this. 对于向我展示有效的方法的建议,我非常感谢。

Personally, I'd do this by hand. 就个人而言,我会手工完成。 It's utterly deterministic. 完全是确定性的。 There will never be a colon in the field name, so you just look for it on each line, chop, and populate a map. 字段名称中永远不会有冒号,因此您只需在每一行上查找它,然后将其剁碎并填充地图即可。 I'd try not to code for specific fields like "Via". 我尽量不要为“ Via”之类的特定字段编码。 Just write something generic that could also read email headers without code changes. 只需编写一些通用的内容即可读取电子邮件标头,而无需更改代码。 You could try to find a library but it would bring baggage you don't need. 您可以尝试找到图书馆,但是它将带来您不需要的行李。 It's small enough to write by hand. 它足够小,可以用手书写。 Watch out for security issues like interminable lines. 当心安全性问题,例如线路不间断。

One of the better approaches has been to use the Lex / Yaml. 更好的方法之一是使用Lex / Yaml。 No matter how much similarities there are in each of the SIP headers, each one has a specific pattern. 不管每个SIP标头有多少相似之处,每个标头都有一个特定的模式。 And one of the ways to capture the parsing logic should be break down into the most common base types defined in the ABNF for the SIP. 捕获解析逻辑的方法之一应该分解为ABIP中为SIP定义的最常见的基本类型。 Then build on them for each header and gradually build it cover more fancier headers that keeps coming out every few days from the IETF. 然后在每个标头的基础上逐步构建,并逐渐覆盖更高级的标头,这些标头每隔几天就会从IETF中脱颖而出。

I have found the approach to be really extendable and SIP stacks do use the similar approach for the parsing. 我发现该方法是真正可扩展的,并且SIP堆栈确实使用类似的方法进行解析。 BTW i would second Adrians approach if you are only intrested in a few common headers. 顺便说一句,如果您只被一些常见的标题所吸引,我将第二次推荐Adrians。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM