コード例 #1
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;
}
コード例 #2
0
ファイル: test_connectx.c プロジェクト: ystk/debian-ltp
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk1, clt_sk2, peeloff_sk;
	sctp_assoc_t svr_associd1, svr_associd2, clt_associd1, clt_associd2; 
	struct iovec iov;
	struct msghdr inmessage;
	int error, i;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	int flags;
	struct sockaddr_in svr_loop[NUMADDR];
	struct sockaddr_in svr_try[NUMADDR];
	struct sockaddr_in clt_loop1[NUMADDR];
	struct sockaddr_in clt_loop2[NUMADDR];
	struct sockaddr_in clt_loop3[NUMADDR];
	sockaddr_storage_t svr_test[NUMADDR], clt_test1[NUMADDR], clt_test2[NUMADDR];

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

	for (i = 0; i < NUMADDR; i++) {
		/* Initialize the server and client addresses. */ 
		svr_loop[i].sin_family = AF_INET;
		svr_loop[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		svr_loop[i].sin_port = htons(SCTP_TESTPORT_1);
		svr_test[i].v4.sin_family = AF_INET;
		svr_test[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		svr_test[i].v4.sin_port = htons(SCTP_TESTPORT_1);
		svr_try[i].sin_family = AF_INET;
		if (i < (NUMADDR-1)) {
			svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
		} else {
			/* Make last address invalid. */
			svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x400);
		}
		svr_try[i].sin_port = htons(SCTP_TESTPORT_1);
		clt_loop1[i].sin_family = AF_INET;
		clt_loop1[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
		clt_loop1[i].sin_port = htons(SCTP_TESTPORT_2);
		clt_test1[i].v4.sin_family = AF_INET;
		clt_test1[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
		clt_test1[i].v4.sin_port = htons(SCTP_TESTPORT_2);
		clt_loop2[i].sin_family = AF_INET;
		clt_loop2[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
		clt_loop2[i].sin_port = htons(SCTP_TESTPORT_2+1);
		clt_test2[i].v4.sin_family = AF_INET;
		clt_test2[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
		clt_test2[i].v4.sin_port = htons(SCTP_TESTPORT_2+1);
		clt_loop3[i].sin_family = AF_INET;
		clt_loop3[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x300);
		clt_loop3[i].sin_port = htons(SCTP_TESTPORT_2+2);
	}

	/* Create and bind the server socket.  */
	svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(svr_sk, (struct sockaddr *)&svr_loop[0], sizeof(svr_loop[0]));
	test_bindx_add(svr_sk, (struct sockaddr *)&svr_loop[1], NUMADDR-1);

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

	/* Create and bind the client sockets.  */
	clt_sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(clt_sk1, (struct sockaddr *)&clt_loop1[0], sizeof(clt_loop1));
	test_bindx_add(clt_sk1, (struct sockaddr *)&clt_loop1[1], NUMADDR-1);
	clt_sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	test_bind(clt_sk2, (struct sockaddr *)&clt_loop2[0], sizeof(clt_loop2));
	test_bindx_add(clt_sk2, (struct sockaddr *)&clt_loop2[1], NUMADDR-1);

	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
	test_enable_assoc_change(svr_sk);
	test_enable_assoc_change(clt_sk1);
	test_enable_assoc_change(clt_sk2);

	/* Set clt_sk1 as non-blocking. */
	flags = fcntl(clt_sk1, F_GETFL, 0);
	if (flags < 0)
		tst_brkm(TBROK, tst_exit, "fcntl F_GETFL: %s", strerror(errno));
	if (fcntl(clt_sk1, F_SETFL, flags | O_NONBLOCK) < 0)
		tst_brkm(TBROK, tst_exit, "fcntl F_SETFL: %s", strerror(errno));

	/* Do a non-blocking connectx from clt_sk1 to svr_sk */      
	error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
	/* Non-blocking connectx should return immediately with EINPROGRESS. */
	if ((error != -1) || (EINPROGRESS != errno))
		tst_brkm(TBROK, tst_exit, "non-blocking connectx error: %d"
			 "errno:%d", error, errno);

	tst_resm(TPASS, "non-blocking connectx");

	/* Doing a connectx on a socket to create an association that is
	 * is already established should return EISCONN.
	 */
	error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
	if ((error != -1) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connectx on a socket to create an "
			 "assoc that is already established error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "connectx on a socket to create an assoc that is "
		 "already established");

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

	/* Get COMM_UP on clt_sk1 */
	error = test_recvmsg(clt_sk1, &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;
	clt_associd1 = sac->sac_assoc_id;

	/* Get COMM_UP on svr_sk */
	error = test_recvmsg(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;
	svr_associd1 = sac->sac_assoc_id;

	/* Do a blocking connectx from clt_sk2 to svr_sk. 
	 * Blocking connectx should block until the association is established
	 * and return success.
	 */
	test_connectx(clt_sk2, (struct sockaddr *)svr_try, NUMADDR);

	/* Get COMM_UP on clt_sk2 */
	error = test_recvmsg(clt_sk2, &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;
	clt_associd2 = sac->sac_assoc_id;

	/* Get COMM_UP on svr_sk */
	error = test_recvmsg(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;
	svr_associd2 = sac->sac_assoc_id;

	tst_resm(TPASS, "blocking connectx");

	peeloff_sk = test_sctp_peeloff(svr_sk, svr_associd1); 

	/* Doing a connectx on a peeled off socket should fail. */
	error = sctp_connectx(peeloff_sk, (struct sockaddr *)clt_loop3, NUMADDR);
	if ((error != -1) || (EISCONN != errno))
		tst_brkm(TBROK, tst_exit, "connectx on a peeled off socket "
			 "error:%d, errno:%d", error, errno);

	tst_resm(TPASS, "connectx on a peeled off socket");

	/* Trying to create an association on a socket that matches an 
	 * existing peeled-off association should fail.
	 */
	error = sctp_connectx(svr_sk, (struct sockaddr *)clt_loop1, NUMADDR);
	if ((error != -1) || (EADDRNOTAVAIL != errno))
		tst_brkm(TBROK, tst_exit, "connectx to create an assoc that "
			 "matches a peeled off assoc error:%d errno:%d",
			 error, errno);

	tst_resm(TPASS, "connectx to create an assoc that matches a peeled off "
		 "assoc");

	test_peer_addr(peeloff_sk, svr_associd1, clt_test1, NUMADDR);
	tst_resm(TPASS, "server association 1 peers ok");
	test_peer_addr(svr_sk, svr_associd2, clt_test2, NUMADDR);
	tst_resm(TPASS, "server association 2 peers ok");
	test_peer_addr(clt_sk1, clt_associd1, svr_test, NUMADDR);
	tst_resm(TPASS, "client association 1 peers ok");
	test_peer_addr(clt_sk2, clt_associd2, svr_test, NUMADDR);
	tst_resm(TPASS, "client association 2 peers ok");
	close(svr_sk);
	close(clt_sk1);
	close(clt_sk2);
	close(peeloff_sk);

	/* Indicate successful completion.  */
	return 0; 
}
コード例 #3
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();
}
コード例 #4
0
ファイル: nagle_rcv.c プロジェクト: Distrotech/lksctp-tools
int
main(int argc, char *argv[])
{
        int sk, i;
        struct hostent *hst;
        sockaddr_storage_t host;
	sockaddr_storage_t msgname;
        struct iovec iov;
        struct msghdr inmessage;
	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
	int error, pf_class;
	char *big_buffer;
	char *local_host = NULL;
	int local_port = SCTP_TESTPORT_1; 
        int option_index = 0;
	time_t from, to;
	int bytes_received = 0;	
        int c;
        static struct option long_options[] = {
                {"local",	1, 0, 1},
		{"local-port",	1, 0, 2},
                {0,		0, 0, 0}
        };

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

        /* Parse the arguments.  */
	while (1) {
		c = getopt_long (argc, argv, "H:P:",
                                 long_options, &option_index);
		if (c == -1)
                        break;

                switch (c) {
                case 0:
                        printf("option %s", long_options[option_index].name);
                        if (optarg) {
                                printf(" with arg %s", optarg);
                        }
                        printf("\n");
                        break;
                case 1:         /* local host */
                case 'H':
                        local_host = optarg;
                        break;
                case 2:         /* local port */
                case 'P':
                        local_port = atoi(optarg);
                        break;
                case '?':
			usage(argv[0]);
                        exit(0);

                default:
                        printf ("%s: unrecognized option 0%c\n", argv[0], c);
			usage(argv[0]);
                        exit(1);
                }
        }

        if (optind < argc)
        {
                fprintf(stderr, "%s: non-option arguments are illegal: ",
                        argv[0]);
                while (optind < argc)
                        fprintf(stderr, "%s ", argv[optind++]);
                fprintf (stderr, "\n");
		usage(argv[0]);
                exit(1);
        }

	if (!local_host) {
                fprintf(stderr, "%s: : option -H, --local is required\n",
                        argv[0]);
		usage(argv[0]);
                exit(1);
	}
	
	/* Set some basic values which depend on the address family. */
#if TEST_V6
        hst = gethostbyname2(local_host, AF_INET6);
        if (hst == NULL || hst->h_length < 1) {
                fprintf(stderr, "%s: bad hostname: %s\n", argv[0], local_host);
                exit(1);
        }
	pf_class = PF_INET6;

        host.v6.sin6_family = AF_INET6;
        memcpy(&host.v6.sin_addr, hst->h_addr_list[0], hst->h_length);
        host.v6.sin6_port = htons(local_port);

#else
	hst = gethostbyname(local_host);
        if (hst == NULL || hst->h_length < 1) {
                fprintf(stderr, "%s: bad hostname: %s\n", argv[0], local_host);
                exit(1);
        }
	pf_class = PF_INET;

        host.v4.sin_family = AF_INET;
        memcpy(&host.v4.sin_addr, hst->h_addr_list[0], hst->h_length);
        host.v4.sin_port = htons(local_port);

#endif /* TEST_V6 */

        /* Create the endpoint which will talk to nagle_snd.  */
        sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);

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

        /* Bind the sockets to the test port.  */
        test_bind(sk, &host.sa, sizeof(host));
        
       /* Mark sk as being able to accept new associations.  */
	test_listen(sk, 1);
       
	printf("Listening on port:%d\n", local_port);
 
        /* Initialize inmessage for receives. */
        memset(&inmessage, 0, sizeof(inmessage));	
	big_buffer = test_malloc(REALLY_BIG);
        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);
	inmessage.msg_name = &msgname;
	inmessage.msg_namelen = sizeof(msgname);
	memset(&msgname, 0, sizeof(msgname));

        /* Get the communication up message on sk.  */
        error = test_recvmsg(sk, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

	printf("Established connection with ");	
	if (AF_INET == msgname.sa.sa_family)
		printf("%d.%d.%d.%d(%d)\n", NIPQUAD(msgname.v4.sin_addr),
		       ntohs(msgname.v4.sin_port));
	if (AF_INET6 == msgname.sa.sa_family)
		printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x(%d)\n",
		       NIP6(msgname.v6.sin6_addr), ntohs(msgname.v6.sin6_port));

	time(&from);
	for (i=0; i<1000000; i++) {
	        inmessage.msg_controllen = sizeof(incmsg);
		inmessage.msg_namelen = sizeof(msgname);
	        error = test_recvmsg(sk, &inmessage, MSG_WAITALL);
		if (inmessage.msg_flags & MSG_NOTIFICATION)
			break;
		printf("Received %d bytes of data\n", error);
		bytes_received += error;
	}
	time(&to);

        printf("\t%d messages(%d bytes) successfully received in %ld "
	       "seconds.\n", i, bytes_received, to - from);
        printf("The receive rate is %ld bytes/second\n",
	       bytes_received/(to - from));

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

	return 0;
}
コード例 #5
0
ファイル: test_assoc_abort.c プロジェクト: ystk/debian-ltp
int
main(int argc, char *argv[])
{
	int svr_sk, clt_sk[MAX_CLIENTS];
	sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
	sctp_assoc_t svr_associd[MAX_CLIENTS];
	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;
        int error;
	uint32_t ppid;
	uint32_t stream;
	struct sctp_assoc_change *sac;
	char *big_buffer;
	int i;
        char *message = "hello, world!\n";
	struct sctp_status status;
	socklen_t status_len;

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

	/* Create and bind the server socket.  */
        svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	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);
	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));

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

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

	/* Create and bind all the client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

		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);
		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));

		test_enable_assoc_change(clt_sk[i]);
	}

	/* Build up a msghdr structure we can use for all sending.  */
	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;
	out_iov.iov_base = message;
	out_iov.iov_len = strlen(message) + 1;
	
        /* Send the first message from all the clients to the server.  This 
	 * will create the associations.  
	 */
	for (i = 0; i < MAX_CLIENTS; i++)
		test_sendmsg(clt_sk[i], &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 all client sockets.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	
	}

	/* Get the communication up message and the data message on the
	 * server sockets for all the clients.  
	 */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
		test_check_msg_data(&inmessage, error, strlen(message) + 1, 
				    MSG_EOR, stream, ppid);
		sac = (struct sctp_assoc_change *)iov.iov_base;
		svr_associd[i] = sac->sac_assoc_id;
	}

	outmessage.msg_name = NULL;
	outmessage.msg_namelen = 0;
	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));
	sinfo->sinfo_flags |= SCTP_ABORT;

	/* Shutdown all the associations of the server socket in a loop.  */
	for (i = 0; i < MAX_CLIENTS; i++) {
		sinfo->sinfo_assoc_id = svr_associd[i];

		/* Verify that the association is present. */
		memset(&status, 0, sizeof(struct sctp_status));
		status.sstat_assoc_id = sinfo->sinfo_assoc_id;
		status_len = sizeof(struct sctp_status);
		error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
				   &status, &status_len);
		if (error)
			tst_brkm(TBROK, tst_exit,
				 "getsockopt(SCTP_STATUS): %s",
				 strerror(errno));

		/* Call sendmsg() to abort the association.  */
		test_sendmsg(svr_sk, &outmessage, 0, 0);

		/* Verify that the association is no longer present.  */
		memset(&status, 0, sizeof(struct sctp_status));
		status.sstat_assoc_id = sinfo->sinfo_assoc_id;
		status_len = sizeof(struct sctp_status);
		error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS, 
				   &status, &status_len);
		if ((error != -1) && (errno != EINVAL))
			tst_brkm(TBROK, tst_exit,
				 "getsockopt(SCTP_STATUS) "
				 "error:%d errno:%d", error, errno);
	}

	close(svr_sk);

        /* Get the COMM_LOST notification. */
	for (i = 0; i < MAX_CLIENTS; i++) {
		inmessage.msg_controllen = sizeof(incmsg);
		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
		test_check_msg_notification(&inmessage, error,
					    sizeof(struct sctp_assoc_change),
					    SCTP_ASSOC_CHANGE, SCTP_COMM_LOST);	

		close(clt_sk[i]);
	}

	tst_resm(TPASS, "ABORT an association using SCTP_ABORT"); 

        /* Indicate successful completion.  */
        return 0;
}
コード例 #6
0
int
main(int argc, char *argv[])
{
	int sk1, sk2;
	sockaddr_storage_t loop1, loop2;
	struct msghdr inmessage, outmessage;
	struct iovec iov, out_iov;
	int error;
	char *big_buffer;
	char *message = "hello, world!\n";
	uint32_t autoclose;

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

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

	loop2.v4.sin_family = AF_INET;
	loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
	loop2.v4.sin_port = htons(SCTP_TESTPORT_2);

	/* Create the two endpoints which will talk to each other.  */
	sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
	sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

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

	/* Bind these sockets to the test ports.  */
	test_bind(sk1, &loop1.sa, sizeof(loop1));
	test_bind(sk2, &loop2.sa, sizeof(loop2));

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

	/* Set the autoclose duration for the associations created on sk1 
	 * and sk2 to be 5 seconds.  
	 */ 
	autoclose = 5;
	test_setsockopt(sk1, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose));
	test_setsockopt(sk2, SCTP_AUTOCLOSE, &autoclose, sizeof(autoclose));

	/* Send the first message.  This will create the association.  */
	memset(&outmessage, 0, sizeof(outmessage));	
	outmessage.msg_name = &loop2;
	outmessage.msg_namelen = sizeof(loop2);
	outmessage.msg_iov = &out_iov;
	outmessage.msg_iovlen = 1;
	outmessage.msg_iov->iov_base = message;
	outmessage.msg_iov->iov_len = strlen(message) + 1;

	test_sendmsg(sk1, &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 = NULL;

	/* Get the communication up message on sk2.  */
	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

	/* Get the communication up message on sk1.  */
	error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
	test_check_msg_notification(&inmessage, error,
				    sizeof(struct sctp_assoc_change),
				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);	

	/* Get the first message which was sent.  */
	error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
	test_check_msg_data(&inmessage, error, strlen(message) + 1,
			    MSG_EOR|MSG_CTRUNC, 0, 0);

	tst_resm(TINFO, "Waiting for the associations to close automatically "
		 "in 5 secs");

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

	tst_resm(TPASS, "Autoclose of associations");

	/* Shut down the link.  */
	close(sk1);
	close(sk2);

	/* Indicate successful completion.  */
	return 0;
}