Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Mar 2025 11:45:55 +0900
From:      Kristof Provost <kp@FreeBSD.org>
To:        Gleb Smirnoff <glebius@FreeBSD.org>
Cc:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   Re: git: 0849f1634a70 - main - tests/netinet: add test for IP_MULTICAST_IF
Message-ID:  <7440BE85-66E8-4D1A-AC15-8B944F5C2951@FreeBSD.org>
In-Reply-To: <202503222340.52MNekCX071219@gitrepo.freebsd.org>
References:  <202503222340.52MNekCX071219@gitrepo.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--=_MailMate_B28CA915-06EB-49CD-961F-2ADDD3DDDB4D_=
Content-Type: text/plain; charset=UTF-8; format=flowed; markup=markdown
Content-Transfer-Encoding: quoted-printable

On 23 Mar 2025, at 8:40, Gleb Smirnoff wrote:
> The branch main has been updated by glebius:
>
> URL: =

> https://cgit.FreeBSD.org/src/commit/?id=3D0849f1634a70099b90256ceece52a=
598eeb3280e
>
> commit 0849f1634a70099b90256ceece52a598eeb3280e
> Author:     Gleb Smirnoff <glebius@FreeBSD.org>
> AuthorDate: 2025-03-22 22:44:20 +0000
> Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
> CommitDate: 2025-03-22 23:39:50 +0000
>
>     tests/netinet: add test for IP_MULTICAST_IF
>
It bugs me a little that we have to jump through hoops to get a C =

program running in a specific jail/network setup.
It=E2=80=99s somewhat common for that to happen, and I=E2=80=99m sure the=
re=E2=80=99d be =

more cases if it were easier to do (e.g. the getaddrinfo tests you=E2=80=99=
ve =

been working on could be even more useful with a custom DNS server, so =

in their own vnet jail).

I took a stab at that:

	diff --git a/tests/sys/netinet/multicast.sh =

b/tests/sys/netinet/multicast.sh
	index eb2b962dac70..7476daeb832a 100644
	--- a/tests/sys/netinet/multicast.sh
	+++ b/tests/sys/netinet/multicast.sh
	@@ -26,6 +26,15 @@

	 . $(atf_get_srcdir)/../common/vnet.subr

	+ctest_j()
	+{
	+       jail=3D$1
	+       shift
	+
	+       cc -x c - -o tmp || atf_fail "Failed to build"
	+       jexec $jail `pwd`/tmp $@ || atf_fail "Test program failed"
	+}
	+
	 # See regression fixed in baad45c9c12028964acd0b58096f3aaa0fb22859
	 atf_test_case "IP_MULTICAST_IF" "cleanup"
	 IP_MULTICAST_IF_head()
	@@ -33,7 +42,6 @@ IP_MULTICAST_IF_head()
	        atf_set descr \
	            'sendto() for IP_MULTICAST_IF socket does not do routing =

lookup'
	        atf_set require.user root
	-
	 }

	 IP_MULTICAST_IF_body()
	@@ -46,8 +54,45 @@ IP_MULTICAST_IF_body()
	        vnet_mkjail mjail ${epair}a
	        jexec mjail ifconfig ${epair}a up
	        jexec mjail ifconfig ${epair}a 192.0.2.1/24
	-       atf_check -s exit:0 -o empty \
	-           jexec mjail $(atf_get_srcdir)/sendto-IP_MULTICAST_IF =

