1f08c3bdfSopenharmony_ci/* SCTP kernel Implementation
2f08c3bdfSopenharmony_ci * (C) Copyright IBM Corp. 2001, 2003
3f08c3bdfSopenharmony_ci * Copyright (c) 1999-2000 Cisco, Inc.
4f08c3bdfSopenharmony_ci * Copyright (c) 1999-2001 Motorola, Inc.
5f08c3bdfSopenharmony_ci * Copyright (c) 2001 Intel Corp.
6f08c3bdfSopenharmony_ci * Copyright (c) 2001 Nokia, Inc.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * The SCTP implementation is free software;
9f08c3bdfSopenharmony_ci * you can redistribute it and/or modify it under the terms of
10f08c3bdfSopenharmony_ci * the GNU General Public License as published by
11f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2, or (at your option)
12f08c3bdfSopenharmony_ci * any later version.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * The SCTP implementation is distributed in the hope that it
15f08c3bdfSopenharmony_ci * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16f08c3bdfSopenharmony_ci *                 ************************
17f08c3bdfSopenharmony_ci * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18f08c3bdfSopenharmony_ci * See the GNU General Public License for more details.
19f08c3bdfSopenharmony_ci *
20f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
21f08c3bdfSopenharmony_ci * along with GNU CC; see the file COPYING.  If not, write to
22f08c3bdfSopenharmony_ci * the Free Software Foundation, 59 Temple Place - Suite 330,
23f08c3bdfSopenharmony_ci * Boston, MA 02111-1307, USA.
24f08c3bdfSopenharmony_ci *
25f08c3bdfSopenharmony_ci * Please send any bug reports or fixes you make to the
26f08c3bdfSopenharmony_ci * email address(es):
27f08c3bdfSopenharmony_ci *    lksctp developers <lksctp-developers@lists.sourceforge.net>
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * Or submit a bug report through the following website:
30f08c3bdfSopenharmony_ci *    http://www.sf.net/projects/lksctp
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci * Any bugs reported to us we will try to fix... any fixes shared will
33f08c3bdfSopenharmony_ci * be incorporated into the next SCTP release.
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci * Written or modified by:
36f08c3bdfSopenharmony_ci * Sridhar Samudrala <sri@us.ibm.com>
37f08c3bdfSopenharmony_ci */
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci/* This is a Functional Test to verify autoclose functionality and the
40f08c3bdfSopenharmony_ci * socket option SCTP_AUTOCLOSE that can be used to specify the duration in
41f08c3bdfSopenharmony_ci * which an idle association is automatically closed.
42f08c3bdfSopenharmony_ci */
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci#include <stdio.h>
45f08c3bdfSopenharmony_ci#include <unistd.h>
46f08c3bdfSopenharmony_ci#include <stdlib.h>
47f08c3bdfSopenharmony_ci#include <string.h>
48f08c3bdfSopenharmony_ci#include <sys/types.h>
49f08c3bdfSopenharmony_ci#include <sys/socket.h>
50f08c3bdfSopenharmony_ci#include <sys/uio.h>
51f08c3bdfSopenharmony_ci#include <netinet/in.h>
52f08c3bdfSopenharmony_ci#include <sys/errno.h>
53f08c3bdfSopenharmony_ci#include <errno.h>
54f08c3bdfSopenharmony_ci#include <netinet/sctp.h>
55f08c3bdfSopenharmony_ci#include <sctputil.h>
56f08c3bdfSopenharmony_ci#include "tst_kernel.h"
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cichar *TCID = __FILE__;
59f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
60f08c3bdfSopenharmony_ciint TST_CNT = 0;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ciint
63f08c3bdfSopenharmony_cimain(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	int sk1, sk2;
66f08c3bdfSopenharmony_ci	sockaddr_storage_t loop1, loop2;
67f08c3bdfSopenharmony_ci	struct msghdr inmessage, outmessage;
68f08c3bdfSopenharmony_ci	struct iovec iov, out_iov;
69f08c3bdfSopenharmony_ci	int error;
70f08c3bdfSopenharmony_ci	char *big_buffer;
71f08c3bdfSopenharmony_ci	char *message = "hello, world!\n";
72f08c3bdfSopenharmony_ci	uint32_t autoclose;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (tst_check_driver("sctp"))
75f08c3bdfSopenharmony_ci		tst_brkm(TCONF, tst_exit, "sctp driver not available");
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	/* Rather than fflush() throughout the code, set stdout to
78f08c3bdfSopenharmony_ci	 * be unbuffered.
79f08c3bdfSopenharmony_ci	 */
80f08c3bdfSopenharmony_ci	setvbuf(stdout, NULL, _IONBF, 0);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	loop1.v4.sin_family = AF_INET;
83f08c3bdfSopenharmony_ci	loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
84f08c3bdfSopenharmony_ci	loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	loop2.v4.sin_family = AF_INET;
87f08c3bdfSopenharmony_ci	loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
88f08c3bdfSopenharmony_ci	loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	/* Create the two endpoints which will talk to each other.  */
91f08c3bdfSopenharmony_ci	sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
92f08c3bdfSopenharmony_ci	sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
95f08c3bdfSopenharmony_ci	test_enable_assoc_change(sk1);
96f08c3bdfSopenharmony_ci	test_enable_assoc_change(sk2);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	/* Bind these sockets to the test ports.  */
99f08c3bdfSopenharmony_ci	test_bind(sk1, &loop1.sa, sizeof(loop1));
100f08c3bdfSopenharmony_ci	test_bind(sk2, &loop2.sa, sizeof(loop2));
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	/* Mark sk2 as being able to accept new associations.  */
103f08c3bdfSopenharmony_ci	test_listen(sk2, 1);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	/* Set the autoclose duration for the associations created on sk1
106f08c3bdfSopenharmony_ci	 * and sk2 to be 5 seconds.
107f08c3bdfSopenharmony_ci	 */
108f08c3bdfSopenharmony_ci	autoclose = 5;
109f08c3bdfSopenharmony_ci	test_setsockopt(sk1, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose));
110f08c3bdfSopenharmony_ci	test_setsockopt(sk2, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose));
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	/* Send the first message.  This will create the association.  */
113f08c3bdfSopenharmony_ci	memset(&outmessage, 0, sizeof(outmessage));
114f08c3bdfSopenharmony_ci	outmessage.msg_name = &loop2;
115f08c3bdfSopenharmony_ci	outmessage.msg_namelen = sizeof(loop2);
116f08c3bdfSopenharmony_ci	outmessage.msg_iov = &out_iov;
117f08c3bdfSopenharmony_ci	outmessage.msg_iovlen = 1;
118f08c3bdfSopenharmony_ci	outmessage.msg_iov->iov_base = message;
119f08c3bdfSopenharmony_ci	outmessage.msg_iov->iov_len = strlen(message) + 1;
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	/* Initialize inmessage for all receives. */
124f08c3bdfSopenharmony_ci	big_buffer = test_malloc(REALLY_BIG);
125f08c3bdfSopenharmony_ci        memset(&inmessage, 0, sizeof(inmessage));
126f08c3bdfSopenharmony_ci	iov.iov_base = big_buffer;
127f08c3bdfSopenharmony_ci	iov.iov_len = REALLY_BIG;
128f08c3bdfSopenharmony_ci	inmessage.msg_iov = &iov;
129f08c3bdfSopenharmony_ci	inmessage.msg_iovlen = 1;
130f08c3bdfSopenharmony_ci	inmessage.msg_control = NULL;
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	/* Get the communication up message on sk2.  */
133f08c3bdfSopenharmony_ci	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
134f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
135f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
136f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	/* Get the communication up message on sk1.  */
139f08c3bdfSopenharmony_ci	error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
140f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
141f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
142f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
143f08c3bdfSopenharmony_ci
144f08c3bdfSopenharmony_ci	/* Get the first message which was sent.  */
145f08c3bdfSopenharmony_ci	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
146f08c3bdfSopenharmony_ci	test_check_msg_data(&inmessage, error, strlen(message) + 1,
147f08c3bdfSopenharmony_ci			    MSG_EOR|MSG_CTRUNC, 0, 0);
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci	tst_resm(TINFO, "Waiting for the associations to close automatically "
150f08c3bdfSopenharmony_ci		 "in 5 secs");
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_ci	/* Get the shutdown complete notification from sk1. */
153f08c3bdfSopenharmony_ci	error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
154f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
155f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
156f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	/* Get the shutdown complete notification from sk2. */
159f08c3bdfSopenharmony_ci	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
160f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
161f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
162f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ci	tst_resm(TPASS, "Autoclose of associations");
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	/* Shut down the link.  */
167f08c3bdfSopenharmony_ci	close(sk1);
168f08c3bdfSopenharmony_ci	close(sk2);
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci	/* Indicate successful completion.  */
171f08c3bdfSopenharmony_ci	return 0;
172f08c3bdfSopenharmony_ci}
173