• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file has test cases to test the shutdown() call for 1-1 style sockets
6  *
7  * TEST1: Bad socket descriptor
8  * TEST2: Invalid socket
9  * TEST3: shutdown with SHUT_WR flag to disable new send
10  * TEST4: shutdown with SHUT_RD flag to disable new receive
11  * TEST5: shutdown with SHUT_RDWR flag to disable new receive/send
12  * TEST6: Unconnected socket
13  *
14  * The SCTP implementation is free software;
15  * you can redistribute it and/or modify it under the terms of
16  * the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * The SCTP implementation is distributed in the hope that it
21  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22  *                 ************************
23  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  * See the GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with GNU CC; see the file COPYING.  If not, write to
28  * the Free Software Foundation, 59 Temple Place - Suite 330,
29  * Boston, MA 02111-1307, USA.
30  *
31  * Please send any bug reports or fixes you make to the
32  * email address(es):
33  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
34  *
35  * Or submit a bug report through the following website:
36  *    http://www.sf.net/projects/lksctp
37  *
38  * Any bugs reported given to us we will try to fix... any fixes shared will
39  * be incorporated into the next SCTP release
40  *
41  */
42 
43 #include <stdio.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <linux/socket.h>
49 #include <netinet/sctp.h>
50 #include <sys/types.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <sctputil.h>
55 #include "tst_kernel.h"
56 
57 char *TCID = __FILE__;
58 int TST_TOTAL = 6;
59 int TST_CNT = 0;
60 
61 #define MAX_CLIENTS 10
62 
63 int
main(void)64 main(void)
65 {
66 	int clnt_sk[MAX_CLIENTS], acpt_sk[MAX_CLIENTS],sk;
67 	int lstn_sk;
68 	struct sockaddr_in lstn_addr, acpt_addr;
69 	socklen_t addrlen;
70 	int error, i;
71         char *message = "hello, world!\n";
72 	char msgbuf[100];
73 	int pf_class;
74 	int fd, err_no = 0;
75 	char filename[21];
76 
77 	if (tst_check_driver("sctp"))
78 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
79 
80         /* Rather than fflush() throughout the code, set stdout to
81 	 * be unbuffered.
82 	 */
83 	setvbuf(stdout, NULL, _IONBF, 0);
84 	setvbuf(stderr, NULL, _IONBF, 0);
85 
86 	/* Initialize the server and client addresses. */
87 	pf_class = PF_INET;
88 
89 	lstn_addr.sin_family = AF_INET;
90 	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
91 	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
92 
93         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
94         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
95 
96 	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
97 
98 	test_listen(lstn_sk, MAX_CLIENTS);
99 
100 	for (i = 0; i < MAX_CLIENTS; i++) {
101 		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
102 		test_connect(clnt_sk[i], (struct sockaddr *)&lstn_addr,
103 			     sizeof(lstn_addr));
104 	}
105 
106 	for (i = 0; i < MAX_CLIENTS; i++) {
107 		addrlen = sizeof(acpt_addr);
108 		acpt_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
109 					 &addrlen);
110 	}
111 
112 	/*shutdown() TEST1: Bad socket descriptor, EBADF Expected error*/
113 	error = shutdown(-1, SHUT_WR);
114 	if (error != -1 || errno != EBADF)
115 		tst_brkm(TBROK, tst_exit, "shutdown with a bad socket "
116 			 "error:%d, errno:%d", error, errno);
117 
118 	tst_resm(TPASS, "shutdown() with a bad socket descriptor - EBADF");
119 
120 	/*shutdown() TEST2: Invalid socket, ENOTSOCK Expected error*/
121 	strcpy(filename, "/tmp/sctptest.XXXXXX");
122 	fd = mkstemp(filename);
123 	if (fd == -1)
124 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
125 				filename, strerror(errno));
126 	error = shutdown(fd, SHUT_WR);
127 	if (error == -1)
128 		err_no = errno;
129 	close(fd);
130 	unlink(filename);
131 	if (error != -1 || err_no != ENOTSOCK)
132 		tst_brkm(TBROK, tst_exit, "shutdown with an invalid socket "
133 			 "error:%d, errno:%d", error, err_no);
134 
135 	tst_resm(TPASS, "shutdown() with an invalid socket - ENOTSOCK");
136 
137 	errno = 0;
138 	/*Do a send first before doing shutdown*/
139 	test_send(acpt_sk[0], message, strlen(message), 0);
140 
141 	/*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send*/
142 	error = shutdown(clnt_sk[0], SHUT_WR);
143 	if (error < 0)
144 		tst_brkm(TBROK, tst_exit, "shutdown with SHUT_WR flag "
145 			 "error:%d, errno:%d", error, errno);
146 
147 	/* Reading on a socket that has received SHUTDOWN should return 0
148 	 * indicating EOF.
149 	 */
150 	error = recv(acpt_sk[0], msgbuf, 100, 0);
151 	if ((error != 0) || (errno != 0))
152 		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
153 			 "error:%d, errno:%d", error, errno);
154 
155 	/* Read the pending message on clnt_sk[0] that was received before
156 	 * SHUTDOWN call.
157 	 */
158 	test_recv(clnt_sk[0], msgbuf, 100, 0);
159 
160 	/* No more messages and the association is SHUTDOWN, should fail. */
161 	error = recv(clnt_sk[0], msgbuf, 100, 0);
162 	if ((error != -1) || (errno != ENOTCONN))
163 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_WR socket with no "
164 			 "messages error:%d, errno:%d", error, errno);
165 
166 	tst_resm(TPASS, "shutdown() with SHUT_WR flag - SUCCESS");
167 
168 	errno = 0;
169 
170 	/*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive*/
171 	test_shutdown(clnt_sk[1], SHUT_RD);
172 
173 	error = recv(clnt_sk[1], msgbuf, 100, 0);
174 	if ((error != 0) || (errno != 0))
175 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
176 			 "error:%d, errno:%d", error, errno);
177 
178 	/* Sending a message on SHUT_RD socket. */
179 	error = test_send(clnt_sk[1], message, strlen(message), 0);
180 	if (error < 0)
181 		tst_brkm(TBROK, tst_exit, "send on a SHUT_RD socket "
182 			 "error:%d, errno:%d", error, errno);
183 
184 	/* Receive the message sent on SHUT_RD socket. */
185 	test_recv(acpt_sk[1], msgbuf, 100, 0);
186 
187 	/* Send a message to the SHUT_RD socket. */
188 	test_send(acpt_sk[1], message, strlen(message), 0);
189 
190 	/* We should not receive the message as the socket is SHUT_RD */
191 	error = recv(clnt_sk[1], msgbuf, 100, 0);
192 	if ((error != 0) || (errno != 0))
193 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
194 			 "error:%d, errno:%d", error, errno);
195 
196 	tst_resm(TPASS, "shutdown() with SHUT_RD flag - SUCCESS");
197 
198 	/*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new
199 	receive/send*/
200         test_shutdown(clnt_sk[2], SHUT_RDWR);
201 
202 	error = recv(acpt_sk[2], msgbuf, 100, 0);
203 	if ((error != 0) || (errno != 0))
204 		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
205 			 "error:%d, errno:%d", error, errno);
206 
207 	error = recv(clnt_sk[2], msgbuf, 100, 0);
208 	if ((error != 0) || (errno != 0))
209 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
210 			 "error:%d, errno:%d", error, errno);
211 
212 	tst_resm(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS");
213 
214 	/*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error*/
215 	error = shutdown(sk, SHUT_RD);
216 	if ((error != -1) || (errno != ENOTCONN))
217 		tst_brkm(TBROK, tst_exit, "shutdown on an unconnected socket "
218 			 "error:%d, errno:%d", error, errno);
219 
220 	tst_resm(TPASS, "shutdown() on an unconnected socket - SUCCESS");
221 
222 	for (i = 0; i < MAX_CLIENTS; i++)
223 		close(clnt_sk[i]);
224 	for (i = 0; i < MAX_CLIENTS; i++)
225 		close(acpt_sk[i]);
226 
227 
228 	close(lstn_sk);
229 	close(sk);
230 
231 	return 0;
232 }
233