sscanf question

S

sieg1974

Hi,

This piece of code returns ip = -1062731404 (192.168.1.116) and port =
5069, when the following buffer is used. Can someone explain me how
"%x.%x" is matching "192.168.1.116:5069"?

int ip,port;
sscanf(buf+tagStart+strlen("branch="),"%x.%x",&ip,&port);

Thanks,

Andre

z9hG4bK8a948468-ee78-db11-9cdf-00112f2f2fed;rport
From: <sip:[email protected]>;tag=82888468-ee78-db11-9cdf-00112f2f2fed
To: <sip:[email protected]>;tag=as3a286274
Call-ID: ecdb2bb4-de78-db11-9cdf-00112f2f2fed@dilbert
CSeq: 244 REGISTER
User-Agent: Asterisk PBX
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY
Expires: 3600
Contact: <sip:[email protected]:5069;transport=udp>;expires=3600
Date: Wed, 22 Nov 2006 23:23:08 GMT
Content-Length: 0
 
S

sieg1974

Hi,

Actually, the right buffer is the following one.

Andre

c0a80174.13cd;received=207.81.158.209
Via: SIP/2.0/UDP
192.168.1.116:5069;branch=z9hG4bK8a948468-ee78-db11-9cdf-00112f2f2fed;rport
From: <sip:[email protected]>;tag=82888468-ee78-db11-9cdf-00112f2f2fed
To: <sip:[email protected]>;tag=as3a286274
Call-ID: ecdb2bb4-de78-db11-9cdf-00112f2f2fed@dilbert
CSeq: 244 REGISTER
User-Agent: Asterisk PBX
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY
Expires: 3600
Contact: <sip:[email protected]:5069;transport=udp>;expires=3600
Date: Wed, 22 Nov 2006 23:23:08 GMT
Content-Length: 0
 
A

attn.steven.kuo

Hi,

This piece of code returns ip = -1062731404 (192.168.1.116) and port =
5069, when the following buffer is used. Can someone explain me how
"%x.%x" is matching "192.168.1.116:5069"?

int ip,port;
sscanf(buf+tagStart+strlen("branch="),"%x.%x",&ip,&port);


The argument corresponding to the "%x" conversion
specifier has to be a pointer to an *unsigned*
integer.

If you continue to have problems after making
the necessary fixes, then please post enough of
your code to allow an accurate diagnosis.
 
R

Richard Bos

This piece of code returns ip = -1062731404 (192.168.1.116) and port =
5069, when the following buffer is used. Can someone explain me how
"%x.%x" is matching "192.168.1.116:5069"?

int ip,port;
sscanf(buf+tagStart+strlen("branch="),"%x.%x",&ip,&port);

Put simply: it isn't. Something is going wrong in your code, because
even when corrected to use unsigned ints, that sscanf() call is never
going to parse those values.

Strip your program to the smallest compilable example that still
exhibits the problematic behaviour, then post that example here. And
copy, don't retype, else you'll introduce typos.

Richard
 
S

sieg1974

Hi,

The following is the piece of code I'm using, and it's output.

Thanks,

Andre


#include <stdio.h>

char buf1[] = "c0a80174.13e1;received=207.81.158.209\r\nVia:
SIP/2.0/UDP\r\n192.168.1.116:5089;branch=z9hG4bK20445d6f-8279-db11-9cdf-00112f2f2fed;rport\r\nFrom:
<sip:[email protected]>;tag=fa375d6f-8279-db11-9cdf-00112f2f2fed\r\nTo:
<sip:[email protected]>\r\nCall-ID:
f6ff5c6f-8279-db11-9cdf-00112f2f2fed@dilbert\r\nCSeq: 2363
REGISTER\r\nUser-Agent: Asterisk PBX\r\nAllow: INVITE, ACK, CANCEL,
OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY\r\nContact:
<sip:[email protected]\r\nContent-Length: 0\r\n";

