Re: struct sockaddr_storage, union (was: Improve getsockname)

Bastien Roucariès
2023-01-20T21:11:37Z

[-- Attachment #1: Type: text/plain, Size: 1672 bytes --]

Le jeudi 19 janvier 2023, 23:31:09 UTC Alejandro Colomar a écrit :
Hi,
> On 1/19/23 22:38, Bastien Roucariès wrote:
>
> However... Considering that most APIs use `struct sockaddr *`, this wouldn't
> allow the internal libc implementation of functions like getnameinfo(3) to be
> free of UB.

libc is safe thanks to the transparent union of pointer.

> Maybe a better thing would be to do the following:
>
>
> struct sockaddr {
> union {
> struct {
> sa_family_t sa_family;
> char sa_data[];
> };
> struct sockaddr_in sin;
> struct sockaddr_in6 sin6;
> struct sockaddr_un sun;
> };
> };
>
> struct sockaddr_storage {
> union {
> sa_family_t ss_family;
> struct sockaddr sa;
> };
> };
>

No I think we could do better with recent C:
struct osockaddr
{
unsigned short int sa_family;
unsigned char sa_data[14];
char _extra[];
};

> This makes sockaddr and sockaddr_storage have the same size, and also both can
> alias any of the children types through the unions, so one can use either of
> them for the same purpose.

We could not due to old talk protocol

Only storage is free
>
> I'll be sending a patch soon for discussion.
>
> Cheers,
>
> Alex
>
> >
> >>
> >>>
> >>> struct sockaddr_storage {
> >>> union {
> >>> sa_family_t ss_family;
> >>> struct sockaddr sa;
> >>> struct sockaddr_in sin;
> >>> struct sockaddr_in6 sin6;
> >>> struct sockaddr_un sun;
> >>> };
> >>> char __reserved_null;
> >>
>
> --
> <http://www.alejandro-colomar.es/>