コード例 #1
0
int 
main (int argc, char **argv)
{
	int sk1, sk2, sk3, pf_class;
	socklen_t len;
	struct sockaddr_in lstn_addr, acpt_addr;
	struct sockaddr_in conn_addr;
	char * buffer_rcv;
	struct sctp_initmsg sinmsg;
	char *message = "Hello World!\n";

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	/* Opening the socket*/
	
	pf_class = PF_INET;

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk3 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

        lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	test_bind(sk3, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	len = sizeof(struct sctp_initmsg);
	sinmsg.sinit_num_ostreams = 65535;
	sinmsg.sinit_max_instreams = 10;
	sinmsg.sinit_max_attempts = 1;
	sinmsg.sinit_max_init_timeo = 0;
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, len);
	sinmsg.sinit_num_ostreams = 10;
	sinmsg.sinit_max_instreams = 65535;
	test_setsockopt(sk3, SCTP_INITMSG, &sinmsg, len);

	test_listen(sk3, 1);

	len = sizeof(struct sockaddr_in);
	test_connect(sk1, (struct sockaddr *) &conn_addr, len);

	sk2 = test_accept(sk3, (struct sockaddr *) &acpt_addr, &len);

	test_sctp_sendmsg(sk1, message, strlen(message) + 1,
			  (struct sockaddr *)&conn_addr, len,
			  0, 0, 65534, 0, 0);

	buffer_rcv = malloc(100);
	test_recv(sk2, buffer_rcv, (strlen(message) + 1), MSG_NOSIGNAL);

	tst_resm(TPASS, "connect() with init timeout set to 0 - SUCCESS");

	close (sk1);
	close (sk2);
	close (sk3);
	
        return 0;
}
コード例 #2
0
ファイル: test_tcp_style.c プロジェクト: ystk/debian-ltp
int
main(int argc, char *argv[])
{
	int clt_sk[MAX_CLIENTS], accept_sk[MAX_CLIENTS];
	int listen_sk, clt2_sk, accept2_sk;
	sockaddr_storage_t clt_loop[MAX_CLIENTS];
	sockaddr_storage_t svr_loop, accept_loop, clt2_loop;
	socklen_t addrlen;
	int error, i;
        char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;
	struct pollfd poll_fd;
	fd_set set;
	struct msghdr outmessage;
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct iovec out_iov;
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
	struct msghdr inmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *big_buffer;
	struct iovec iov;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
#if TEST_V6
	pf_class = PF_INET6;
        svr_loop.v6.sin6_family = AF_INET6;
        svr_loop.v6.sin6_addr = in6addr_loopback;
        svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
        	clt_loop[i].v6.sin6_family = AF_INET6;
        	clt_loop[i].v6.sin6_addr = in6addr_loopback;
        	clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
	}
        clt2_loop.v6.sin6_family = AF_INET6;
        clt2_loop.v6.sin6_addr = in6addr_loopback;
        clt2_loop.v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
#else
	pf_class = PF_INET;
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_loop[i].v4.sin_family = AF_INET;
		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
	}
	clt2_loop.v4.sin_family = AF_INET;
	clt2_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	clt2_loop.v4.sin_port = htons(SCTP_TESTPORT_2 + i);
