I am trying to parse SIP headers into a line. I do this by iterating over a string line by line. Each header should be separated by a new line character.
The input string will look something similar to this:
INVITE sip:user2@server2.com SIP/2.0
Via: SIP/2.0/UDP pc33.server1.com;branch=z9hG4bK776asdhds Max-Forwards: 70
To: user2 <sip:user2@server2.com>
From: user1 <sip:user1@server1.com>;tag=1928301774
Call-ID: a84b4c76e66710@pc33.server1.com
CSeq: 314159 INVITE
Contact: <sip:user1@pc33.server1.com>
Content-Type: application/sdp
Content-Length: 142
My code:
void readlines(char *str){
int i;
int reset;
char current_line[500];
char tmp = 0;
for( i=0; i<strlen(str); i++ ){
tmp = str[i];
if (tmp == '\n'){
strncpy(current_line, str+tmp, i);
strcat(current_line, '\0');
printf("current line = %s", current_line);
}
}
}
In my code you can see an if block. In the if block I print out the current line as a cheap way to test my solution, the result of this print statement is nothing. Maybe my understanding on how c interprets the \n
character is not complete.