1 /*
2  * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
3  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <assert.h>
31 #include <unistd.h>
32 
33 #include "msghdr.h"
34 
35 int
main(void)36 main(void)
37 {
38 	tprintf("%s", "");
39 
40 	int fds[2];
41 	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds))
42 		perror_msg_and_skip("socketpair");
43 	assert(0 == fds[0]);
44 	assert(1 == fds[1]);
45 
46 	static const char w0_c[] = "012";
47 	const char *w0_d = hexdump_strdup(w0_c);
48 	void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
49 
50 	static const char w1_c[] = "34567";
51 	const char *w1_d = hexdump_strdup(w1_c);
52 	void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
53 
54 	static const char w2_c[] = "89abcde";
55 	const char *w2_d = hexdump_strdup(w2_c);
56 	void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
57 
58 	const struct iovec w0_iov_[] = {
59 		{
60 			.iov_base = w0,
61 			.iov_len = LENGTH_OF(w0_c)
62 		}, {
63 			.iov_base = w1,
64 			.iov_len = LENGTH_OF(w1_c)
65 		}
66 	};
67 	struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_));
68 
69 	const struct iovec w1_iov_[] = {
70 		{
71 			.iov_base = w2,
72 			.iov_len = LENGTH_OF(w2_c)
73 		}
74 	};
75 	struct iovec *w1_iov = tail_memdup(w1_iov_, sizeof(w1_iov_));
76 
77 	const struct mmsghdr w_mmh_[] = {
78 		{
79 			.msg_hdr = {
80 				.msg_iov = w0_iov,
81 				.msg_iovlen = ARRAY_SIZE(w0_iov_),
82 			}
83 		}, {
84 			.msg_hdr = {
85 				.msg_iov = w1_iov,
86 				.msg_iovlen = ARRAY_SIZE(w1_iov_),
87 			}
88 		}
89 	};
90 	void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_));
91 	const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_);
92 
93 	int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL);
94 	if (r < 0)
95 		perror_msg_and_skip("sendmmsg");
96 	assert(r == (int) n_w_mmh);
97 	assert(close(1) == 0);
98 	tprintf("sendmmsg(1, [{msg_hdr={msg_name=NULL, msg_namelen=0"
99 		", msg_iov=[{iov_base=\"%s\", iov_len=%u}"
100 		", {iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
101 		", msg_controllen=0, msg_flags=0}, msg_len=%u}"
102 		", {msg_hdr={msg_name=NULL, msg_namelen=0"
103 		", msg_iov=[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
104 		", msg_controllen=0, msg_flags=0}, msg_len=%u}], %u"
105 		", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n"
106 		" = %u buffers in vector 0\n"
107 		" * %u bytes in buffer 0\n"
108 		" | 00000 %-49s  %-16s |\n"
109 		" * %u bytes in buffer 1\n"
110 		" | 00000 %-49s  %-16s |\n"
111 		" = %u buffers in vector 1\n"
112 		" * %u bytes in buffer 0\n"
113 		" | 00000 %-49s  %-16s |\n",
114 		w0_c, LENGTH_OF(w0_c),
115 		w1_c, LENGTH_OF(w1_c),
116 		ARRAY_SIZE(w0_iov_),
117 		LENGTH_OF(w0_c) + LENGTH_OF(w1_c),
118 		w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w1_iov_),
119 		LENGTH_OF(w2_c),
120 		n_w_mmh, r,
121 		ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c,
122 		LENGTH_OF(w1_c), w1_d, w1_c,
123 		ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c);
124 
125 	const unsigned int w_len =
126 		LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
127 	const unsigned int r_len = (w_len + 1) / 2;
128 	void *r0 = tail_alloc(r_len);
129 	void *r1 = tail_alloc(r_len);
130 	void *r2 = tail_alloc(r_len);
131 	const struct iovec r0_iov_[] = {
132 		{
133 			.iov_base = r0,
134 			.iov_len = r_len
135 		}
136 	};
137 	struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
138 	const struct iovec r1_iov_[] = {
139 		{
140 			.iov_base = r1,
141 			.iov_len = r_len
142 		},
143 		{
144 			.iov_base = r2,
145 			.iov_len = r_len
146 		}
147 	};
148 	struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
149 
150 	const struct mmsghdr r_mmh_[] = {
151 		{
152 			.msg_hdr = {
153 				.msg_iov = r0_iov,
154 				.msg_iovlen = ARRAY_SIZE(r0_iov_),
155 			}
156 		}, {
157 			.msg_hdr = {
158 				.msg_iov = r1_iov,
159 				.msg_iovlen = ARRAY_SIZE(r1_iov_),
160 			}
161 		}
162 	};
163 	void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_));
164 	const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_);
165 
166 	static const char r0_c[] = "01234567";
167 	const char *r0_d = hexdump_strdup(r0_c);
168 	static const char r1_c[] = "89abcde";
169 	const char *r1_d = hexdump_strdup(r1_c);
170 
171 	assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh);
172 	assert(close(0) == 0);
173 	tprintf("recvmmsg(0, [{msg_hdr={msg_name=NULL, msg_namelen=0"
174 		", msg_iov=[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
175 		", msg_controllen=0, msg_flags=0}, msg_len=%u}"
176 		", {msg_hdr={msg_name=NULL, msg_namelen=0"
177 		", msg_iov=[{iov_base=\"%s\", iov_len=%u}"
178 		", {iov_base=\"\", iov_len=%u}], msg_iovlen=%u"
179 		", msg_controllen=0, msg_flags=0}, msg_len=%u}], %u"
180 		", MSG_DONTWAIT, NULL) = %d\n"
181 		" = %u buffers in vector 0\n"
182 		" * %u bytes in buffer 0\n"
183 		" | 00000 %-49s  %-16s |\n"
184 		" = %u buffers in vector 1\n"
185 		" * %u bytes in buffer 0\n"
186 		" | 00000 %-49s  %-16s |\n",
187 		r0_c, r_len, ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c),
188 		r1_c, r_len, r_len, ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c),
189 		n_r_mmh, r,
190 		ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), r0_d, r0_c,
191 		ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), r1_d, r1_c);
192 
193 	tprintf("+++ exited with 0 +++\n");
194 	return 0;
195 }
196