converting from unsigned char array to IPV6 string

S

sam.barker0

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
 
J

Jim Langston

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.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top