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