⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_sockopt.c

📁 SCTP 协议实现源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* SCTP kernel Implementation * (C) Copyright IBM Corp. 2001, 2004 * Copyright (c) 1999-2000 Cisco, Inc. * Copyright (c) 1999-2001 Motorola, Inc. * Copyright (c) 2001 Intel Corp. * Copyright (c) 2001 Nokia, Inc. * * The SCTP implementation is free software; * you can redistribute it and/or modify it under the terms of * the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * The SCTP implementation is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied *                 ************************ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU CC; see the file COPYING.  If not, write to * the Free Software Foundation, 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * Please send any bug reports or fixes you make to the * email address(es): *    lksctp developers <lksctp-developers@lists.sourceforge.net> * * Or submit a bug report through the following website: *    http://www.sf.net/projects/lksctp * * Any bugs reported to us we will try to fix... any fixes shared will * be incorporated into the next SCTP release. * * Written or modified by: *    La Monte H.P. Yarroll <piggy@acm.org> *    Karl Knutson <karl@athena.chicago.il.us> *    Hui Huang <hui.huang@nokia.com> *    Jon Grimm <jgrimm@us.ibm.com> *    Sridhar Samudrala <samudrala@us.ibm.com> *//* This is a functional test to verify the various SCTP level socket * options that can be used to get information about existing SCTP * associations and to configure certain parameters. */#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/uio.h>#include <sys/errno.h>#include <netinet/in.h>#include <netinet/sctp.h>#include <sctputil.h>char *TCID = __FILE__;int TST_TOTAL = 29;int TST_CNT = 0;intmain(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_paddrparams paddrparams;	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;	int dflt_pathmaxrxt;	socklen_t optlen, addrlen;	struct sctp_status status;	struct sctp_assoc_value value;        /* 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, tst_exit, "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, tst_exit, "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, tst_exit, "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, tst_exit, "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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -