/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ void test_enable_assoc_change(int fd) { struct sctp_event_subscribe subscribe; memset(&subscribe, 0, sizeof(subscribe)); subscribe.sctp_data_io_event = 1; subscribe.sctp_association_event = 1; test_setsockopt(fd, SCTP_EVENTS, (char *)&subscribe, sizeof(subscribe)); }
/* test_text() verifies that we drop messages properly when sending invalid UTF-8, but not when we send valid data. */ void test_text () { int sb; int sc; int opt; uint8_t bad[20]; /* Negative testing... bad UTF-8 data for text. */ sb = test_socket (AF_SP, NN_PAIR); sc = test_socket (AF_SP, NN_PAIR); opt = NN_WS_MSG_TYPE_TEXT; test_setsockopt (sb, NN_WS, NN_WS_MSG_TYPE, &opt, sizeof (opt)); opt = NN_WS_MSG_TYPE_TEXT; test_setsockopt (sc, NN_WS, NN_WS_MSG_TYPE, &opt, sizeof (opt)); opt = 500; test_setsockopt (sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sizeof (opt)); test_bind (sb, socket_address); test_connect (sc, socket_address); test_send (sc, "GOOD"); test_recv (sb, "GOOD"); /* and the bad ... */ strcpy ((char *)bad, "BAD."); bad[2] = (char)0xDD; test_send (sc, (char *)bad); /* Make sure we dropped the frame. */ test_drop (sb, ETIMEDOUT); test_close (sb); test_close (sc); return; }
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; }
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(); }
int main () { int enda; int endb; int endc; int endd; int ende1; int ende2; struct nn_thread thread1; struct nn_thread thread2; struct nn_thread thread3; int timeo; /* Test the bi-directional device. */ /* Start the device. */ nn_thread_init (&thread1, device1, NULL); /* Create two sockets to connect to the device. */ enda = test_socket (AF_SP, NN_PAIR); test_connect (enda, SOCKET_ADDRESS_A); endb = test_socket (AF_SP, NN_PAIR); test_connect (endb, SOCKET_ADDRESS_B); /* Pass a pair of messages between endpoints. */ test_send (enda, "ABC"); test_recv (endb, "ABC"); test_send (endb, "ABC"); test_recv (enda, "ABC"); /* Clean up. */ test_close (endb); test_close (enda); /* Test the uni-directional device. */ /* Start the device. */ nn_thread_init (&thread2, device2, NULL); /* Create two sockets to connect to the device. */ endc = test_socket (AF_SP, NN_PUSH); test_connect (endc, SOCKET_ADDRESS_C); endd = test_socket (AF_SP, NN_PULL); test_connect (endd, SOCKET_ADDRESS_D); /* Pass a message between endpoints. */ test_send (endc, "XYZ"); test_recv (endd, "XYZ"); /* Clean up. */ test_close (endd); test_close (endc); /* Test the loopback device. */ /* Start the device. */ nn_thread_init (&thread3, device3, NULL); /* Create two sockets to connect to the device. */ ende1 = test_socket (AF_SP, NN_BUS); test_connect (ende1, SOCKET_ADDRESS_E); ende2 = test_socket (AF_SP, NN_BUS); test_connect (ende2, SOCKET_ADDRESS_E); /* BUS is unreliable so wait a bit for connections to be established. */ nn_sleep (100); /* Pass a message to the bus. */ test_send (ende1, "KLM"); test_recv (ende2, "KLM"); /* Make sure that the message doesn't arrive at the socket it was originally sent to. */ timeo = 100; test_setsockopt (ende1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo)); test_drop (ende1, ETIMEDOUT); /* Clean up. */ test_close (ende2); test_close (ende1); /* Shut down the devices. */ nn_term (); nn_thread_term (&thread1); nn_thread_term (&thread2); nn_thread_term (&thread3); return 0; }
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; }
int main(int argc, char *argv[]) { int sk1, sk2; sockaddr_storage_t loop1; sockaddr_storage_t loop2; 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; int pf_class, af_family; uint32_t ppid; uint32_t stream; sctp_assoc_t associd1, associd2; struct sctp_assoc_change *sac; struct sctp_event_subscribe subscribe; char *big_buffer; int offset; struct sctp_send_failed *ssf; socklen_t len; /* Really becomes 2xlen when set. */ int orig_len; struct sctp_status gstatus; /* 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; loop1.v6.sin6_family = AF_INET6; loop1.v6.sin6_addr = in6addr_loopback; loop1.v6.sin6_port = htons(SCTP_TESTPORT_1); loop2.v6.sin6_family = AF_INET6; loop2.v6.sin6_addr = in6addr_loopback; loop2.v6.sin6_port = htons(SCTP_TESTPORT_2); #else pf_class = PF_INET; af_family = AF_INET; 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); #endif /* TEST_V6 */ /* Create the two endpoints which will talk to each other. */ sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); len = sizeof(int); error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &orig_len, &len); if (error) tst_brkm(TBROK, NULL, "can't get rcvbuf size: %s", strerror(errno)); /* Set the MAXSEG to something smallish. */ { int val = SMALL_MAXSEG; test_setsockopt(sk1, SCTP_MAXSEG, &val, sizeof(val)); } memset(&subscribe, 0, sizeof(subscribe)); subscribe.sctp_data_io_event = 1; subscribe.sctp_association_event = 1; subscribe.sctp_send_failure_event = 1; test_setsockopt(sk1, SCTP_EVENTS, &subscribe, sizeof(subscribe)); test_setsockopt(sk2, SCTP_EVENTS, &subscribe, sizeof(subscribe)); /* Bind these sockets to the test ports. */ test_bind(sk1, &loop1.sa, sizeof(loop1)); test_bind(sk2, &loop2.sa, sizeof(loop2)); /* * This code sets the associations RWND very small so we can * fill it. It does this by manipulating the rcvbuf as follows: * 1) Reduce the rcvbuf size on the socket * 2) create an association so that we advertize rcvbuf/2 as * our initial rwnd * 3) raise the rcvbuf value so that we don't drop data wile * receiving later data */ len = SMALL_RCVBUF; error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); if (error) tst_brkm(TBROK, NULL, "setsockopt(SO_RCVBUF): %s", strerror(errno)); /* Mark sk2 as being able to accept new associations. */ test_listen(sk2, 1); /* Send the first message. This will create the association. */ outmessage.msg_name = &loop2; outmessage.msg_namelen = sizeof(loop2); 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(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 = incmsg; /* Get the communication up message on sk2. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(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; associd2 = sac->sac_assoc_id; /* Get the communication up message on sk1. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(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; associd1 = sac->sac_assoc_id; /* restore the rcvbuffer size for the receiving socket */ error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &orig_len, sizeof(orig_len)); if (error) tst_brkm(TBROK, NULL, "setsockopt(SO_RCVBUF): %s", strerror(errno)); /* Get the first data message which was sent. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); test_check_msg_data(&inmessage, error, strlen(message) + 1, MSG_EOR, stream, ppid); /* Figure out how big to make our fillmsg */ len = sizeof(struct sctp_status); memset(&gstatus,0,sizeof(struct sctp_status)); gstatus.sstat_assoc_id = associd1; error = getsockopt(sk1, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len); if (error) tst_brkm(TBROK, NULL, "can't get rwnd size: %s", strerror(errno)); tst_resm(TINFO, "Creating fillmsg of size %d", gstatus.sstat_rwnd+RWND_SLOP); fillmsg = malloc(gstatus.sstat_rwnd+RWND_SLOP); /* Send a fillmsg */ 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++; stream++; sinfo->sinfo_ppid = ppid; sinfo->sinfo_stream = stream; memset(fillmsg, 'X', gstatus.sstat_rwnd+RWND_SLOP); fillmsg[gstatus.sstat_rwnd+RWND_SLOP-1] = '\0'; outmessage.msg_iov->iov_base = fillmsg; outmessage.msg_iov->iov_len = gstatus.sstat_rwnd+RWND_SLOP; outmessage.msg_name = NULL; outmessage.msg_namelen = 0; sinfo->sinfo_assoc_id = associd1; sinfo->sinfo_timetolive = 0; test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, gstatus.sstat_rwnd+RWND_SLOP); /* Now send the message with timeout. */ sinfo->sinfo_ppid = ppid; sinfo->sinfo_stream = stream; outmessage.msg_iov->iov_base = ttlmsg; outmessage.msg_iov->iov_len = strlen(ttlmsg) + 1; outmessage.msg_name = NULL; outmessage.msg_namelen = 0; sinfo->sinfo_assoc_id = associd1; sinfo->sinfo_timetolive = 2000; test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(ttlmsg) + 1); tst_resm(TPASS, "Send a message with timeout"); /* Next send a message with no timeout. */ sinfo->sinfo_ppid = ppid; sinfo->sinfo_stream = stream; outmessage.msg_iov->iov_base = nottlmsg; outmessage.msg_iov->iov_len = strlen(nottlmsg) + 1; outmessage.msg_name = NULL; outmessage.msg_namelen = 0; sinfo->sinfo_assoc_id = associd1; sinfo->sinfo_timetolive = 0; test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, strlen(nottlmsg)+1); tst_resm(TPASS, "Send a message with no timeout"); /* And finally a fragmented message that will time out. */ sinfo->sinfo_ppid = ppid; sinfo->sinfo_stream = stream; memset(ttlfrag, '0', sizeof(ttlfrag)); ttlfrag[sizeof(ttlfrag)-1] = '\0'; outmessage.msg_iov->iov_base = ttlfrag; outmessage.msg_iov->iov_len = sizeof(ttlfrag); outmessage.msg_name = NULL; outmessage.msg_namelen = 0; sinfo->sinfo_assoc_id = associd1; sinfo->sinfo_timetolive = 2000; test_sendmsg(sk1, &outmessage, MSG_NOSIGNAL, sizeof(ttlfrag)); tst_resm(TPASS, "Send a fragmented message with timeout"); /* Sleep waiting for the message to time out. */ tst_resm(TINFO, " ** SLEEPING for 3 seconds **"); sleep(3); /* Read the fillmsg snuck in between the ttl'd messages. */ do { inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); } while (!(inmessage.msg_flags & MSG_EOR)); /* Now get the message that did NOT time out. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); test_check_msg_data(&inmessage, error, strlen(nottlmsg) + 1, MSG_EOR, stream, ppid); if (0 != strncmp(iov.iov_base, nottlmsg, strlen(nottlmsg)+1)) tst_brkm(TBROK, NULL, "Received Wrong Message !!!"); tst_resm(TPASS, "Receive message with no timeout"); /* Get the SEND_FAILED notification for the message that DID * time out. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); test_check_msg_notification(&inmessage, error, sizeof(struct sctp_send_failed) + strlen(ttlmsg) + 1, SCTP_SEND_FAILED, 0); ssf = (struct sctp_send_failed *)iov.iov_base; if (0 != strncmp(ttlmsg, (char *)ssf->ssf_data, strlen(ttlmsg) + 1)) tst_brkm(TBROK, NULL, "SEND_FAILED data mismatch"); tst_resm(TPASS, "Receive SEND_FAILED for message with timeout"); /* Get the SEND_FAILED notification for the fragmented message that * DID time out. */ offset = 0; do { inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); test_check_msg_notification(&inmessage, error, sizeof(struct sctp_send_failed) + SMALL_MAXSEG, SCTP_SEND_FAILED, 0); ssf = (struct sctp_send_failed *)iov.iov_base; if (0 != strncmp(&ttlfrag[offset], (char *)ssf->ssf_data, SMALL_MAXSEG)) tst_brkm(TBROK, NULL, "SEND_FAILED data mismatch"); offset += SMALL_MAXSEG; } while (!(ssf->ssf_info.sinfo_flags & 0x01)); /* LAST_FRAG */ tst_resm(TPASS, "Receive SEND_FAILED for fragmented message with " "timeout"); /* Shut down the link. */ close(sk1); /* Get the shutdown complete notification. */ inmessage.msg_controllen = sizeof(incmsg); error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); test_check_msg_notification(&inmessage, error, sizeof(struct sctp_assoc_change), SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); close(sk2); /* Indicate successful completion. */ tst_exit(); }
int main(int argc, char *argv[]) { int sk1, sk2; sockaddr_storage_t loop1; sockaddr_storage_t loop2; sockaddr_storage_t msgname; int error; int pf_class; uint32_t ppid; uint32_t stream; struct sctp_event_subscribe subscribe; char *big_buffer; int offset, msg_flags; socklen_t msgname_len; size_t buflen; struct sctp_send_failed *ssf; struct sctp_sndrcvinfo sinfo; struct sctp_sndrcvinfo snd_sinfo; sctp_assoc_t associd1; socklen_t len, oldlen; struct sctp_status gstatus; /* 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; loop1.v6.sin6_family = AF_INET6; loop1.v6.sin6_addr = in6addr_loopback; loop1.v6.sin6_port = htons(SCTP_TESTPORT_1); loop2.v6.sin6_family = AF_INET6; loop2.v6.sin6_addr = in6addr_loopback; loop2.v6.sin6_port = htons(SCTP_TESTPORT_2); #else pf_class = PF_INET; 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); #endif /* TEST_V6 */ /* Create the two endpoints which will talk to each other. */ sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); /* Set the MAXSEG to something smallish. */ { int val = SMALL_MAXSEG; test_setsockopt(sk1, SCTP_MAXSEG, &val, sizeof(val)); } memset(&subscribe, 0, sizeof(subscribe)); subscribe.sctp_data_io_event = 1; subscribe.sctp_association_event = 1; subscribe.sctp_send_failure_event = 1; test_setsockopt(sk1, SCTP_EVENTS, &subscribe, sizeof(subscribe)); test_setsockopt(sk2, SCTP_EVENTS, &subscribe, sizeof(subscribe)); /* Bind these sockets to the test ports. */ test_bind(sk1, &loop1.sa, sizeof(loop1)); test_bind(sk2, &loop2.sa, sizeof(loop2)); /* * Set the RWND small so we can fill it up easily. * then reset RCVBUF to avoid frame droppage */ len = sizeof(int); error = getsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &oldlen, &len); if (error) tst_brkm(TBROK, tst_exit, "can't get rcvbuf size: %s", strerror(errno)); len = SMALL_RCVBUF; /* Really becomes 2xlen when set. */ error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); if (error) tst_brkm(TBROK, tst_exit, "setsockopt(SO_RCVBUF): %s", strerror(errno)); /* Mark sk2 as being able to accept new associations. */ test_listen(sk2, 1); /* Send the first message. This will create the association. */ ppid = rand(); stream = 1; test_sctp_sendmsg(sk1, message, strlen(message) + 1, (struct sockaddr *)&loop2, sizeof(loop2), ppid, 0, stream, 0, 0); tst_resm(TPASS, "sctp_sendmsg"); /* Get the communication up message on sk2. */ buflen = REALLY_BIG; big_buffer = test_malloc(buflen); msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); #if 0 associd2 = ((struct sctp_assoc_change *)big_buffer)->sac_assoc_id; #endif test_check_buf_notification(big_buffer, error, msg_flags, sizeof(struct sctp_assoc_change), SCTP_ASSOC_CHANGE, SCTP_COMM_UP); /* restore the rcvbuffer size for the receiving socket */ error = setsockopt(sk2, SOL_SOCKET, SO_RCVBUF, &oldlen, sizeof(oldlen)); if (error) tst_brkm(TBROK, tst_exit, "setsockopt(SO_RCVBUF): %s", strerror(errno)); /* Get the communication up message on sk1. */ buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk1, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); associd1 = ((struct sctp_assoc_change *)big_buffer)->sac_assoc_id; test_check_buf_notification(big_buffer, error, msg_flags, sizeof(struct sctp_assoc_change), SCTP_ASSOC_CHANGE, SCTP_COMM_UP); tst_resm(TPASS, "sctp_recvmsg SCTP_COMM_UP notification"); /* Get the first message which was sent. */ buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_data(big_buffer, error, msg_flags, &sinfo, strlen(message) + 1, MSG_EOR, stream, ppid); tst_resm(TPASS, "sctp_recvmsg data"); /* Figure out how big to make our fillmsg */ len = sizeof(struct sctp_status); memset(&gstatus,0,sizeof(struct sctp_status)); gstatus.sstat_assoc_id = associd1; error = getsockopt(sk1, IPPROTO_SCTP, SCTP_STATUS, &gstatus, &len); if (error) tst_brkm(TBROK, tst_exit, "can't get rwnd size: %s", strerror(errno)); tst_resm(TINFO, "creating a fillmsg of size %d", gstatus.sstat_rwnd+RWND_SLOP); fillmsg = malloc(gstatus.sstat_rwnd+RWND_SLOP); /* Send a fillmsg */ memset(fillmsg, 'X', gstatus.sstat_rwnd+RWND_SLOP); fillmsg[gstatus.sstat_rwnd+RWND_SLOP-1] = '\0'; ppid++; stream++; test_sctp_sendmsg(sk1, fillmsg, gstatus.sstat_rwnd+RWND_SLOP, (struct sockaddr *)&loop2, sizeof(loop2), ppid, 0, stream, 0, 0); /* Now send a message that will timeout. */ test_sctp_sendmsg(sk1, ttlmsg, strlen(ttlmsg) + 1, (struct sockaddr *)&loop2, sizeof(loop2), ppid, 0, stream, 2000, 0); tst_resm(TPASS, "sctp_sendmsg with ttl"); /* Next send a message that won't time out. */ test_sctp_sendmsg(sk1, nottlmsg, strlen(nottlmsg) + 1, (struct sockaddr *)&loop2, sizeof(loop2), ppid, 0, stream, 0, 0); tst_resm(TPASS, "sctp_sendmsg with zero ttl"); /* And finally a fragmented message that will time out. */ memset(ttlfrag, '0', sizeof(ttlfrag)); ttlfrag[sizeof(ttlfrag)-1] = '\0'; test_sctp_sendmsg(sk1, ttlfrag, sizeof(ttlfrag), (struct sockaddr *)&loop2, sizeof(loop2), ppid, 0, stream, 2000, 0); tst_resm(TPASS, "sctp_sendmsg fragmented msg with ttl"); /* Sleep waiting for the message to time out. */ tst_resm(TINFO, "** SLEEPING for 3 seconds **"); sleep(3); /* Get the fillmsg. */ do { buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); } while (!(msg_flags & MSG_EOR)); /* Get the message that did NOT time out. */ buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_data(big_buffer, error, msg_flags, &sinfo, strlen(nottlmsg) + 1, MSG_EOR, stream, ppid); if (0 != strncmp(big_buffer, nottlmsg, strlen(nottlmsg))) tst_brkm(TBROK, tst_exit, "sctp_recvmsg: Wrong Message !!!"); tst_resm(TPASS, "sctp_recvmsg msg with zero ttl"); /* Get the SEND_FAILED notification for the message that DID * time out. */ buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk1, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_notification(big_buffer, error, msg_flags, sizeof(struct sctp_send_failed) + strlen(ttlmsg) + 1, SCTP_SEND_FAILED, 0); ssf = (struct sctp_send_failed *)big_buffer; if (0 != strncmp(ttlmsg, (char *)ssf->ssf_data, strlen(ttlmsg) + 1)) tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch"); tst_resm(TPASS, "sctp_recvmsg SEND_FAILED for message with ttl"); offset = 0; /* Get the SEND_FAILED notifications for the fragmented message that * timed out. */ do { buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk1, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_notification(big_buffer, error, msg_flags, sizeof(struct sctp_send_failed) + SMALL_MAXSEG, SCTP_SEND_FAILED, 0); ssf = (struct sctp_send_failed *)big_buffer; if (0 != strncmp(&ttlfrag[offset], (char *)ssf->ssf_data, SMALL_MAXSEG)) tst_brkm(TBROK, tst_exit, "SEND_FAILED data mismatch"); offset += SMALL_MAXSEG; } while (!(ssf->ssf_info.sinfo_flags & 0x01)); /* LAST FRAG */ tst_resm(TPASS, "sctp_recvmsg SEND_FAILED for fragmented message with " "ttl"); snd_sinfo.sinfo_ppid = rand(); snd_sinfo.sinfo_flags = 0; snd_sinfo.sinfo_stream = 2; snd_sinfo.sinfo_timetolive = 0; snd_sinfo.sinfo_assoc_id = associd1; test_sctp_send(sk1, message, strlen(message) + 1, &snd_sinfo, MSG_NOSIGNAL); buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_data(big_buffer, error, msg_flags, &sinfo, strlen(message) + 1, MSG_EOR, snd_sinfo.sinfo_stream, snd_sinfo.sinfo_ppid); tst_resm(TPASS, "sctp_send"); /* Shut down the link. */ close(sk1); /* Get the shutdown complete notification. */ buflen = REALLY_BIG; msgname_len = sizeof(msgname); msg_flags = 0; error = test_sctp_recvmsg(sk2, big_buffer, buflen, (struct sockaddr *)&msgname, &msgname_len, &sinfo, &msg_flags); test_check_buf_notification(big_buffer, error, msg_flags, sizeof(struct sctp_assoc_change), SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); close(sk2); /* Indicate successful completion. */ return 0; }
int main (int argc, const char *argv[]) { int rc; int sb; int sc; int sb2; int opt; size_t sz; int i; char any_address[128]; test_addr_from (socket_address, "ws", "127.0.0.1", get_test_port (argc, argv)); test_addr_from (any_address, "ws", "*", get_test_port (argc, argv)); /* Try closing bound but unconnected socket. */ sb = test_socket (AF_SP, NN_PAIR); test_bind (sb, any_address); test_close (sb); /* Try closing a TCP socket while it not connected. At the same time test specifying the local address for the connection. */ sc = test_socket (AF_SP, NN_PAIR); test_connect (sc, socket_address); test_close (sc); /* Open the socket anew. */ sc = test_socket (AF_SP, NN_PAIR); /* Check socket options. */ sz = sizeof (opt); rc = nn_getsockopt (sc, NN_WS, NN_WS_MSG_TYPE, &opt, &sz); errno_assert (rc == 0); nn_assert (sz == sizeof (opt)); nn_assert (opt == NN_WS_MSG_TYPE_BINARY); /* Default port 80 should be assumed if not explicitly declared. */ rc = nn_connect (sc, "ws://127.0.0.1"); errno_assert (rc >= 0); /* Try using invalid address strings. */ rc = nn_connect (sc, "ws://*:"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://*:1000000"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://*:some_port"); nn_assert (rc < 0); rc = nn_connect (sc, "ws://eth10000;127.0.0.1:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == ENODEV); rc = nn_bind (sc, "ws://127.0.0.1:"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_bind (sc, "ws://127.0.0.1:1000000"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_bind (sc, "ws://eth10000:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == ENODEV); rc = nn_connect (sc, "ws://:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://-hostname:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://abc.123.---.#:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://[::1]:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://abc.123.:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://abc...123:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); rc = nn_connect (sc, "ws://.123:5555"); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); test_close (sc); sb = test_socket (AF_SP, NN_PAIR); test_bind (sb, socket_address); sc = test_socket (AF_SP, NN_PAIR); test_connect (sc, socket_address); /* Ping-pong test. */ for (i = 0; i != 100; ++i) { test_send (sc, "ABC"); test_recv (sb, "ABC"); test_send (sb, "DEF"); test_recv (sc, "DEF"); } /* Batch transfer test. */ for (i = 0; i != 100; ++i) { test_send (sc, "0123456789012345678901234567890123456789"); } for (i = 0; i != 100; ++i) { test_recv (sb, "0123456789012345678901234567890123456789"); } test_close (sc); test_close (sb); /* Test two sockets binding to the same address. */ sb = test_socket (AF_SP, NN_PAIR); test_bind (sb, socket_address); sb2 = test_socket (AF_SP, NN_PAIR); rc = nn_bind (sb2, socket_address); nn_assert (rc < 0); errno_assert (nn_errno () == EADDRINUSE); test_close(sb); test_close(sb2); /* Test that NN_RCVMAXSIZE can be -1, but not lower */ sb = test_socket (AF_SP, NN_PAIR); opt = -1; rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); nn_assert (rc >= 0); opt = -2; rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); nn_assert (rc < 0); errno_assert (nn_errno () == EINVAL); test_close (sb); /* Test NN_RCVMAXSIZE limit */ sb = test_socket (AF_SP, NN_PAIR); test_bind (sb, socket_address); sc = test_socket (AF_SP, NN_PAIR); test_connect (sc, socket_address); opt = 1000; test_setsockopt (sc, NN_SOL_SOCKET, NN_SNDTIMEO, &opt, sizeof (opt)); nn_assert (opt == 1000); opt = 1000; test_setsockopt (sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sizeof (opt)); nn_assert (opt == 1000); opt = 4; test_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); test_send (sc, "ABC"); test_recv (sb, "ABC"); test_send (sc, "ABCD"); test_recv (sb, "ABCD"); test_send (sc, "ABCDE"); test_drop (sb, ETIMEDOUT); /* Increase the size limit, reconnect, then try sending again. The reason a reconnect is necessary is because after a protocol violation, the connecting socket will not continue automatic reconnection attempts. */ opt = 5; test_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt)); test_connect (sc, socket_address); test_send (sc, "ABCDE"); test_recv (sb, "ABCDE"); test_close (sb); test_close (sc); test_text (); /* Test closing a socket that is waiting to connect. */ sc = test_socket (AF_SP, NN_PAIR); test_connect (sc, socket_address); nn_sleep (100); test_close (sc); return 0; }
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(); }
int main (int argc, const char *argv[]) { int rc; int pair; int pull; int timeo; char socket_address_tcp[128]; test_addr_from(socket_address_tcp, "tcp", "127.0.0.1", get_test_port(argc, argv)); /* Inproc: Bind first, connect second. */ pair = test_socket (AF_SP, NN_PAIR); test_bind (pair, SOCKET_ADDRESS_INPROC); pull = test_socket (AF_SP, NN_PULL); test_connect (pull, SOCKET_ADDRESS_INPROC); timeo = 100; test_setsockopt (pair, NN_SOL_SOCKET, NN_SNDTIMEO, &timeo, sizeof (timeo)); rc = nn_send (pair, "ABC", 3, 0); errno_assert (rc < 0 && nn_errno () == ETIMEDOUT); test_close (pull); test_close (pair); /* Inproc: Connect first, bind second. */ pull = test_socket (AF_SP, NN_PULL); test_connect (pull, SOCKET_ADDRESS_INPROC); pair = test_socket (AF_SP, NN_PAIR); test_bind (pair, SOCKET_ADDRESS_INPROC); timeo = 100; test_setsockopt (pair, NN_SOL_SOCKET, NN_SNDTIMEO, &timeo, sizeof (timeo)); rc = nn_send (pair, "ABC", 3, 0); errno_assert (rc < 0 && nn_errno () == ETIMEDOUT); test_close (pull); test_close (pair); #if !defined NN_HAVE_WINDOWS /* IPC */ pair = test_socket (AF_SP, NN_PAIR); test_bind (pair, SOCKET_ADDRESS_IPC); pull = test_socket (AF_SP, NN_PULL); test_connect (pull, SOCKET_ADDRESS_IPC); timeo = 100; test_setsockopt (pair, NN_SOL_SOCKET, NN_SNDTIMEO, &timeo, sizeof (timeo)); rc = nn_send (pair, "ABC", 3, 0); errno_assert (rc < 0 && nn_errno () == ETIMEDOUT); test_close (pull); test_close (pair); #endif /* TCP */ pair = test_socket (AF_SP, NN_PAIR); test_bind (pair, socket_address_tcp); pull = test_socket (AF_SP, NN_PULL); test_connect (pull, socket_address_tcp); timeo = 100; test_setsockopt (pair, NN_SOL_SOCKET, NN_SNDTIMEO, &timeo, sizeof (timeo)); rc = nn_send (pair, "ABC", 3, 0); errno_assert (rc < 0 && nn_errno () == ETIMEDOUT); test_close (pull); test_close (pair); return 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; }