3

I need python script that can sniff and decode SIP messages in order to check their correctness. As a base for this script I use python-libpcap library for packets sniffing. I can catch UDP packet and extract SIP payload from it, but I don't know how to decode it. Does python has any libraries for packets decoding? I've found only dpkt, but as I understood it can't decode SIP. If there are no such libraries how can I do this stuff by hands?

Thank you in advance!

4

3 回答 3

4

Finally I did this with help of pyshark from the sharktools (http://www.mit.edu/~armenb/sharktools/). In order to sniff IP packages I used scapy instead of libpcap.

于 2012-10-03T12:51:32.543 回答
1

The twisted framework for python includes some support for SIP. However it appears to be written for RFC 2543, not RFC 3261.

The dpkt library has some basic SIP support.

Another approach would be to use a parser generator like ANTLR to parse the SIP message. ANTLR can create python-based parsers. I wouldn't be surprised to find an ANTLR grammar for SIP available on the web, though I didn't find one in my first few minutes of searching.

于 2012-08-24T20:03:19.937 回答
0

Since your needs seems pretty custom, I would suggest to do it yourself. The syntax is defined in the BNF grammar at the end of the RFC 3261. You may want to validate only what you need and not the whole grammar because it is quite huge.

Try to procceed by steps. If you try to handle everything in the same function you will end up with a huge and complex function. Try to generate data at each steps to simplify processing. For example, at the first step, you could read each line of the packet and make pairs of header/content. This makes a more friendly data structure that can be refined further.

Like yotommy said, you could use ANTLR but it wouldn't be my choice unless you are very familiar with it. The grammar is massive and won't be easy to represent using ANTLR.

于 2012-08-24T20:33:40.027 回答