192.0.2.1
	+
	+       ctest_j mjail 192.0.2.1 <<EOF
	+#include <sys/socket.h>
	+#include <netinet/in.h>
	+#include <arpa/inet.h>
	+#include <assert.h>
	+#include <err.h>
	+
	+int
	+main(int argc, char *argv[])
	+{
	+       struct sockaddr_in sin =3D {
	+               .sin_family =3D AF_INET,
	+               .sin_len =3D sizeof(struct sockaddr_in),
	+       };
	+       struct in_addr in;
	+       int s, rv;
	+
	+       if (argc < 2)
	+               errx(1, "Usage: %s IPv4-address", argv[0]);
	+
	+       if (inet_pton(AF_INET, argv[1], &in) !=3D 1)
	+               err(1, "inet_pton(%s) failed", argv[1]);
	+
	+       assert((s =3D socket(PF_INET, SOCK_DGRAM, 0)) > 0);
	+       assert(bind(s, (struct sockaddr *)&sin, sizeof(sin)) =3D=3D 0);
	+       assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &in, =

sizeof(in))
	+         =3D=3D 0);
	+       /* RFC 6676 */
	+       assert(inet_pton(AF_INET, "233.252.0.1", &sin.sin_addr) =3D=3D 1=
);
	+       sin.sin_port =3D htons(6676);
	+       rv =3D sendto(s, &sin, sizeof(sin), 0,
	+           (struct sockaddr *)&sin, sizeof(sin));
	+       if (rv !=3D sizeof(sin))
	+               err(1, "sendto failed");
	+
	+       return (0);
	+}
	+EOF
	 }

	 IP_MULTICAST_IF_cleanup()

The downside is that we=E2=80=99re compiling the C test code on every run=
, but =

I expect test programs to be tiny, so that=E2=80=99s not too much of a co=
st.

Does this seem useful to you too?

Best regards,
Kristof
--=_MailMate_B28CA915-06EB-49CD-961F-2ADDD3DDDB4D_=
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/xhtml; charset=3Dutf-8"=
>
</head>
<body><div style=3D"font-family: sans-serif;"><div class=3D"markdown" sty=
le=3D"white-space: normal;">
<p dir=3D"auto">On 23 Mar 2025, at 8:40, Gleb Smirnoff wrote:</p>
<blockquote style=3D"margin: 0 0 5px; padding-left: 5px; border-left: 2px=
 solid #136BCE; color: #136BCE;">
<p dir=3D"auto">The branch main has been updated by glebius:</p>
<p dir=3D"auto">URL: <a href=3D"https://cgit.FreeBSD.org/src/commit/?id=3D=
0849f1634a70099b90256ceece52a598eeb3280e">https://cgit.FreeBSD.org/src/co=
mmit/?id=3D0849f1634a70099b90256ceece52a598eeb3280e</a></p>
<p dir=3D"auto">commit 0849f1634a70099b90256ceece52a598eeb3280e<br>
Author:     Gleb Smirnoff <a href=3D"mailto:glebius@FreeBSD.org">glebius@=
FreeBSD.org</a><br>
AuthorDate: 2025-03-22 22:44:20 +0000<br>
Commit:     Gleb Smirnoff <a href=3D"mailto:glebius@FreeBSD.org">glebius@=
FreeBSD.org</a><br>
CommitDate: 2025-03-22 23:39:50 +0000</p>
<pre style=3D"margin-left: 15px; margin-right: 15px; padding: 5px; border=
: thin solid gray; overflow-x: auto; max-width: 90vw; background-color: #=
E4E4E4;"><code style=3D"padding: 0 0.25em; background-color: #E4E4E4;">te=
sts/netinet: add test for IP_MULTICAST_IF
</code></pre>
</blockquote>
<p dir=3D"auto">It bugs me a little that we have to jump through hoops to=
 get a C program running in a specific jail/network setup.<br>
It=E2=80=99s somewhat common for that to happen, and I=E2=80=99m sure the=
re=E2=80=99d be more cases if it were easier to do (e.g. the getaddrinfo =
tests you=E2=80=99ve been working on could be even more useful with a cus=
tom DNS server, so in their own vnet jail).</p>
<p dir=3D"auto">I took a stab at that:</p>
<pre style=3D"margin-left: 15px; margin-right: 15px; padding: 5px; border=
: thin solid gray; overflow-x: auto; max-width: 90vw; background-color: #=
E4E4E4;"><code style=3D"padding: 0 0.25em; background-color: #E4E4E4;">di=
ff --git a/tests/sys/netinet/multicast.sh b/tests/sys/netinet/multicast.s=
h
index eb2b962dac70..7476daeb832a 100644
--- a/tests/sys/netinet/multicast.sh
+++ b/tests/sys/netinet/multicast.sh
@@ -26,6 +26,15 @@

 . $(atf_get_srcdir)/../common/vnet.subr

