Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 1 | /* SCTP kernel Implementation |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003 Hewlett-Packard Development Company, L.P |
| 3 | * (C) Copyright IBM Corp. 2004 |
| 4 | * |
| 5 | * This test tests the events for 1-1 style sockets. |
| 6 | * |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 7 | * The SCTP implementation is free software; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 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 | * |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 13 | * The SCTP implementation is distributed in the hope that it |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 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 | * |
| 32 | * Any bugs reported given to us we will try to fix... any fixes shared will |
| 33 | * be incorporated into the next SCTP release. |
| 34 | * |
| 35 | */ |
| 36 | #include <stdio.h> |
| 37 | #include <unistd.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <sys/types.h> |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 40 | #include <sys/socket.h> /* needed by linux/sctp.h */ |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 41 | #include <sys/uio.h> |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 42 | #include <netinet/in.h> /* for sockaddr_in */ |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 43 | #include <errno.h> |
| 44 | #include <netinet/sctp.h> |
| 45 | #include <sctputil.h> |
| 46 | #include <string.h> |
| 47 | |
| 48 | char *TCID = __FILE__; |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 49 | int TST_TOTAL = 4; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 50 | int TST_CNT = 0; |
| 51 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 52 | int |
| 53 | main(int argc, char *argv[]) |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 54 | { |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 55 | int svr_sk, clt_sk,acpt_sk; |
| 56 | struct sockaddr_in svr_loop, clt_loop,acpt_loop; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 57 | struct iovec iov, out_iov; |
| 58 | struct msghdr inmessage, outmessage; |
| 59 | char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 60 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 61 | int error; |
| 62 | socklen_t len; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 63 | char *big_buffer; |
| 64 | struct sctp_event_subscribe event; |
| 65 | struct cmsghdr *cmsg; |
| 66 | struct sctp_sndrcvinfo *sinfo; |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 67 | char *message = "hello, world!\n"; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 68 | uint32_t ppid; |
| 69 | uint32_t stream; |
| 70 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 71 | /* Rather than fflush() throughout the code, set stdout to |
| 72 | * be unbuffered. |
| 73 | */ |
| 74 | setvbuf(stdout, NULL, _IONBF, 0); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 75 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 76 | /* Initialize the server and client addresses. */ |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 77 | svr_loop.sin_family = AF_INET; |
| 78 | svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 79 | svr_loop.sin_port = htons(SCTP_TESTPORT_1); |
| 80 | |
| 81 | clt_loop.sin_family = AF_INET; |
| 82 | clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 83 | clt_loop.sin_port = htons(SCTP_TESTPORT_1); |
| 84 | |
| 85 | /* Create and bind the server socket. */ |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 86 | svr_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 87 | test_bind(svr_sk, (struct sockaddr *) &svr_loop, sizeof(svr_loop)); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 88 | |
| 89 | /* Mark server socket as being able to accept new associations. */ |
| 90 | test_listen(svr_sk, 3); |
| 91 | |
| 92 | /* Create the client socket. */ |
| 93 | clt_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 94 | |
| 95 | event.sctp_data_io_event = 1; |
| 96 | event.sctp_association_event = 1; |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 97 | event.sctp_shutdown_event = 1; |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 98 | len = sizeof(struct sctp_event_subscribe); |
| 99 | test_setsockopt(svr_sk, SCTP_EVENTS, &event, len); |
| 100 | test_setsockopt(clt_sk, SCTP_EVENTS, &event, len); |
| 101 | |
| 102 | len = sizeof(struct sockaddr_in); |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 103 | test_connect(clt_sk, (struct sockaddr *) &clt_loop, len); |
| 104 | |
| 105 | acpt_sk = test_accept(svr_sk, (struct sockaddr *) &acpt_loop, &len); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 106 | |
| 107 | /* Build up a msghdr structure we can use for all sending. */ |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 108 | memset(&outmessage, 0, sizeof(outmessage)); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 109 | outmessage.msg_name = &svr_loop; |
| 110 | outmessage.msg_namelen = sizeof(svr_loop); |
| 111 | outmessage.msg_iov = &out_iov; |
| 112 | outmessage.msg_iovlen = 1; |
| 113 | outmessage.msg_control = outcmsg; |
| 114 | outmessage.msg_controllen = sizeof(outcmsg); |
| 115 | outmessage.msg_flags = 0; |
| 116 | |
| 117 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 118 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 119 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 120 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 121 | outmessage.msg_controllen = cmsg->cmsg_len; |
| 122 | sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 123 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 124 | ppid = rand(); /* Choose an arbitrary value. */ |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 125 | stream = 1; |
| 126 | |
| 127 | sinfo->sinfo_ppid = ppid; |
| 128 | sinfo->sinfo_stream = stream; |
| 129 | |
| 130 | outmessage.msg_iov->iov_base = message; |
| 131 | outmessage.msg_iov->iov_len = (strlen(message) + 1); |
| 132 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 133 | /* Send . This will create the association*/ |
| 134 | test_sendmsg(clt_sk, &outmessage, 0, strlen(message)+1); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 135 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 136 | memset(&inmessage, 0, sizeof(inmessage)); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 137 | /* NOW initialize inmessage with enough space for DATA... */ |
| 138 | big_buffer = malloc(REALLY_BIG); |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 139 | if (!big_buffer) { DUMP_CORE; } |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 140 | |
| 141 | /* Let's do a test to do a recvmsg when we are not listening and |
| 142 | * when we have no associations. |
| 143 | */ |
| 144 | iov.iov_base = big_buffer; |
| 145 | iov.iov_len = REALLY_BIG; |
| 146 | inmessage.msg_iov = &iov; |
| 147 | inmessage.msg_iovlen = 1; |
| 148 | inmessage.msg_control = incmsg; |
| 149 | inmessage.msg_controllen = sizeof(incmsg); |
| 150 | |
| 151 | error = test_recvmsg(clt_sk, &inmessage, MSG_WAITALL); |
| 152 | test_check_msg_notification(&inmessage, |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 153 | error, |
| 154 | sizeof(struct sctp_assoc_change), |
| 155 | SCTP_ASSOC_CHANGE, |
| 156 | SCTP_COMM_UP); |
| 157 | |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 158 | tst_resm(TPASS, "COMM_UP notification on client socket - SUCCESS"); |
| 159 | |
| 160 | error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL); |
| 161 | test_check_msg_notification(&inmessage, |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 162 | error, |
| 163 | sizeof(struct sctp_assoc_change), |
| 164 | SCTP_ASSOC_CHANGE, |
| 165 | SCTP_COMM_UP); |
| 166 | |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 167 | tst_resm(TPASS, "COMM_UP notification on server socket - SUCCESS"); |
| 168 | |
| 169 | inmessage.msg_control = incmsg; |
| 170 | inmessage.msg_controllen = sizeof(incmsg); |
| 171 | error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL); |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 172 | test_check_msg_data(&inmessage, error, strlen(message) + 1, |
| 173 | MSG_EOR, stream, ppid); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 174 | |
| 175 | tst_resm(TPASS, "Data message on server socket - SUCCESS"); |
| 176 | |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 177 | close(clt_sk); |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 178 | error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL); |
| 179 | test_check_msg_notification(&inmessage, |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 180 | error, |
| 181 | sizeof(struct sctp_shutdown_event), |
| 182 | SCTP_SHUTDOWN_EVENT, |
| 183 | 0); |
mridge | 64a341f | 2006-01-03 19:16:46 +0000 | [diff] [blame] | 184 | |
| 185 | tst_resm(TPASS, "SHUTDOWN notification on accepted socket - SUCCESS"); |
| 186 | close(svr_sk); |
mridge | 5ba96af | 2004-02-23 16:17:34 +0000 | [diff] [blame] | 187 | close(acpt_sk); |
| 188 | |
Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 189 | return 0; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 190 | } |