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