+ctest_j()
+{
+       jail=3D$1
+       shift
+
+       cc -x c - -o tmp || atf_fail &quot;Failed to build&quot;
+       jexec $jail `pwd`/tmp $@ || atf_fail &quot;Test program failed&qu=
ot;
+}
+
 # See regression fixed in baad45c9c12028964acd0b58096f3aaa0fb22859
 atf_test_case &quot;IP_MULTICAST_IF&quot; &quot;cleanup&quot;
 IP_MULTICAST_IF_head()
@@ -33,7 +42,6 @@ IP_MULTICAST_IF_head()
        atf_set descr \
            'sendto() for IP_MULTICAST_IF socket does not do routing look=
up'
        atf_set require.user root
-
 }

 IP_MULTICAST_IF_body()
@@ -46,8 +54,45 @@ IP_MULTICAST_IF_body()
        vnet_mkjail mjail ${epair}a
        jexec mjail ifconfig ${epair}a up
        jexec mjail ifconfig ${epair}a 192.0.2.1/24
-       atf_check -s exit:0 -o empty \
-           jexec mjail $(atf_get_srcdir)/sendto-IP_MULTICAST_IF 192.0.2.=
1
+
+       ctest_j mjail 192.0.2.1 &lt;&lt;EOF
+#include &lt;sys/socket.h&gt;
+#include &lt;netinet/in.h&gt;
+#include &lt;arpa/inet.h&gt;
+#include &lt;assert.h&gt;
+#include &lt;err.h&gt;
+
+int
+main(int argc, char *argv[])
+{
+       struct sockaddr_in sin =3D {
+               .sin_family =3D AF_INET,
+               .sin_len =3D sizeof(struct sockaddr_in),
+       };
+       struct in_addr in;
+       int s, rv;
+
+       if (argc &lt; 2)
+               errx(1, &quot;Usage: %s IPv4-address&quot;, argv[0]);
+
+       if (inet_pton(AF_INET, argv[1], &amp;in) !=3D 1)
+               err(1, &quot;inet_pton(%s) failed&quot;, argv[1]);
+
+       assert((s =3D socket(PF_INET, SOCK_DGRAM, 0)) &gt; 0);
+       assert(bind(s, (struct sockaddr *)&amp;sin, sizeof(sin)) =3D=3D 0=
);
+       assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &amp;in, sizeof=
(in))
+         =3D=3D 0);
+       /* RFC 6676 */
+       assert(inet_pton(AF_INET, &quot;233.252.0.1&quot;, &amp;sin.sin_a=
ddr) =3D=3D 1);
+       sin.sin_port =3D htons(6676);
+       rv =3D sendto(s, &amp;sin, sizeof(sin), 0,
+           (struct sockaddr *)&amp;sin, sizeof(sin));
+       if (rv !=3D sizeof(sin))
+               err(1, &quot;sendto failed&quot;);
+
+       return (0);
+}
+EOF
 }

 IP_MULTICAST_IF_cleanup()
</code></pre>
<p dir=3D"auto">The downside is that we=E2=80=99re compiling the C test c=
ode on every run, but I expect test programs to be tiny, so that=E2=80=99=
s not too much of a cost.</p>
<p dir=3D"auto">Does this seem useful to you too?</p>
<p dir=3D"auto">Best regards,<br>
Kristof</p>

</div>
</div>
</body>

</html>

--=_MailMate_B28CA915-06EB-49CD-961F-2ADDD3DDDB4D_=--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?7440BE85-66E8-4D1A-AC15-8B944F5C2951>