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  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 
33 #include <netinet/in.h>
34 
35 #include <arpa/inet.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 /*
45  * The UDP code allows transmitting zero-byte datagrams, but are they
46  * received?
47  */
48 
49 #define	THEPORT	9543		/* Arbitrary. */
50 
51 static void
52 usage(void)
53 {
54 
55 	errx(-1, "no arguments allowed\n");
56 }
57 
58 static void
59 test(int domain, const char *domainstr, struct sockaddr *sa, socklen_t salen)
60 {
61 	int sock_send, sock_receive;
62 	ssize_t size;
63 
64 	sock_send = socket(domain, SOCK_DGRAM, 0);
65 	if (sock_send < 0)
66 		err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
67 
68 	sock_receive = socket(domain, SOCK_DGRAM, 0);
69 	if (sock_receive < 0)
70 		err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);
71 
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);
77 
78 	if (connect(sock_send, sa, salen) < 0)
79 		err(-1, "Protocol %s connect(sock_send)", domainstr);
80 
81 	size = recv(sock_receive, NULL, 0, 0);
82 	if (size > 0)
83 		errx(-1, "Protocol %s recv(sock_receive, NULL, 0) before: %d",
84 		    domainstr, size);
85 	else if (size < 0)
86 		err(-1, "Protocol %s recv(sock_receive, NULL, 0) before",
87 		    domainstr);
88 
89 	size = send(sock_send, NULL, 0, 0);
90 	if (size < 0)
91 		err(-1, "Protocol %s send(sock_send, NULL, 0)", domainstr);
92 
93 	(void)sleep(1);
94 	size = recv(sock_receive, NULL, 0, 0);
95 	if (size < 0)
96 		err(-1, "Protocol %s recv(sock_receive, NULL, 0) test",
97 		    domainstr);
98 
99 	size = recv(sock_receive, NULL, 0, 0);
100 	if (size > 0)
101 		errx(-1, "Protocol %s recv(sock_receive, NULL, 0) after: %d",
102 		    domainstr, size);
103 	else if (size < 0)
104 		err(-1, "Protocol %s recv(sock_receive, NULL, 0) after",
105 		    domainstr);
106 }
107 
108 int
109 main(int argc, __unused char *argv[])
110 {
111 	struct sockaddr_un sun;
112 	struct sockaddr_in6 sin6;
113 	struct sockaddr_in sin;
114 	struct in6_addr loopback6addr = IN6ADDR_LOOPBACK_INIT;
115 
116 	if (argc != 1)
117 		usage();
118 
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);
124 
125 	test(PF_INET, "PF_INET", (struct sockaddr *)&sin, sizeof(sin));
126 
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);
132 
133 	test(PF_INET6, "PF_INET6", (struct sockaddr *)&sin6, sizeof(sin6));
134 
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);
141 
142 	test(PF_LOCAL, "PF_LOCAL", (struct sockaddr *)&sun, sizeof(sun));
143 
144 	return (0);
145 }
146