Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 26 Apr 2024 21:41:09 -0600
From:      Warner Losh <imp@bsdimp.com>
To:        Bakul Shah <bakul@iitbombay.org>
Cc:        Mike Karels <mike@karels.net>, FreeBSD Net <freebsd-net@freebsd.org>
Subject:   Re: Question about netinet6/in6.h
Message-ID:  <CANCZdfqEf-TniYhp0Cyv_DnFeKcHKvxeRBotyLYgzN0Jcw5BcQ@mail.gmail.com>
In-Reply-To: <54E63C68-2713-4247-A57C-D3AA9C571327@iitbombay.org>
References:  <CANCZdfrDTktpyW9Ad=3-K9qnVYmY_wCnrmyizvgwJktVfHfV3Q@mail.gmail.com> <229EB3F8-FB68-461C-BF1F-3B2846510EBA@karels.net> <AA706B2F-1C77-47B7-915E-6574E1F3654C@karels.net> <CANCZdfrtxsGKKn3bzaWRDhYphYb0DuZ7VTOWeTbR_8X980u_1A@mail.gmail.com> <4AF50212-9141-44FF-937F-A06AF8B15121@karels.net> <54E63C68-2713-4247-A57C-D3AA9C571327@iitbombay.org>

next in thread | previous in thread | raw e-mail | index | archive | help
--00000000000033267b06170bcb31
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, Apr 26, 2024, 9:33=E2=80=AFPM Bakul Shah <bakul@iitbombay.org> wrot=
e:

>
>
> > On Apr 26, 2024, at 5:02=E2=80=AFPM, Mike Karels <mike@karels.net> wrot=
e:
> >
> > On 26 Apr 2024, at 18:06, Warner Losh wrote:
> >
> >> On Fri, Apr 26, 2024 at 4:21=E2=80=AFPM Mike Karels <mike@karels.net> =
wrote:
> >>
> >>> On 26 Apr 2024, at 15:49, Mike Karels wrote:
> >>>
> >>>> On 26 Apr 2024, at 15:01, Warner Losh wrote:
> >>>>
> >>>>> This has to be a FAQ
> >>>>>
> >>>>> I'm porting a program from Linux, I often see an error like:
> >>>>> ./test/mock-ifaddrs.c:95:19: error: no member named 's6_addr32' in
> >>> 'struct
> >>>>> in6_addr'
> >>>>>   95 |                 ipv6->sin6_addr.s6_addr32[3] =3D 0;
> >>>>>      |                 ~~~~~~~~~~~~~~~ ^
> >>>>> but yet, we kinda define them, but only for the kernel and boot
> loader:
> >>>>> /*
> >>>>> * IPv6 address
> >>>>> */
> >>>>> struct in6_addr {
> >>>>>        union {
> >>>>>                uint8_t         __u6_addr8[16];
> >>>>>                uint16_t        __u6_addr16[8];
> >>>>>                uint32_t        __u6_addr32[4];
> >>>>>        } __u6_addr;                    /* 128-bit IP6 address */
> >>>>> };
> >>>>>
> >>>>> #define s6_addr   __u6_addr.__u6_addr8
> >>>>> #if defined(_KERNEL) || defined(_STANDALONE) /* XXX nonstandard */
> >>>>> #define s6_addr8  __u6_addr.__u6_addr8
> >>>>> #define s6_addr16 __u6_addr.__u6_addr16
> >>>>> #define s6_addr32 __u6_addr.__u6_addr32
> >>>>> #endif
> >>>>>
> >>>>> I'm wondering if anybody why it's like that? git blame suggests we
> >>> imported
> >>>>> that from kame, with
> >>>>> only tweaks by people that are now deceased*.*
> >>>>>
> >>>>> Why not just expose them?
> >>>>
> >>>> Looks like only s6_addr is specified in the RFCs (2553 and 3493).
> Oddly,
> >>>> though, the RFCs give an example implementation using that union wit=
h
> >>>> different element names (like _S6_u8), and show the one #define.
> >>>> Similarly, POSIX specifies only s6_addr, but it allows other members
> >>>> of the structure, so I don't see a problem with exposing them all ev=
en
> >>>> in a POSIX environment.
> >>>>
> >>>> I would have no objection to exposing all four definitions, especial=
ly
> >>>> if Linux apps use them.
> >>>
> >>> I put the change, along with an explanatory comment, in
> >>> https://reviews.freebsd.org/D44979.  Comments welcome.
> >>>
> >>
> >> Thanks! I was testing a similar change, but I like yours better...
> though
> >> maybe
> >> we should just make it visible when __BSD_VISIBLE is true.... I'll hav=
e
> to
> >> look
> >> closely at what Linux does here... I think they have it always visible=
,
> or
> >> at least
> >> musl does that (glibc is harder to track down due to the many layers o=
f
> >> indirection).
> >
> > I thought briefly about __BSD_VISIBLE, but wasn't sure it was necessary=
.
> > Let me know what you find out.  I think it should work either way; in.h
> > includes cdefs.h, so it's guaranteed to have been included.
>
> If the -ms-extensions option is used with gcc or clang, this ugliness can
> go away as you can have nested anonymous unions or -structs and their
> fields
> can be referenced as if they're directly in the parent struct/union.
>
> [IIRC this was present in Plan9 C from very early on. Also in C11 or late=
r]


