Simon Xu | 6f22494 | 2013-12-06 11:51:16 +0800 | [diff] [blame] | 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 Non-Blocking mode of connect(), |
| 6 | * accept() and recvmsg() calls. |
| 7 | * |
| 8 | * TEST1: Non blocking accept return EAGAIN if connect is not called |
| 9 | * TEST2: Non blocking connect should return EINPROGRESS |
| 10 | * TEST3: accept() passes when connect called in Non-blocking mode |
| 11 | * TEST4: Non blocking recvmsg should return EAGAIN |
| 12 | * TEST5: recvmsg() should succeed if data present to receive in non blocking |
| 13 | * mode |
| 14 | * |
| 15 | * The SCTP implementation is free software; |
| 16 | * you can redistribute it and/or modify it under the terms of |
| 17 | * the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2, or (at your option) |
| 19 | * any later version. |
| 20 | * |
| 21 | * The SCTP implementation is distributed in the hope that it |
| 22 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 23 | * ************************ |
| 24 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 25 | * See the GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with GNU CC; see the file COPYING. If not, write to |
| 29 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
| 30 | * Boston, MA 02111-1307, USA. |
| 31 | * |
| 32 | * Please send any bug reports or fixes you make to the |
| 33 | * email address(es): |
| 34 | * lksctp developers <lksctp-developers@lists.sourceforge.net> |
| 35 | * |
| 36 | * Or submit a bug report through the following website: |
| 37 | * http://www.sf.net/projects/lksctp |
| 38 | * |
| 39 | * Any bugs reported given to us we will try to fix... any fixes shared will |
| 40 | * be incorporated into the next SCTP release |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <unistd.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/socket.h> |
| 51 | #include <netinet/in.h> /* for sockaddr_in */ |
| 52 | #include <arpa/inet.h> |
| 53 | #include <errno.h> |
| 54 | #include <netinet/sctp.h> |
| 55 | #include <sys/uio.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <sctputil.h> |
| 58 | |
| 59 | char *TCID = __FILE__; |
| 60 | int TST_TOTAL = 5; |
| 61 | int TST_CNT = 0; |
| 62 | |
| 63 | int |
| 64 | main(int argc, char *argv[]) |
| 65 | { |
| 66 | int error,msg_count; |
| 67 | socklen_t len; |
| 68 | int sk,pf_class,lstn_sk,acpt_sk,flag,cflag,sflag; |
| 69 | struct msghdr outmessage; |
| 70 | struct msghdr inmessage; |
| 71 | char *message = "hello, world!\n"; |
| 72 | struct iovec iov_rcv; |
| 73 | struct sctp_sndrcvinfo *sinfo; |
| 74 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 75 | struct cmsghdr *cmsg; |
| 76 | struct iovec out_iov; |
| 77 | char * buffer_rcv; |
| 78 | char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 79 | |
| 80 | struct sockaddr_in conn_addr,lstn_addr,svr_addr; |
| 81 | |
| 82 | /* Rather than fflush() throughout the code, set stdout to |
| 83 | * be unbufferd |
| 84 | */ |
| 85 | setvbuf(stdout, NULL, _IONBF, 0); |
| 86 | setvbuf(stderr, NULL, _IONBF, 0); |
| 87 | |
| 88 | pf_class = PF_INET; |
| 89 | |
| 90 | sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 91 | |
| 92 | lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 93 | |
| 94 | conn_addr.sin_family = AF_INET; |
| 95 | conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 96 | conn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 97 | |
| 98 | lstn_addr.sin_family = AF_INET; |
| 99 | lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 100 | lstn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 101 | |
| 102 | /*Binding the listen socket*/ |
| 103 | test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr)); |
| 104 | |
| 105 | /*Listening the socket*/ |
| 106 | test_listen(lstn_sk, 10); |
| 107 | |
| 108 | len = sizeof(struct sockaddr_in); |
| 109 | flag = MSG_NOSIGNAL; |
| 110 | |
| 111 | /*Setting server socket non-blocking*/ |
| 112 | sflag = fcntl(lstn_sk, F_GETFL, 0); |
| 113 | if (sflag < 0) |
| 114 | tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed " |
| 115 | "sflag:%d, errno:%d", sflag, errno); |
| 116 | |
| 117 | error = fcntl(lstn_sk, F_SETFL, sflag | O_NONBLOCK); |
| 118 | if (error < 0) |
| 119 | tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed " |
| 120 | "error:%d, errno:%d", error, errno); |
| 121 | |
| 122 | /* TEST1: accept should return EAGAIN instead blocking. */ |
| 123 | error = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len); |
| 124 | if (error != -1 || errno != EAGAIN) |
| 125 | tst_brkm(TBROK, tst_exit, "non-blocking accept " |
| 126 | "error:%d, errno:%d", error, errno); |
| 127 | |
| 128 | tst_resm(TPASS, "non-blocking accept() - EAGAIN"); |
| 129 | |
| 130 | /* TEST2: Non Block connect should return EINPROGRESS */ |
| 131 | /*Set client socket as non-blocking*/ |
| 132 | cflag = fcntl(sk, F_GETFL, 0); |
| 133 | if (cflag < 0) |
| 134 | tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed " |
| 135 | "cflag:%d, errno:%d", cflag, errno); |
| 136 | |
| 137 | error = fcntl(sk, F_SETFL, sflag | O_NONBLOCK); |
| 138 | if (error < 0) |
| 139 | tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed " |
| 140 | "error:%d, errno:%d", error, errno); |
| 141 | |
| 142 | error = connect(sk, (const struct sockaddr *) &conn_addr, len); |
| 143 | if (error != -1 || errno != EINPROGRESS) |
| 144 | tst_brkm(TBROK, tst_exit, "non-blocking connect " |
| 145 | "error:%d, errno:%d", error, errno); |
| 146 | |
| 147 | tst_resm(TPASS, "non-blocking connect() - EINPROGRESS"); |
| 148 | |
| 149 | /* TEST3: Now that connect() called, accept will succeed */ |
| 150 | acpt_sk = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len); |
| 151 | if (acpt_sk < 0) |
| 152 | tst_brkm(TBROK, tst_exit, "accept after a non-blocking connect " |
| 153 | "error:%d, errno:%d", error, errno); |
| 154 | |
| 155 | tst_resm(TPASS, "accept() after a non-blocking connect - SUCCESS"); |
| 156 | |
| 157 | memset(&outmessage, 0, sizeof(outmessage)); |
| 158 | outmessage.msg_name = &svr_addr; |
| 159 | outmessage.msg_namelen = sizeof(svr_addr); |
| 160 | outmessage.msg_iov = &out_iov; |
| 161 | outmessage.msg_iovlen = 1; |
| 162 | outmessage.msg_control = outcmsg; |
| 163 | outmessage.msg_controllen = sizeof(outcmsg); |
| 164 | outmessage.msg_flags = 0; |
| 165 | |
| 166 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 167 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 168 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 169 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 170 | outmessage.msg_controllen = cmsg->cmsg_len; |
| 171 | sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 172 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 173 | |
| 174 | outmessage.msg_iov->iov_base = message; |
| 175 | outmessage.msg_iov->iov_len = strlen(message) + 1; |
| 176 | |
| 177 | memset(&inmessage, 0, sizeof(inmessage)); |
| 178 | buffer_rcv = malloc(REALLY_BIG); |
| 179 | |
| 180 | iov_rcv.iov_base = buffer_rcv; |
| 181 | iov_rcv.iov_len = REALLY_BIG; |
| 182 | inmessage.msg_iov = &iov_rcv; |
| 183 | inmessage.msg_iovlen = 1; |
| 184 | inmessage.msg_control = incmsg; |
| 185 | inmessage.msg_controllen = sizeof(incmsg); |
| 186 | |
| 187 | msg_count = strlen(message) + 1; |
| 188 | |
| 189 | /* TEST4: recvmsg() should return EAGAIN instead blocking */ |
| 190 | error = recvmsg(sk, &inmessage, MSG_WAITALL); |
| 191 | if ( error != -1 || errno != EAGAIN) |
| 192 | tst_brkm(TBROK, tst_exit, "non-blocking recvmsg " |
| 193 | "error:%d, errno:%d", error, errno); |
| 194 | |
| 195 | tst_resm(TPASS, "non-blocking recvmsg() - EAGAIN"); |
| 196 | |
| 197 | test_sendmsg(acpt_sk, &outmessage, flag, msg_count); |
| 198 | |
| 199 | /* TEST5: recvmsg() should succeed now as data is available. */ |
| 200 | error = test_recvmsg(sk, &inmessage, flag); |
| 201 | test_check_msg_data(&inmessage, error, msg_count, MSG_EOR, 0, 0); |
| 202 | |
| 203 | tst_resm(TPASS, "non-blocking recvmsg() when data is available - " |
| 204 | "SUCCESS"); |
| 205 | |
| 206 | close(lstn_sk); |
| 207 | close(acpt_sk); |
| 208 | return 0; |
| 209 | } |