18c3bc1afSRobert Watson /*-
28c3bc1afSRobert Watson * Copyright (c) 2008 Robert N. M. Watson
38c3bc1afSRobert Watson * All rights reserved.
48c3bc1afSRobert Watson *
58c3bc1afSRobert Watson * Redistribution and use in source and binary forms, with or without
68c3bc1afSRobert Watson * modification, are permitted provided that the following conditions
78c3bc1afSRobert Watson * are met:
88c3bc1afSRobert Watson * 1. Redistributions of source code must retain the above copyright
98c3bc1afSRobert Watson * notice, this list of conditions and the following disclaimer.
108c3bc1afSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
118c3bc1afSRobert Watson * notice, this list of conditions and the following disclaimer in the
128c3bc1afSRobert Watson * documentation and/or other materials provided with the distribution.
138c3bc1afSRobert Watson *
148c3bc1afSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158c3bc1afSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168c3bc1afSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178c3bc1afSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188c3bc1afSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198c3bc1afSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208c3bc1afSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218c3bc1afSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228c3bc1afSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238c3bc1afSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248c3bc1afSRobert Watson * SUCH DAMAGE.
258c3bc1afSRobert Watson */
268c3bc1afSRobert Watson
278c3bc1afSRobert Watson #include <sys/param.h>
288c3bc1afSRobert Watson #include <sys/socket.h>
29a848550aSRobert Watson #include <sys/un.h>
308c3bc1afSRobert Watson
318c3bc1afSRobert Watson #include <netinet/in.h>
328c3bc1afSRobert Watson
338c3bc1afSRobert Watson #include <arpa/inet.h>
348c3bc1afSRobert Watson
358c3bc1afSRobert Watson #include <err.h>
368c3bc1afSRobert Watson #include <errno.h>
378c3bc1afSRobert Watson #include <fcntl.h>
388c3bc1afSRobert Watson #include <stdio.h>
398c3bc1afSRobert Watson #include <string.h>
408c3bc1afSRobert Watson #include <unistd.h>
418c3bc1afSRobert Watson
428c3bc1afSRobert Watson /*
438c3bc1afSRobert Watson * The UDP code allows transmitting zero-byte datagrams, but are they
448c3bc1afSRobert Watson * received?
458c3bc1afSRobert Watson */
468c3bc1afSRobert Watson
478c3bc1afSRobert Watson #define THEPORT 9543 /* Arbitrary. */
488c3bc1afSRobert Watson
498c3bc1afSRobert Watson static void
usage(void)508c3bc1afSRobert Watson usage(void)
518c3bc1afSRobert Watson {
528c3bc1afSRobert Watson
538c3bc1afSRobert Watson errx(-1, "no arguments allowed\n");
548c3bc1afSRobert Watson }
558c3bc1afSRobert Watson
56dba96bd9SRobert Watson static void
test(int domain,const char * domainstr,struct sockaddr * sa,socklen_t salen)57dba96bd9SRobert Watson test(int domain, const char *domainstr, struct sockaddr *sa, socklen_t salen)
58dba96bd9SRobert Watson {
59dba96bd9SRobert Watson int sock_send, sock_receive;
60a848550aSRobert Watson ssize_t size;
61dba96bd9SRobert Watson
62dba96bd9SRobert Watson sock_send = socket(domain, SOCK_DGRAM, 0);
63dba96bd9SRobert Watson if (sock_send < 0)
64dba96bd9SRobert Watson err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
65dba96bd9SRobert Watson
66dba96bd9SRobert Watson sock_receive = socket(domain, SOCK_DGRAM, 0);
67dba96bd9SRobert Watson if (sock_receive < 0)
68dba96bd9SRobert Watson err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
69dba96bd9SRobert Watson
70dba96bd9SRobert Watson if (bind(sock_receive, sa, salen) < 0)
71dba96bd9SRobert Watson err(-1, "Protocol %s bind(sock_receive)", domainstr);
72dba96bd9SRobert Watson if (fcntl(sock_receive, F_SETFL, O_NONBLOCK, 1) < 0)
73dba96bd9SRobert Watson err(-1, "Protocll %s fcntl(sock_receive, FL_SETFL, "
74dba96bd9SRobert Watson "O_NONBLOCK)", domainstr);
75dba96bd9SRobert Watson
76dba96bd9SRobert Watson if (connect(sock_send, sa, salen) < 0)
77dba96bd9SRobert Watson err(-1, "Protocol %s connect(sock_send)", domainstr);
78dba96bd9SRobert Watson
79a848550aSRobert Watson size = recv(sock_receive, NULL, 0, 0);
80a848550aSRobert Watson if (size > 0)
81*4039f071SEd Maste errx(-1, "Protocol %s recv(sock_receive, NULL, 0) before: %zd",
82a848550aSRobert Watson domainstr, size);
83a848550aSRobert Watson else if (size < 0)
84dba96bd9SRobert Watson err(-1, "Protocol %s recv(sock_receive, NULL, 0) before",
85dba96bd9SRobert Watson domainstr);
86dba96bd9SRobert Watson
87a848550aSRobert Watson size = send(sock_send, NULL, 0, 0);
88a848550aSRobert Watson if (size < 0)
89dba96bd9SRobert Watson err(-1, "Protocol %s send(sock_send, NULL, 0)", domainstr);
90dba96bd9SRobert Watson
91dba96bd9SRobert Watson (void)sleep(1);
92a848550aSRobert Watson size = recv(sock_receive, NULL, 0, 0);
93a848550aSRobert Watson if (size < 0)
94dba96bd9SRobert Watson err(-1, "Protocol %s recv(sock_receive, NULL, 0) test",
95dba96bd9SRobert Watson domainstr);
96dba96bd9SRobert Watson
97a848550aSRobert Watson size = recv(sock_receive, NULL, 0, 0);
98a848550aSRobert Watson if (size > 0)
99*4039f071SEd Maste errx(-1, "Protocol %s recv(sock_receive, NULL, 0) after: %zd",
100a848550aSRobert Watson domainstr, size);
101a848550aSRobert Watson else if (size < 0)
102dba96bd9SRobert Watson err(-1, "Protocol %s recv(sock_receive, NULL, 0) after",
103dba96bd9SRobert Watson domainstr);
104dba96bd9SRobert Watson }
105dba96bd9SRobert Watson
1068c3bc1afSRobert Watson int
main(int argc,__unused char * argv[])1078c3bc1afSRobert Watson main(int argc, __unused char *argv[])
1088c3bc1afSRobert Watson {
109a848550aSRobert Watson struct sockaddr_un sun;
110dba96bd9SRobert Watson struct sockaddr_in6 sin6;
1118c3bc1afSRobert Watson struct sockaddr_in sin;
112dba96bd9SRobert Watson struct in6_addr loopback6addr = IN6ADDR_LOOPBACK_INIT;
1138c3bc1afSRobert Watson
1148c3bc1afSRobert Watson if (argc != 1)
1158c3bc1afSRobert Watson usage();
1168c3bc1afSRobert Watson
1178c3bc1afSRobert Watson bzero(&sin, sizeof(sin));
1188c3bc1afSRobert Watson sin.sin_len = sizeof(sin);
1198c3bc1afSRobert Watson sin.sin_family = AF_INET;
1208c3bc1afSRobert Watson sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1218c3bc1afSRobert Watson sin.sin_port = htons(THEPORT);
1228c3bc1afSRobert Watson
123dba96bd9SRobert Watson test(PF_INET, "PF_INET", (struct sockaddr *)&sin, sizeof(sin));
1248c3bc1afSRobert Watson
125dba96bd9SRobert Watson bzero(&sin6, sizeof(sin6));
126dba96bd9SRobert Watson sin6.sin6_len = sizeof(sin6);
127dba96bd9SRobert Watson sin6.sin6_family = AF_INET6;
128dba96bd9SRobert Watson sin6.sin6_addr = loopback6addr;
129dba96bd9SRobert Watson sin6.sin6_port = htons(THEPORT);
1308c3bc1afSRobert Watson
131dba96bd9SRobert Watson test(PF_INET6, "PF_INET6", (struct sockaddr *)&sin6, sizeof(sin6));
1328c3bc1afSRobert Watson
133a848550aSRobert Watson bzero(&sun, sizeof(sun));
134a848550aSRobert Watson sun.sun_len = sizeof(sun);
135a848550aSRobert Watson sun.sun_family = AF_LOCAL;
136a848550aSRobert Watson strlcpy(sun.sun_path, "/tmp/udpzerosize-socket", sizeof(sun.sun_path));
137a848550aSRobert Watson if (unlink(sun.sun_path) < 0 && errno != ENOENT)
138a848550aSRobert Watson err(-1, "unlink: %s", sun.sun_path);
139a848550aSRobert Watson
140a848550aSRobert Watson test(PF_LOCAL, "PF_LOCAL", (struct sockaddr *)&sun, sizeof(sun));
141a848550aSRobert Watson
1428c3bc1afSRobert Watson return (0);
1438c3bc1afSRobert Watson }
144