| 1 |
/* SCTP kernel Implementation |
| 2 |
* (C) Copyright IBM Corp. 2001, 2003 |
| 3 |
* Copyright (c) 1999-2000 Cisco, Inc. |
| 4 |
* Copyright (c) 1999-2001 Motorola, Inc. |
| 5 |
* Copyright (c) 2001 Intel Corp. |
| 6 |
* Copyright (c) 2001 Nokia, Inc. |
| 7 |
* |
| 8 |
* The SCTP implementation is free software; |
| 9 |
* you can redistribute it and/or modify it under the terms of |
| 10 |
* the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* The SCTP implementation is distributed in the hope that it |
| 15 |
* will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 |
* ************************ |
| 17 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* See the GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with GNU CC; see the file COPYING. If not, write to |
| 22 |
* the Free Software Foundation, 59 Temple Place - Suite 330, |
| 23 |
* Boston, MA 02111-1307, USA. |
| 24 |
* |
| 25 |
* Please send any bug reports or fixes you make to the |
| 26 |
* email address(es): |
| 27 |
* lksctp developers <[email protected]> |
| 28 |
* |
| 29 |
* Or submit a bug report through the following website: |
| 30 |
* https://github.com/sctp/lksctp-tools/ |
| 31 |
* |
| 32 |
* Any bugs reported to us we will try to fix... any fixes shared will |
| 33 |
* be incorporated into the next SCTP release. |
| 34 |
* |
| 35 |
* Written or modified by: |
| 36 |
* Sridhar Samudrala <[email protected]> |
| 37 |
*/ |
| 38 |
|
| 39 |
/* This is a functional test to verify the graceful shutdown of an |
| 40 |
* association. |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <unistd.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
#include <sys/types.h> |
| 48 |
#include <sys/socket.h> |
| 49 |
#include <sys/uio.h> |
| 50 |
#include <netinet/in.h> |
| 51 |
#include <errno.h> |
| 52 |
#include <netinet/sctp.h> |
| 53 |
#include <sctputil.h> |
| 54 |
|
| 55 |
char *TCID = __FILE__; |
| 56 |
int TST_TOTAL = 1; |
| 57 |
int TST_CNT = 0; |
| 58 |
|
| 59 |
#define MAX_CLIENTS 10 |
| 60 |
|
| 61 |
int |
| 62 |
main(int argc, char *argv[]) |
| 63 |
{ |
| 64 |
int svr_sk, clt_sk[MAX_CLIENTS]; |
| 65 |
sctp_assoc_t svr_associd[MAX_CLIENTS]; |
| 66 |
sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS]; |
| 67 |
struct iovec iov; |
| 68 |
struct msghdr inmessage; |
| 69 |
struct msghdr outmessage; |
| 70 |
char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 71 |
char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 72 |
struct cmsghdr *cmsg; |
| 73 |
struct sctp_sndrcvinfo *sinfo; |
| 74 |
struct iovec out_iov; |
| 75 |
int error; |
| 76 |
uint32_t ppid; |
| 77 |
uint32_t stream; |
| 78 |
struct sctp_assoc_change *sac; |
| 79 |
char *big_buffer; |
| 80 |
int i; |
| 81 |
char *message = "hello, world!\n"; |
| 82 |
struct sctp_status status; |
| 83 |
socklen_t status_len; |
| 84 |
|
| 85 |
/* Rather than fflush() throughout the code, set stdout to |
| 86 |
* be unbuffered. |
| 87 |
*/ |
| 88 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 89 |
|
| 90 |
/* Create and bind the server socket. */ |
| 91 |
svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 92 |
|
| 93 |
svr_loop.v4.sin_family = AF_INET; |
| 94 |
svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 95 |
svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1); |
| 96 |
test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop)); |
| 97 |
|
| 98 |
/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ |
| 99 |
test_enable_assoc_change(svr_sk); |
| 100 |
|
| 101 |
/* Mark server socket as being able to accept new associations. */ |
| 102 |
test_listen(svr_sk, 1); |
| 103 |
|
| 104 |
/* Create and bind all the client sockets. */ |
| 105 |
for (i = 0; i < MAX_CLIENTS; i++) { |
| 106 |
clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 107 |
|
| 108 |
clt_loop[i].v4.sin_family = AF_INET; |
| 109 |
clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 110 |
clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i); |
| 111 |
test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i])); |
| 112 |
|
| 113 |
test_enable_assoc_change(clt_sk[i]); |
| 114 |
} |
| 115 |
|
| 116 |
/* Build up a msghdr structure we can use for all sending. */ |
| 117 |
outmessage.msg_name = &svr_loop; |
| 118 |
outmessage.msg_namelen = sizeof(svr_loop); |
| 119 |
outmessage.msg_iov = &out_iov; |
| 120 |
outmessage.msg_iovlen = 1; |
| 121 |
outmessage.msg_control = outcmsg; |
| 122 |
outmessage.msg_controllen = sizeof(outcmsg); |
| 123 |
outmessage.msg_flags = 0; |
| 124 |
cmsg = CMSG_FIRSTHDR(&outmessage); |
| 125 |
cmsg->cmsg_level = IPPROTO_SCTP; |
| 126 |
cmsg->cmsg_type = SCTP_SNDRCV; |
| 127 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 128 |
outmessage.msg_controllen = cmsg->cmsg_len; |
| 129 |
sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 130 |
memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 131 |
ppid = rand(); /* Choose an arbitrary value. */ |
| 132 |
stream = 1; |
| 133 |
sinfo->sinfo_ppid = ppid; |
| 134 |
sinfo->sinfo_stream = stream; |
| 135 |
out_iov.iov_base = message; |
| 136 |
out_iov.iov_len = strlen(message) + 1; |
| 137 |
|
| 138 |
/* Send the first message from all the clients to the server. This |
| 139 |
* will create the associations. |
| 140 |
*/ |
| 141 |
for (i = 0; i < MAX_CLIENTS; i++) |
| 142 |
test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message)+1); |
| 143 |
|
| 144 |
/* Initialize inmessage for all receives. */ |
| 145 |
big_buffer = test_malloc(REALLY_BIG); |
| 146 |
memset(&inmessage, 0, sizeof(inmessage)); |
| 147 |
iov.iov_base = big_buffer; |
| 148 |
iov.iov_len = REALLY_BIG; |
| 149 |
inmessage.msg_iov = &iov; |
| 150 |
inmessage.msg_iovlen = 1; |
| 151 |
inmessage.msg_control = incmsg; |
| 152 |
|
| 153 |
/* Get the communication up message on all client sockets. */ |
| 154 |
for (i = 0; i < MAX_CLIENTS; i++) { |
| 155 |
inmessage.msg_controllen = sizeof(incmsg); |
| 156 |
error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL); |
| 157 |
test_check_msg_notification(&inmessage, error, |
| 158 |
sizeof(struct sctp_assoc_change), |
| 159 |
SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 160 |
} |
| 161 |
|
| 162 |
/* Get the communication up message and the data message on the |
| 163 |
* server sockets for all the clients. |
| 164 |
*/ |
| 165 |
for (i = 0; i < MAX_CLIENTS; i++) { |
| 166 |
inmessage.msg_controllen = sizeof(incmsg); |
| 167 |
error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL); |
| 168 |
test_check_msg_notification(&inmessage, error, |
| 169 |
sizeof(struct sctp_assoc_change), |
| 170 |
SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 171 |
|
| 172 |
inmessage.msg_controllen = sizeof(incmsg); |
| 173 |
error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL); |
| 174 |
test_check_msg_data(&inmessage, error, strlen(message)+1, |
| 175 |
MSG_EOR, stream, ppid); |
| 176 |
sac = (struct sctp_assoc_change *)iov.iov_base; |
| 177 |
svr_associd[i] = sac->sac_assoc_id; |
| 178 |
} |
| 179 |
|
| 180 |
/* Build up a msghdr structure we can use for all sending. */ |
| 181 |
outmessage.msg_name = NULL; |
| 182 |
outmessage.msg_namelen = 0; |
| 183 |
outmessage.msg_iov = NULL; |
| 184 |
outmessage.msg_iovlen = 0; |
| 185 |
outmessage.msg_control = outcmsg; |
| 186 |
outmessage.msg_controllen = sizeof(outcmsg); |
| 187 |
outmessage.msg_flags = 0; |
| 188 |
cmsg = CMSG_FIRSTHDR(&outmessage); |
| 189 |
cmsg->cmsg_level = IPPROTO_SCTP; |
| 190 |
cmsg->cmsg_type = SCTP_SNDRCV; |
| 191 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 192 |
outmessage.msg_controllen = cmsg->cmsg_len; |
| 193 |
sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 194 |
memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 195 |
sinfo->sinfo_flags |= SCTP_EOF; |
| 196 |
|
| 197 |
/* Shutdown all the associations of the server socket in a loop. */ |
| 198 |
for (i = 0; i < MAX_CLIENTS; i++) { |
| 199 |
sinfo->sinfo_assoc_id = svr_associd[i]; |
| 200 |
|
| 201 |
/* Verify that the association is present. */ |
| 202 |
memset(&status, 0, sizeof(struct sctp_status)); |
| 203 |
status.sstat_assoc_id = sinfo->sinfo_assoc_id; |
| 204 |
status_len = sizeof(struct sctp_status); |
| 205 |
error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS, |
| 206 |
&status, &status_len); |
| 207 |
if (error) |
| 208 |
tst_brkm(TBROK, tst_exit, |
| 209 |
"getsockopt(SCTP_STATUS): %s", |
| 210 |
strerror(errno)); |
| 211 |
|
| 212 |
/* Call sendmsg() to shutdown the association. */ |
| 213 |
test_sendmsg(svr_sk, &outmessage, 0, 0); |
| 214 |
|
| 215 |
/* Verify that the association is no longer present. */ |
| 216 |
memset(&status, 0, sizeof(struct sctp_status)); |
| 217 |
status.sstat_assoc_id = sinfo->sinfo_assoc_id; |
| 218 |
status_len = sizeof(struct sctp_status); |
| 219 |
error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS, |
| 220 |
&status, &status_len); |
| 221 |
if ((error != -1) && (errno != EINVAL)) |
| 222 |
tst_brkm(TBROK, tst_exit, |
| 223 |
"getsockopt(SCTP_STATUS) " |
| 224 |
"error:%d errno:%d", error, errno); |
| 225 |
} |
| 226 |
|
| 227 |
close(svr_sk); |
| 228 |
|
| 229 |
/* Get the shutdown complete notification. */ |
| 230 |
for (i = 0; i < MAX_CLIENTS; i++) { |
| 231 |
inmessage.msg_controllen = sizeof(incmsg); |
| 232 |
error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL); |
| 233 |
test_check_msg_notification(&inmessage, error, |
| 234 |
sizeof(struct sctp_assoc_change), |
| 235 |
SCTP_ASSOC_CHANGE, |
| 236 |
SCTP_SHUTDOWN_COMP); |
| 237 |
|
| 238 |
close(clt_sk[i]); |
| 239 |
} |
| 240 |
|
| 241 |
tst_resm(TPASS, "Graceful shutdown of associations using SCTP_EOF"); |
| 242 |
|
| 243 |
free(big_buffer); |
| 244 |
/* Indicate successful completion. */ |
| 245 |
return 0; |
| 246 |
} |