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. Advertisements

  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. Advertisements

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. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    862
    Jack Klein
    May 9, 2005
  2. hamishd
    Replies:
    12
    Views:
    5,305
  3. Ioannis Vranos
    Replies:
    11
    Views:
    1,096
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    843
    Ben Bacarisse
    Mar 29, 2008
  5. Obnoxious User
    Replies:
    5
    Views:
    1,003
    Jim Langston
    Apr 1, 2008
  6. Replies:
    6
    Views:
    1,064
    James Kanze
    Apr 2, 2008
  7. Alex Vinokur
    Replies:
    9
    Views:
    1,114
    James Kanze
    Oct 13, 2008
  8. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

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