char buf2[] = "z9hG4bK3c36655c;rport\r\nFrom: \"asterisk\"
<sip:[email protected]>;tag=as41da1dfe\r\nTo:
<sip:[email protected]:5089;transport=udp>\r\nContact:
<sip:[email protected]>\r\nCall-ID:
[email protected]\r\nCSeq: 102
OPTIONS\r\nUser-Agent: Asterisk PBX\r\nMax-Forwards: 70\r\nDate: Thu,
23 Nov 2006 17:02:40 GMT\r\nAllow: INVITE, ACK, CANCEL, OPTIONS, BYE,
REFER, SUBSCRIBE, NOTIFY\r\nContent-Length: 0\r\n";

int main()
{
int ip = 0;
int port = 0;

sscanf(buf1, "%x.%x", &ip, &port);
printf("buf1: ip=%d port=%d\n", ip, port);

ip = 0;
port = 0;

sscanf(buf2, "%x.%x", &ip, &port);
printf("buf2: ip=%d port=%d\n", ip, port);

return 0;
}

Output:

buf1: ip=-1062731404 port=5089
buf2: ip=0 port=0
 
A

attn.steven.kuo

Hi,

The following is the piece of code I'm using, and it's output.

Thanks,

Andre


#include <stdio.h>

char buf1[] = "c0a80174.13e1;received=207.81.158.209\r\nVia:
SIP/2.0/UDP\r\n192.168.1.116:5089;branch=z9hG4bK20445d6f-8279-db11-9cdf-00112f2f2fed;rport\r\nFrom:
<sip:[email protected]>;tag=fa375d6f-8279-db11-9cdf-00112f2f2fed\r\nTo:
<sip:[email protected]>\r\nCall-ID:
f6ff5c6f-8279-db11-9cdf-00112f2f2fed@dilbert\r\nCSeq: 2363
REGISTER\r\nUser-Agent: Asterisk PBX\r\nAllow: INVITE, ACK, CANCEL,
OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY\r\nContact:
<sip:[email protected]\r\nContent-Length: 0\r\n";

char buf2[] = "z9hG4bK3c36655c;rport\r\nFrom: \"asterisk\"
<sip:[email protected]>;tag=as41da1dfe\r\nTo:
<sip:[email protected]:5089;transport=udp>\r\nContact:
<sip:[email protected]>\r\nCall-ID:
[email protected]\r\nCSeq: 102
OPTIONS\r\nUser-Agent: Asterisk PBX\r\nMax-Forwards: 70\r\nDate: Thu,
23 Nov 2006 17:02:40 GMT\r\nAllow: INVITE, ACK, CANCEL, OPTIONS, BYE,
REFER, SUBSCRIBE, NOTIFY\r\nContent-Length: 0\r\n";

int main()
{
int ip = 0;
int port = 0;

sscanf(buf1, "%x.%x", &ip, &port);
printf("buf1: ip=%d port=%d\n", ip, port);

ip = 0;
port = 0;

sscanf(buf2, "%x.%x", &ip, &port);
printf("buf2: ip=%d port=%d\n", ip, port);

return 0;
}

Output:

buf1: ip=-1062731404 port=5089
buf2: ip=0 port=0



You're converting the beginning of buf1:

"c0a80174.13e1"

and not the portion of the string that appears later:

"192.168.1.116:5089"

So:

(1) Use unsigned int for the "%x" conversion specifier,
(2) Use %n to see how far into the input string the
conversion has progressed,
(3) Check the return value from sscanf



int main(void)
{
unsigned int ip = 0;
unsigned int port = 0;
int consumed ;

if (sscanf(buf1, "%x.%x%n", &ip, &port,&consumed) < 2)
{
fprintf(stderr, "sscanf did not convert as expected\n");
}
else
{
printf("buf1: ip=%u port=%u\n", ip, port);
printf("%d characters consumed in conversion\n", consumed);
printf("Remainder of string is %s", buf1+consumed);
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top