I am trying to parse a SIP packet and get some information from it. To be more specific, the package is as follows:
REGISTER sip:open-ims.test SIP/2.0
Via: SIP/2.0/UDP 192.168.1.64:5060;rport;branch=z9hG4bK1489975971
From: <sip:alice@open-ims.test>;tag=1627897650
To: <sip:alice@open-ims.test>
Call-ID: 1097412971
CSeq: 1 REGISTER
Contact: <sip:alice@192.168.1.64:5060;line=5fc3b39f127158d>;+sip.instance="<urn:uuid:46f525fe-3f60-11e0-bec1-d965d1488cfa>"
Authorization: Digest username="alice@open-ims.test", realm="open-ims.test", nonce=" ", uri="sip:open-ims.test", response=" "
Max-Forwards: 70
User-Agent: UCT IMS Client
Expires: 600000
Supported: path
Supported: gruu
Content-Length: 0
Now from this package I need to extract the following:
- The value after "From:" (in this case
<sip:alice@open-ims.test>) - The value after "Contact:" (in this case
<sip:alice@192.168.1.64) - The value after "username" (in this case
alice@open-ims.test)
My code so far
char * tch;
char * saved;
tch = strtok (payload,"<>;");
while (tch != NULL)
{
int savenext = 0;
if (!strcmp(tch, "From: "))
{
savenext = 1;
}
tch = strtok (NULL, "<>;");
if (savenext == 1)
{
saved = tch;
}
}
printf ("### SIP Contact: %s ###\n", saved);
}
}
Where the payload contains the packet as described above.
, , . , strtok "< > ;:", strcmp "sip", , . .
sip ?