| 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 <[email protected]> |
| 34 | * |
| 35 | * Or submit a bug report through the following website: |
| 36 | * https://github.com/sctp/lksctp-tools/ |
| 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 <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 | |
| 56 | char *TCID = __FILE__; |
| 57 | int TST_TOTAL = 6; |
| 58 | int TST_CNT = 0; |
| 59 | |
| 60 | #define MAX_CLIENTS 10 |
| 61 | |
| 62 | int |
| 63 | main(int argc, char *argv[]) |
| 64 | { |
| 65 | int clnt_sk[MAX_CLIENTS], acpt_sk[MAX_CLIENTS],sk; |
| 66 | int lstn_sk; |
| 67 | struct sockaddr_in lstn_addr, acpt_addr; |
| 68 | socklen_t addrlen; |
| 69 | int error, i; |
| 70 | char *message = "hello, world!\n"; |
| 71 | char msgbuf[100]; |
| 72 | int pf_class; |
| 73 | int fd, err_no = 0; |
| 74 | char filename[21]; |
| 75 | |
| 76 | /* Rather than fflush() throughout the code, set stdout to |
| 77 | * be unbuffered. |
| 78 | */ |
| 79 | setvbuf(stdout, NULL, _IONBF, 0); |
| 80 | setvbuf(stderr, NULL, _IONBF, 0); |
| 81 | |
| 82 | /* Initialize the server and client addresses. */ |
| 83 | pf_class = PF_INET; |
| 84 | |
| 85 | lstn_addr.sin_family = AF_INET; |
| 86 | lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 87 | lstn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 88 | |
| 89 | sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 90 | lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 91 | |
| 92 | test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr)); |
| 93 | |
| 94 | test_listen(lstn_sk, MAX_CLIENTS); |
| 95 | |
| 96 | for (i = 0; i < MAX_CLIENTS; i++) { |
| 97 | clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 98 | test_connect(clnt_sk[i], (struct sockaddr *)&lstn_addr, |
| 99 | sizeof(lstn_addr)); |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < MAX_CLIENTS; i++) { |
| 103 | addrlen = sizeof(acpt_addr); |
| 104 | acpt_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr, |
| 105 | &addrlen); |
| 106 | } |
| 107 | |
| 108 | /*shutdown() TEST1: Bad socket descriptor, EBADF Expected error*/ |
| 109 | error = shutdown(-1, SHUT_WR); |
| 110 | if (error != -1 || errno != EBADF) |
| 111 | tst_brkm(TBROK, tst_exit, "shutdown with a bad socket " |
| 112 | "error:%d, errno:%d", error, errno); |
| 113 | |
| 114 | tst_resm(TPASS, "shutdown() with a bad socket descriptor - EBADF"); |
| 115 | |
| 116 | /*shutdown() TEST2: Invalid socket, ENOTSOCK Expected error*/ |
| 117 | strcpy(filename, "/tmp/sctptest.XXXXXX"); |
| 118 | fd = mkstemp(filename); |
| 119 | if (fd == -1) |
| 120 | tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s", |
| 121 | filename, strerror(errno)); |
| 122 | error = shutdown(fd, SHUT_WR); |
| 123 | if (error == -1) |
| 124 | err_no = errno; |
| 125 | close(fd); |
| 126 | unlink(filename); |
| 127 | if (error != -1 || err_no != ENOTSOCK) |
| 128 | tst_brkm(TBROK, tst_exit, "shutdown with an invalid socket " |
| 129 | "error:%d, errno:%d", error, err_no); |
| 130 | |
| 131 | tst_resm(TPASS, "shutdown() with an invalid socket - ENOTSOCK"); |
| 132 | |
| 133 | errno = 0; |
| 134 | /*Do a send first before doing shutdown*/ |
| 135 | test_send(acpt_sk[0], message, strlen(message), 0); |
| 136 | |
| 137 | /*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send*/ |
| 138 | error = shutdown(clnt_sk[0], SHUT_WR); |
| 139 | if (error < 0) |
| 140 | tst_brkm(TBROK, tst_exit, "shutdown with SHUT_WR flag " |
| 141 | "error:%d, errno:%d", error, errno); |
| 142 | |
| 143 | /* Reading on a socket that has received SHUTDOWN should return 0 |
| 144 | * indicating EOF. |
| 145 | */ |
| 146 | error = recv(acpt_sk[0], msgbuf, 100, 0); |
| 147 | if ((error != 0) || (errno != 0)) |
| 148 | tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket " |
| 149 | "error:%d, errno:%d", error, errno); |
| 150 | |
| 151 | /* Read the pending message on clnt_sk[0] that was received before |
| 152 | * SHUTDOWN call. |
| 153 | */ |
| 154 | test_recv(clnt_sk[0], msgbuf, 100, 0); |
| 155 | |
| 156 | /* No more messages and the association is SHUTDOWN, should fail. */ |
| 157 | error = recv(clnt_sk[0], msgbuf, 100, 0); |
| 158 | if ((error != -1) || (errno != ENOTCONN)) |
| 159 | tst_brkm(TBROK, tst_exit, "recv on a SHUT_WR socket with no " |
| 160 | "messages error:%d, errno:%d", error, errno); |
| 161 | |
| 162 | tst_resm(TPASS, "shutdown() with SHUT_WR flag - SUCCESS"); |
| 163 | |
| 164 | errno = 0; |
| 165 | |
| 166 | /*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive*/ |
| 167 | test_shutdown(clnt_sk[1], SHUT_RD); |
| 168 | |
| 169 | error = recv(clnt_sk[1], msgbuf, 100, 0); |
| 170 | if ((error != 0) || (errno != 0)) |
| 171 | tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket " |
| 172 | "error:%d, errno:%d", error, errno); |
| 173 | |
| 174 | /* Sending a message on SHUT_RD socket. */ |
| 175 | error = test_send(clnt_sk[1], message, strlen(message), 0); |
| 176 | if (error < 0) |
| 177 | tst_brkm(TBROK, tst_exit, "send on a SHUT_RD socket " |
| 178 | "error:%d, errno:%d", error, errno); |
| 179 | |
| 180 | /* Receive the message sent on SHUT_RD socket. */ |
| 181 | test_recv(acpt_sk[1], msgbuf, 100, 0); |
| 182 | |
| 183 | /* Send a message to the SHUT_RD socket. */ |
| 184 | test_send(acpt_sk[1], message, strlen(message), 0); |
| 185 | |
| 186 | /* We should not receive the message as the socket is SHUT_RD */ |
| 187 | error = recv(clnt_sk[1], msgbuf, 100, 0); |
| 188 | if ((error != 0) || (errno != 0)) |
| 189 | tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket " |
| 190 | "error:%d, errno:%d", error, errno); |
| 191 | |
| 192 | tst_resm(TPASS, "shutdown() with SHUT_RD flag - SUCCESS"); |
| 193 | |
| 194 | /*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new |
| 195 | receive/send*/ |
| 196 | test_shutdown(clnt_sk[2], SHUT_RDWR); |
| 197 | |
| 198 | error = recv(acpt_sk[2], msgbuf, 100, 0); |
| 199 | if ((error != 0) || (errno != 0)) |
| 200 | tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket " |
| 201 | "error:%d, errno:%d", error, errno); |
| 202 | |
| 203 | error = recv(clnt_sk[2], msgbuf, 100, 0); |
| 204 | if ((error != 0) || (errno != 0)) |
| 205 | tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket " |
| 206 | "error:%d, errno:%d", error, errno); |
| 207 | |
| 208 | tst_resm(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS"); |
| 209 | |
| 210 | /*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error*/ |
| 211 | error = shutdown(sk, SHUT_RD); |
| 212 | if ((error != -1) || (errno != ENOTCONN)) |
| 213 | tst_brkm(TBROK, tst_exit, "shutdown on an unconnected socket " |
| 214 | "error:%d, errno:%d", error, errno); |
| 215 | |
| 216 | tst_resm(TPASS, "shutdown() on an unconnected socket - SUCCESS"); |
| 217 | |
| 218 | for (i = 0; i < MAX_CLIENTS; i++) |
| 219 | close(clnt_sk[i]); |
| 220 | for (i = 0; i < MAX_CLIENTS; i++) |
| 221 | close(acpt_sk[i]); |
| 222 | |
| 223 | |
| 224 | close(lstn_sk); |
| 225 | close(sk); |
| 226 | |
| 227 | return 0; |
| 228 | } |