Wireshark-dev: Re: [Wireshark-dev] ip6_to_str equivalent/alternative with variable length for i
From: Sake Blok <[email protected]>
Date: Tue, 11 Aug 2009 08:44:55 +0200
On Mon, Aug 10, 2009 at 07:03:09PM -0700, ivan jr sy wrote:
> Hi all:
> 
> I need some advise on getting an IPv6 address to str with variable number of bytes.
> 
> Example:
> 
> /* the hex part is */
> 20 01 db 08 ff ff 00 02 20 86 20 03 de ad be ef
> 
> addr_len = 6;	/* variable */
> 
> /* if I do this: */
> addr = tvb_get_ptr(tvb, cur_offset, addr_len);

This just gives you a ptr (addr) to the start of some data portion but
it ensures that at least addr_len bytes are present.

>  .... ip6_to_str((const struct e_in6_addr *)addr);
> 
> this will result to:
> 2001:db8:ffff:2:2086:2003:dead:beef

Which is as it is designed.

> but what i wanted was:
> /* the hex part is */
> 20 01 db 08 ff ff
> 
> so it will result to "2001:db8:ffff" (the network portion) coz 
> the "00 02 20 86 20 03 de ad be ef" is for a different purpose.. 
> then I also need to append "::" to the last part of the str to 
> make it "2001:db8:ffff::"

If *that* is what you want, you need to copy the first addr_len bytes
into a new variable, add 16-addr_len zeroes to that variable and then
call ip6_to_str on the new variable.

The copying can be done with:

/** Returns target for convenience. Does not suffer from possible
 * expense of tvb_get_ptr(), since this routine is smart enough
 * to copy data in chunks if the request range actually exists in
 * different TVBUFF_REAL_DATA tvbuffs. This function assumes that the
 * target memory is already allocated; it does not allocate or free the
 * target memory. */
extern void* tvb_memcpy(tvbuff_t*, void* target, gint offset, size_t
length);

(see epan/tvbuff.h)

Hope this helps,
Cheers,
    Sake