Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 1 | /* SCTP kernel Implementation |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 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 | * |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 8 | * The SCTP implementation is free software; |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 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 | * |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 14 | * The SCTP implementation is distributed in the hope that it |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 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 <lksctp-developers@lists.sourceforge.net> |
| 28 | * |
| 29 | * Or submit a bug report through the following website: |
| 30 | * http://www.sf.net/projects/lksctp |
| 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 | * Sridhar Samudrala <sri@us.ibm.com> |
| 37 | */ |
| 38 | |
| 39 | /* This is a Functional Test to verify autoclose functionality and the |
| 40 | * socket option SCTP_AUTOCLOSE that can be used to specify the duration in |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 41 | * which an idle association is automatically closed. |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <unistd.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <sys/uio.h> |
| 51 | #include <netinet/in.h> |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 52 | #include <sys/errno.h> |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 53 | #include <errno.h> |
| 54 | #include <netinet/sctp.h> |
| 55 | #include <sctputil.h> |
| 56 | |
| 57 | char *TCID = __FILE__; |
| 58 | int TST_TOTAL = 1; |
| 59 | int TST_CNT = 0; |
| 60 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 61 | int |
| 62 | main(int argc, char *argv[]) |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 63 | { |
| 64 | int sk1, sk2; |
| 65 | sockaddr_storage_t loop1, loop2; |
| 66 | struct msghdr inmessage, outmessage; |
| 67 | struct iovec iov, out_iov; |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 68 | int error; |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 69 | char *big_buffer; |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 70 | char *message = "hello, world!\n"; |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 71 | uint32_t autoclose; |
| 72 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 73 | /* Rather than fflush() throughout the code, set stdout to |
| 74 | * be unbuffered. |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 75 | */ |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 76 | setvbuf(stdout, NULL, _IONBF, 0); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 77 | |
| 78 | loop1.v4.sin_family = AF_INET; |
| 79 | loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 80 | loop1.v4.sin_port = htons(SCTP_TESTPORT_1); |
| 81 | |
| 82 | loop2.v4.sin_family = AF_INET; |
| 83 | loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 84 | loop2.v4.sin_port = htons(SCTP_TESTPORT_2); |
| 85 | |
| 86 | /* Create the two endpoints which will talk to each other. */ |
| 87 | sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 88 | sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 89 | |
| 90 | /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ |
| 91 | test_enable_assoc_change(sk1); |
| 92 | test_enable_assoc_change(sk2); |
| 93 | |
| 94 | /* Bind these sockets to the test ports. */ |
| 95 | test_bind(sk1, &loop1.sa, sizeof(loop1)); |
| 96 | test_bind(sk2, &loop2.sa, sizeof(loop2)); |
| 97 | |
| 98 | /* Mark sk2 as being able to accept new associations. */ |
| 99 | test_listen(sk2, 1); |
| 100 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 101 | /* Set the autoclose duration for the associations created on sk1 |
| 102 | * and sk2 to be 5 seconds. |
| 103 | */ |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 104 | autoclose = 5; |
| 105 | test_setsockopt(sk1, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose)); |
| 106 | test_setsockopt(sk2, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose)); |
| 107 | |
| 108 | /* Send the first message. This will create the association. */ |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 109 | memset(&outmessage, 0, sizeof(outmessage)); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 110 | outmessage.msg_name = &loop2; |
| 111 | outmessage.msg_namelen = sizeof(loop2); |
| 112 | outmessage.msg_iov = &out_iov; |
| 113 | outmessage.msg_iovlen = 1; |
| 114 | outmessage.msg_iov->iov_base = message; |
| 115 | outmessage.msg_iov->iov_len = strlen(message) + 1; |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 116 | |
| 117 | test_sendmsg(sk1, &outmessage, 0, strlen(message)+1); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 118 | |
| 119 | /* Initialize inmessage for all receives. */ |
| 120 | big_buffer = test_malloc(REALLY_BIG); |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 121 | memset(&inmessage, 0, sizeof(inmessage)); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 122 | iov.iov_base = big_buffer; |
| 123 | iov.iov_len = REALLY_BIG; |
| 124 | inmessage.msg_iov = &iov; |
| 125 | inmessage.msg_iovlen = 1; |
| 126 | inmessage.msg_control = NULL; |
| 127 | |
| 128 | /* Get the communication up message on sk2. */ |
| 129 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 130 | test_check_msg_notification(&inmessage, error, |
| 131 | sizeof(struct sctp_assoc_change), |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 132 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 133 | |
| 134 | /* Get the communication up message on sk1. */ |
| 135 | error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); |
| 136 | test_check_msg_notification(&inmessage, error, |
| 137 | sizeof(struct sctp_assoc_change), |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 138 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 139 | |
| 140 | /* Get the first message which was sent. */ |
| 141 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 142 | test_check_msg_data(&inmessage, error, strlen(message) + 1, |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 143 | MSG_EOR|MSG_CTRUNC, 0, 0); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 144 | |
| 145 | tst_resm(TINFO, "Waiting for the associations to close automatically " |
| 146 | "in 5 secs"); |
| 147 | |
| 148 | /* Get the shutdown complete notification from sk1. */ |
| 149 | error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); |
| 150 | test_check_msg_notification(&inmessage, error, |
| 151 | sizeof(struct sctp_assoc_change), |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 152 | SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); |
| 153 | |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 154 | /* Get the shutdown complete notification from sk2. */ |
| 155 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 156 | test_check_msg_notification(&inmessage, error, |
| 157 | sizeof(struct sctp_assoc_change), |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 158 | SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); |
uid52817 | 89de9aa | 2003-10-23 18:42:24 +0000 | [diff] [blame] | 159 | |
| 160 | tst_resm(TPASS, "Autoclose of associations"); |
| 161 | |
| 162 | /* Shut down the link. */ |
| 163 | close(sk1); |
| 164 | close(sk2); |
| 165 | |
| 166 | /* Indicate successful completion. */ |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 167 | return 0; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 168 | } |