xref: /freebsd/tests/sys/netgraph/ksocket.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netgraph.h>
31 #include <netgraph/ng_socket.h>
32 #include <netgraph/ng_ksocket.h>
33 
34 #include <errno.h>
35 
36 #include <atf-c.h>
37 
38 static void
39 hellocheck(int wr, int rd)
40 {
41 	char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
42 
43 	ATF_REQUIRE(send(wr, sbuf, sizeof(sbuf), 0) == sizeof(sbuf));
44 	ATF_REQUIRE(recv(rd, rbuf, sizeof(rbuf), 0) == sizeof(sbuf));
45 	ATF_REQUIRE(strcmp(sbuf, rbuf) == 0);
46 }
47 
48 #define	OURHOOK	"ks"
49 
50 ATF_TC_WITHOUT_HEAD(udp_connect);
51 ATF_TC_BODY(udp_connect, tc)
52 {
53 	struct sockaddr sa = {
54 		.sa_family = AF_INET,
55 	};
56 	socklen_t slen = sizeof(sa);
57 	int ds, cs, us;
58 
59 	ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
60 	ATF_REQUIRE(bind(us, &sa, sizeof(sa)) == 0);
61 	ATF_REQUIRE(getsockname(us, &sa, &slen) == 0);
62 
63 	struct ngm_mkpeer mkp = {
64 		.type = NG_KSOCKET_NODE_TYPE,
65 		.ourhook = OURHOOK,
66 		.peerhook = "inet/dgram/udp",
67 	};
68 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
69 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
70 	    sizeof(mkp)) >= 0);
71 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
72 	    NGM_KSOCKET_CONNECT, &sa, sizeof(sa)) >= 0);
73 
74 	hellocheck(ds, us);
75 }
76 
77 ATF_TC_WITHOUT_HEAD(udp_bind);
78 ATF_TC_BODY(udp_bind, tc)
79 {
80 	struct sockaddr_in sin = {
81 		.sin_family = AF_INET,
82 		.sin_len = sizeof(sin),
83 	};
84 	struct ng_mesg *rep;
85 	int ds, cs, us;
86 
87 	struct ngm_mkpeer mkp = {
88 		.type = NG_KSOCKET_NODE_TYPE,
89 		.ourhook = OURHOOK,
90 		.peerhook = "inet/dgram/udp",
91 	};
92 	ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
93 	ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
94 	    sizeof(mkp)) >= 0);
95 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
96 	    NGM_KSOCKET_BIND, &sin, sizeof(sin)) >= 0);
97 	ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
98 	    NGM_KSOCKET_GETNAME, NULL, 0) >= 0);
99 	ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) +
100 	    sizeof(struct sockaddr_in));
101 
102 	ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
103 	ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data,
104 	    sizeof(struct sockaddr_in)) == 0);
105 
106 	hellocheck(us, ds);
107 }
108 
109 ATF_TP_ADD_TCS(tp)
110 {
111 	ATF_TP_ADD_TC(tp, udp_connect);
112 	ATF_TP_ADD_TC(tp, udp_bind);
113 
114 	return (atf_no_error());
115 }
116