• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file has test cases to test the sendto () call
6  * for 1-1 style sockets
7  *
8  * TEST1: Sending data from client socket to server socket
9  * TEST2: Sending data from accept (server) socket to client socket
10  * TEST3: Sending data from unconnected client socket to server
11  * TEST4: sending partial data from a buffer
12  *
13  * The SCTP implementation is free software;
14  * you can redistribute it and/or modify it under the terms of
15  * the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * The SCTP implementation is distributed in the hope that it
20  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21  *                 ************************
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with GNU CC; see the file COPYING.  If not, write to
27  * the Free Software Foundation, 59 Temple Place - Suite 330,
28  * Boston, MA 02111-1307, USA.
29  *
30  * Please send any bug reports or fixes you make to the
31  * email address(es):
32  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
33  *
34  * Or submit a bug report through the following website:
35  *    http://www.sf.net/projects/lksctp
36  *
37  * Any bugs reported given to us we will try to fix... any fixes shared will
38  * be incorporated into the next SCTP release
39  */
40 
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>         /* for sockaddr_in */
49 #include <arpa/inet.h>
50 #include <errno.h>
51 #include <netinet/sctp.h>
52 #include <sys/uio.h>
53 #include <linux/socket.h>
54 #include <sctputil.h>
55 #include "tst_kernel.h"
56 
57 char *TCID = __FILE__;
58 int TST_TOTAL = 4;
59 int TST_CNT = 0;
60 
61 int
main(void)62 main(void)
63 {
64         int msg_count;
65 	socklen_t len;
66 	int sk,sk1,pf_class,lstn_sk,acpt_sk,flag;
67         char *message = "hello, world!\n";
68         char *message_rcv;
69         int count;
70 
71         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
72 
73 	if (tst_check_driver("sctp"))
74 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
75 
76 	/* Rather than fflush() throughout the code, set stdout to
77          * be unbufferd
78          */
79         setvbuf(stdout, NULL, _IONBF, 0);
80         setvbuf(stderr, NULL, _IONBF, 0);
81 
82         pf_class = PF_INET;
83 
84         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
85 
86         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
87 
88 	message_rcv = malloc(512);
89 	conn_addr.sin_family = AF_INET;
90         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
91         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
92 
93 	lstn_addr.sin_family = AF_INET;
94         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
95         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
96 
97 	/*Binding the listen socket*/
98         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
99 
100         /*Listening the socket*/
101         test_listen(lstn_sk, 10);
102 
103 	len = sizeof(struct sockaddr_in);
104 	flag = MSG_NOSIGNAL;
105 
106 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
107 
108 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
109 
110 	msg_count = strlen(message) + 1;
111 
112 	/*sendto() TEST1: Sending data from client socket to server socket*/
113 	count = sendto(sk, message, msg_count, flag,
114 		       (const struct sockaddr *) &conn_addr, len);
115 	if (count != msg_count)
116 		tst_brkm(TBROK, tst_exit, "sendto from client to server "
117                          "count:%d, errno:%d", count, errno);
118 
119 	tst_resm(TPASS, "sendto() from client to server - SUCCESS");
120 
121 	test_recv(acpt_sk, message_rcv, msg_count, flag);
122 
123 	strncpy(message_rcv,"\0",512);
124 
125 	/*sendto() TEST2: Sending data from accept socket to client socket*/
126 	count = sendto(acpt_sk, message, msg_count, flag,
127 		       (const struct sockaddr *) &svr_addr, len);
128 	if (count != msg_count)
129 		tst_brkm(TBROK, tst_exit, "sendto from accept socket to client "
130                          "count:%d, errno:%d", count, errno);
131 
132 	tst_resm(TPASS, "sendto() from accept socket to client - SUCCESS");
133 
134 	test_recv(sk, message_rcv, msg_count, flag);
135 
136         close(sk);
137         close(acpt_sk);
138 
139         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
140 
141 	/*sendto() TEST3: Sending data from unconnected client socket to
142         server socket*/
143         count = sendto(sk1, message, msg_count, flag,
144 		       (const struct sockaddr *) &conn_addr, len);
145         if (count != msg_count)
146 		tst_brkm(TBROK, tst_exit, "sendto from unconnected client to "
147 			 "server count:%d, errno:%d", count, errno);
148 
149 	tst_resm(TPASS, "sendto() from unconnected client to server - SUCCESS");
150 
151         acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
152 
153         test_recv(acpt_sk, message_rcv, msg_count, flag);
154 
155 	/*send() TEST4: Sending less number of data from the buffer*/
156 	/*Sending only 5 bytes so that only hello is received*/
157 	test_sendto(sk, message, 5 , flag, (const struct sockaddr *)&conn_addr,
158 		    len);
159 	test_recv(acpt_sk, message_rcv, 5, flag);
160 
161 	tst_resm(TPASS, "sendto() partial data from a buffer - SUCCESS");
162 
163 	close(sk1);
164 	close(lstn_sk);
165 	close(acpt_sk);
166 	return 0;
167 
168 }
169