Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netinet/udp_io.c
39536 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 <errno.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34
35
#include <atf-c.h>
36
37
/*
38
* Create a pair of UDP sockets. The first one is bound to a local
39
* address and the second one is connected to it.
40
*/
41
static void
42
udp_socketpair(int *s)
43
{
44
struct sockaddr_in sin = {
45
.sin_family = AF_INET,
46
.sin_len = sizeof(sin),
47
};
48
socklen_t slen = sizeof(sin);
49
int b, c;
50
51
ATF_REQUIRE((b = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
52
ATF_REQUIRE((c = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
53
ATF_REQUIRE(bind(b, (struct sockaddr *)&sin, sizeof(sin)) == 0);
54
ATF_REQUIRE(getsockname(b, (struct sockaddr *)&sin, &slen) == 0);
55
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
56
ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);
57
58
s[0] = b;
59
s[1] = c;
60
}
61
62
/*
63
* Check MSG_TRUNC.
64
*/
65
ATF_TC_WITHOUT_HEAD(trunc);
66
ATF_TC_BODY(trunc, tc)
67
{
68
char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
69
struct iovec iov = {
70
.iov_base = sbuf,
71
.iov_len = sizeof(sbuf),
72
};
73
struct msghdr msg = {
74
.msg_iov = &iov,
75
.msg_iovlen = 1,
76
};
77
int s[2];
78
u_int n;
79
80
udp_socketpair(s);
81
82
ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf));
83
n = (arc4random() % (sizeof(sbuf) - 1)) + 1;
84
iov.iov_base = rbuf;
85
iov.iov_len = n;
86
ATF_REQUIRE(recvmsg(s[0], &msg, 0) == (ssize_t)n);
87
ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
88
ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0);
89
iov.iov_len = sizeof(rbuf);
90
ATF_REQUIRE(recvmsg(s[0], &msg, MSG_DONTWAIT) == -1);
91
ATF_REQUIRE(errno == EAGAIN);
92
93
close(s[0]);
94
close(s[1]);
95
}
96
97
/*
98
* Check MSG_PEEK.
99
*/
100
ATF_TC_WITHOUT_HEAD(peek);
101
ATF_TC_BODY(peek, tc)
102
{
103
char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
104
struct iovec iov = {
105
.iov_base = sbuf,
106
.iov_len = sizeof(sbuf),
107
};
108
struct msghdr msg = {
109
.msg_iov = &iov,
110
.msg_iovlen = 1,
111
};
112
int s[2];
113
u_int n;
114
115
udp_socketpair(s);
116
117
ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf));
118
iov.iov_base = rbuf;
119
for (int i = 0; i < 10; i++) {
120
n = (arc4random() % sizeof(sbuf)) + 1;
121
iov.iov_len = n;
122
ATF_REQUIRE(recvmsg(s[0], &msg, MSG_PEEK) == (ssize_t)n);
123
if (n < sizeof(sbuf))
124
ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
125
else
126
ATF_REQUIRE(msg.msg_flags == MSG_PEEK);
127
ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0);
128
}
129
130
close(s[0]);
131
close(s[1]);
132
}
133
134
ATF_TP_ADD_TCS(tp)
135
{
136
ATF_TP_ADD_TC(tp, trunc);
137
ATF_TP_ADD_TC(tp, peek);
138
139
return (atf_no_error());
140
}
141
142