Parse SIP package in C

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 ?

+5
6

, -

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); 
+4

, Rup, , , , .

GNU oSIP .

-:

   SIP parser:
   ==========

, osip - SIP. : SIP .

:

1 / SIP
 2 SIP uri
 3
 4 Via
 5 CSeq
 6 Call-ID
 7 To, From, Route, Record-Route...
 8 , ,
 9 ,
 10
 11...
 12
 13 ( mime)
 14 SDP

+3

('From:', 'Contact:', 'username') strstr().

, , strtok() , .

, SIP- , , .

+1

, . SIP ​​ , ABNF ABCF RFC 3261. , - , RFC 4475, SIP-, , , , , .

+1

strtok "< > ;", , ( )

REGISTER sip:open-ims.test SIP/2.0Via: SIP/2.0/UDP 192.168.1.64:5060

rport

branch=z9hG4bK1489975971From:

sip:alice@open-ims.test 

 if (!strcmp(tch, "From: "))     

, , strtok "From:".

0

strtokno need to use the same set of delimiters every time. You can use a colon if you expect a field label, and leave it waiting for the right side.

0
source

All Articles