1f08c3bdfSopenharmony_ci/* SCTP kernel Implementation
2f08c3bdfSopenharmony_ci * (C) Copyright IBM Corp. 2002, 2003
3f08c3bdfSopenharmony_ci * Copyright (c) 1999-2001 Motorola, Inc.
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This file is part of the SCTP kernel Implementation
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * The SCTP implementation is free software;
8f08c3bdfSopenharmony_ci * you can redistribute it and/or modify it under the terms of
9f08c3bdfSopenharmony_ci * the GNU General Public License as published by
10f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2, or (at your option)
11f08c3bdfSopenharmony_ci * any later version.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * The SCTP implementation is distributed in the hope that it
14f08c3bdfSopenharmony_ci * will be useful, but WITHOUT ANY WARRANTY; without even the implied
15f08c3bdfSopenharmony_ci *                 ************************
16f08c3bdfSopenharmony_ci * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17f08c3bdfSopenharmony_ci * See the GNU General Public License for more details.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
20f08c3bdfSopenharmony_ci * along with GNU CC; see the file COPYING.  If not, write to
21f08c3bdfSopenharmony_ci * the Free Software Foundation, 59 Temple Place - Suite 330,
22f08c3bdfSopenharmony_ci * Boston, MA 02111-1307, USA.
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * Please send any bug reports or fixes you make to the
25f08c3bdfSopenharmony_ci * email address(es):
26f08c3bdfSopenharmony_ci *    lksctp developers <lksctp-developers@lists.sourceforge.net>
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * Or submit a bug report through the following website:
29f08c3bdfSopenharmony_ci *    http://www.sf.net/projects/lksctp
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * Written or modified by:
32f08c3bdfSopenharmony_ci *    Sridhar Samudrala		<sri@us.ibm.com>
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci * Any bugs reported to us we will try to fix... any fixes shared will
35f08c3bdfSopenharmony_ci * be incorporated into the next SCTP release.
36f08c3bdfSopenharmony_ci */
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci/* This is a kernel test to verify
39f08c3bdfSopenharmony_ci * 1. MSG_EOR flag is set correctly when a single message is read using multiple
40f08c3bdfSopenharmony_ci *    recvmsg() calls.
41f08c3bdfSopenharmony_ci * 2. MSG_PEEK support.
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 <fcntl.h>
49f08c3bdfSopenharmony_ci#include <sys/types.h>
50f08c3bdfSopenharmony_ci#include <sys/socket.h>
51f08c3bdfSopenharmony_ci#include <sys/uio.h>
52f08c3bdfSopenharmony_ci#include <netinet/in.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 = 2;
60f08c3bdfSopenharmony_ciint TST_CNT = 0;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ciint
63f08c3bdfSopenharmony_cimain(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	int svr_sk, clt_sk;
66f08c3bdfSopenharmony_ci	struct sockaddr_in svr_loop, clt_loop;
67f08c3bdfSopenharmony_ci	struct iovec iov, out_iov;
68f08c3bdfSopenharmony_ci	struct msghdr inmessage, outmessage;
69f08c3bdfSopenharmony_ci	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
70f08c3bdfSopenharmony_ci	int error, msglen, i;
71f08c3bdfSopenharmony_ci	char *big_buffer;
72f08c3bdfSopenharmony_ci	void *msg_buf;
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	/* Initialize the server and client addresses. */
83f08c3bdfSopenharmony_ci	svr_loop.sin_family = AF_INET;
84f08c3bdfSopenharmony_ci	svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
85f08c3bdfSopenharmony_ci	svr_loop.sin_port = htons(SCTP_TESTPORT_1);
86f08c3bdfSopenharmony_ci	clt_loop.sin_family = AF_INET;
87f08c3bdfSopenharmony_ci	clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
88f08c3bdfSopenharmony_ci	clt_loop.sin_port = htons(SCTP_TESTPORT_2);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	/* Create and bind the server socket.  */
91f08c3bdfSopenharmony_ci        svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
92f08c3bdfSopenharmony_ci	test_bind(svr_sk, (struct sockaddr *)&svr_loop, sizeof(svr_loop));
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	/* Mark server socket as being able to accept new associations.  */
95f08c3bdfSopenharmony_ci	test_listen(svr_sk, 1);
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	/* Create and bind the client sockets.  */
98f08c3bdfSopenharmony_ci	clt_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
99f08c3bdfSopenharmony_ci	test_bind(clt_sk, (struct sockaddr *)&clt_loop, sizeof(clt_loop));
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
102f08c3bdfSopenharmony_ci	test_enable_assoc_change(svr_sk);
103f08c3bdfSopenharmony_ci	test_enable_assoc_change(clt_sk);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	/* Send a message. This will create the association.  */
106f08c3bdfSopenharmony_ci	memset(&outmessage, 0, sizeof(outmessage));
107f08c3bdfSopenharmony_ci	outmessage.msg_name = &svr_loop;
108f08c3bdfSopenharmony_ci	outmessage.msg_namelen = sizeof(svr_loop);
109f08c3bdfSopenharmony_ci	outmessage.msg_iov = &out_iov;
110f08c3bdfSopenharmony_ci	outmessage.msg_iovlen = 1;
111f08c3bdfSopenharmony_ci	msg_buf = test_build_msg(30000);
112f08c3bdfSopenharmony_ci	outmessage.msg_iov->iov_base = msg_buf;
113f08c3bdfSopenharmony_ci	outmessage.msg_iov->iov_len = 30000;
114f08c3bdfSopenharmony_ci	test_sendmsg(clt_sk, &outmessage, 0, 30000);
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci	/* Initialize inmessage for all receives. */
117f08c3bdfSopenharmony_ci	big_buffer = test_malloc(REALLY_BIG);
118f08c3bdfSopenharmony_ci        memset(&inmessage, 0, sizeof(inmessage));
119f08c3bdfSopenharmony_ci	iov.iov_base = big_buffer;
120f08c3bdfSopenharmony_ci	iov.iov_len = 2000;
121f08c3bdfSopenharmony_ci	inmessage.msg_iov = &iov;
122f08c3bdfSopenharmony_ci	inmessage.msg_iovlen = 1;
123f08c3bdfSopenharmony_ci	inmessage.msg_control = incmsg;
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	/* Receive COMM_UP on clt_sk. */
126f08c3bdfSopenharmony_ci	inmessage.msg_controllen = sizeof(incmsg);
127f08c3bdfSopenharmony_ci	error = test_recvmsg(clt_sk, &inmessage, 0);
128f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
129f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
130f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	/* Receive COMM_UP on svr_sk. */
133f08c3bdfSopenharmony_ci	inmessage.msg_controllen = sizeof(incmsg);
134f08c3bdfSopenharmony_ci	error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
135f08c3bdfSopenharmony_ci	test_check_msg_notification(&inmessage, error,
136f08c3bdfSopenharmony_ci				    sizeof(struct sctp_assoc_change),
137f08c3bdfSopenharmony_ci				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	/* Read the 30000 byte message using multiple recvmsg() calls in a
140f08c3bdfSopenharmony_ci	 * loop with 2000 bytes per read.
141f08c3bdfSopenharmony_ci	 */
142f08c3bdfSopenharmony_ci	for (i = 0, msglen = 30000; i < 15; i++, msglen-=2000) {
143f08c3bdfSopenharmony_ci		iov.iov_len = REALLY_BIG;
144f08c3bdfSopenharmony_ci		inmessage.msg_controllen = sizeof(incmsg);
145f08c3bdfSopenharmony_ci		error = test_recvmsg(svr_sk, &inmessage, MSG_PEEK);
146f08c3bdfSopenharmony_ci		test_check_msg_data(&inmessage, error, msglen,
147f08c3bdfSopenharmony_ci				    MSG_EOR, 0, 0);
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci		iov.iov_len = 2000;
150f08c3bdfSopenharmony_ci		inmessage.msg_controllen = sizeof(incmsg);
151f08c3bdfSopenharmony_ci		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
152f08c3bdfSopenharmony_ci		test_check_msg_data(&inmessage, error, 2000,
153f08c3bdfSopenharmony_ci				    ((i==14)?MSG_EOR:0), 0, 0);
154f08c3bdfSopenharmony_ci	}
155f08c3bdfSopenharmony_ci
156f08c3bdfSopenharmony_ci	tst_resm(TPASS, "recvmsg with MSG_PEEK flag");
157f08c3bdfSopenharmony_ci	tst_resm(TPASS, "MSG_EOR in msg_flags set correctly");
158f08c3bdfSopenharmony_ci
159f08c3bdfSopenharmony_ci	close(svr_sk);
160f08c3bdfSopenharmony_ci	close(clt_sk);
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci        /* Indicate successful completion.  */
163f08c3bdfSopenharmony_ci        return 0;
164f08c3bdfSopenharmony_ci}
165