sscanf question

Discussion in 'C Programming' started by sieg1974@yahoo.com, Nov 22, 2006.

  1. Guest

    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:102@192.168.1.113>;tag=82888468-ee78-db11-9cdf-00112f2f2fed
    To: <sip:102@192.168.1.113>;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:102@192.168.1.116:5069;transport=udp>;expires=3600
    Date: Wed, 22 Nov 2006 23:23:08 GMT
    Content-Length: 0
     
    , Nov 22, 2006
    #1
    1. Advertising

  2. Guest

    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:102@192.168.1.113>;tag=82888468-ee78-db11-9cdf-00112f2f2fed
    To: <sip:102@192.168.1.113>;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:102@192.168.1.116:5069;transport=udp>;expires=3600
    Date: Wed, 22 Nov 2006 23:23:08 GMT
    Content-Length: 0



    wrote:
    > 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:102@192.168.1.113>;tag=82888468-ee78-db11-9cdf-00112f2f2fed
    > To: <sip:102@192.168.1.113>;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:102@192.168.1.116:5069;transport=udp>;expires=3600
    > Date: Wed, 22 Nov 2006 23:23:08 GMT
    > Content-Length: 0
     
    , Nov 22, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > 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.

    --
    Hope this helps,
    Steven
     
    , Nov 23, 2006
    #3
  4. Richard Bos Guest

    wrote:

    > 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
     
    Richard Bos, Nov 23, 2006
    #4
  5. Guest

    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:102@192.168.1.113>;tag=fa375d6f-8279-db11-9cdf-00112f2f2fed\r\nTo:
    <sip:102@192.168.1.113>\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:102@64.180.111.244\r\nContent-Length: 0\r\n";

    char buf2[] = "z9hG4bK3c36655c;rport\r\nFrom: \"asterisk\"
    <sip:asterisk@64.180.111.244>;tag=as41da1dfe\r\nTo:
    <sip:102@192.168.1.116:5089;transport=udp>\r\nContact:
    <sip:asterisk@64.180.111.244>\r\nCall-ID:
    3d1983143939d5141ccd181f2db26e19@64.180.111.244\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
     
    , Nov 23, 2006
    #5
  6. Guest

    wrote:
    > 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:102@192.168.1.113>;tag=fa375d6f-8279-db11-9cdf-00112f2f2fed\r\nTo:
    > <sip:102@192.168.1.113>\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:102@64.180.111.244\r\nContent-Length: 0\r\n";
    >
    > char buf2[] = "z9hG4bK3c36655c;rport\r\nFrom: \"asterisk\"
    > <sip:asterisk@64.180.111.244>;tag=as41da1dfe\r\nTo:
    > <sip:102@192.168.1.116:5089;transport=udp>\r\nContact:
    > <sip:asterisk@64.180.111.244>\r\nCall-ID:
    > 3d1983143939d5141ccd181f2db26e19@64.180.111.244\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;
    }


    --
    Hope this helps,
    Steven
     
    , Nov 23, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Smith

    Question on sscanf

    Dan Smith, Aug 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    414
    Martin Ambuhl
    Aug 14, 2003
  2. Brent Lievers

    sscanf fixed-width integer question

    Brent Lievers, Apr 22, 2004, in forum: C Programming
    Replies:
    8
    Views:
    649
    Dan Pop
    Apr 26, 2004
  3. sscanf() question?

    , Jun 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    522
  4. broeisi

    Programmer wannabee question about sscanf

    broeisi, Mar 4, 2006, in forum: C Programming
    Replies:
    10
    Views:
    538
    CBFalconer
    Mar 5, 2006
  5. alij

    sscanf Question

    alij, Jun 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    347
Loading...

Share This Page