| 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 recvfrom () call for 1-1 style sockets |
| 6 | * |
| 7 | * TEST1: Bad socket descriptor |
| 8 | * TEST2: Invalid socket |
| 9 | * TEST3: Invalid message pointer |
| 10 | * TEST4: On a listening socket |
| 11 | * TEST5: Reading on a socket that received SHUTDOWN |
| 12 | * TEST6: Reading the pending message on socket that received SHUTDOWN |
| 13 | * TEST7: No more message and association is shutdown |
| 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> |
| 55 | #include <sys/uio.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <sctputil.h> |
| 58 | |
| 59 | char *TCID = __FILE__; |
| 60 | int TST_TOTAL = 7; |
| 61 | int TST_CNT = 0; |
| 62 | |
| 63 | int |
| 64 | main(int argc, char *argv[]) |
| 65 | { |
| 66 | int msg_count; |
| 67 | socklen_t len; |
| 68 | int sk,pf_class,lstn_sk,acpt_sk, flag; |
| 69 | char *message = "hello, world!\n"; |
| 70 | char *message_rcv; |
| 71 | int count; |
| 72 | int fd, err_no = 0; |
| 73 | char filename[21]; |
| 74 | |
| 75 | struct sockaddr_in conn_addr,lstn_addr,svr_addr; |
| 76 | |
| 77 | /* Rather than fflush() throughout the code, set stdout to |
| 78 | * be unbuffered. |
| 79 | */ |
| 80 | setvbuf(stdout, NULL, _IONBF, 0); |
| 81 | setvbuf(stderr, NULL, _IONBF, 0); |
| 82 | |
| 83 | message_rcv = test_malloc(512); |
| 84 | |
| 85 | pf_class = PF_INET; |
| 86 | |
| 87 | sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 88 | |
| 89 | lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); |
| 90 | |
| 91 | conn_addr.sin_family = AF_INET; |
| 92 | conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 93 | conn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 94 | |
| 95 | lstn_addr.sin_family = AF_INET; |
| 96 | lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 97 | lstn_addr.sin_port = htons(SCTP_TESTPORT_1); |
| 98 | |
| 99 | /*Binding the listen socket*/ |
| 100 | test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr)); |
| 101 | |
| 102 | /*Listening the socket*/ |
| 103 | test_listen(lstn_sk, 10); |
| 104 | |
| 105 | len = sizeof(struct sockaddr_in); |
| 106 | |
| 107 | test_connect(sk, (struct sockaddr *) &conn_addr, len); |
| 108 | |
| 109 | acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len); |
| 110 | |
| 111 | msg_count = (strlen(message) + 1); |
| 112 | |
| 113 | flag = MSG_NOSIGNAL; |
| 114 | /*Sending the message*/ |
| 115 | count = test_send(sk, message, msg_count, flag); |
| 116 | |
| 117 | /*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/ |
| 118 | count = recvfrom(-1, message_rcv, msg_count, flag, |
| 119 | (struct sockaddr *)&svr_addr, &len); |
| 120 | if (count != -1 || errno != EBADF) |
| 121 | tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket " |
| 122 | "descriptor count:%d, errno:%d", count, errno); |
| 123 | |
| 124 | tst_resm(TPASS, "recvfrom() with a bad socket descriptor - EBADF"); |
| 125 | |
| 126 | /*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/ |
| 127 | strcpy(filename, "/tmp/sctptest.XXXXXX"); |
| 128 | fd = mkstemp(filename); |
| 129 | if (fd == -1) |
| 130 | tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s", |
| 131 | filename, strerror(errno)); |
| 132 | count = recvfrom(fd, message_rcv, msg_count, flag, |
| 133 | (struct sockaddr *)&svr_addr, &len); |
| 134 | if (count == -1) |
| 135 | err_no = errno; |
| 136 | close(fd); |
| 137 | unlink(filename); |
| 138 | if (count != -1 || err_no != ENOTSOCK) |
| 139 | tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket " |
| 140 | "count:%d, errno:%d", count, err_no); |
| 141 | |
| 142 | tst_resm(TPASS, "recvfrom() with invalid socket - ENOTSOCK"); |
| 143 | |
| 144 | /*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/ |
| 145 | count = recvfrom(acpt_sk, (char *)-1, msg_count, flag, |
| 146 | (struct sockaddr *)&svr_addr, &len); |
| 147 | if (count != -1 || errno != EFAULT) |
| 148 | tst_brkm(TBROK, tst_exit, "recvfrom with invalid message " |
| 149 | "pointer count:%d, errno:%d", count, errno); |
| 150 | |
| 151 | tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT"); |
| 152 | |
| 153 | /*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/ |
| 154 | count = recvfrom(lstn_sk, message_rcv, msg_count, flag, |
| 155 | (struct sockaddr *)&svr_addr, &len); |
| 156 | if (count != -1 || errno != ENOTCONN) |
| 157 | tst_brkm(TBROK, tst_exit, "recvfrom on listening socket " |
| 158 | "count:%d, errno:%d", count, errno); |
| 159 | |
| 160 | tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN"); |
| 161 | |
| 162 | count = test_send(acpt_sk, message, msg_count, flag); |
| 163 | |
| 164 | test_shutdown(sk, SHUT_WR); |
| 165 | |
| 166 | /*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/ |
| 167 | count = recvfrom(acpt_sk, message_rcv, msg_count, flag, |
| 168 | (struct sockaddr *)&svr_addr, &len); |
| 169 | if (count < 0) |
| 170 | tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has " |
| 171 | "received shutdown count:%d, errno:%d", count, errno); |
| 172 | |
| 173 | tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - " |
| 174 | "EOF"); |
| 175 | |
| 176 | /*recvfrom () TEST6:reading the pending message on socket that sent |
| 177 | SHUTDOWN*/ |
| 178 | count = recvfrom(sk, message_rcv, msg_count, flag, |
| 179 | (struct sockaddr *)&svr_addr, &len); |
| 180 | if (count < 0) |
| 181 | tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending " |
| 182 | "message that has sent shutdown count:%d, errno:%d", |
| 183 | count, errno); |
| 184 | |
| 185 | tst_resm(TPASS, "recvfrom() on a socket with pending message that has " |
| 186 | "sent shutdown - SUCCESS"); |
| 187 | |
| 188 | /*recvfrom () TEST7: No more message and association is shutdown, |
| 189 | ENOTCONN Expected error*/ |
| 190 | count = recvfrom(sk, message_rcv, msg_count, flag, |
| 191 | (struct sockaddr *)&svr_addr, &len); |
| 192 | if (count != -1 || errno != ENOTCONN) |
| 193 | tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no " |
| 194 | "pending messages and has sent shutdown count:%d, " |
| 195 | "errno:%d", count, errno); |
| 196 | |
| 197 | tst_resm(TPASS, "recvfrom() on a socket with no pending messages and " |
| 198 | " has sent shutdown - ENOTCONN"); |
| 199 | |
| 200 | close(sk); |
| 201 | close(lstn_sk); |
| 202 | close(acpt_sk); |
| 203 | |
| 204 | free(message_rcv); |
| 205 | return 0; |
| 206 | } |