True. In fact c11 and newer doesn't need anything on the command line here.
If it were only in the kernel then I'd chamge it like thay while I was
here... but lots of code in ports will specify c99 + POSIX 2001 and to
compile there your only hope is this construct....

Warner

--00000000000033267b06170bcb31
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div><br><br><div class=3D"gmail_quote"><div dir=3D"ltr" =
class=3D"gmail_attr">On Fri, Apr 26, 2024, 9:33=E2=80=AFPM Bakul Shah &lt;<=
a href=3D"mailto:bakul@iitbombay.org">bakul@iitbombay.org</a>&gt; wrote:<br=
></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-=
left:1px #ccc solid;padding-left:1ex"><br>
<br>
&gt; On Apr 26, 2024, at 5:02=E2=80=AFPM, Mike Karels &lt;<a href=3D"mailto=
:mike@karels.net" target=3D"_blank" rel=3D"noreferrer">mike@karels.net</a>&=
gt; wrote:<br>
&gt; <br>
&gt; On 26 Apr 2024, at 18:06, Warner Losh wrote:<br>
&gt; <br>
&gt;&gt; On Fri, Apr 26, 2024 at 4:21=E2=80=AFPM Mike Karels &lt;<a href=3D=
"mailto:mike@karels.net" target=3D"_blank" rel=3D"noreferrer">mike@karels.n=
et</a>&gt; wrote:<br>
&gt;&gt; <br>
&gt;&gt;&gt; On 26 Apr 2024, at 15:49, Mike Karels wrote:<br>
&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt; On 26 Apr 2024, at 15:01, Warner Losh wrote:<br>
&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt;&gt; This has to be a FAQ<br>
&gt;&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt;&gt; I&#39;m porting a program from Linux, I often see an e=
rror like:<br>
&gt;&gt;&gt;&gt;&gt; ./test/mock-ifaddrs.c:95:19: error: no member named &#=
39;s6_addr32&#39; in<br>
&gt;&gt;&gt; &#39;struct<br>
&gt;&gt;&gt;&gt;&gt; in6_addr&#39;<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A095 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0ipv6-&gt;sin6_addr.s6_addr32[3] =3D 0;<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0~~~~~~~~~~~~~~~ ^<br>
&gt;&gt;&gt;&gt;&gt; but yet, we kinda define them, but only for the kernel=
 and boot loader:<br>
