2 * Copyright (c) 2008 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
45 * The UDP code allows transmitting zero-byte datagrams, but are they
49 #define THEPORT 9543 /* Arbitrary. */
55 errx(-1, "no arguments allowed\n");
59 test(int domain, const char *domainstr, struct sockaddr *sa, socklen_t salen)
61 int sock_send, sock_receive;
64 sock_send = socket(domain, SOCK_DGRAM, 0);
66 err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
68 sock_receive = socket(domain, SOCK_DGRAM, 0);
70 err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
72 if (bind(sock_receive, sa, salen) < 0)
73 err(-1, "Protocol %s bind(sock_receive)", domainstr);
74 if (fcntl(sock_receive, F_SETFL, O_NONBLOCK, 1) < 0)
75 err(-1, "Protocll %s fcntl(sock_receive, FL_SETFL, "
76 "O_NONBLOCK)", domainstr);
78 if (connect(sock_send, sa, salen) < 0)
79 err(-1, "Protocol %s connect(sock_send)", domainstr);
81 size = recv(sock_receive, NULL, 0, 0);
83 errx(-1, "Protocol %s recv(sock_receive, NULL, 0) before: %zd",
86 err(-1, "Protocol %s recv(sock_receive, NULL, 0) before",
89 size = send(sock_send, NULL, 0, 0);
91 err(-1, "Protocol %s send(sock_send, NULL, 0)", domainstr);
94 size = recv(sock_receive, NULL, 0, 0);
96 err(-1, "Protocol %s recv(sock_receive, NULL, 0) test",
99 size = recv(sock_receive, NULL, 0, 0);
101 errx(-1, "Protocol %s recv(sock_receive, NULL, 0) after: %zd",
104 err(-1, "Protocol %s recv(sock_receive, NULL, 0) after",
109 main(int argc, __unused char *argv[])
111 struct sockaddr_un sun;
112 struct sockaddr_in6 sin6;
113 struct sockaddr_in sin;
114 struct in6_addr loopback6addr = IN6ADDR_LOOPBACK_INIT;
119 bzero(&sin, sizeof(sin));
120 sin.sin_len = sizeof(sin);
121 sin.sin_family = AF_INET;
122 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
123 sin.sin_port = htons(THEPORT);
125 test(PF_INET, "PF_INET", (struct sockaddr *)&sin, sizeof(sin));
127 bzero(&sin6, sizeof(sin6));
128 sin6.sin6_len = sizeof(sin6);
129 sin6.sin6_family = AF_INET6;
130 sin6.sin6_addr = loopback6addr;
131 sin6.sin6_port = htons(THEPORT);
133 test(PF_INET6, "PF_INET6", (struct sockaddr *)&sin6, sizeof(sin6));
135 bzero(&sun, sizeof(sun));
136 sun.sun_len = sizeof(sun);
137 sun.sun_family = AF_LOCAL;
138 strlcpy(sun.sun_path, "/tmp/udpzerosize-socket", sizeof(sun.sun_path));
139 if (unlink(sun.sun_path) < 0 && errno != ENOENT)
140 err(-1, "unlink: %s", sun.sun_path);
142 test(PF_LOCAL, "PF_LOCAL", (struct sockaddr *)&sun, sizeof(sun));