Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse SIP packet in C

Tags:

c

parsing

sip

I am trying to parse a SIP packet and get some information out of it. To be more specific, the packet looks like this

REGISTER sip:open-ims.test SIP/2.0
Via: SIP/2.0/UDP 192.168.1.64:5060;rport;branch=z9hG4bK1489975971
From: <sip:[email protected]>;tag=1627897650
To: <sip:[email protected]>
Call-ID: 1097412971
CSeq: 1 REGISTER
Contact: <sip:[email protected]:5060;line=5fc3b39f127158d>;+sip.instance="<urn:uuid:46f525fe-3f60-11e0-bec1-d965d1488cfa>"
Authorization: Digest username="[email protected]", 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 that packet I need to extract the following :

My code so far is this

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 payload contains the packet as described above.

However, when I run my program, it will result in a segmentation fault. The weird thing is that if I use in strtok the characters "<>;: " and in strcmp the value "sip" the message will parse successfully and it will keep the saved value. But I need to parse all three of the upper values.

Would a sip library help me more with my problem ?

Thanks in advance

like image 923
kamperone Avatar asked Feb 23 '11 15:02

kamperone


1 Answers

I think something like this could work

char * tch;
        char * saved;                    
        tch = strtok (payload,"<>;\n\"");
        while (tch != NULL)
        { 
            int savenext = 0;              
            if (strncmp(tch, "From",4)==0)   
            {                                 
            tch = strtok (NULL, "<>;\n\"");
            saved = tch;                 
            printf ("   SIP From: %s \n", saved); 
            }   
            else if (strncmp(tch, "Contact",7)==0) 
            {                                 
            tch = strtok (NULL, "<>;\n\"");
            saved = tch;                 
            printf ("   SIP Cont: %s \n", saved); 
            } 
            if (strncmp(tch, "Authorization",13)==0)  
            {                                     
            tch = strtok (NULL, "<>;\n\"");
            saved = tch;                 
            printf ("   SIP User: %s \n", saved); 
like image 167
tzoukos Avatar answered Sep 23 '22 22:09

tzoukos