#endif

	/* Create and bind the listening server socket.  */
        listen_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(listen_sk, &svr_loop.sa, sizeof(svr_loop));

	/* Mark listen_sk as being able to accept new associations.  */
	test_listen(listen_sk, MAX_CLIENTS-1);

	/* Create and bind the client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
	}
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	addrlen = sizeof(accept_loop);
	/* Try to do accept on a non-listening socket. It should fail. */
	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept on non-listening socket");

	/* Try to do a connect from a listening socket. It should fail. */
	error = connect(listen_sk, (struct sockaddr *)&clt_loop[0],
			sizeof(clt_loop[0]));
	if ((-1 != error) && (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect to non-listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect to non-listening socket");

	/* Do a blocking connect from clt_sk's to listen_sk */      
	for (i = 0; i < MAX_CLIENTS; i++)
		test_connect(clt_sk[i], &svr_loop.sa, sizeof(svr_loop));

	tst_resm(TPASS, "connect to listening socket");

	/* Verify that no more connect's can be done after the acceptq
	 * backlog has reached the max value.
	 */
	error = connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));
	if ((-1 != error) && (ECONNREFUSED != errno))
		tst_brkm(TBROK, tst_exit, "connect after max backlog "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connect after max backlog");

	/* Extract the associations on the listening socket as new sockets. */
	for (i = 0; i < MAX_CLIENTS; i++) {
		poll_fd.fd = listen_sk;
		poll_fd.events = POLLIN;
		poll_fd.revents = 0;
		error = poll(&poll_fd, 1, -1);
		if ((1 != error) && (1 != poll_fd.revents))
			tst_brkm(TBROK, tst_exit, "Unexpected return value "
				 "with poll, error:%d errno:%d, revents:%d",
				 error, errno, poll_fd.revents);

		addrlen = sizeof(accept_loop);
		accept_sk[i] = test_accept(listen_sk, &accept_loop.sa,
					   &addrlen); 
	}

	tst_resm(TPASS, "accept from listening socket");

	/* Try to do a connect on an established socket. It should fail. */
	error = connect(accept_sk[0], &clt_loop[0].sa, sizeof(clt_loop[0]));
	if ((-1 != error) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connect on an established socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "connect on an established socket");

	/* Try to do accept on an established socket. It should fail. */
	error = accept(accept_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "error:%d errno:%d", error, errno);

	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
	if ((-1 != error) && (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "accept on an established socket "
			 "failure: error:%d errno:%d", error, errno);

	tst_resm(TPASS, "accept on an established socket");

	/* Send and receive a message from the client sockets to the accepted
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(clt_sk[i], message, strlen(message), 0);
		test_recv(accept_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "client sockets -> accepted sockets");

	/* Send and receive a message from the accepted sockets to the client
	 * sockets.
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		test_send(accept_sk[i], message, strlen(message), 0);
		test_recv(clt_sk[i], msgbuf, 100, 0);
	}

	tst_resm(TPASS, "accepted sockets -> client sockets");

	/* Sending a message on a listening socket should fail. */
	error = send(listen_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send on a listening socket");

	/* Trying to receive a message on a listening socket should fail. */
	error = recv(listen_sk, msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a listening socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a listening socket");

	/* TESTCASES for shutdown() */
	errno = 0;
	test_send(accept_sk[0], message, strlen(message), 0);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(clt_sk[0]);

	/* Do a SHUT_WR on clt_sk[0] to disable any new sends. */
	test_shutdown(clt_sk[0], SHUT_WR);

	/* Reading on a socket that has received SHUTDOWN should return 0 
	 * indicating EOF.
	 */
	error = recv(accept_sk[0], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN received socket");

	/* Read the pending message on clt_sk[0] that was received before
	 * SHUTDOWN call.
	 */  
	test_recv(clt_sk[0], msgbuf, 100, 0);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
	memset(&inmessage, 0, sizeof(inmessage));	
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	/* Receive the SHUTDOWN_COMP notification as they are enabled. */
	error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	tst_resm(TPASS, "recv SHUTDOWN_COMP notification on a SHUT_WR socket");

	/* No more messages and the association is SHUTDOWN, should fail. */
	error = recv(clt_sk[0], msgbuf, 100, 0);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN sent socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUTDOWN sent socket");

	errno = 0;

	/* Do a SHUT_RD on clt_sk[1] to disable any new receives. */
	test_shutdown(clt_sk[1], SHUT_RD);

	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Sending a message on SHUT_RD socket. */
	test_send(clt_sk[1], message, strlen(message), 0);

	/* Receive the message sent on SHUT_RD socket. */
	test_recv(accept_sk[1], msgbuf, 100, 0);

	/* Send a message to the SHUT_RD socket. */
	test_send(accept_sk[1], message, strlen(message), 0);

	/* We should not receive the message as the socket is SHUT_RD */ 
	error = recv(clt_sk[1], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RD socket");

	/* Do a SHUT_RDWR on clt_sk[2] to disable any new sends/receives. */
	test_shutdown(clt_sk[2], SHUT_RDWR);

	error = recv(accept_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	error = recv(clt_sk[2], msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "recv on a SHUT_RDWR socket");

	error = 0;

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(accept_sk[i]);

	/* Test case to verify accept of a CLOSED association. */
	/* Do a connect, send and a close to ESTABLISH and CLOSE an
	 * association on the listening socket.
	 */
	test_connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));

	test_send(clt2_sk, message, strlen(message), 0);

	close(clt2_sk);

	FD_ZERO(&set);
	FD_SET(listen_sk, &set);

	error = select(listen_sk + 1, &set, NULL, NULL, NULL);
	if (1 != error)
		tst_brkm(TBROK, tst_exit, "select error:%d, "
			 "errno: %d", error, errno);

	/* Now accept the CLOSED association waiting on the listening 
	 * socket.
	 */  
	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	/* Receive the message sent before doing a close. */
	test_recv(accept2_sk, msgbuf, 100, 0);

	/* Receive EOF indication as there are no more messages and the
	 * socket is SHUTDOWN.
	 */
	error = recv(accept2_sk, msgbuf, 100, 0);
	if ((0 != error) || (0 != errno))
		tst_brkm(TBROK, tst_exit, "Unexpected error return on "
			 "recv(error:%d, errno:%d)", error, errno);

	tst_resm(TPASS, "accept of a CLOSED association");

	/* Trying to send a message over the CLOSED association should
	 * generate EPIPE.
	 */
	error = send(accept2_sk, message, strlen(message), MSG_NOSIGNAL);
	if ((-1 != error) || (EPIPE != errno))
		tst_brkm(TBROK, tst_exit, "send to a CLOSED association "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "send to a CLOSED association");

	error = 0;
	close(accept2_sk);

	/* Verify that auto-connect can be done on a TCP-style socket using
	 * sendto/sendmsg.
	 */
	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));

	/* Do a sendto() without a connect() */
	test_sendto(clt2_sk, message, strlen(message), 0, &svr_loop.sa,
		    sizeof(svr_loop));

	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 

	test_recv(accept2_sk, msgbuf, 100, 0);

	tst_resm(TPASS, "auto-connect using sendto");

	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = NULL;
	outmessage.msg_iovlen = 0;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

	/* Verify that SCTP_EOF cannot be used to shutdown an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_EOF;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_EOF flag");

	/* Verify that SCTP_ABORT cannot be used to abort an association
	 * on a TCP-style socket.
	 */
	sinfo->sinfo_flags |= SCTP_ABORT;
	error = sendmsg(clt2_sk, &outmessage, 0);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sendmsg with SCTP_ABORT flag");

	/* Verify that a normal message can be sent using sendmsg. */
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	out_iov.iov_base = message;
	out_iov.iov_len = strlen(message) + 1;
	sinfo->sinfo_flags = 0;
	test_sendmsg(clt2_sk, &outmessage, 0, strlen(message)+1);

	test_recv(accept2_sk, msgbuf, 100, 0);
	
	tst_resm(TPASS, "sendmsg with no flags");

	close(clt2_sk);
	close(accept2_sk);
	close(listen_sk);

        /* Indicate successful completion.  */
	return 0;
}
コード例 #3
0
int main(int argc, char *argv[])
{
	int clnt_sk[MAX_CLIENTS], acpt_sk[MAX_CLIENTS], sk;
	int lstn_sk;
	struct sockaddr_in lstn_addr, acpt_addr;
	socklen_t addrlen;
	int error, i;
	char *message = "hello, world!\n";
	char msgbuf[100];
	int pf_class;

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	/* Initialize the server and client addresses. */
	pf_class = PF_INET;

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	test_bind(lstn_sk, (struct sockaddr *)&lstn_addr, sizeof(lstn_addr));

	test_listen(lstn_sk, MAX_CLIENTS);

	for (i = 0; i < MAX_CLIENTS; i++) {
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
		test_connect(clnt_sk[i], (struct sockaddr *)&lstn_addr,
			     sizeof(lstn_addr));
	}

	for (i = 0; i < MAX_CLIENTS; i++) {
		addrlen = sizeof(acpt_addr);
		acpt_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
					 &addrlen);
	}

	/*shutdown() TEST1: Bad socket descriptor, EBADF Expected error */
	error = shutdown(-1, SHUT_WR);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "shutdown with a bad socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with a bad socket descriptor - EBADF");

	/*shutdown() TEST2: Invalid socket, ENOTSOCK Expected error */
	error = shutdown(0, SHUT_WR);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "shutdown with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with an invalid socket - ENOTSOCK");

	errno = 0;
	/*Do a send first before doing shutdown */
	test_send(acpt_sk[0], message, strlen(message), 0);

	/*shutdown() TEST3: shutdown with SHUT_WR flag to disable new send */
	error = shutdown(clnt_sk[0], SHUT_WR);
	if (error < 0)
		tst_brkm(TBROK, NULL, "shutdown with SHUT_WR flag "
			 "error:%d, errno:%d", error, errno);

	/* Reading on a socket that has received SHUTDOWN should return 0
	 * indicating EOF.
	 */
	error = recv(acpt_sk[0], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUTDOWN received socket "
			 "error:%d, errno:%d", error, errno);

	/* Read the pending message on clnt_sk[0] that was received before
	 * SHUTDOWN call.
	 */
	test_recv(clnt_sk[0], msgbuf, 100, 0);

	/* No more messages and the association is SHUTDOWN, should fail. */
	error = recv(clnt_sk[0], msgbuf, 100, 0);
	if ((error != -1) || (errno != ENOTCONN))
		tst_brkm(TBROK, NULL, "recv on a SHUT_WR socket with no "
			 "messages error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_WR flag - SUCCESS");

	errno = 0;

	/*shutdown() TEST4: shutdown with SHUT_RD flag to disable new receive */
	test_shutdown(clnt_sk[1], SHUT_RD);

	error = recv(clnt_sk[1], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Sending a message on SHUT_RD socket. */
	error = test_send(clnt_sk[1], message, strlen(message), 0);
	if (error < 0)
		tst_brkm(TBROK, NULL, "send on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	/* Receive the message sent on SHUT_RD socket. */
	test_recv(acpt_sk[1], msgbuf, 100, 0);

	/* Send a message to the SHUT_RD socket. */
	test_send(acpt_sk[1], message, strlen(message), 0);

	/* We should not receive the message as the socket is SHUT_RD */
	error = recv(clnt_sk[1], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RD socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_RD flag - SUCCESS");

	/*shutdown() TEST5: shutdown with SHUT_RDWR flag to disable new
	   receive/send */
	test_shutdown(clnt_sk[2], SHUT_RDWR);

	error = recv(acpt_sk[2], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUTDOWN received socket "
			 "error:%d, errno:%d", error, errno);

	error = recv(clnt_sk[2], msgbuf, 100, 0);
	if ((error != 0) || (errno != 0))
		tst_brkm(TBROK, NULL, "recv on a SHUT_RDWR socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() with SHUT_RDWR flag - SUCCESS");

	/*shutdown() TEST6: Unconnected socket, ENOTCONN Expected error */
	error = shutdown(sk, SHUT_RD);
	if ((error != -1) || (errno != ENOTCONN))
		tst_brkm(TBROK, NULL, "shutdown on an unconnected socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "shutdown() on an unconnected socket - SUCCESS");

	for (i = 0; i < MAX_CLIENTS; i++)
		close(clnt_sk[i]);
	for (i = 0; i < MAX_CLIENTS; i++)
		close(acpt_sk[i]);

	close(lstn_sk);
	close(sk);

	tst_exit();
}
コード例 #4
0
int main(void)
{
	int error;
	socklen_t len;
	int sk, sk1, sk2, acpt_sk, pf_class;
	struct sctp_rtoinfo grtinfo;
	struct sockaddr_in lstn_addr, conn_addr;
	struct sctp_initmsg ginmsg;	/*get the value for SCTP_INITMSG */
	struct sctp_initmsg sinmsg;	/*set the value for SCTP_INITMSG */
	struct linger slinger;	/*SO_LINGER structure */
	struct linger glinger;	/*SO_LINGER structure */
	struct sockaddr_in addr;
	struct sockaddr_in *gaddr;
	struct sctp_status gstatus;	/*SCTP_STATUS option */
	int rcvbuf_val_get, rcvbuf_val_set;	/*get and set var for SO_RCVBUF */
	int sndbuf_val_get, sndbuf_val_set;	/*get and set var for SO_SNDBUF */
	struct sctp_prim gprimaddr;	/*SCTP_PRIMARY_ADDR get */
	struct sctp_prim sprimaddr;	/*SCTP_PRIMARY_ADDR set */
	struct sctp_assocparams sassocparams;	/* SCTP_ASSOCPARAMS set */
	struct sctp_assocparams gassocparams;	/* SCTP_ASSOCPARAMS get */

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	pf_class = PF_INET;

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*setsockopt() TEST1: Bad socket descriptor EBADF, Expected error */
	error = setsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "setsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with a bad socket descriptor - EBADF");

	/*setsockopt() TEST2: Invalid socket ENOTSOCK, Expected error */
	error = setsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "setsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid socket - ENOTSOCK");

	/*setsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	error = setsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with an invalid level - ENOPROTOOPT");

	/*setsockopt() TEST4: Invalid option buffer EFAULT, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (const struct sctp_rtoinfo *)-1,
			   sizeof(struct sctp_rtoinfo));
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option buffer - EFAULT");

	/*setsockopt() TEST5: Invalid option Name EOPNOTSUPP, Expected error */
	error = setsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, 0, 0);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "setsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() with invalid option name - EOPNOTSUPP");

	/*getsockopt() TEST6: Bad socket descriptor EBADF, Expected error */
	error = getsockopt(-1, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "getsockopt with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with a bad socket descriptor - EBADF");

	/*getsockopt() TEST7: Invalid socket ENOTSOCK, Expected error */
	error = getsockopt(0, IPPROTO_SCTP, 0, 0, 0);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "getsockopt with an invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid socket - ENOTSOCK");
#if 0
	/*getsockopt() TEST3: Invalid level ENOPROTOOPT, Expected error */
	/*I have commented this test case because it is returning EOPNOTSUPP.
	   When I checked the code there also it is returning EOPNOTSUPP. As this
	   is not specific to TCP style, I do not want to do the code change */

	error = getsockopt(sk, -1, SCTP_RTOINFO, 0, 0);
	if (error != -1 || errno != ENOPROTOOPT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid level "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with an invalid level - ENOPROTOOPT");
