Debian Codegraph @

src/func_tests/test_timetolive.c

raw
1 /* SCTP kernel Implementation
2 * (C) Copyright IBM Corp. 2001, 2003
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 Nokia, Inc.
7 *
8 * The SCTP implementation is free software;
9 * you can redistribute it and/or modify it under the terms of
10 * the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * The SCTP implementation is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * ************************
17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 * See the GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with GNU CC; see the file COPYING. If not, write to
22 * the Free Software Foundation, 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *
25 * Please send any bug reports or fixes you make to the
26 * email address(es):
27 * lksctp developers <[email protected]>
28 *
29 * Or submit a bug report through the following website:
30 * https://github.com/sctp/lksctp-tools/
31 *
32 * Any bugs reported to us we will try to fix... any fixes shared will
33 * be incorporated into the next SCTP release.
34 *
35 * Written or modified by:
36 * Jon Grimm <[email protected]>
37 * Sridhar Samudrala <[email protected]>
38 */
39
40 /*
41 * This is a basic functional test for the SCTP kernel
42 * implementation of sndrcvinfo.sinfo_timetolive.
43 *
44 * 1) Create two sockets, the listening socket sets its RECVBUF small
45 * 2) Create a connection. Send enough data to the non-reading listener
46 * to fill the RCVBUF.
47 * 5) Set sinfo_timetolive on a message and send.
48 * 6) Disable sinfo_timetolive on a message and send.
49 * 7) Wait sinfo_timetolive.
50 * 8) Read out all the data at the receiver.
51 * 9) Make sure timed out message did not make it.
52 * 10) Make sure that the message with no timeout makes it to the receiver.
53 *
54 * Also test with SEND_FAILED notifications. Also, use a fragmented
55 * message so as to also exercise the SEND_FAILED of fragmentation
56 * code.
57 */
58
59 #include <stdio.h>
60 #include <unistd.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <sys/uio.h>
66 #include <netinet/in.h>
67 #include <errno.h>
68 #include <netinet/sctp.h>
69 #include <sctputil.h>
70
71 char *TCID = __FILE__;
72 int TST_TOTAL = 6;
73 int TST_CNT = 0;
74
75 /* This is the size of our RCVBUF */
76 #define SMALL_RCVBUF 3000
77
78 /* MAX segment size */
79 #define SMALL_MAXSEG 500
80
81 /* RWND_SLOP is the extra data that fills up the rwnd */
82 #define RWND_SLOP 100
83 static char *fillmsg = NULL;
84 static char *ttlmsg = "This should time out!\n";
85 static char *nottlmsg = "This should NOT time out!\n";
86 static char ttlfrag[SMALL_MAXSEG*3] = {0};
87 static char *message = "Hello world\n";
88
89 int main(int argc, char *argv[])
90 {
91 int sk1, sk2;
92 sockaddr_storage_t loop1;
93 sockaddr_storage_t loop2;
94 struct iovec iov;
95 struct msghdr inmessage;
96 struct msghdr outmessage;
97 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
98 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
99 struct cmsghdr *cmsg;
100 struct sctp_sndrcvinfo *sinfo;
101 struct iovec out_iov;
102 int error;
103 int pf_class;
104 uint32_t ppid;
105 uint32_t stream;
106 sctp_assoc_t associd1;
107 struct sctp_assoc_change *sac;
108 struct sctp_event_subscribe subscribe;
109 char *big_buffer;
110 int offset;
111 struct sctp_send_failed *ssf;
112 socklen_t len; /* Really becomes 2xlen when set. */
113 int orig_len;
114 struct sctp_status gstatus;
115
116 /* Rather than fflush() throughout the code, set stdout to
117 * be unbuffered.
118 */
119 setvbuf(stdout, NULL, _IONBF, 0);
120
121 /* Set some basic values which depend on the address family. */
122 #if TEST_V6
123 pf_class = PF_INET6;
124
125 loop1.v6.sin6_family = AF_INET6;
126 loop1.v6.sin6_addr = in6addr_loopback;
127 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
128
129 loop2.v6.sin6_family = AF_INET6;
130 loop2.v6.sin6_addr = in6addr_loopback;
131 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
132 #else
133 pf_class = PF_INET;
134
135 loop1.v4.sin_family = AF_INET;
136 loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
137 loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
138
139 loop2.v4.sin_family = AF_INET;
140 loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
141 loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
142 #endif /* TEST_V6 */
143
144 /* Create the two endpoints which will talk to each other. */
145 sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
146 sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
147
148 len = sizeof(int);
149 error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &orig_len,
150 &len);
151 if (error)
152 tst_brkm(TBROK, tst_exit, "can't get rcvbuf size: %s",
153 strerror(errno));
154 /* Set the MAXSEG to something smallish. */
155 {
156 int val = SMALL_MAXSEG;
157 test_setsockopt(sk1, SCTP_MAXSEG, &val, sizeof(val));
158 }
159
160 memset(&subscribe, 0, sizeof(subscribe));
161 subscribe.sctp_data_io_event = 1;
162 subscribe.sctp_association_event = 1;
163 subscribe.sctp_send_failure_event = 1;
164 test_setsockopt(sk1, SCTP_EVENTS, &subscribe, sizeof(subscribe));
165 test_setsockopt(sk2, SCTP_EVENTS, &subscribe, sizeof(subscribe));
166
167 /* Bind these sockets to the test ports. */
168 test_bind(sk1, &loop1.sa, sizeof(loop1));
169 test_bind(sk2, &loop2.sa, sizeof(loop2));
170
171 /*
172 * This code sets the associations RWND very small so we can
173 * fill it. It does this by manipulating the rcvbuf as follows:
174 * 1) Reduce the rcvbuf size on the socket
175 * 2) create an association so that we advertise rcvbuf/2 as
176 * our initial rwnd
177 */
178 len = SMALL_RCVBUF;
179 error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &len,
180 sizeof(len));
181 if (error)
182 tst_brkm(TBROK, tst_exit, "setsockopt(SO_RCVBUF): %s",
183 strerror(errno));
184
185 /* Mark sk2 as being able to accept new associations. */
186 test_listen(sk2, 1);
187
188 /* Send the first message. This will create the association. */
189 outmessage.msg_name = &loop2;
190 outmessage.msg_namelen = sizeof(loop2);
191 outmessage.msg_iov = &out_iov;
192 outmessage.msg_iovlen = 1;
193 outmessage.msg_control = outcmsg;
194 outmessage.msg_controllen = sizeof(outcmsg);
195 outmessage.msg_flags = 0;
196 cmsg = CMSG_FIRSTHDR(&outmessage);
197 cmsg->cmsg_level = IPPROTO_SCTP;
198 cmsg->cmsg_type = SCTP_SNDRCV;
199 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
200 outmessage.msg_controllen = cmsg->cmsg_len;
201 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
202 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
203 ppid = rand(); /* Choose an arbitrary value. */
204 stream = 1;
205 sinfo->sinfo_ppid = ppid;
206 sinfo->sinfo_stream = stream;
207 outmessage.msg_iov->iov_base = message;
208 outmessage.msg_iov->iov_len = strlen(message) + 1;
209 test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);
210
211 /* Initialize inmessage for all receives. */
212 big_buffer = test_malloc(REALLY_BIG);
213 memset(&inmessage, 0, sizeof(inmessage));
214 iov.iov_base = big_buffer;
215 iov.iov_len = REALLY_BIG;
216 inmessage.msg_iov = &iov;
217 inmessage.msg_iovlen = 1;
218 inmessage.msg_control = incmsg;
219
220 /* Get the communication up message on sk2. */
221 inmessage.msg_controllen = sizeof(incmsg);
222 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
223 test_check_msg_notification(&inmessage, error,
224 sizeof(struct sctp_assoc_change),
225 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
226 #if 0
227 sac = (struct sctp_assoc_change *)iov.iov_base;
228 associd2 = sac->sac_assoc_id;
229 #endif
230
231 /* Get the communication up message on sk1. */
232 inmessage.msg_controllen = sizeof(incmsg);
233 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
234 test_check_msg_notification(&inmessage, error,
235 sizeof(struct sctp_assoc_change),
236 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
237 sac = (struct sctp_assoc_change *)iov.iov_base;
238 associd1 = sac->sac_assoc_id;
239
240 /* Get the first data message which was sent. */
241 inmessage.msg_controllen = sizeof(incmsg);
242 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
243 test_check_msg_data(&inmessage, error, strlen(message) + 1,
244 MSG_EOR, stream, ppid);
245
246 /* Figure out how big to make our fillmsg */
247 len = sizeof(struct sctp_status);
248 memset(&gstatus,0,sizeof(struct sctp_status));
249 gstatus.sstat_assoc_id = associd1;
250 error = getsockopt(sk1, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);
251
252 if (error)
253 tst_brkm(TBROK, tst_exit, "can't get rwnd size: %s",
254 strerror(errno));
255 tst_resm(TINFO, "Creating fillmsg of size %d",
256 gstatus.sstat_rwnd+RWND_SLOP);
257 fillmsg = test_malloc(gstatus.sstat_rwnd+RWND_SLOP);
258
259 /* Send a fillmsg */
260 outmessage.msg_controllen = sizeof(outcmsg);
261 outmessage.msg_flags = 0;
262 cmsg = CMSG_FIRSTHDR(&outmessage);
263 cmsg->cmsg_level = IPPROTO_SCTP;
264 cmsg->cmsg_type = SCTP_SNDRCV;
265 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
266 outmessage.msg_controllen = cmsg->cmsg_len;
267 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
268 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
269 ppid++;
270 stream++;
271 sinfo->sinfo_ppid = ppid;
272 sinfo->sinfo_stream = stream;
273 memset(fillmsg, 'X', gstatus.sstat_rwnd+RWND_SLOP);
274 fillmsg[gstatus.sstat_rwnd+RWND_SLOP-1] = '\0';
275 outmessage.msg_iov->iov_base = fillmsg;
276 outmessage.msg_iov->iov_len = gstatus.sstat_rwnd+RWND_SLOP;
277 outmessage.msg_name = NULL;
278 outmessage.msg_namelen = 0;
279 sinfo->sinfo_assoc_id = associd1;
280 sinfo->sinfo_timetolive = 0;
281 test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL,
282 gstatus.sstat_rwnd+RWND_SLOP);
283
284 /* Now send the message with timeout. */
285 sinfo->sinfo_ppid = ppid;
286 sinfo->sinfo_stream = stream;
287 outmessage.msg_iov->iov_base = ttlmsg;
288 outmessage.msg_iov->iov_len = strlen(ttlmsg) + 1;
289 outmessage.msg_name = NULL;
290 outmessage.msg_namelen = 0;
291 sinfo->sinfo_assoc_id = associd1;
292 sinfo->sinfo_timetolive = 2000;
293 test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(ttlmsg) + 1);
294
295 tst_resm(TPASS, "Send a message with timeout");
296
297 /* Next send a message with no timeout. */
298 sinfo->sinfo_ppid = ppid;
299 sinfo->sinfo_stream = stream;
300 outmessage.msg_iov->iov_base = nottlmsg;
301 outmessage.msg_iov->iov_len = strlen(nottlmsg) + 1;
302 outmessage.msg_name = NULL;
303 outmessage.msg_namelen = 0;
304 sinfo->sinfo_assoc_id = associd1;
305 sinfo->sinfo_timetolive = 0;
306 test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(nottlmsg)+1);
307
308 tst_resm(TPASS, "Send a message with no timeout");
309
310 /* And finally a fragmented message that will time out. */
311 sinfo->sinfo_ppid = ppid;
312 sinfo->sinfo_stream = stream;
313 memset(ttlfrag, 48, sizeof(ttlfrag)); /* 48 is the ascii of '0' */
314 ttlfrag[sizeof(ttlfrag)-1] = '\0';
315 outmessage.msg_iov->iov_base = ttlfrag;
316 outmessage.msg_iov->iov_len = sizeof(ttlfrag);
317 outmessage.msg_name = NULL;
318 outmessage.msg_namelen = 0;
319 sinfo->sinfo_assoc_id = associd1;
320 sinfo->sinfo_timetolive = 2000;
321 test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, sizeof(ttlfrag));
322
323 tst_resm(TPASS, "Send a fragmented message with timeout");
324
325 /* Sleep waiting for the message to time out. */
326 tst_resm(TINFO, " ** SLEEPING for 3 seconds **");
327 sleep(3);
328
329 /* Read the fillmsg snuck in between the ttl'd messages. */
330 do {
331 inmessage.msg_controllen = sizeof(incmsg);
332 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
333 } while (!(inmessage.msg_flags & MSG_EOR));
334
335 /* Now get the message that did NOT time out. */
336 inmessage.msg_controllen = sizeof(incmsg);
337 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
338 test_check_msg_data(&inmessage, error, strlen(nottlmsg) + 1,
339 MSG_EOR, stream, ppid);
340 if (0 != strncmp(iov.iov_base, nottlmsg, strlen(nottlmsg)+1))
341 tst_brkm(TBROK, tst_exit, "Received Wrong Message !!!");
342
343 tst_resm(TPASS, "Receive message with no timeout");
344
345 /* Get the SEND_FAILED notification for the message that DID
346 * time out.
347 */
348 inmessage.msg_controllen = sizeof(incmsg);
349 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
350 test_check_msg_notification(&inmessage, error,
351 sizeof(struct sctp_send_failed) +
352 strlen(ttlmsg) + 1,
353 SCTP_SEND_FAILED, 0);
354 ssf = (struct sctp_send_failed *)iov.iov_base;
355 if (0 != strncmp(ttlmsg, (char *)ssf->ssf_data, strlen(ttlmsg) + 1))
356 tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch");
357
358 tst_resm(TPASS, "Receive SEND_FAILED for message with timeout");
359
360 /* Get the SEND_FAILED notification for the fragmented message that
361 * DID time out.
362 */
363 offset = 0;
364 do {
365 inmessage.msg_controllen = sizeof(incmsg);
366 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
367 test_check_msg_notification(&inmessage, error,
368 sizeof(struct sctp_send_failed) +
369 SMALL_MAXSEG,
370 SCTP_SEND_FAILED, 0);
371 ssf = (struct sctp_send_failed *)iov.iov_base;
372 if (0 != strncmp(&ttlfrag[offset], (char *)ssf->ssf_data,
373 SMALL_MAXSEG))
374 tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch");
375 offset += SMALL_MAXSEG;
376 } while (!(ssf->ssf_info.sinfo_flags & 0x01)); /* LAST_FRAG */
377
378 tst_resm(TPASS, "Receive SEND_FAILED for fragmented message with "
379 "timeout");
380
381 /* Shut down the link. */
382 close(sk1);
383
384 /* Get the shutdown complete notification. */
385 inmessage.msg_controllen = sizeof(incmsg);
386 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
387 test_check_msg_notification(&inmessage, error,
388 sizeof(struct sctp_assoc_change),
389 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
390
391 close(sk2);
392
393 free(big_buffer);
394 free(fillmsg);
395 /* Indicate successful completion. */
396 return 0;
397 }