Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netgraph/ksocket.c
102331 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2024 Gleb Smirnoff <[email protected]>
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 ngm_mkpeer mkp = {
54
.type = NG_KSOCKET_NODE_TYPE,
55
.ourhook = OURHOOK,
56
.peerhook = "inet/dgram/udp",
57
};
58
struct sockaddr_in sin = {
59
.sin_family = AF_INET,
60
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
61
.sin_len = sizeof(sin),
62
};
63
socklen_t slen = sizeof(sin);
64
int cs, ds, us;
65
66
ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
67
ATF_REQUIRE(bind(us, (struct sockaddr *)&sin, sizeof(sin)) == 0);
68
ATF_REQUIRE(getsockname(us, (struct sockaddr *)&sin, &slen) == 0);
69
70
ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
71
ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
72
sizeof(mkp)) >= 0);
73
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
74
NGM_KSOCKET_CONNECT, &sin, sizeof(sin)) >= 0);
75
76
hellocheck(ds, us);
77
}
78
79
ATF_TC_WITHOUT_HEAD(udp_bind);
80
ATF_TC_BODY(udp_bind, tc)
81
{
82
struct ngm_mkpeer mkp = {
83
.type = NG_KSOCKET_NODE_TYPE,
84
.ourhook = OURHOOK,
85
.peerhook = "inet/dgram/udp",
86
};
87
struct sockaddr_in sin = {
88
.sin_family = AF_INET,
89
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
90
.sin_len = sizeof(sin),
91
};
92
struct ng_mesg *rep;
93
int cs, ds, us;
94
95
ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
96
ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
97
sizeof(mkp)) >= 0);
98
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
99
NGM_KSOCKET_BIND, &sin, sizeof(sin)) >= 0);
100
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
101
NGM_KSOCKET_GETNAME, NULL, 0) >= 0);
102
ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) +
103
sizeof(struct sockaddr_in));
104
105
ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
106
ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data,
107
sizeof(struct sockaddr_in)) == 0);
108
109
hellocheck(us, ds);
110
}
111
112
ATF_TC_WITHOUT_HEAD(udp6_connect);
113
ATF_TC_BODY(udp6_connect, tc)
114
{
115
struct ngm_mkpeer mkp = {
116
.type = NG_KSOCKET_NODE_TYPE,
117
.ourhook = OURHOOK,
118
.peerhook = "inet6/dgram/udp6",
119
};
120
struct sockaddr_in6 sin6 = {
121
.sin6_family = AF_INET6,
122
.sin6_len = sizeof(sin6),
123
.sin6_addr = in6addr_loopback,
124
};
125
socklen_t slen = sizeof(sin6);
126
int cs, ds, us;
127
128
ATF_REQUIRE((us = socket(PF_INET6, SOCK_DGRAM, 0)) > 0);
129
ATF_REQUIRE(bind(us, (struct sockaddr *)&sin6, sizeof(sin6)) == 0);
130
ATF_REQUIRE(getsockname(us, (struct sockaddr *)&sin6, &slen) == 0);
131
132
ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
133
ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
134
sizeof(mkp)) >= 0);
135
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
136
NGM_KSOCKET_CONNECT, &sin6, sizeof(sin6)) >= 0);
137
138
hellocheck(ds, us);
139
}
140
141
142
ATF_TC_WITHOUT_HEAD(udp6_bind);
143
ATF_TC_BODY(udp6_bind, tc)
144
{
145
struct ngm_mkpeer mkp = {
146
.type = NG_KSOCKET_NODE_TYPE,
147
.ourhook = OURHOOK,
148
.peerhook = "inet6/dgram/udp6",
149
};
150
struct sockaddr_in6 sin6 = {
151
.sin6_family = AF_INET6,
152
.sin6_len = sizeof(sin6),
153
.sin6_addr = in6addr_loopback,
154
};
155
struct ng_mesg *rep;
156
int cs, ds, us;
157
158
ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0);
159
ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp,
160
sizeof(mkp)) >= 0);
161
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
162
NGM_KSOCKET_BIND, &sin6, sizeof(sin6)) >= 0);
163
ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE,
164
NGM_KSOCKET_GETNAME, NULL, 0) >= 0);
165
ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) +
166
sizeof(struct sockaddr_in6));
167
168
ATF_REQUIRE((us = socket(PF_INET6, SOCK_DGRAM, 0)) > 0);
169
ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data,
170
sizeof(struct sockaddr_in6)) == 0);
171
172
hellocheck(us, ds);
173
}
174
175
ATF_TP_ADD_TCS(tp)
176
{
177
ATF_TP_ADD_TC(tp, udp_connect);
178
ATF_TP_ADD_TC(tp, udp_bind);
179
ATF_TP_ADD_TC(tp, udp6_connect);
180
ATF_TP_ADD_TC(tp, udp6_bind);
181
182
return (atf_no_error());
183
}
184
185