#endif
	len = sizeof(struct sctp_rtoinfo);

	/*getsockopt() TEST8: Invalid option buffer EFAULT, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_RTOINFO,
			   (struct sctp_rtoinfo *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "buffer error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option buffer - EFAULT");

	/*getsockopt() TEST9: Invalid option Name EOPNOTSUPP, Expected error */
	error = getsockopt(sk, IPPROTO_SCTP, SCTP_AUTOCLOSE, &grtinfo, &len);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "getsockopt with invalid option "
			 "name error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() with invalid option name - EOPNOTSUPP");

	close(sk);

	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk2 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	conn_addr.sin_family = AF_INET;
	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	len = sizeof(struct sctp_initmsg);

	/* TEST10: Test cases for getsockopt SCTP_INITMSG */
	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	tst_resm(TPASS, "getsockopt() SCTP_INITMSG - SUCCESS");

	sinmsg.sinit_num_ostreams = 5;
	sinmsg.sinit_max_instreams = 5;
	sinmsg.sinit_max_attempts = 3;
	sinmsg.sinit_max_init_timeo = 30;
	/* TEST11: Test case for setsockopt SCTP_INITMSG */
	test_setsockopt(sk1, SCTP_INITMSG, &sinmsg, sizeof(sinmsg));

	test_getsockopt(sk1, SCTP_INITMSG, &ginmsg, &len);

	if (sinmsg.sinit_num_ostreams != ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams != ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts != ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo != ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "compare failed");

	tst_resm(TPASS, "setsockopt() SCTP_INITMSG - SUCCESS");

	/*Now get the values on different endpoint */
	test_getsockopt(sk2, SCTP_INITMSG, &ginmsg, &len);

	/*Comparison should not succeed here */
	if (sinmsg.sinit_num_ostreams == ginmsg.sinit_num_ostreams &&
	    sinmsg.sinit_max_instreams == ginmsg.sinit_max_instreams &&
	    sinmsg.sinit_max_attempts == ginmsg.sinit_max_attempts &&
	    sinmsg.sinit_max_init_timeo == ginmsg.sinit_max_init_timeo)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SCTP_INITMSG "
			 "unexpected compare success");

	/* SO_LINGER Test with l_onff = 0 and l_linger = 0 */
	slinger.l_onoff = 0;
	slinger.l_linger = 0;
	test_bind(sk1, (struct sockaddr *)&lstn_addr, sizeof(lstn_addr));
	test_listen(sk1, 10);
	len = sizeof(struct sockaddr_in);
	test_connect(sk2, (struct sockaddr *)&conn_addr, len);

	acpt_sk = test_accept(sk1, (struct sockaddr *)&addr, &len);

	len = sizeof(struct linger);
	/* TEST12: Test case for setsockopt SO_LINGER */
	error = setsockopt(sk2, SOL_SOCKET, SO_LINGER, &slinger, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_LINGER - SUCCESS");

	/* TEST13: Test case for getsockopt SO_LINGER */
	error = getsockopt(sk2, SOL_SOCKET, SO_LINGER, &glinger, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_LINGER "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_LINGER - SUCCESS");

	if (slinger.l_onoff != glinger.l_onoff ||
	    slinger.l_linger != glinger.l_linger)
		tst_brkm(TBROK, NULL, "setsockopt/getsockopt SO_LINGER "
			 "compare failed");

	/*First gets the default SO_RCVBUF value and comapres with the
	   value obtained from SCTP_STATUS */
	len = sizeof(int);
	/* TEST14: Test case for getsockopt SO_RCVBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_RCVBUF - SUCCESS");

	len = sizeof(struct sctp_status);
	/* TEST15: Test case for getsockopt SCTP_STATUS */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_STATUS "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_STATUS - SUCCESS");

	/* Reducing the SO_RCVBUF value using setsockopt() */
	/*Minimum value is 128 and hence I am using it */
	len = sizeof(int);
	rcvbuf_val_set = 128;
	/* TEST16: Test case for setsockopt SO_RCVBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_RCVBUF - SUCCESS");

	error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_RCVBUF "
			 "error:%d, errno:%d", error, errno);

	if ((2 * rcvbuf_val_set) != rcvbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d",
			 (2 * rcvbuf_val_set), rcvbuf_val_get);

	sndbuf_val_set = 1024;
	/* TEST17: Test case for setsockopt SO_SNDBUF */
	error = setsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_set, len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SO_SNDBUF - SUCCESS");

	/* TEST18: Test case for getsockopt SO_SNDBUF */
	error = getsockopt(sk2, SOL_SOCKET, SO_SNDBUF, &sndbuf_val_get, &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SO_SNDBUF "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SO_SNDBUF - SUCCESS");

	if ((2 * sndbuf_val_set) != sndbuf_val_get)
		tst_brkm(TBROK, NULL, "Comparison failed:Set value and "
			 "got value differs Set Value=%d Get Value=%d\n",
			 (2 * sndbuf_val_set), sndbuf_val_get);

	/* Getting the primary address using SCTP_PRIMARY_ADDR */
	len = sizeof(struct sctp_prim);
	/* TEST19: Test case for getsockopt SCTP_PRIMARY_ADDR */
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &gprimaddr,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	gaddr = (struct sockaddr_in *)&gprimaddr.ssp_addr;
	if (htons(gaddr->sin_port) != lstn_addr.sin_port &&
	    gaddr->sin_family != lstn_addr.sin_family &&
	    gaddr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_PRIMARY_ADDR value "
			 "mismatch");

	memcpy(&sprimaddr, &gprimaddr, sizeof(struct sctp_prim));

	/* TEST20: Test case for setsockopt SCTP_PRIMARY_ADDR */
	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, &sprimaddr,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_PRIMARY_ADDR "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt() SCTP_PRIMARY_ADDR - SUCCESS");

	/* TEST21: Test case for getsockopt SCTP_PRIMARY_ADDR */
	/* Getting the association info using SCTP_ASSOCINFO */
	len = sizeof(struct sctp_assocparams);
	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt() SCTP_ASSOCINFO - SUCCESS");

	/* TEST21: Test case for setsockopt SCTP_ASSOCINFO */
	memcpy(&sassocparams, &gassocparams, sizeof(struct sctp_assocparams));
	sassocparams.sasoc_asocmaxrxt += 5;
	sassocparams.sasoc_cookie_life += 10;

	error = setsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &sassocparams,
			   len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "setsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	error = getsockopt(sk2, IPPROTO_SCTP, SCTP_ASSOCINFO, &gassocparams,
			   &len);
	if (error < 0)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO "
			 "error:%d, errno:%d", error, errno);

	if (sassocparams.sasoc_asocmaxrxt != gassocparams.sasoc_asocmaxrxt ||
	    sassocparams.sasoc_cookie_life != gassocparams.sasoc_cookie_life)
		tst_brkm(TBROK, NULL, "getsockopt SCTP_ASSOCINFO value "
			 "mismatch");
	tst_resm(TPASS, "setsockopt() SCTP_ASSOCINFO - SUCCESS");

	close(sk2);
	close(sk1);
	close(acpt_sk);

	tst_exit();
}
コード例 #5
0
ファイル: test_1_to_1_connectx.c プロジェクト: 1587/ltp
int
main(int argc, char *argv[])
{
	int error,i;
	socklen_t len;
	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
	int sk1,clnt2_sk;

	struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
	struct sockaddr *tmp_addr;

	/* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);

	pf_class = PF_INET;

	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a listen socket*/
	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a regular socket*/
	for (i = 0 ; i < SK_MAX ; i++)
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/*Listening the socket*/
	test_listen(lstn_sk, SK_MAX-1);


	/*sctp_connectx () TEST1: Bad socket descriptor, EBADF Expected error*/
	len = sizeof(struct sockaddr_in);
	error = sctp_connectx(-1, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with bad socket descriptor - EBADF");
	
	/*sctp_connectx () TEST2: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_connectx(0, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");

	/*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
	tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
	tmp_addr->sa_family = AF_INET;
	error = sctp_connectx(sk, tmp_addr, 1, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address - EINVAL");

	/*sctp_connectx () TEST4: Invalid address length, EINVAL Expected error*/
	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 0, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address length "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address length - EINVAL");

	/*sctp_connectx () TEST5: Invalid address family, EINVAL Expect error*/
	conn_addr.sin_family = 9090; /*Assigning invalid address family*/
	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address family "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() with invalid address family - EINVAL");

	conn_addr.sin_family = AF_INET;

	/*sctp_connectx () TEST6: Blocking sctp_connectx, should pass*/
	/*All the be below blocking sctp_connectx should pass as socket will be 
	listening SK_MAX clients*/
	for (i = 0 ; i < SK_MAX ; i++) {
		error = sctp_connectx(clnt_sk[i], (struct sockaddr *)&conn_addr,
			      1, NULL);
		if (error < 0)
			tst_brkm(TBROK, tst_exit, "valid blocking sctp_connectx "
				 "error:%d, errno:%d", error, errno);
	}

	tst_resm(TPASS, "valid blocking sctp_connectx() - SUCCESS");

	/*sctp_connectx () TEST7: sctp_connectx when accept queue is full, ECONNREFUSED
	Expect error*/
	/*Now that accept queue is full, the below sctp_connectx should fail*/
	error = sctp_connectx(clnt2_sk, (struct sockaddr *) &conn_addr, 1, NULL);
	if (error != -1 || errno != ECONNREFUSED)
		tst_brkm(TBROK, tst_exit, "sctp_connectx when accept queue is full "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() when accept queue is full - ECONNREFUSED");
	
	/*Calling a accept first to estblish the pending sctp_connectxions*/
	for (i=0 ; i < SK_MAX ; i++)
		acpt_sk[i] = test_accept(lstn_sk,
					 (struct sockaddr *) &acpt_addr, &len);

	/*sctp_connectx () TEST8: from a listening socket, EISCONN Expect error*/
	error = sctp_connectx(lstn_sk, (struct sockaddr *) &lstn_addr, 1, NULL);
	if (error != -1 || errno != EISCONN)
		tst_brkm(TBROK, tst_exit, "sctp_connectx on a listening socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() on a listening socket - EISCONN");

	/*sctp_connectx() TEST9: On established socket, EISCONN Expect error*/
	i=0;
	error = sctp_connectx(acpt_sk[i], (struct sockaddr *) &lstn_addr, 1, NULL);
	if (error != -1 || errno != EISCONN)
		tst_brkm(TBROK, tst_exit, "sctp_connectx on an established socket "
	                 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() on an established socket - EISCONN");

	for (i = 0 ; i < 4 ; i++) {
		close(clnt_sk[i]);
		close(acpt_sk[i]);
	} 

	/* sctp_connectx() TEST10: Re-establish an association that is closed.
	 * should succeed.
	 */
	error = sctp_connectx(sk1, (struct sockaddr *)&conn_addr, 1, NULL);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "Re-establish an association that "
				 "is closed error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_connectx() to re-establish a closed association - "
		 "SUCCESS");

	close(sk);
	close(sk1);
	close(lstn_sk);

	return 0;
}
コード例 #6
0
ファイル: test_1_to_1_events.c プロジェクト: 1587/ltp
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk,acpt_sk;
	struct sockaddr_in svr_loop, clt_loop,acpt_loop;
	struct iovec iov, out_iov;
	struct msghdr inmessage, outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	int error;
	socklen_t len;
	char *big_buffer;
	struct sctp_event_subscribe event;
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
	char *message = "hello, world!\n";
	uint32_t ppid;
	uint32_t stream;

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
	svr_loop.sin_family = AF_INET;
	svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	svr_loop.sin_port = htons(SCTP_TESTPORT_1);

	clt_loop.sin_family = AF_INET;
	clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	clt_loop.sin_port = htons(SCTP_TESTPORT_1);

	/* Create and bind the server socket.  */
        svr_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(svr_sk, (struct sockaddr *) &svr_loop, sizeof(svr_loop));

	/* Mark server socket as being able to accept new associations.  */
	test_listen(svr_sk, 3);

	/* Create the client socket.  */
	clt_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);

	event.sctp_data_io_event = 1;
	event.sctp_association_event = 1;
	event.sctp_shutdown_event = 1;
	len = sizeof(struct sctp_event_subscribe);
	test_setsockopt(svr_sk, SCTP_EVENTS, &event, len);
	test_setsockopt(clt_sk, SCTP_EVENTS, &event, len);

	len = sizeof(struct sockaddr_in);
	test_connect(clt_sk, (struct sockaddr *) &clt_loop, len);
	
	acpt_sk = test_accept(svr_sk, (struct sockaddr *) &acpt_loop, &len);

	/* Build up a msghdr structure we can use for all sending.  */
	memset(&outmessage, 0, sizeof(outmessage));	
	outmessage.msg_name = &svr_loop;
	outmessage.msg_namelen = sizeof(svr_loop);
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	outmessage.msg_control = outcmsg;
	outmessage.msg_controllen = sizeof(outcmsg);
	outmessage.msg_flags = 0;

	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;

	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;

	outmessage.msg_iov->iov_base = message;
	outmessage.msg_iov->iov_len = (strlen(message) + 1);

	/* Send . This will create the association*/
	test_sendmsg(clt_sk, &outmessage, 0, strlen(message)+1);

        memset(&inmessage, 0, sizeof(inmessage));
	/* NOW initialize inmessage with enough space for DATA... */
	big_buffer = malloc(REALLY_BIG);
	if (!big_buffer) { DUMP_CORE; }

	/* Let's do a test to do a recvmsg when we are not listening and
	 * when we have no associations.
	 */
	iov.iov_base = big_buffer;
	iov.iov_len = REALLY_BIG;
	inmessage.msg_iov = &iov;
	inmessage.msg_iovlen = 1;
	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);

	error = test_recvmsg(clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_assoc_change),
                                    SCTP_ASSOC_CHANGE,
                                    SCTP_COMM_UP);
	
	tst_resm(TPASS, "COMM_UP notification on client socket - SUCCESS");

	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_assoc_change),
                                    SCTP_ASSOC_CHANGE,
                                    SCTP_COMM_UP);
	
	tst_resm(TPASS, "COMM_UP notification on server socket - SUCCESS");

	inmessage.msg_control = incmsg;
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
                            MSG_EOR, stream, ppid);

	tst_resm(TPASS, "Data message on server socket - SUCCESS");

	close(clt_sk);
	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage,
                                    error,
                                    sizeof(struct sctp_shutdown_event),
                                    SCTP_SHUTDOWN_EVENT,
                                    0);

	tst_resm(TPASS, "SHUTDOWN notification on accepted socket - SUCCESS");
	close(svr_sk);
	close(acpt_sk);

	return 0;
}
コード例 #7
0
int
main(int argc, char *argv[])
{
        socklen_t len,len_snd;
	int msg_count;
	int sk,sk1,pf_class,lstn_sk,acpt_sk,acpt1_sk, flag, count;
        char *message = "hello, world!\n";
        char *message_rcv;

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	len_snd = (strlen(message) + 1);

	flag = MSG_NOSIGNAL;
	/*send () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = send(-1, message, len_snd, flag);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "send with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with a bad socket descriptor - EBADF");
	
	/*send () TEST2: Invalid socket, ENOTSOCK Expected error*/
	count = send(0, message, len_snd, flag);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "send with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with invalid socket - ENOTSOCK");

	/*send () TEST3: send on listening socket, EPIPE Expected error*/
	count = send(lstn_sk, message, len_snd, flag);
	if (count != -1 || errno != EPIPE)
		tst_brkm(TBROK, tst_exit, "send on a listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() on a listening socket - EPIPE");
