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 
56 char *TCID = __FILE__;
57 int TST_TOTAL = 4;
58 int TST_CNT = 0;
59 
60 int
main(int argc,char * argv[])61 main(int argc, char *argv[])
62 {
63         int msg_count;
64 	socklen_t len;
65 	int sk,sk1,pf_class,lstn_sk,acpt_sk,flag;
66         char *message = "hello, world!\n";
67         char *message_rcv;
68         int count;
69 
70         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
71 
72 	/* Rather than fflush() throughout the code, set stdout to
73          * be unbufferd
74          */
75         setvbuf(stdout, NULL, _IONBF, 0);
76         setvbuf(stderr, NULL, _IONBF, 0);
77 
78         pf_class = PF_INET;
79 
80         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
81 
82         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
83 
84 	message_rcv = malloc(512);
85 	conn_addr.sin_family = AF_INET;
86         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
87         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
88 
89 	lstn_addr.sin_family = AF_INET;
90         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
91         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
92 
93 	/*Binding the listen socket*/
94         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
95 
96         /*Listening the socket*/
97         test_listen(lstn_sk, 10);
98 
99 	len = sizeof(struct sockaddr_in);
100 	flag = MSG_NOSIGNAL;
101 
102 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
103 
104 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
105 
106 	msg_count = strlen(message) + 1;
107 
108 	/*sendto() TEST1: Sending data from client socket to server socket*/
109 	count = sendto(sk, message, msg_count, flag,
110 		       (const struct sockaddr *) &conn_addr, len);
111 	if (count != msg_count)
112 		tst_brkm(TBROK, tst_exit, "sendto from client to server "
113                          "count:%d, errno:%d", count, errno);
114 
115 	tst_resm(TPASS, "sendto() from client to server - SUCCESS");
116 
117 	test_recv(acpt_sk, message_rcv, msg_count, flag);
118 
119 	strncpy(message_rcv,"\0",512);
120 
121 	/*sendto() TEST2: Sending data from accept socket to client socket*/
122 	count = sendto(acpt_sk, message, msg_count, flag,
123 		       (const struct sockaddr *) &svr_addr, len);
124 	if (count != msg_count)
125 		tst_brkm(TBROK, tst_exit, "sendto from accept socket to client "
126                          "count:%d, errno:%d", count, errno);
127 
128 	tst_resm(TPASS, "sendto() from accept socket to client - SUCCESS");
129 
130 	test_recv(sk, message_rcv, msg_count, flag);
131 
132         close(sk);
133         close(acpt_sk);
134 
135         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
136 
137 	/*sendto() TEST3: Sending data from unconnected client socket to
138         server socket*/
139         count = sendto(sk1, message, msg_count, flag,
140 		       (const struct sockaddr *) &conn_addr, len);
141         if (count != msg_count)
142 		tst_brkm(TBROK, tst_exit, "sendto from unconnected client to "
143 			 "server count:%d, errno:%d", count, errno);
144 
145 	tst_resm(TPASS, "sendto() from unconnected client to server - SUCCESS");
146 
147         acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
148 
149         test_recv(acpt_sk, message_rcv, msg_count, flag);
150 
151 	/*send() TEST4: Sending less number of data from the buffer*/
152 	/*Sending only 5 bytes so that only hello is received*/
153 	test_sendto(sk, message, 5 , flag, (const struct sockaddr *)&conn_addr,
154 		    len);
155 	test_recv(acpt_sk, message_rcv, 5, flag);
156 
157 	tst_resm(TPASS, "sendto() partial data from a buffer - SUCCESS");
158 
159 	close(sk1);
160 	close(lstn_sk);
161 	close(acpt_sk);
162 	return 0;
163 
164 }
165