converting from unsigned char array to IPV6 string

Discussion in 'C++' started by sam.barker0@gmail.com, Apr 26, 2008.

  1. Guest

    Hi guys,
    I am trying to form an IPV6 address string from the address bytes
    contained in a unsigned char buffer

    char tempstring[35];
    sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
    *)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
    +2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
    short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
    +8))),htons(*((unsigned short *)(buf.GetStart()
    +10))),htons(*((unsigned short *)(buf.GetStart()
    +12))),htons(*((unsigned short *)(buf.GetStart()+14))))

    There is a stack over flow when I do this.Its becase tempstring is
    char instead of unsigned char.
    But sprintf allows only char array.

    How can I solve the problem.Is there a better way to write this?
    Cheers,
    Sam
     
    , Apr 26, 2008
    #1
    1. Advertising

  2. Jim Langston Guest

    wrote:
    > Hi guys,
    > I am trying to form an IPV6 address string from the address bytes
    > contained in a unsigned char buffer
    >
    > char tempstring[35];


    Becuase you're getting stack overflow, 35 is probably not enough characters.
    to test this make the size some rediculously large value, like 100, 1000,
    run the code. look at the output.

    > sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
    > *)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
    > +2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
    > short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
    > +8))),htons(*((unsigned short *)(buf.GetStart()
    > +10))),htons(*((unsigned short *)(buf.GetStart()
    > +12))),htons(*((unsigned short *)(buf.GetStart()+14))))
    >
    > There is a stack over flow when I do this.Its becase tempstring is
    > char instead of unsigned char.
    > But sprintf allows only char array.
    >
    > How can I solve the problem.Is there a better way to write this?


    std::string +
    std::eek:string
    std::iostring

    Anything that has a dynamic buffer that will grow with the size so you don't
    have to guess how big to make it.

    I don't know the format of IP6, but notice you are using unsidned short,
    which is on my system 2 bytes. which goes from 0 to 65535. At their
    largest, 5 * 8 = 40 plus null terminator is 41 characters, which 35 isn't.
    I really don't know though. stack overflow type problems are usually from
    buffer overflows, especially when you are using a buffer like you are here.

    --
    Jim Langston
     
    Jim Langston, Apr 27, 2008
    #2
    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. Obnoxious User
    Replies:
    5
    Views:
    872
    Jim Langston
    Apr 1, 2008
  2. Replies:
    6
    Views:
    867
    James Kanze
    Apr 2, 2008
  3. Alex Vinokur
    Replies:
    9
    Views:
    820
    James Kanze
    Oct 13, 2008
  4. Gabriel Genellina
    Replies:
    0
    Views:
    521
    Gabriel Genellina
    Aug 13, 2009
  5. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

    Jesse van den Kieboom, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    379
    Dibya Prakash
    Jun 5, 2005
Loading...

Share This Page