Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tools/regression/netinet/udpzerobyte/udpzerobyte.c
39498 views
1
/*-
2
* Copyright (c) 2008 Robert N. M. Watson
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
* SUCH DAMAGE.
25
*/
26
27
#include <sys/param.h>
28
#include <sys/socket.h>
29
#include <sys/un.h>
30
31
#include <netinet/in.h>
32
33
#include <arpa/inet.h>
34
35
#include <err.h>
36
#include <errno.h>
37
#include <fcntl.h>
38
#include <stdio.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
/*
43
* The UDP code allows transmitting zero-byte datagrams, but are they
44
* received?
45
*/
46
47
#define THEPORT 9543 /* Arbitrary. */
48
49
static void
50
usage(void)
51
{
52
53
errx(-1, "no arguments allowed\n");
54
}
55
56
static void
57
test(int domain, const char *domainstr, struct sockaddr *sa, socklen_t salen)
58
{
59
int sock_send, sock_receive;
60
ssize_t size;
61
62
sock_send = socket(domain, SOCK_DGRAM, 0);
63
if (sock_send < 0)
64
err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
65
66
sock_receive = socket(domain, SOCK_DGRAM, 0);
67
if (sock_receive < 0)
68
err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
69
70
if (bind(sock_receive, sa, salen) < 0)
71
err(-1, "Protocol %s bind(sock_receive)", domainstr);
72
if (fcntl(sock_receive, F_SETFL, O_NONBLOCK, 1) < 0)
73
err(-1, "Protocll %s fcntl(sock_receive, FL_SETFL, "
74
"O_NONBLOCK)", domainstr);
75
76
if (connect(sock_send, sa, salen) < 0)
77
err(-1, "Protocol %s connect(sock_send)", domainstr);
78
79
size = recv(sock_receive, NULL, 0, 0);
80
if (size > 0)
81
errx(-1, "Protocol %s recv(sock_receive, NULL, 0) before: %zd",
82
domainstr, size);
83
else if (size < 0)
84
err(-1, "Protocol %s recv(sock_receive, NULL, 0) before",
85
domainstr);
86
87
size = send(sock_send, NULL, 0, 0);
88
if (size < 0)
89
err(-1, "Protocol %s send(sock_send, NULL, 0)", domainstr);
90
91
(void)sleep(1);
92
size = recv(sock_receive, NULL, 0, 0);
93
if (size < 0)
94
err(-1, "Protocol %s recv(sock_receive, NULL, 0) test",
95
domainstr);
96
97
size = recv(sock_receive, NULL, 0, 0);
98
if (size > 0)
99
errx(-1, "Protocol %s recv(sock_receive, NULL, 0) after: %zd",
100
domainstr, size);
101
else if (size < 0)
102
err(-1, "Protocol %s recv(sock_receive, NULL, 0) after",
103
domainstr);
104
}
105
106
int
107
main(int argc, __unused char *argv[])
108
{
109
struct sockaddr_un sun;
110
struct sockaddr_in6 sin6;
111
struct sockaddr_in sin;
112
struct in6_addr loopback6addr = IN6ADDR_LOOPBACK_INIT;
113
114
if (argc != 1)
115
usage();
116
117
bzero(&sin, sizeof(sin));
118
sin.sin_len = sizeof(sin);
119
sin.sin_family = AF_INET;
120
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
121
sin.sin_port = htons(THEPORT);
122
123
test(PF_INET, "PF_INET", (struct sockaddr *)&sin, sizeof(sin));
124
125
bzero(&sin6, sizeof(sin6));
126
sin6.sin6_len = sizeof(sin6);
127
sin6.sin6_family = AF_INET6;
128
sin6.sin6_addr = loopback6addr;
129
sin6.sin6_port = htons(THEPORT);
130
131
test(PF_INET6, "PF_INET6", (struct sockaddr *)&sin6, sizeof(sin6));
132
133
bzero(&sun, sizeof(sun));
134
sun.sun_len = sizeof(sun);
135
sun.sun_family = AF_LOCAL;
136
strlcpy(sun.sun_path, "/tmp/udpzerosize-socket", sizeof(sun.sun_path));
137
if (unlink(sun.sun_path) < 0 && errno != ENOENT)
138
err(-1, "unlink: %s", sun.sun_path);
139
140
test(PF_LOCAL, "PF_LOCAL", (struct sockaddr *)&sun, sizeof(sun));
141
142
return (0);
143
}
144
145