I'm starting work on a peer-to-peer application that needs to understand a number of protocols including SIP, RTCP, RTP, etc. I don't want to write a parser for each of these. Is there a generic approach/library for parsing a packet and exposing its fields to the rest of your application?
For example you get an SIP packet:
Code:
SIP/2.0 200 OK
Via: SIP/2.0/UDP site4.server2.com;branch=z9hG4bKnashds8;received=192.0.2.3
Via: SIP/2.0/UDP site3.server1.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2
Via: SIP/2.0/UDP pc33.server1.com;branch=z9hG4bK776asdhds;received=192.0.2.1
To: user2 ;tag=a6c85cf
From: user1 ;tag=1928301774
Call-ID: a84b4c76e66710@pc33.server1.com
CSeq: 314159 INVITE
Contact:
Content-Type: application/sdp
Content-Length: 131
I have not yet found decent libraries (C, C++, Python) that will take this packet, and
- Recognize the protocol type.
- Parse the fields and fill up a data structure. It should be a collection or associative array so that you can access the fields by name. (To, From, CSeq, etc.)
- Call an appropriate handler based on protocol type, and hand it the data structure.
To make this approach useful as possible, it should also provide a generic means to define the protocols using simple text (field names, field data types and the parsing rules). That way you could feed various protocol definitions to your parser, without adding extra code for each one.
Am I running in the wrong direction here? If it doesn't exist, could such a library/object be useful?