Wireshark-dev: Re: [Wireshark-dev] ip6_to_str equivalent/alternative with variablelength for ip
From: ivan jr sy <ivan_jr@xxxxxxxxx>
Date: Tue, 11 Aug 2009 07:59:21 -0700 (PDT)
Thank you for your tip.

I've read it too late thou. 

submitted:
https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=3870

Thanks!

--- On Wed, 8/12/09, Anders Broman <a.broman@xxxxxxxxx> wrote:

> From: Anders Broman <a.broman@xxxxxxxxx>
> Subject: Re: [Wireshark-dev] ip6_to_str equivalent/alternative with variablelength for ipv6 address
> To: "'Developer support list for Wireshark'" <wireshark-dev@xxxxxxxxxxxxx>
> Date: Wednesday, August 12, 2009, 1:22 AM
> Hi,
> Wouldn't it be more efficient to first add the family item
> to the tree  and
> then depending of the address type add
> The IP address item.
> 
> Oct = tvb_get_guint8(tvb,offset);
> proto_tree_add_item(..,hf_x_afamily,..)
> switch(oct){
>  case IPV4:
>    proto_tree_add_item(..,hf_x_ipv4_addr,..)
>   
> case IPV6:
>    proto_tree_add_item(..,hf_x_ipv6_addr,..)
> default:
>    proto_tree_add_text(..,"Unknown",..);
> }
> /Anders
> -----Ursprungligt meddelande-----
> Från: wireshark-dev-bounces@xxxxxxxxxxxxx
> [mailto:wireshark-dev-bounces@xxxxxxxxxxxxx]
> För ivan jr sy
> Skickat: den 11 augusti 2009 15:03
> Till: Developer support list for Wireshark
> Ämne: Re: [Wireshark-dev] ip6_to_str
> equivalent/alternative with
> variablelength for ipv6 address
> 
> Thanks for the tip.
> 
> I was able to make it work, and did not notice any
> problems. But along the
> way, I encountered some memory errors during exit of
> wireshark.
> 
> please recommend if I get the thing right?
> My UI tests shows its already okay, im not sure if this is
> efficient.
> 
> Thanks,
> 
> --------
> 
> addr = tvb_get_ptr(tvb, cur_offset, afdpart_len);
> 
> addr_copy = se_alloc0((afamily == 0x02) ? 16 : 4);
> 
> tvb_memcpy(tvb, (guint8 *)addr_copy, cur_offset,
> afdpart_len);
> 
> proto_tree_add_text(rr_tree, tvb, cur_offset, afdpart_len,
> " %s address:
> %s", val_to_str(afamily, apl_afamily_vals, "Unknown"), 
>  (afamily == 0x02) ? ip6_to_str((const struct e_in6_addr
> *)addr_copy)
>            :
> ip_to_str(addr_copy) );
> 
> 
> 
> --- On Tue, 8/11/09, Sake Blok <sake@xxxxxxxxxx>
> wrote:
> 
> > From: Sake Blok <sake@xxxxxxxxxx>
> > Subject: Re: [Wireshark-dev] ip6_to_str
> equivalent/alternative with
> variable length for ipv6 address
> > To: "Developer support list for Wireshark" <wireshark-dev@xxxxxxxxxxxxx>
> > Date: Tuesday, August 11, 2009, 6:44 PM
> > 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
> >
> ___________________________________________________________________________
> > Sent via:    Wireshark-dev mailing list <wireshark-dev@xxxxxxxxxxxxx>
> > Archives:    http://www.wireshark.org/lists/wireshark-dev
> > Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
> >          
> >    mailto:wireshark-dev-request@xxxxxxxxxxxxx?subject=unsubscribe
> > 
> 
> 
>       
> ___________________________________________________________________________
> Sent via:    Wireshark-dev mailing list <wireshark-dev@xxxxxxxxxxxxx>
> Archives:    http://www.wireshark.org/lists/wireshark-dev
> Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
>          
>    mailto:wireshark-dev-request@xxxxxxxxxxxxx?subject=unsubscribe
> 
> ___________________________________________________________________________
> Sent via:    Wireshark-dev mailing list <wireshark-dev@xxxxxxxxxxxxx>
> Archives:    http://www.wireshark.org/lists/wireshark-dev
> Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
>          
>    mailto:wireshark-dev-request@xxxxxxxxxxxxx?subject=unsubscribe
>