title: How to decode SIP packets?
body: <p>A SIP (Session Initiation Protocol) packet's format is something along the lines of:</p>
  
  <pre><code>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: &lt;sip:192.168.100.86&gt;;tag=60f4c35a-6cdb-4b19-b580-f3d3ad5c1abf
  To: &lt;sip:999999@192.168.202.90&gt;
  Contact: &lt;sip:192.168.101.86:5060;ob&gt;
  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
  </code></pre>
  
  <p>I need to decode this packet and extract some of its fields, like <code>From</code>, <code>To</code>, <code>Call-ID</code>, etc. The packet is in ASCII format and each filed is encoded in a separate line. 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. </p>