public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rich Felker <dalias@libc.org>
To: Alejandro Colomar <alx.manpages@gmail.com>
Cc: "Xi Ruoyao" <xry111@xry111.site>,
	linux-man@vger.kernel.org, "Alejandro Colomar" <alx@kernel.org>,
	GCC <gcc@gcc.gnu.org>, glibc <libc-alpha@sourceware.org>,
	"Bastien Roucariès" <rouca@debian.org>,
	"Stefan Puiu" <stefan.puiu@gmail.com>,
	"Igor Sysoev" <igor@sysoev.ru>,
	"Andrew Clayton" <a.clayton@nginx.com>,
	"Richard Biener" <richard.guenther@gmail.com>,
	"Zack Weinberg" <zack@owlfolio.org>,
	"Florian Weimer" <fweimer@redhat.com>,
	"Joseph Myers" <joseph@codesourcery.com>,
	"Jakub Jelinek" <jakub@redhat.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Michael Kerrisk" <mtk.manpages@gmail.com>
Subject: Re: [PATCH] sockaddr.3type: BUGS: Document that libc should be fixed using a union
Date: Mon, 6 Feb 2023 12:48:49 -0500	[thread overview]
Message-ID: <20230206174849.GJ3298@brightrain.aerifal.cx> (raw)
In-Reply-To: <9998a4eb-528b-b006-ebeb-d94816e62d82@gmail.com>

On Mon, Feb 06, 2023 at 03:11:10PM +0100, Alejandro Colomar wrote:
> Hi Rich,
> 
> On 2/6/23 14:38, Rich Felker wrote:
> >There is absolutely not any need to declare the union for application
> >code calling the socket APIs. You declare whatever type you will be
> >using. For binding or connecting a unix socket, sockaddr_un. For IPv6,
> >sockaddr_in6. Etc. Then you cast the pointer to struct sockaddr * and
> >pass it to the function.
> 
> Except that you may be using generic code that may use either of
> AF_UNIX, AF_INET, and AF_INET6.  A web server may very well use all
> the three.

If you have generic code, the generic code is not what's creating the
object. It got the object from the calling code or from some callback
that allocated it, in which case it doesn't have to care about any of
this.

> >But normally you don't use declared-type objects for this anyway. You
> >use the struct sockaddr * obtained from getaddrinfo as an abstract
> >pointer and never dereference it at all.
> 
> That's right.  Most of the time, we should be using getaddrinfo(3),
> which already provides the storage.  I don't know for sure if there
> are any cases that can't be rewritten to work that way.
> 
> However, there are some APIs that require you to allocate an object.
> For example recvfrom(2).  How would you recommend using recvfrom(2),
> or is it some API to avoid?  Example of usage:
> <Mhttps://man7.org/tlpi/code/online/dist/sockets/id_echo_sv.c.html>.

If using allocated storage, there's nothing special you have to do.
But if you want to avoid malloc and use a declared object, you have a
few options:

If it's your socket and you know what address family it's associated
with, you just pass an object of that type. recvfrom will always
produce output of the same AF.

If you're in a situation like having been invoked from inetd, you can
use getsockname into a sockaddr_storage object or union or whatever,
then read out the family field (with memcpy if needed). Then you know
the AF to use. Or, you can just recvfrom into a sockaddr_storage
object, determine the family at that time, then memcpy the the
appropriate object type. Alternatively, for the common case where you
want a printable name for the address, you just pass it to getnameinfo
as-is and let getnameinfo deal with how to read it.

Rich

  parent reply	other threads:[~2023-02-06 17:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-05 15:28 Alejandro Colomar
2023-02-05 15:31 ` Alejandro Colomar
2023-02-06  6:02   ` Xi Ruoyao
2023-02-06 11:20     ` Rich Felker
2023-02-06 11:55     ` Alejandro Colomar
2023-02-06 13:38       ` Rich Felker
2023-02-06 14:11         ` Alejandro Colomar
2023-02-06 17:21           ` Zack Weinberg
2023-02-06 17:48           ` Rich Felker [this message]
2023-02-05 23:43 ` Rich Felker
2023-02-05 23:59   ` Alejandro Colomar
2023-02-06  0:15     ` Rich Felker
2023-02-06 18:45 ` Eric Blake
2023-02-07  1:21   ` Alejandro Colomar
2023-03-18  7:54   ` roucaries bastien
2023-03-20 10:49     ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230206174849.GJ3298@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=a.clayton@nginx.com \
    --cc=alx.manpages@gmail.com \
    --cc=alx@kernel.org \
    --cc=eblake@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=igor@sysoev.ru \
    --cc=jakub@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=richard.guenther@gmail.com \
    --cc=rouca@debian.org \
    --cc=stefan.puiu@gmail.com \
    --cc=xry111@xry111.site \
    --cc=zack@owlfolio.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).