&gt;&gt;&gt;&gt;&gt; /*<br>
&gt;&gt;&gt;&gt;&gt; * IPv6 address<br>
&gt;&gt;&gt;&gt;&gt; */<br>
&gt;&gt;&gt;&gt;&gt; struct in6_addr {<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 union {<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 uint8_t=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0__u6_addr8[16];<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 uint16_t=C2=A0 =C2=A0 =C2=A0 =C2=A0 __u6_addr16[8];<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 uint32_t=C2=A0 =C2=A0 =C2=A0 =C2=A0 __u6_addr32[4];<br>
&gt;&gt;&gt;&gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 } __u6_addr;=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* 128-bit IP6 addr=
ess */<br>
&gt;&gt;&gt;&gt;&gt; };<br>
&gt;&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt;&gt; #define s6_addr=C2=A0 =C2=A0__u6_addr.__u6_addr8<br>
&gt;&gt;&gt;&gt;&gt; #if defined(_KERNEL) || defined(_STANDALONE) /* XXX no=
nstandard */<br>
&gt;&gt;&gt;&gt;&gt; #define s6_addr8=C2=A0 __u6_addr.__u6_addr8<br>
&gt;&gt;&gt;&gt;&gt; #define s6_addr16 __u6_addr.__u6_addr16<br>
&gt;&gt;&gt;&gt;&gt; #define s6_addr32 __u6_addr.__u6_addr32<br>
&gt;&gt;&gt;&gt;&gt; #endif<br>
&gt;&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt;&gt; I&#39;m wondering if anybody why it&#39;s like that? g=
it blame suggests we<br>
&gt;&gt;&gt; imported<br>
&gt;&gt;&gt;&gt;&gt; that from kame, with<br>
&gt;&gt;&gt;&gt;&gt; only tweaks by people that are now deceased*.*<br>
&gt;&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt;&gt; Why not just expose them?<br>
&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt; Looks like only s6_addr is specified in the RFCs (2553 and=
 3493).=C2=A0 Oddly,<br>
&gt;&gt;&gt;&gt; though, the RFCs give an example implementation using that=
 union with<br>
&gt;&gt;&gt;&gt; different element names (like _S6_u8), and show the one #d=
efine.<br>
&gt;&gt;&gt;&gt; Similarly, POSIX specifies only s6_addr, but it allows oth=
er members<br>
&gt;&gt;&gt;&gt; of the structure, so I don&#39;t see a problem with exposi=
ng them all even<br>
&gt;&gt;&gt;&gt; in a POSIX environment.<br>
&gt;&gt;&gt;&gt; <br>
&gt;&gt;&gt;&gt; I would have no objection to exposing all four definitions=
, especially<br>
&gt;&gt;&gt;&gt; if Linux apps use them.<br>
&gt;&gt;&gt; <br>
&gt;&gt;&gt; I put the change, along with an explanatory comment, in<br>
&gt;&gt;&gt; <a href=3D"https://reviews.freebsd.org/D44979" rel=3D"noreferr=
er noreferrer" target=3D"_blank">https://reviews.freebsd.org/D44979</a>.=C2=
=A0 Comments welcome.<br>
&gt;&gt;&gt; <br>
&gt;&gt; <br>
&gt;&gt; Thanks! I was testing a similar change, but I like yours better...=
 though<br>
&gt;&gt; maybe<br>
&gt;&gt; we should just make it visible when __BSD_VISIBLE is true.... I&#3=
9;ll have to<br>
&gt;&gt; look<br>
&gt;&gt; closely at what Linux does here... I think they have it always vis=
ible, or<br>
&gt;&gt; at least<br>
&gt;&gt; musl does that (glibc is harder to track down due to the many laye=
rs of<br>
&gt;&gt; indirection).<br>
&gt; <br>
&gt; I thought briefly about __BSD_VISIBLE, but wasn&#39;t sure it was nece=
ssary.<br>
&gt; Let me know what you find out.=C2=A0 I think it should work either way=
; in.h<br>
&gt; includes cdefs.h, so it&#39;s guaranteed to have been included.<br>
<br>
If the -ms-extensions option is used with gcc or clang, this ugliness can<b=
r>
go away as you can have nested anonymous unions or -structs and their field=
s<br>
can be referenced as if they&#39;re directly in the parent struct/union.<br=
>
<br>
[IIRC this was present in Plan9 C from very early on. Also in C11 or later]=
</blockquote></div></div><div dir=3D"auto"><br></div><div dir=3D"auto">True=
. In fact c11 and newer doesn&#39;t need anything on the command line here.=
 If it were only in the kernel then I&#39;d chamge it like thay while I was=
 here... but lots of code in ports will specify c99 + POSIX 2001 and to com=
pile there your only hope is this construct....</div><div dir=3D"auto"><br>=
</div><div dir=3D"auto">Warner=C2=A0</div><div dir=3D"auto"></div></div>

--00000000000033267b06170bcb31--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfqEf-TniYhp0Cyv_DnFeKcHKvxeRBotyLYgzN0Jcw5BcQ>