public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Zack Weinberg" <zack@owlfolio.org>
To: "GNU libc development" <libc-alpha@sourceware.org>
Subject: Re: struct sockaddr_storage
Date: Thu, 19 Jan 2023 10:40:40 -0500	[thread overview]
Message-ID: <cc9f1304-5eaa-4c25-b1f5-4148c035d602@app.fastmail.com> (raw)
In-Reply-To: <ab492040-2058-bcbe-c920-a9088a20f071@gmail.com>

On Thu, Jan 19, 2023, at 9:11 AM, Alejandro Colomar via Libc-alpha wrote:
> I just received a report about struct sockaddr_storage in the man pages.  It 
> reminded me of some concern I've always had about it: it doesn't seem to be a 
> usable type.

Yeah, struct sockaddr_storage has only one genuine use case in my experience: to
know how big to make the buffer for a call to `getpeername` (or, equivalently,
`accept` with non-null second argument) *when you know that the only address
families you care about are IPv4 and IPv6*. And you can do that just as well with

union {
  struct sockaddr sa;
  struct sockaddr_in sa_in;
  struct sockaddr_in6 sa_in6;
} addrbuf;

which also means fewer aliasing issues.

I'd suggest that the manpages should say:

0. It is almost never the Right Thing to declare a variable of type `struct sockaddr` or `struct sockaddr_storage`.
1. To the maximum extent possible, rely on `getaddrinfo` and `getnameinfo` to deal with socket addresses for you.  Treat them as opaque.
2. When you need to work with a specific socket address type that `getaddrinfo` and `getnameinfo` don't handle, declare a variable of that specific `struct sockaddr_XX` type, and cast its address to `struct sockaddr *` _only_ at the point of call to a function that requires an argument of that type.  For example, AF_LOCAL / struct sockaddr_un usually has to be handled this way.
3. To call `getpeername`, or `accept` with non-null second argument, use a union containing each of the concrete socket address structs for the address families you care about (normally at least _in and _in6).  Also include `struct sockaddr` so you can write `&addrbuf.sa` instead of `(struct sockaddr *)&addrbuf`.

However, like with the string functions, I would not use the word "deprecated".

zw

p.s. see https://stackoverflow.com/a/42190913 for my thoughts on the socket address API in way more detail; feel free to crib from there for the manpages

  reply	other threads:[~2023-01-19 15:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-19 14:11 Alejandro Colomar
2023-01-19 15:40 ` Zack Weinberg [this message]
2023-01-19 17:37   ` Alejandro Colomar
2023-01-20 10:06 ` Stefan Puiu
2023-01-20 12:39   ` Alejandro Colomar
2023-01-23  7:40     ` Stefan Puiu
2023-01-23 16:03       ` Alejandro Colomar
2023-01-23 16:28         ` Richard Biener
2023-01-24 16:38           ` Alex Colomar
2023-01-23 16:37         ` Jakub Jelinek
2023-01-24 16:40           ` Alex Colomar
2023-01-24 18:00           ` Alex Colomar
2023-01-24 11:16   ` Rich Felker
2023-01-24 16:53     ` Alex 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=cc9f1304-5eaa-4c25-b1f5-4148c035d602@app.fastmail.com \
    --to=zack@owlfolio.org \
    --cc=libc-alpha@sourceware.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).