#if 0
	/*send () TEST4: Invalid message address, EFAULT Expected error*/
       /* FIXME this test should pass. Don't catch why...  */
	count = send(sk, (char *)0x1, len_snd, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "send with invalid message "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() with invalid message ptr - EFAULT");
#endif

	test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
		 
	count = test_send(sk1, message, len_snd, flag);

	close(sk1);

	acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);

	/*send () TEST5: send on closed association, EPIPE Expected error*/
	count = send(acpt1_sk, message, len_snd, flag);
	if (count != -1 || errno != EPIPE)
		tst_brkm(TBROK, tst_exit, "send on a closed association "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() on a closed association - EPIPE");

	close(acpt1_sk);
	close(sk);
	close(lstn_sk);
	close(acpt_sk);

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	message_rcv = malloc(512);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	len = sizeof(struct sockaddr_in);

	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	
	msg_count = strlen(message) + 1;

	/*send() TEST6: Sending data from client socket to server socket*/
	count = send(sk, message, msg_count, flag);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "send from client to server "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() from client to server - SUCCESS");

	test_recv(acpt_sk, message_rcv, msg_count, flag);

	strncpy(message_rcv,"\0",512);

	/*send() TEST7: Sending data from accept socket to client socket*/
	count = send(acpt_sk, message, msg_count, flag);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "send from accept socket to client "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "send() from accept socket to client - SUCCESS");

	test_recv(sk, message_rcv, msg_count, flag);

	/*send() TEST8: Sending less number of data from the buffer*/
	/*Sending only 5 bytes so that only hello is received*/
	test_send(sk, message, 5 , flag);
	test_recv(acpt_sk, message_rcv, 5, flag);
	
	tst_resm(TPASS, "send() partial data from a buffer - SUCCESS");

	/* TEST9: sctp_send with no sinfo */
	test_sctp_send(sk, message, strlen(message) + 1 , NULL, flag);
	test_recv(acpt_sk, message_rcv, strlen(message) + 1, flag);
	tst_resm(TPASS, "sctp_send() with no sinfo - SUCCESS");

	close(sk1);
	close(lstn_sk);
	close(acpt_sk);

	return 0;
}
コード例 #8
0
ファイル: test_1_to_1_addrs.c プロジェクト: shubmit/shub-ltp
int
main(int argc, char *argv[])
{
        int error;
	socklen_t len;
	int sk,lstn_sk,clnt_sk,acpt_sk,pf_class,sk1;
	struct msghdr outmessage;
        struct msghdr inmessage;
        char *message = "hello, world!\n";
        struct iovec iov;
        struct iovec iov_rcv;
        struct sctp_sndrcvinfo *sinfo;
        int msg_count;
        char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
        struct cmsghdr *cmsg;
        struct iovec out_iov;
        char * buffer_snd;
        char * buffer_rcv;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	struct sockaddr *laddrs, *paddrs;

        struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
	struct sockaddr_in *addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a regular socket*/
	clnt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*Creating a listen socket*/
        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/*Listening many sockets as we are calling too many connect here*/
	test_listen(lstn_sk, 1);

	len = sizeof(struct sockaddr_in);

	test_connect(clnt_sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

        msg_count = strlen(message) + 1;

	memset(&outmessage, 0, sizeof(outmessage));
        buffer_snd = malloc(REALLY_BIG);

        outmessage.msg_name = &lstn_addr;
        outmessage.msg_namelen = sizeof(lstn_addr);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;

        cmsg = CMSG_FIRSTHDR(&outmessage);
        cmsg->cmsg_level = IPPROTO_SCTP;
        cmsg->cmsg_type = SCTP_SNDRCV;
        cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
        outmessage.msg_controllen = cmsg->cmsg_len;
        sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
        memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));

        iov.iov_base = buffer_snd;
	iov.iov_len = REALLY_BIG;
        outmessage.msg_iov->iov_base = message;

        outmessage.msg_iov->iov_len = msg_count;
	test_sendmsg(clnt_sk, &outmessage, MSG_NOSIGNAL, msg_count);

	test_recvmsg(acpt_sk, &inmessage, MSG_NOSIGNAL);

	/*sctp_getladdrs() TEST1: Bad socket descriptor, EBADF Expected error*/
	error = sctp_getladdrs(-1, 0, &laddrs);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with a bad socket descriptor - "
		 "EBADF");

	/*sctp_getladdrs() TEST2: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_getladdrs(0, 0, &laddrs);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with invalid socket - ENOTSOCK");

	/*sctp_getladdrs() TEST3: socket of different protocol
	EOPNOTSUPP Expected error*/
        sk1 = socket(pf_class, SOCK_STREAM, IPPROTO_IP);
	error = sctp_getladdrs(sk1, 0, &laddrs);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with socket of "
			 "different protocol error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getladdrs() with socket of different protocol - "
		 "EOPNOTSUPP");

	/*sctp_getladdrs() TEST4: Getting the local addresses*/
	error = sctp_getladdrs(lstn_sk, 0, &laddrs);
	if (error < 0)
		tst_brkm(TBROK, NULL, "sctp_getladdrs with valid socket "
			 "error:%d, errno:%d", error, errno);

	addr = (struct sockaddr_in *)laddrs;
	if (addr->sin_port != lstn_addr.sin_port ||
	    addr->sin_family != lstn_addr.sin_family ||
	    addr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "sctp_getladdrs comparision failed");

	tst_resm(TPASS, "sctp_getladdrs() - SUCCESS");

	/*sctp_freealddrs() TEST5: freeing the local address*/
	if ((sctp_freeladdrs(laddrs)) < 0)
		tst_brkm(TBROK, NULL, "sctp_freeladdrs "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_freeladdrs() - SUCCESS");

	/*sctp_getpaddrs() TEST6: Bad socket descriptor, EBADF Expected error*/
	error = sctp_getpaddrs(-1, 0, &paddrs);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with a bad socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with a bad socket descriptor - "
		 "EBADF");

	/*sctp_getpaddrs() TEST7: Invalid socket, ENOTSOCK Expected error*/
	error = sctp_getpaddrs(0, 0, &paddrs);
	if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with invalid socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with invalid socket - ENOTSOCK");

	/*sctp_getpaddrs() TEST8: socket of different protocol
	EOPNOTSUPP Expected error*/
	error = sctp_getpaddrs(sk1, 0, &laddrs);
	if (error != -1 || errno != EOPNOTSUPP)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with socket of "
			 "different protocol error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_getpaddrs() with socket of different protocol - "
		 "EOPNOTSUPP");

	/*sctp_getpaddrs() TEST9: Getting the peer addresses*/
	error = sctp_getpaddrs(acpt_sk, 0, &paddrs);
	if (error < 0)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs with valid socket "
			 "error:%d, errno:%d", error, errno);

	addr = (struct sockaddr_in *)paddrs;
	if (addr->sin_port != acpt_addr.sin_port ||
            addr->sin_family != acpt_addr.sin_family ||
            addr->sin_addr.s_addr != acpt_addr.sin_addr.s_addr)
		tst_brkm(TBROK, NULL, "sctp_getpaddrs comparision failed");

	tst_resm(TPASS, "sctp_getpaddrs() - SUCCESS");

	/*sctp_freeapddrs() TEST10: freeing the peer address*/
	if ((sctp_freepaddrs(paddrs)) < 0)
		tst_brkm(TBROK, NULL, "sctp_freepaddrs "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "sctp_freepaddrs() - SUCCESS");

	close(clnt_sk);

	tst_exit();
}
コード例 #9
0
int
main(int argc, char *argv[])
{
        int msg_count;
	socklen_t len;
	int sk,pf_class,lstn_sk,acpt_sk, flag;
        char *message = "hello, world!\n";
	char *message_rcv;
        int count;

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

	message_rcv = malloc(512);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
	
	msg_count = (strlen(message) + 1);

	flag = MSG_NOSIGNAL;
	/*Sending the message*/
	count = test_send(sk, message, msg_count, flag);

	/*recvfrom () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvfrom(-1, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "recvfrom with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with a bad socket descriptor - EBADF");

	/*recvfrom () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvfrom(0, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with invalid socket - ENOTSOCK");

	/*recvfrom () TEST3: Invalid message pointer EFAULT, Expected error*/
	count = recvfrom(acpt_sk, (char *)-1, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "recvfrom with invalid message "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() with invalid message ptr - EFAULT");

	/*TEST4: recvfrom on listening socket,ENOTCONN Expected error*/
	count = recvfrom(lstn_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message, msg_count, flag);

	test_shutdown(sk, SHUT_WR);

	/*recvfrom () TEST5:reading on a socket that received SHUTDOWN*/
	count = recvfrom(acpt_sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket that has received shutdown - "
		 "EOF");

	/*recvfrom () TEST6:reading the pending message on socket that sent 
	SHUTDOWN*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count < 0)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvfrom () TEST7: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvfrom(sk, message_rcv, msg_count, flag,
			 (struct sockaddr *)&svr_addr, &len);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, tst_exit, "recvfrom on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvfrom() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	return 0;
	
}
コード例 #10
0
int
main(int argc, char *argv[])
{
        socklen_t len;
	int i;
	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk,pf_class;
	int new_sk[SK_MAX],clnt2_sk[SK_MAX];
	int error;

        struct sockaddr_in conn_addr,lstn_addr,acpt_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	for (i=0 ; i < SK_MAX ; i++)
		new_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Creating a regular socket */
	for (i = 0 ; i < SK_MAX ; i++)
		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	for (i = 0 ; i < SK_MAX ; i++)
		clnt2_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Creating a listen socket */
        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/* Binding the listen socket */
	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

	/* Listening many sockets as we are calling too many connect here */
	test_listen(lstn_sk, SK_MAX );

	/* connect() is called just to make sure accept() doesn't block the
	 * program
	 */
	i = 0;
	len = sizeof(struct sockaddr_in);
	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

	/* accept() TEST1: Bad socket descriptor EBADF, Expected error */
        error = accept(-1, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "accept with a bad socket descriptor"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with a bad socket descriptor - EBADF");

        /*accept() TEST2: Invalid socket ENOTSOCK, Expected error*/
        error = accept(0, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "accept with invalid socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with invalid socket - ENOTSOCK");

        /*accept() TEST3: Invalid address EFAULT, Expected error*/
        error = accept(lstn_sk, (struct sockaddr *) -1, &len);
        if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "accept with invalid address"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() with invalid address - EFAULT");

	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

        /*accept() TEST4: on a non-listening socket EINVAL, Expected error*/
        error = accept(sk, (struct sockaddr *) &acpt_addr, &len);
        if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "accept on a non-listening socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() on a non-listening socket - EINVAL");
	
	test_connect(clnt_sk[i++], (struct sockaddr *) &conn_addr, len);

	/*Calling accept to establish the connection*/
	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);

	/*accept() TEST5: On a established socket EINVAL, Expected error*/
	error = accept(acpt_sk, (struct sockaddr *) &acpt_addr, &len);
	if (error != -1 || errno != EINVAL)
		tst_brkm(TBROK, tst_exit, "accept on an established socket"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() on an established socket - EINVAL");

	/*Closing the previously established association*/
	close(acpt_sk);

	test_connect(clnt_sk[i], (struct sockaddr *) &conn_addr, len);

	/*accept() TEST6: On the CLOSED association should succeed*/
	acpt_sk = accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);
        if (acpt_sk < 0)
		tst_brkm(TBROK, tst_exit, "accept a closed association"
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "accept() a closed association - SUCCESS");

	close(acpt_sk);

	/*accept() TEST7: Extracting the association on the listening socket
	as new socket, new socket socket descriptor should return*/
	for (i = 0 ; i < (SK_MAX - 1); i++)
		test_connect(clnt2_sk[i], (struct sockaddr *) &conn_addr, len);

	for (i = 0 ; i < (SK_MAX - 1); i++)
		new_sk[i] = test_accept(lstn_sk, (struct sockaddr *)&acpt_addr,
					&len);

	tst_resm(TPASS, "accept() on a listening socket - SUCCESS");

	
        /*close() TEST8: Bad socket descriptor, EBADF Expected error*/
	error = close(-1);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "close with a bad socket descriptor "
                         "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a bad socket descriptor - EBADF");

	/*close() TEST9: valid socket descriptor should succeed*/
	error = close(sk);
	if (error < 0)
		tst_brkm(TBROK, tst_exit, "close with a valid socket descriptor"
                         " error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a valid socket descriptor - SUCCESS");

	/*close() TEST10: closed socket descriptor, EBADF Expected error*/
        error = close(sk);
        if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "close with a closed socket "
			 "descriptor error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "close() with a closed socket descriptor - EBADF");
	
	for (i = 0 ; i < SK_MAX ; i++) {
		close(clnt_sk[i]);
		close(new_sk[i]);
		close(clnt2_sk[i]);
	}

	return 0;
}
コード例 #11
0
ファイル: test.cpp プロジェクト: mastergreg/Project_Euler
void run_tests(void)
{
  test_digits();
  test_accept();
}
コード例 #12
0
ファイル: test_sockopt.c プロジェクト: shubmit/shub-ltp
int
main(void)
{
	int udp_svr_sk, udp_clt_sk, tcp_svr_sk, tcp_clt_sk;
	int accept_sk, peeloff_sk;
        sockaddr_storage_t udp_svr_loop, udp_clt_loop;
        sockaddr_storage_t tcp_svr_loop, tcp_clt_loop;
        struct iovec iov;
        struct msghdr inmessage;
	struct msghdr outmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg;
	struct sctp_sndrcvinfo *sinfo;
        struct iovec out_iov;
        char *message = "hello, world!\n";
        int error;
	int pf_class, af_family;
	uint32_t ppid;
	uint32_t stream;
	sctp_assoc_t udp_svr_associd, udp_clt_associd;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	struct sctp_event_subscribe subscribe;
	struct sctp_initmsg initmsg;
	struct sctp_sndrcvinfo set_udp_sk_dflt_param, get_udp_sk_dflt_param;
	struct sctp_sndrcvinfo set_tcp_sk_dflt_param, get_tcp_sk_dflt_param;
	struct sctp_sndrcvinfo set_udp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_udp_assoc_dflt_param;
	struct sctp_sndrcvinfo set_tcp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_tcp_assoc_dflt_param;
	struct sctp_sndrcvinfo get_peeloff_assoc_dflt_param;
	struct sctp_sndrcvinfo get_accept_assoc_dflt_param;
	struct sctp_paddrinfo pinfo;
	socklen_t optlen, addrlen;
	struct sctp_status status;

        /* Rather than fflush() throughout the code, set stdout to
	 * be unbuffered.
	 */
	setvbuf(stdout, NULL, _IONBF, 0);

	/* Set some basic values which depend on the address family. */
#if TEST_V6
	pf_class = PF_INET6;
	af_family = AF_INET6;

        udp_svr_loop.v6.sin6_family = AF_INET6;
        udp_svr_loop.v6.sin6_addr = in6addr_loopback;
        udp_svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);

        udp_clt_loop.v6.sin6_family = AF_INET6;
        udp_clt_loop.v6.sin6_addr = in6addr_loopback;
        udp_clt_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+1);

        tcp_svr_loop.v6.sin6_family = AF_INET6;
        tcp_svr_loop.v6.sin6_addr = in6addr_loopback;
        tcp_svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+2);

        tcp_clt_loop.v6.sin6_family = AF_INET6;
        tcp_clt_loop.v6.sin6_addr = in6addr_loopback;
        tcp_clt_loop.v6.sin6_port = htons(SCTP_TESTPORT_1+3);
