xref: /freebsd/tests/sys/kern/socket_accf.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 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 <errno.h>
31 #include <fcntl.h>
32 
33 #include <atf-c.h>
34 
35 static int
36 listensock(struct sockaddr_in *sin)
37 {
38 	int l;
39 
40 	ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0);
41 	ATF_REQUIRE(fcntl(l, F_SETFL, O_NONBLOCK) != -1);
42 	ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1},
43 	    sizeof(int)) == 0);
44 	*sin = (struct sockaddr_in){
45 		.sin_len = sizeof(sin),
46 		.sin_family = AF_INET,
47 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
48 	};
49 	ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0);
50 	ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin,
51 	    &(socklen_t){ sizeof(*sin) }) == 0);
52 	ATF_REQUIRE(listen(l, -1) == 0);
53 
54 	return (l);
55 }
56 
57 static int
58 clientsock(struct sockaddr_in *sin)
59 {
60 	int s;
61 
62 	ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0);
63 	ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0);
64 
65 	return (s);
66 }
67 
68 static void
69 accfon(int l, struct accept_filter_arg *af)
70 {
71 
72 	if (setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, af, sizeof(*af)) != 0) {
73 		if (errno == ENOENT)
74 			atf_tc_skip("Accept filter %s not loaded in kernel",
75 			    af->af_name);
76 		else
77 			atf_tc_fail("setsockopt(SO_ACCEPTFILTER): %s",
78 			    strerror(errno));
79 	}
80 }
81 
82 /*
83  * XXX: return from send(2) on a localhost connection doesn't guarantee that
84  * netisr has fully processed and delivered the data to the remote local
85  * socket.  Sleep a fraction of second to "guarantee" that it did.
86  */
87 static ssize_t
88 usend(int s, const void *msg, size_t len)
89 {
90 	ssize_t rv;
91 
92 	rv = send(s, msg, len, 0);
93 	usleep(100000);
94 	return (rv);
95 }
96 
97 ATF_TC_WITHOUT_HEAD(data);
98 ATF_TC_BODY(data, tc)
99 {
100 	struct accept_filter_arg afa = {
101 		.af_name = "dataready"
102 	};
103 	struct sockaddr_in sin;
104 	int l, s, a;
105 
106 	l = listensock(&sin);
107 	accfon(l, &afa);
108 	s = clientsock(&sin);
109 	ATF_REQUIRE(accept(l, NULL, 0) == -1);
110 	ATF_REQUIRE(errno == EAGAIN);
111 	ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo"));
112 	ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
113 }
114 
115 ATF_TC_WITHOUT_HEAD(http);
116 ATF_TC_BODY(http, tc)
117 {
118 	struct accept_filter_arg afa = {
119 		.af_name = "httpready"
120 	};
121 	struct sockaddr_in sin;
122 	int l, s, a;
123 
124 	l = listensock(&sin);
125 	accfon(l, &afa);
126 	s = clientsock(&sin);
127 
128 	/* 1) No data. */
129 	ATF_REQUIRE(accept(l, NULL, 0) == -1);
130 	ATF_REQUIRE(errno == EAGAIN);
131 
132 	/* 2) Data, that doesn't look like HTTP. */
133 	ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo"));
134 	ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
135 
136 	close(s);
137 	close(a);
138 
139 #define	CHUNK1	"GET / "
140 #define	CHUNK2	"HTTP/1.0\r\n\n"
141 #define	LEN(c)	(sizeof(c) - 1)
142 
143 	/* 3) Partial HTTP. */
144 	s = clientsock(&sin);
145 	ATF_REQUIRE(usend(s, CHUNK1, LEN(CHUNK1)) == LEN(CHUNK1));
146 	ATF_REQUIRE(accept(l, NULL, 0) == -1);
147 	ATF_REQUIRE(errno == EAGAIN);
148 
149 	/* 4) Complete HTTP. */
150 	ATF_REQUIRE(usend(s, CHUNK2, LEN(CHUNK2)) == LEN(CHUNK2));
151 	ATF_REQUIRE((a = accept(l, NULL, 0)) > 0);
152 }
153 
154 ATF_TP_ADD_TCS(tp)
155 {
156 	ATF_TP_ADD_TC(tp, data);
157 	ATF_TP_ADD_TC(tp, http);
158 
159 	return (atf_no_error());
160 }
161