[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Adding ipv6 support.



On Mon, Feb 07, 2011 at 01:02:42PM -0500, Simon Perreault wrote:
> On 2011-02-07 12:53, Dave Hart wrote:
> > If you only care about IPv4 and IPv6 sockets, consider using a union
> > of the useful types:
> > 
> > typedef struct union_saddr_tag {
> >     struct sockaddr            sa;
> >     struct sockaddr_in       sin;
> >     struct sockaddr_in6     sin6;
> > } union_saddr;
> > 
> > Then use union_saddr as your generic type, distinguishing variants
> > using sa.sa_family.  When calling functions that take a sockaddr *,
> > use &my_saddr.sa and no potentially-bug-hiding cast is required.
> 
> Bad idea. You're substituting an explicit cast with an implicit cast.
> 
> An implicit cast is no less "potentially-bug-hiding". An explicit cast
> at least *looks* like a cast, so that you can be extra careful when you
> see one. When casting, you *want* the code to look ugly, to force you to
> think about what you're doing.

Implicit conversions are usually less bug prone, but I agree that there is
not much difference in this context. I tend to use Mr. Hart's approach,
however.

> Furthermore, you're ignoring alignment issues.

There are no alignment issues here, arguably not even if we're talking pure
standard C. Certainly not in any implementation that supports the BSD
sockets API and the .sa_family member overlay.

> The best current practice is to use sockaddr_storage. See e.g.
> http://www.kame.net/newsletter/19980604/ for more info.

That page is very BSD centric. For example, SysV derivatives like Solaris
and Linux don't have .sa_len.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.