#else
	pf_class = PF_INET;
	af_family = AF_INET;

        udp_svr_loop.v4.sin_family = AF_INET;
        udp_svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        udp_svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);

        udp_clt_loop.v4.sin_family = AF_INET;
        udp_clt_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        udp_clt_loop.v4.sin_port = htons(SCTP_TESTPORT_1+1);

        tcp_svr_loop.v4.sin_family = AF_INET;
        tcp_svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        tcp_svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1+2);

        tcp_clt_loop.v4.sin_family = AF_INET;
        tcp_clt_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        tcp_clt_loop.v4.sin_port = htons(SCTP_TESTPORT_2+3);
#endif /* TEST_V6 */

        /* Create the two endpoints which will talk to each other.  */
        udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
        udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

        /* Bind these sockets to the test ports.  */
        test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
        test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

       /* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

	/* TEST #1: SCTP_STATUS socket option. */
	/* Make sure that SCTP_STATUS getsockopt on a socket with no
	 * association fails.
	 */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	error = getsockopt(udp_svr_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) on a "
			 "socket with no assoc error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) on a socket with no assoc");

        /* Send the first message.  This will create the association.  */
        outmessage.msg_name = &udp_svr_loop;
        outmessage.msg_namelen = sizeof(udp_svr_loop);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
        outmessage.msg_iov->iov_base = message;
        outmessage.msg_iov->iov_len = strlen(message) + 1;
        test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

	/* Initialize inmessage for all receives. */
	big_buffer = test_malloc(REALLY_BIG);
        memset(&inmessage, 0, sizeof(inmessage));
        iov.iov_base = big_buffer;
        iov.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;

        /* Get the communication up message on udp_svr_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_svr_associd = sac->sac_assoc_id;

        /* Get the communication up message on udp_clt_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

        /* Get the first message which was sent.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR, stream, ppid);

	/* Get SCTP_STATUS for udp_clt_sk's given association. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = udp_clt_associd;
	test_getsockopt(udp_clt_sk, SCTP_STATUS, &status, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS)");

	/* Make sure that SCTP_STATUS getsockopt with invalid associd fails. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = udp_svr_associd;
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
        	tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) with "
			 "associd error: %d errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) with invalid associd");

	/* Make sure that SCTP_STATUS getsockopt with NULL associd fails. */
	optlen = sizeof(struct sctp_status);
	memset(&status, 0, optlen);
	status.sstat_assoc_id = 0;
	error = getsockopt(udp_svr_sk, SOL_SCTP, SCTP_STATUS, &status,
			   &optlen);
	if ((error != -1) && (errno != EINVAL))
        	tst_brkm(TBROK, NULL, "getsockopt(SCTP_STATUS) with "
			 "NULL associd error: %d errno:%d", error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_STATUS) with NULL associd");

        /* Shut down the link.  */
        close(udp_clt_sk);

        /* Get the shutdown complete notification. */
	inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);

	error = 0;
        close(udp_svr_sk);

	/* TEST #2: SCTP_EVENTS socket option and SCTP_SHUTDOWN_EVENT
	 * notification.
	 */
        /* Create the two endpoints which will talk to each other.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

	/* Bind these sockets to the test ports.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

	/* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

	/* Get the default events that are enabled on udp_svr_sk. */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe, &optlen);

	/* Get the default events that are enabled on udp_clt_sk. */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_EVENTS)");

	/* Disable all the events on udp_svr_sk and udp_clt_sk. */
	memset(&subscribe, 0, sizeof(struct sctp_event_subscribe));
	test_setsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));
	test_setsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));

	tst_resm(TPASS, "setsockopt(SCTP_EVENTS)");

	/* Get the updated list of enabled events on udp_svr_sk and
	 * udp_clt_sk.
	 */
	optlen = sizeof(subscribe);
	test_getsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe, &optlen);
	optlen = sizeof(subscribe);
	test_getsockopt(udp_clt_sk, SCTP_EVENTS, &subscribe, &optlen);

	/* Send a message.  This will create the association.  */
	outmessage.msg_iov->iov_base = message;
	outmessage.msg_iov->iov_len = strlen(message) + 1;
	test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

	/* Get the message which was sent.  */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
        test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR, 0, 0);
	/* Verify that we received the msg without any ancillary data. */
	if (inmessage.msg_controllen != 0)
		tst_brkm(TBROK, NULL, "Receive unexpected ancillary"
			 "data");

	/* Enable SCTP_SHUTDOWN_EVENTs on udp_svr_sk. */
	memset(&subscribe, 0, sizeof(struct sctp_event_subscribe));
	subscribe.sctp_shutdown_event = 1;
	test_setsockopt(udp_svr_sk, SCTP_EVENTS, &subscribe,
			sizeof(subscribe));

	error = 0;
        /* Shut down the link.  */
        close(udp_clt_sk);

	/* Get the SHUTDOWN_EVENT notification on udp_svr_sk. */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_shutdown_event),
				    SCTP_SHUTDOWN_EVENT, 0);

	tst_resm(TPASS, "setsockopt(SCTP_EVENTS) - SCTP_SHUTDOWN_EVENT");

        close(udp_svr_sk);

	/* TEST #3: whether sctp_opt_info equals */
        /* Create the two endpoints which will talk to each other.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);

	/* Bind these sockets to the test ports.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));

	/* Mark udp_svr_sk as being able to accept new associations.  */
	test_listen(udp_svr_sk, 1);

        /* Send the first message.  This will create the association.  */
        outmessage.msg_name = &udp_svr_loop;
        outmessage.msg_namelen = sizeof(udp_svr_loop);
        outmessage.msg_iov = &out_iov;
        outmessage.msg_iovlen = 1;
        outmessage.msg_control = outcmsg;
        outmessage.msg_controllen = sizeof(outcmsg);
        outmessage.msg_flags = 0;
	cmsg = CMSG_FIRSTHDR(&outmessage);
	cmsg->cmsg_level = IPPROTO_SCTP;
	cmsg->cmsg_type = SCTP_SNDRCV;
	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
	outmessage.msg_controllen = cmsg->cmsg_len;
	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
	ppid = rand(); /* Choose an arbitrary value. */
	stream = 1;
	sinfo->sinfo_ppid = ppid;
	sinfo->sinfo_stream = stream;
        outmessage.msg_iov->iov_base = message;
        outmessage.msg_iov->iov_len = strlen(message) + 1;
        test_sendmsg(udp_clt_sk, &outmessage, 0, strlen(message)+1);

        /* Get the communication up message on udp_clt_sk.  */
        inmessage.msg_controllen = sizeof(incmsg);
        error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

	/* Compare the SCTP_STATUS result between sctp_opt_info and
	 * getsockopt
	 */
	{
		struct sctp_status status1, status2;

		memset(&status1, 0, sizeof(status1));
		memset(&status2, 0, sizeof(status2));
		optlen = sizeof(struct sctp_status);

		/* Test SCTP_STATUS for udp_clt_sk's given association. */
		error = sctp_opt_info(udp_clt_sk,udp_clt_associd,SCTP_STATUS,
				(char *)&status1, &optlen);
		if (error != 0)
	                tst_brkm(TBROK, NULL,
				 "sctp_opt_info(SCTP_STATUS): %s",
				 strerror(errno));

		status2.sstat_assoc_id = udp_clt_associd;
		error = getsockopt(udp_clt_sk, IPPROTO_SCTP, SCTP_STATUS,
                		(char *)&status2, &optlen);
		if (error != 0)
	                tst_brkm(TBROK, NULL,
				 "getsockopt(SCTP_STATUS): %s",
				 strerror(errno));
		if (strncmp((char *)&status1, (char *)&status2, optlen))
	                tst_brkm(TBROK, NULL, "sctp_opt_info(SCTP_STAUS)"
			       "doesn't match getsockopt(SCTP_STATUS)");

                tst_resm(TPASS, "sctp_opt_info(SCTP_STATUS)");
	}
	error = 0;
        /* Shut down the link.  */
        close(udp_svr_sk);
        close(udp_clt_sk);

	/* TEST #4: SCTP_INITMSG socket option. */
        /* Create a socket.  */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

	/* Bind this socket to the test port.  */
	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);

	/* Get the default parameters for association initialization. */
	optlen = sizeof(initmsg);
	test_getsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_INITMSG)");

	/* Change the parameters for association initialization. */
	initmsg.sinit_num_ostreams = 5;
	initmsg.sinit_max_instreams = 5;
	initmsg.sinit_max_attempts = 3;
	initmsg.sinit_max_init_timeo = 30;
	test_setsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, sizeof(initmsg));

	tst_resm(TPASS, "setsockopt(SCTP_INITMSG)");

	/* Get the updated parameters for association initialization. */
	optlen = sizeof(initmsg);
	test_getsockopt(udp_svr_sk, SCTP_INITMSG, &initmsg, &optlen);

	close(udp_svr_sk);

	/* TEST #5: SCTP_DEFAULT_SEND_PARAM socket option. */
	/* Create and bind 2 UDP-style sockets(udp_svr_sk, udp_clt_sk) and
	 * 2 TCP-style sockets. (tcp_svr_sk, tcp_clt_sk)
	 */
	udp_svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	udp_clt_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
	tcp_svr_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	tcp_clt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(udp_svr_sk);
	test_enable_assoc_change(udp_clt_sk);
	test_enable_assoc_change(tcp_svr_sk);
	test_enable_assoc_change(tcp_clt_sk);

	test_bind(udp_svr_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));
	test_bind(udp_clt_sk, &udp_clt_loop.sa, sizeof(udp_clt_loop));
	test_bind(tcp_svr_sk, &tcp_svr_loop.sa, sizeof(tcp_svr_loop));
	test_bind(tcp_clt_sk, &tcp_clt_loop.sa, sizeof(tcp_clt_loop));

	/* Mark udp_svr_sk and tcp_svr_sk as being able to accept new
	 * associations.
	 */
	test_listen(udp_svr_sk, 5);
	test_listen(tcp_svr_sk, 5);

	/* Set default send parameters on the unconnected UDP-style sockets. */
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
	test_setsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_sk_dflt_param, sizeof(set_udp_sk_dflt_param));
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
	test_setsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_sk_dflt_param, sizeof(set_udp_sk_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style socket");

	/* Get default send parameters on the unconnected UDP-style socket. */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_udp_sk_dflt_param.sinfo_ppid !=
			get_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the unconnected UDP-style socket. */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
		       &get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_udp_sk_dflt_param.sinfo_ppid !=
			get_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style socket");

	/* Verify that trying to set send params with an invalid assoc id
	 * on an UDP-style socket fails.
	 */
	memset(&set_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_sk_dflt_param.sinfo_ppid = 1000;
       	/* Invalid assoc id */
	set_udp_sk_dflt_param.sinfo_assoc_id = 1234;
        error = setsockopt(udp_clt_sk, SOL_SCTP, SCTP_DEFAULT_SEND_PARAM,
			   &set_udp_sk_dflt_param,
			   sizeof(set_udp_sk_dflt_param));
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "invalid associd error:%d, errno:%d\n",
			 error, errno);

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
		 "- one-to-many style invalid associd");

	/* Do a connect on a UDP-style socket and establish an association. */
	test_connect(udp_clt_sk, &udp_svr_loop.sa, sizeof(udp_svr_loop));

	/* Receive the COMM_UP notifications and get the associd's */
	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_svr_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_svr_associd = sac->sac_assoc_id;

	inmessage.msg_controllen = sizeof(incmsg);
	error = test_recvmsg(udp_clt_sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
	sac = (struct sctp_assoc_change *)iov.iov_base;
	udp_clt_associd = sac->sac_assoc_id;

	/* Verify that trying to set send params with an assoc id not
	 * belonging to the socket on an UDP-style socket fails.
	 */
	memset(&set_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_assoc_dflt_param.sinfo_ppid = 3000;
	set_udp_assoc_dflt_param.sinfo_assoc_id = udp_clt_associd;
	error = setsockopt(udp_svr_sk, SOL_SCTP, SCTP_DEFAULT_SEND_PARAM,
			   &set_udp_assoc_dflt_param,
			   sizeof(set_udp_assoc_dflt_param));
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "setsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "associd belonging to another socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style associd belonging to another socket");

	/* Set default send parameters of an association on the listening
	 * UDP-style socket with a valid associd.
	 */
	memset(&set_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_udp_assoc_dflt_param.sinfo_ppid = 3000;
	set_udp_assoc_dflt_param.sinfo_assoc_id = udp_svr_associd;
	test_setsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_udp_assoc_dflt_param,
			sizeof(set_udp_assoc_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style valid associd");

	/* Get default send parameters of an association on the listening
	 * UDP-style socket with a valid associd.
	 */
	memset(&get_udp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	get_udp_assoc_dflt_param.sinfo_assoc_id = udp_svr_associd ;
	optlen = sizeof(get_udp_assoc_dflt_param);
	test_getsockopt(udp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_assoc_dflt_param, &optlen);

	/* Verify that the get param matches the set param. */
	if (get_udp_assoc_dflt_param.sinfo_ppid !=
			set_udp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style valid associd");

	/* Get default send parameters of an association on the connected
	 * UDP-style socket with zero associd. This should return the
	 * socket wide default parameters.
	 */
	memset(&get_udp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	get_udp_sk_dflt_param.sinfo_assoc_id = 0 ;
	optlen = sizeof(get_udp_sk_dflt_param);
	test_getsockopt(udp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_udp_sk_dflt_param, &optlen);

	/* Verify that the get param matches the socket-wide set param. */
	if (get_udp_sk_dflt_param.sinfo_ppid !=
			set_udp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style zero associd");

	peeloff_sk = test_sctp_peeloff(udp_svr_sk, udp_svr_associd);

	/* Get default send parameters of an association on the peeled off
	 * UDP-style socket. This should return the association's default
	 * parameters.
	 */
	memset(&get_peeloff_assoc_dflt_param, 0,
	       sizeof(struct sctp_sndrcvinfo));
	get_peeloff_assoc_dflt_param.sinfo_assoc_id = 0 ;
	optlen = sizeof(get_peeloff_assoc_dflt_param);
	test_getsockopt(peeloff_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_peeloff_assoc_dflt_param, &optlen);

	/* Verify that the get param matches the association's set param. */
	if (get_peeloff_assoc_dflt_param.sinfo_ppid !=
			set_udp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-many style peeled off socket");

	/* Set default send parameters on the unconnected TCP-style sockets. */
	memset(&set_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_sk_dflt_param.sinfo_ppid = 2000;
	/* Invalid assoc id, ignored on a TCP-style socket. */
	set_tcp_sk_dflt_param.sinfo_assoc_id = 1234;
	test_setsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_sk_dflt_param,
			sizeof(set_tcp_sk_dflt_param));

	/* Set default send parameters on the unconnected TCP-style sockets. */
	memset(&set_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_sk_dflt_param.sinfo_ppid = 2000;
	/* Invalid assoc id, ignored on a TCP-style socket. */
	set_tcp_sk_dflt_param.sinfo_assoc_id = 1234;
	test_setsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_sk_dflt_param,
			sizeof(set_tcp_sk_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style socket");

	/* Get default send parameters on the unconnected TCP-style socket. */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_tcp_sk_dflt_param.sinfo_ppid !=
			get_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the unconnected TCP-style socket. */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get param matches set param. */
	if (set_tcp_sk_dflt_param.sinfo_ppid !=
			get_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style socket");

	/* Do a connect on a TCP-style socket and establish an association. */
	test_connect(tcp_clt_sk, &tcp_svr_loop.sa, sizeof(tcp_svr_loop));

	/* Set default send parameters of an association on the connected
	 * TCP-style socket.
	 */
	memset(&set_tcp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	set_tcp_assoc_dflt_param.sinfo_ppid = 4000;
	set_tcp_assoc_dflt_param.sinfo_assoc_id = 0;
	test_setsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&set_tcp_assoc_dflt_param,
			sizeof(set_tcp_assoc_dflt_param));

	tst_resm(TPASS, "setsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style assoc");

	/* Get default send parameters of an association on the connected
	 * TCP-style socket.
	 */
	memset(&get_tcp_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_assoc_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_assoc_dflt_param, &optlen);

	if (set_tcp_assoc_dflt_param.sinfo_ppid !=
			get_tcp_assoc_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the connected TCP-style socket.  */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_clt_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get parameters returned matches the set param
	 * set for the association, not the socket-wide param.
	 */
	if ((get_tcp_sk_dflt_param.sinfo_ppid ==
			set_tcp_sk_dflt_param.sinfo_ppid) ||
	    (get_tcp_sk_dflt_param.sinfo_ppid !=
	    		set_tcp_assoc_dflt_param.sinfo_ppid))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	/* Get default send parameters on the listening TCP-style socket.  */
	memset(&get_tcp_sk_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_tcp_sk_dflt_param);
	test_getsockopt(tcp_svr_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_tcp_sk_dflt_param, &optlen);

	/* Verify that the get parameters returned matches the socket-wide
	 * set param.
	 */
	if (get_tcp_sk_dflt_param.sinfo_ppid !=
			set_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style assoc");

	accept_sk = test_accept(tcp_svr_sk, NULL, &addrlen);

	/* Get default send parameters of an association on the accepted
	 * TCP-style socket.
	 */
	memset(&get_accept_assoc_dflt_param, 0, sizeof(struct sctp_sndrcvinfo));
	optlen = sizeof(get_accept_assoc_dflt_param);
	test_getsockopt(accept_sk, SCTP_DEFAULT_SEND_PARAM,
			&get_accept_assoc_dflt_param, &optlen);

	error = 0;

	/* Verify that the get parameters returned matches the socket-wide
	 * set param.
	 */
	if (get_tcp_sk_dflt_param.sinfo_ppid !=
			set_tcp_sk_dflt_param.sinfo_ppid)
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_DEFAULT_SEND_PARAM) "
			 "mismatch.");

	tst_resm(TPASS, "getsockopt(SCTP_DEFAULT_SEND_PARAM) - "
		 "one-to-one style accepted socket");

	/* TEST #6: SCTP_GET_PEER_ADDR_INFO socket option. */
	/* Try 0 associd and 0 addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "null associd, null addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "null associd and null addr");

	/* Try valid associd, but 0 addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "valid associd, null addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and null addr");

	/* Try valid associd, invalid addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	memcpy(&pinfo.spinfo_address, &udp_clt_loop, sizeof(udp_clt_loop));
	error = getsockopt(udp_clt_sk, SOL_SCTP, SCTP_GET_PEER_ADDR_INFO,
			   &pinfo, &optlen);
	if ((-1 != error) || (EINVAL != errno))
		tst_brkm(TBROK, NULL, "getsockopt(SCTP_GET_PEER_ADDR_INFO) "
			 "valid associd, invalid addr error:%d, errno:%d\n",
			error, errno);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and invalid addr");

	/* Try valid associd, valid addr */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = udp_clt_associd;
	memcpy(&pinfo.spinfo_address, &udp_svr_loop, sizeof(udp_svr_loop));
	test_getsockopt(udp_clt_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr");

	/* Try valid addr, peeled off socket */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = 0;
	memcpy(&pinfo.spinfo_address, &udp_clt_loop, sizeof(udp_clt_loop));
	test_getsockopt(peeloff_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo, &optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr peeled off socket");

	/* Try valid addr, TCP-style accept socket */
	memset(&pinfo, 0, sizeof(pinfo));
	optlen = sizeof(pinfo);
	pinfo.spinfo_assoc_id = 0;
	memcpy(&pinfo.spinfo_address, &tcp_clt_loop, sizeof(tcp_clt_loop));
	error = test_getsockopt(accept_sk, SCTP_GET_PEER_ADDR_INFO, &pinfo,
				&optlen);

	tst_resm(TPASS, "getsockopt(SCTP_GET_PEER_ADDR_INFO) - "
		 "valid associd and valid addr accepted socket");

	close(udp_svr_sk);
	close(udp_clt_sk);
	close(tcp_svr_sk);
	close(tcp_clt_sk);
	close(accept_sk);
	close(peeloff_sk);

        /* Indicate successful completion.  */
      tst_exit();
}
コード例 #13
0
ファイル: test_getname.c プロジェクト: kraj/ltp
int
main(int argc, char *argv[])
{
	int clt_sk, svr_sk, accept_sk;
	sockaddr_storage_t svr_loop, accept_loop;
	sockaddr_storage_t svr_local_addr, svr_peer_addr;
	sockaddr_storage_t clt_local_addr, clt_peer_addr;
	socklen_t len;
	int error;
	int pf_class;
	int fd, err_no = 0;
	char filename[21];

        /* Rather than fflush() throughout the code, set stdout to 
	 * be unbuffered.  
	 */ 
	setvbuf(stdout, NULL, _IONBF, 0); 

	/* Initialize the server and client addresses. */ 
#if TEST_V6
	pf_class = PF_INET6;
        svr_loop.v6.sin6_family = AF_INET6;
        svr_loop.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
        svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
#else
	pf_class = PF_INET;
	svr_loop.v4.sin_family = AF_INET;
	svr_loop.v4.sin_addr.s_addr = INADDR_ANY;
	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
#endif

	/* Create and bind the listening server socket.  */
        svr_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));

	bzero(&svr_local_addr, sizeof(svr_local_addr));
	len = sizeof(svr_local_addr);
	/* Verify that getsockname() on an unconnected socket works fine. */
	error = getsockname(svr_sk, (struct sockaddr *)&svr_local_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on an unconnected socket");

	bzero(&svr_peer_addr, sizeof(svr_peer_addr));
	len = sizeof(svr_peer_addr);
	/* Verify that getpeername() on an unconnected socket fails. */
	error = getpeername(svr_sk, (struct sockaddr *)&svr_peer_addr, &len);
	if ((-1 != error) || (ENOTCONN != errno))
		tst_brkm(TBROK, tst_exit, "getpeername on an unconnected "
			 "socket error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "getpeername on an unconnected socket");

	/* Mark svr_sk as being able to accept new associations.  */
	test_listen(svr_sk, 5);

	/* Create the client socket.  */
	clt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
	
	/* Do a blocking connect from clt_sk to svr_sk */      
#if TEST_V6
	svr_loop.v6.sin6_addr = in6addr_loopback;
#else
	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
#endif
	test_connect(clt_sk, &svr_loop.sa, sizeof(svr_loop));

	bzero(&clt_local_addr, sizeof(clt_local_addr));
	len = sizeof(clt_local_addr);
	/* Get the client's local address. */
	error = getsockname(clt_sk, (struct sockaddr *)&clt_local_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname on a connected client "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on a connected client socket");

	bzero(&clt_peer_addr, sizeof(clt_peer_addr));
	len = sizeof(clt_peer_addr);
	/* Get the client's peer address. */
	error = getpeername(clt_sk, (struct sockaddr *)&clt_peer_addr, &len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getpeername on a connected client "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getpeername on a connected client socket");

	/* Extract the association on the listening socket as a new socket. */
	len = sizeof(accept_loop);
	accept_sk = test_accept(svr_sk, &accept_loop.sa, &len); 

	bzero(&svr_local_addr, sizeof(svr_local_addr));
	len = sizeof(svr_local_addr);
	/* Get the server's local address. */
	error = getsockname(accept_sk, (struct sockaddr *)&svr_local_addr,
				&len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getsockname on a connected server "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getsockname on a connected server socket");

	bzero(&svr_peer_addr, sizeof(svr_peer_addr));
	len = sizeof(svr_peer_addr);
	/* Get the server's peer address. */
	error = getpeername(accept_sk, (struct sockaddr *)&svr_peer_addr,
				&len);
	if (0 != error)
		tst_brkm(TBROK, tst_exit, "getpeername on a connected server "
			 "socket: %s", strerror(errno));

	tst_resm(TPASS, "getpeername on a connected server socket");

	if (svr_local_addr.v4.sin_port != clt_peer_addr.v4.sin_port)
		tst_brkm(TBROK, tst_exit, "Server's local port(%d) doesn't "
			 "match Client's peer port(%d)\n",
			 svr_local_addr.v4.sin_port, clt_peer_addr.v4.sin_port);

	if (svr_peer_addr.v4.sin_port != clt_local_addr.v4.sin_port)
		tst_brkm(TBROK, tst_exit, "Server's peer port(%d) doesn't "
			 "match Client's local port(%d)\n",
			 svr_peer_addr.v4.sin_port, clt_local_addr.v4.sin_port);
#if TEST_V6
	if (memcmp(&svr_local_addr, &clt_peer_addr, len) != 0)
		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
			 "peer addresses do not match\n");

	if (memcmp(&svr_peer_addr, &clt_local_addr, len) != 0)
		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
			 "local addresses do not match\n");
#else
	if (svr_local_addr.v4.sin_addr.s_addr !=
		 		clt_peer_addr.v4.sin_addr.s_addr)
		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
			 "peer addresses do not match\n");
	if (svr_peer_addr.v4.sin_addr.s_addr !=
		 		clt_local_addr.v4.sin_addr.s_addr)
		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
			 "local addresses do not match\n");
#endif
	tst_resm(TPASS, "getsockname/getpeername server/client match");

	bzero(&clt_local_addr, sizeof(clt_local_addr));
	len = sizeof(clt_local_addr);
	/*getsockname():  Bad socket descriptor, EBADF expected error*/
	error = getsockname(-1, (struct sockaddr *)&clt_local_addr, &len);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "getsockname on a bad socket "
			 "descriptor. error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getsockname on a bad socket descriptor - EBADF");

	/*getsockname(): Invalid socket, ENOTSOCK expected error*/
	strcpy(filename, "/tmp/sctptest.XXXXXX");
	fd = mkstemp(filename);
	if (fd == -1)
		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
				filename, strerror(errno));
	error = getsockname(fd, (struct sockaddr *)&clt_local_addr, &len);
	if (error == -1)
		err_no = errno;
	close(fd);
	unlink(filename);
	if (error != -1 || err_no != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "getsockname on an invalid socket "
			 "error:%d errno:%d", error, err_no);

	tst_resm(TPASS, "getsockname on an invalid socket - ENOTSOCK");

	/*getsockname(): Invalid structure, EFAULT expected error*/
	error = getsockname(clt_sk, (struct sockaddr *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "getsockname with invalid buffer "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getsockname with invalid buffer - EFAULT");
 
	bzero(&clt_peer_addr, sizeof(clt_peer_addr));
	len = sizeof(clt_peer_addr);
	/*getpeername():  Bad socket descriptor, EBADF expected error*/
	error = getpeername(-1, (struct sockaddr *)&clt_local_addr, &len);
	if (error != -1 || errno != EBADF)
		tst_brkm(TBROK, tst_exit, "getpeername on a bad socket "
			 "descriptor. error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getpeername on a bad socket descriptor - EBADF");

	/*getpeername(): Invalid socket, ENOTSOCK expected error*/
	strcpy(filename, "/tmp/sctptest.XXXXXX");
	fd = mkstemp(filename);
	if (fd == -1)
		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
				filename, strerror(errno));
	error = getpeername(fd, (struct sockaddr *)&clt_local_addr, &len);
	if (error == -1)
		err_no = errno;
	close(fd);
	unlink(filename);
	if (error != -1 || err_no != ENOTSOCK)
		tst_brkm(TBROK, tst_exit, "getpeername on an invalid socket "
			 "error:%d errno:%d", error, err_no);

	tst_resm(TPASS, "getpeername on an invalid socket - ENOTSOCK");

	/*getpeername(): Invalid structure, EFAULT expected error*/
	error = getpeername(clt_sk, (struct sockaddr *)-1, &len);
	if (error != -1 || errno != EFAULT)
		tst_brkm(TBROK, tst_exit, "getpeername with invalid buffer "
			 "error:%d errno:%d", error, errno);

	tst_resm(TPASS, "getpeername with invalid buffer - EFAULT");
 
	close(clt_sk);
	close(svr_sk);
	close(accept_sk);

        /* Indicate successful completion.  */
	return 0;
}
コード例 #14
0
ファイル: test_1_to_1_sendto.c プロジェクト: 1587/ltp
int
main(int argc, char *argv[])
{
        int msg_count;
	socklen_t len;
	int sk,sk1,pf_class,lstn_sk,acpt_sk,flag;
        char *message = "hello, world!\n";
        char *message_rcv;
        int count;
	
        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbufferd
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	message_rcv = malloc(512);
	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);
	flag = MSG_NOSIGNAL;
	
	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	msg_count = strlen(message) + 1;

	/*sendto() TEST1: Sending data from client socket to server socket*/
	count = sendto(sk, message, msg_count, flag,
		       (const struct sockaddr *) &conn_addr, len);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from client to server "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from client to server - SUCCESS");

	test_recv(acpt_sk, message_rcv, msg_count, flag);

	strncpy(message_rcv,"\0",512);

	/*sendto() TEST2: Sending data from accept socket to client socket*/
	count = sendto(acpt_sk, message, msg_count, flag,
		       (const struct sockaddr *) &svr_addr, len);
	if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from accept socket to client "
                         "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from accept socket to client - SUCCESS");

	test_recv(sk, message_rcv, msg_count, flag);

        close(sk);
        close(acpt_sk);

        sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	/*sendto() TEST3: Sending data from unconnected client socket to
        server socket*/
        count = sendto(sk1, message, msg_count, flag,
		       (const struct sockaddr *) &conn_addr, len);
        if (count != msg_count)
		tst_brkm(TBROK, tst_exit, "sendto from unconnected client to "
			 "server count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "sendto() from unconnected client to server - SUCCESS");

        acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

        test_recv(acpt_sk, message_rcv, msg_count, flag);

	/*send() TEST4: Sending less number of data from the buffer*/
	/*Sending only 5 bytes so that only hello is received*/
	test_sendto(sk, message, 5 , flag, (const struct sockaddr *)&conn_addr,
		    len);
	test_recv(acpt_sk, message_rcv, 5, flag);
	
	tst_resm(TPASS, "sendto() partial data from a buffer - SUCCESS");

	close(sk1);
	close(lstn_sk);
	close(acpt_sk);
	return 0;
	
}
コード例 #15
0
int
main(int argc, char *argv[])
{
        socklen_t len;
	int ret;
	int sk,pf_class,lstn_sk,acpt_sk;
	int flag = 0;
	struct msghdr inmessage;
        char *message = "hello, world!\n";
	struct iovec iov_rcv;
        int count;
	char * buffer_rcv;
        char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	char *message1 = "hello, world!\n";

        struct sockaddr_in conn_addr,lstn_addr,svr_addr;

	/* Rather than fflush() throughout the code, set stdout to
         * be unbuffered.
         */
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

        pf_class = PF_INET;

        sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

        lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);

	conn_addr.sin_family = AF_INET;
        conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        conn_addr.sin_port = htons(SCTP_TESTPORT_1);

	lstn_addr.sin_family = AF_INET;
        lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
        lstn_addr.sin_port = htons(SCTP_TESTPORT_1);

	/*Binding the listen socket*/
        test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));

        /*Listening the socket*/
        test_listen(lstn_sk, 10);

	len = sizeof(struct sockaddr_in);

	test_connect(sk, (struct sockaddr *) &conn_addr, len);

	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);

	memset(&inmessage, 0, sizeof(inmessage));
        buffer_rcv = malloc(REALLY_BIG);

        iov_rcv.iov_base = buffer_rcv;
        iov_rcv.iov_len = REALLY_BIG;
        inmessage.msg_iov = &iov_rcv;
        inmessage.msg_iovlen = 1;
        inmessage.msg_control = incmsg;
        inmessage.msg_controllen = sizeof(incmsg);

	/*recvmsg () TEST1: Bad socket descriptor, EBADF Expected error*/
	count = recvmsg(-1, &inmessage, flag);
	if (count != -1 || errno != EBADF)
		tst_brkm(TBROK, NULL, "recvmsg with a bad socket "
			 "descriptor count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with a bad socket descriptor - EBADF");

	/*recvmsg () TEST2: Invalid socket , ENOTSOCK Expected error*/
	count = recvmsg(0, &inmessage, flag);
	if (count != -1 || errno != ENOTSOCK)
		tst_brkm(TBROK, NULL, "recvmsg with invalid socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid socket - ENOTSOCK");

	/*recvmsg () TEST3: Invalid iovec pointer EFAULT, Expected error*/
	inmessage.msg_iov = (struct iovec *)-1;
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid iovec "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid iovec ptr - EFAULT");

	inmessage.msg_iov = &iov_rcv;

	/*recvmsg () TEST4: Invalid msghdr pointer EFAULT, Expected error*/
	count = recvmsg(acpt_sk, (struct msghdr *)-1, flag);
	if (count != -1 || errno != EFAULT)
		tst_brkm(TBROK, NULL, "recvmsg with invalid msghdr "
			 "pointer count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() with invalid msghdr ptr - EFAULT");

	/*recvmsg () TEST5:recvmsg on listening socket,ENOTCONN Expected error*/
	count = recvmsg(lstn_sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on listening socket "
			 "count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on listening socket - ENOTCONN");

	count = test_send(acpt_sk, message1, strlen(message), 0);

	ret = test_shutdown(sk, SHUT_WR);

	flag = MSG_NOSIGNAL;
	/*recvmsg () TEST6:reading on a socket that received SHUTDOWN*/
	count = recvmsg(acpt_sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket that has "
			 "received shutdown count:%d, errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket that has received shutdown - "
		 "EOF");

	/*recvmsg () TEST7:reading the pending message socket that sent
	SHUTDOWN*/
	count = recvmsg(sk, &inmessage, flag);
	if (count < 0)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with pending "
			 "message that has sent shutdown count:%d, errno:%d",
			 count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with pending message that has "
		 "sent shutdown - SUCCESS");

	/*recvmsg () TEST8: No more message and association is shutdown,
	ENOTCONN Expected error*/
	count = recvmsg(sk, &inmessage, flag);
	if (count != -1 || errno != ENOTCONN)
		tst_brkm(TBROK, NULL, "recvmsg on a socket with no "
			 "pending messages and has sent shutdown count:%d, "
			 "errno:%d", count, errno);

	tst_resm(TPASS, "recvmsg() on a socket with no pending messages and "
		 " has sent shutdown - ENOTCONN");

	close(sk);
	close(lstn_sk);
	close(acpt_sk);
	tst_exit();
}