| 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 <[email protected]> |
| 35 | * |
| 36 | * Or submit a bug report through the following website: |
| 37 | * https://github.com/sctp/lksctp-tools/ |
| 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>cxx . . $ `<file>/src/include/netinet/sctp.h`/ |
| 55 | #include <sys/uio.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <sctputil.h>cxx . . $ `<file>/src/testlib/sctputil.h`/ |
| 58 | |
| 59 | char *TCIDcxx . . $ TCID. = __FILE__; |
| 60 | int TST_TOTALcxx . . $ TST_TOTAL. = 5; |
| 61 | int TST_CNTcxx . . $ TST_CNT. = 0; |
| 62 | |
| 63 | int |
| 64 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 65 | { |
| 66 | int error,msg_count; |
| 67 | socklen_tcxx . . $ socklen_t# len; |
| 68 | int sk,pf_class,lstn_sk,acpt_sk,flag,cflag,sflag; |
| 69 | struct msghdrcxx . . $ msghdr# outmessage; |
| 70 | struct msghdrcxx . . $ msghdr# inmessage; |
| 71 | char *message = "hello, world!\n"; |
| 72 | struct ioveccxx . . $ iovec# iov_rcv; |
| 73 | struct sctp_sndrcvinfocxx . . $ sctp_sndrcvinfo# *sinfo; |
| 74 | char outcmsg[CMSG_SPACEcxx . . $ `<external>/8962547315054503424/socket.h:309:9`!(sizeof(struct sctp_sndrcvinfo))]; |
| 75 | struct cmsghdrcxx . . $ cmsghdr# *cmsg; |
| 76 | struct ioveccxx . . $ iovec# out_iov; |
| 77 | char * buffer_rcv; |
| 78 | char incmsg[CMSG_SPACEcxx . . $ `<external>/8962547315054503424/socket.h:309:9`!(sizeof(sctp_cmsg_data_t))]; |
| 79 | |
| 80 | struct sockaddr_incxx . . $ sockaddr_in# conn_addr,lstn_addr,svr_addr; |
| 81 | |
| 82 | /* Rather than fflush() throughout the code, set stdout to |
| 83 | * be unbufferd |
| 84 | */ |
| 85 | setvbufcxx . . $ setvbuf(4da3ba8c983f31bc).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, _IONBFcxx . . $ `<external>/232824719582829767/stdio.h:100:9`!, 0); |
| 86 | setvbufcxx . . $ setvbuf(4da3ba8c983f31bc).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, _IONBFcxx . . $ `<external>/232824719582829767/stdio.h:100:9`!, 0); |
| 87 | |
| 88 | pf_class = PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!; |
| 89 | |
| 90 | sk = test_socketcxx . . $ test_socket(2326284602b0bb2d).(pf_class, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!); |
| 91 | |
| 92 | lstn_sk = test_socketcxx . . $ test_socket(2326284602b0bb2d).(pf_class, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!); |
| 93 | |
| 94 | conn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 95 | conn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 96 | conn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 97 | |
| 98 | lstn_addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 99 | lstn_addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = SCTP_IP_LOOPBACKcxx . . $ `src/testlib/sctputil.h:76:9`!; |
| 100 | lstn_addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(SCTP_TESTPORT_1cxx . . $ `src/testlib/sctputil.h:71:9`!); |
| 101 | |
| 102 | /*Binding the listen socket*/ |
| 103 | test_bindcxx . . $ test_bind(18e507052d945257).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *) &lstn_addr, sizeof(lstn_addr)); |
| 104 | |
| 105 | /*Listening the socket*/ |
| 106 | test_listencxx . . $ test_listen(9b79fb6aee4c0440).(lstn_sk, 10); |
| 107 | |
| 108 | len = sizeof(struct sockaddr_incxx . . $ sockaddr_in#); |
| 109 | flag = MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!; |
| 110 | |
| 111 | /*Setting server socket non-blocking*/ |
| 112 | sflag = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(lstn_sk, F_GETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:168:9`!, 0); |
| 113 | if (sflag < 0) |
| 114 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "fcnt F_GETFL failed " |
| 115 | "sflag:%d, errno:%d", sflag, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 116 | |
| 117 | error = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(lstn_sk, F_SETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:169:9`!, sflag | O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!); |
| 118 | if (error < 0) |
| 119 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "fcnt F_SETFL failed " |
| 120 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 121 | |
| 122 | /* TEST1: accept should return EAGAIN instead blocking. */ |
| 123 | error = acceptcxx . . $ accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 124 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!) |
| 125 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "non-blocking accept " |
| 126 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 127 | |
| 128 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "non-blocking accept() - EAGAIN"); |
| 129 | |
| 130 | /* TEST2: Non Block connect should return EINPROGRESS */ |
| 131 | /*Set client socket as non-blocking*/ |
| 132 | cflag = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(sk, F_GETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:168:9`!, 0); |
| 133 | if (cflag < 0) |
| 134 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "fcnt F_GETFL failed " |
| 135 | "cflag:%d, errno:%d", cflag, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 136 | |
| 137 | error = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(sk, F_SETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:169:9`!, sflag | O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!); |
| 138 | if (error < 0) |
| 139 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "fcnt F_SETFL failed " |
| 140 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 141 | |
| 142 | error = connectcxx . . $ connect(1ce9971e209c3c95).(sk, (const struct sockaddrcxx . . $ sockaddr# *) &conn_addr, len); |
| 143 | if (error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINPROGRESScxx . . $ `<external>/17922322188510340118/errno.h:99:9`!) |
| 144 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "non-blocking connect " |
| 145 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 146 | |
| 147 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "non-blocking connect() - EINPROGRESS"); |
| 148 | |
| 149 | /* TEST3: Now that connect() called, accept will succeed */ |
| 150 | acpt_sk = acceptcxx . . $ accept(46fcd054f4a4d98b).(lstn_sk, (struct sockaddrcxx . . $ sockaddr# *)&svr_addr, &len); |
| 151 | if (acpt_sk < 0) |
| 152 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "accept after a non-blocking connect " |
| 153 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 154 | |
| 155 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "accept() after a non-blocking connect - SUCCESS"); |
| 156 | |
| 157 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&outmessage, 0, sizeof(outmessage)); |
| 158 | outmessage.msg_namecxx . . $ msghdr#msg_name. = &svr_addr; |
| 159 | outmessage.msg_namelencxx . . $ msghdr#msg_namelen. = sizeof(svr_addr); |
| 160 | outmessage.msg_iovcxx . . $ msghdr#msg_iov. = &out_iov; |
| 161 | outmessage.msg_iovlencxx . . $ msghdr#msg_iovlen. = 1; |
| 162 | outmessage.msg_controlcxx . . $ msghdr#msg_control. = outcmsg; |
| 163 | outmessage.msg_controllencxx . . $ msghdr#msg_controllen. = sizeof(outcmsg); |
| 164 | outmessage.msg_flagscxx . . $ msghdr#msg_flags. = 0; |
| 165 | |
| 166 | cmsg = CMSG_FIRSTHDRcxx . . $ `<external>/8962547315054503424/socket.h:304:9`!(&outmessage); |
| 167 | cmsg->cmsg_levelcxx . . $ cmsghdr#cmsg_level. = IPPROTO_SCTPcxx . . $ `<external>/9581057695539925927/in.h:87:9`!; |
| 168 | cmsg->cmsg_typecxx . . $ cmsghdr#cmsg_type. = SCTP_SNDRCVcxx . . $ `<external>/17169092326581286071/sctp.h:333:9`!; |
| 169 | cmsg->cmsg_lencxx . . $ cmsghdr#cmsg_len. = CMSG_LENcxx . . $ `<external>/8962547315054503424/socket.h:311:9`!(sizeof(struct sctp_sndrcvinfo)); |
| 170 | outmessage.msg_controllencxx . . $ msghdr#msg_controllen. = cmsg->cmsg_lencxx . . $ cmsghdr#cmsg_len.; |
| 171 | sinfo = (struct sctp_sndrcvinfocxx . . $ sctp_sndrcvinfo# *)CMSG_DATAcxx . . $ `<external>/8962547315054503424/socket.h:299:10`!(cmsg); |
| 172 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(sinfo, 0x00, sizeof(struct sctp_sndrcvinfocxx . . $ sctp_sndrcvinfo#)); |
| 173 | |
| 174 | outmessage.msg_iovcxx . . $ msghdr#msg_iov.->iov_basecxx . . $ iovec#iov_base. = message; |
| 175 | outmessage.msg_iovcxx . . $ msghdr#msg_iov.->iov_lencxx . . $ iovec#iov_len. = strlencxx . . $ strlen(751346f8406fd082).(message) + 1; |
| 176 | |
| 177 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&inmessage, 0, sizeof(inmessage)); |
| 178 | buffer_rcv = test_malloccxx . . $ test_malloc(bfcfa4d6b7f7ef64).(REALLY_BIGcxx . . $ `src/testlib/sctputil.h:65:9`!); |
| 179 | |
| 180 | iov_rcv.iov_basecxx . . $ iovec#iov_base. = buffer_rcv; |
| 181 | iov_rcv.iov_lencxx . . $ iovec#iov_len. = REALLY_BIGcxx . . $ `src/testlib/sctputil.h:65:9`!; |
| 182 | inmessage.msg_iovcxx . . $ msghdr#msg_iov. = &iov_rcv; |
| 183 | inmessage.msg_iovlencxx . . $ msghdr#msg_iovlen. = 1; |
| 184 | inmessage.msg_controlcxx . . $ msghdr#msg_control. = incmsg; |
| 185 | inmessage.msg_controllencxx . . $ msghdr#msg_controllen. = sizeof(incmsg); |
| 186 | |
| 187 | msg_count = strlencxx . . $ strlen(751346f8406fd082).(message) + 1; |
| 188 | |
| 189 | /* TEST4: recvmsg() should return EAGAIN instead blocking */ |
| 190 | error = recvmsgcxx . . $ recvmsg(616999ca12de9af7).(sk, &inmessage, MSG_WAITALLcxx . . $ `<external>/8962547315054503424/socket.h:230:9`!); |
| 191 | if ( error != -1 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!) |
| 192 | tst_brkmcxx . . $ `src/testlib/sctputil.h:116:9`!(TBROK, tst_exit, "non-blocking recvmsg " |
| 193 | "error:%d, errno:%d", error, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 194 | |
| 195 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "non-blocking recvmsg() - EAGAIN"); |
| 196 | |
| 197 | test_sendmsgcxx . . $ test_sendmsg(c45a39c477f473e).(acpt_sk, &outmessage, flag, msg_count); |
| 198 | |
| 199 | /* TEST5: recvmsg() should succeed now as data is available. */ |
| 200 | error = test_recvmsgcxx . . $ test_recvmsg(2284ea468be03a33).(sk, &inmessage, flag); |
| 201 | test_check_msg_datacxx . . $ test_check_msg_data(87a4c472fd2f0d28).(&inmessage, error, msg_count, MSG_EORcxx . . $ `<external>/8962547315054503424/socket.h:228:9`!, 0, 0); |
| 202 | |
| 203 | tst_resmcxx . . $ `src/testlib/sctputil.h:123:9`!(TPASS, "non-blocking recvmsg() when data is available - " |
| 204 | "SUCCESS"); |
| 205 | |
| 206 | closecxx . . $ close(7864480464b09eea).(lstn_sk); |
| 207 | closecxx . . $ close(7864480464b09eea).(acpt_sk); |
| 208 | |
| 209 | freecxx . . $ free(5d22bdacc48458e8).(buffer_rcv); |
| 210 | return 0; |
| 211 | } |