• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2001, 2003
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * The SCTP implementation is free software;
10  * you can redistribute it and/or modify it under the terms of
11  * the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * The SCTP implementation is distributed in the hope that it
16  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  *                 ************************
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with GNU CC; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email address(es):
28  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
29  *
30  * Or submit a bug report through the following website:
31  *    http://www.sf.net/projects/lksctp
32  *
33  * Any bugs reported to us we will try to fix... any fixes shared will
34  * be incorporated into the next SCTP release.
35  *
36  * Written or modified by:
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Karl Knutson <karl@athena.chicago.il.us>
39  *    Randall Stewart <randall@stewart.chicago.il.us>
40  *    Ken Morneau <kmorneau@cisco.com>
41  *    Qiaobing Xie <qxie1@motorola.com>
42  *    Daisy Chang <daisyc@us.ibm.com>
43  *    Jon Grimm <jgrimm@us.ibm.com>
44  *    Sridhar Samudrala <samudrala@us.ibm.com>
45  *    Hui Huang <hui.huang@nokia.com>
46  */
47 
48 #ifndef __sctputil_h__
49 #define __sctputil_h__
50 
51 #ifdef LTP
52 #include <test.h>
53 #include <usctest.h>
54 #endif
55 
56 #include <string.h>
57 
58 typedef union {
59 	struct sockaddr_in v4;
60 	struct sockaddr_in6 v6;
61 	struct sockaddr sa;
62 } sockaddr_storage_t;
63 
64 
65 #define REALLY_BIG 65536
66 
67 /* Literal defines.  */
68 #ifdef PROT_SOCK
69 #define SCTP_TESTPORT_1 PROT_SOCK
70 #else
71 #define SCTP_TESTPORT_1 1024
72 #endif
73 #define SCTP_TESTPORT_2 (SCTP_TESTPORT_1+1)
74 
75 #define SCTP_IP_BCAST  	htonl(0xffffffff)
76 #define SCTP_IP_LOOPBACK  htonl(0x7f000001)
77 
78 /* These are stolen from <netinet/in.h>.  */
79 #define SCTP_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
80 #define SCTP_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
81 
82 /* Display an IPv4 address in readable format.  */
83 #define NIPQUAD(addr) \
84         ((unsigned char *)&addr)[0], \
85         ((unsigned char *)&addr)[1], \
86         ((unsigned char *)&addr)[2], \
87         ((unsigned char *)&addr)[3]
88 
89 /* Display an IPv6 address in readable format.  */
90 #define NIP6(addr) \
91         ntohs((addr).s6_addr16[0]), \
92         ntohs((addr).s6_addr16[1]), \
93         ntohs((addr).s6_addr16[2]), \
94         ntohs((addr).s6_addr16[3]), \
95         ntohs((addr).s6_addr16[4]), \
96         ntohs((addr).s6_addr16[5]), \
97         ntohs((addr).s6_addr16[6]), \
98         ntohs((addr).s6_addr16[7])
99 
100 #define DUMP_CORE { 					 \
101 	char *diediedie = 0;				 \
102 	printf("DUMP_CORE %s: %d\n", __FILE__, __LINE__);\
103 	*diediedie = 0;					 \
104 }
105 
106 #ifndef LTP
107 enum {
108 	TPASS,
109 	TINFO,
110 };
111 
112 extern char *TCID;
113 extern int TST_TOTAL;
114 extern int TST_CNT;
115 
116 #define tst_brkm(a1, a2, whatever...) \
117 	{ \
118 		printf("%s %2d BROK : ", TCID, ++TST_CNT); \
119 		printf(whatever); \
120 		printf("\n"); \
121 		DUMP_CORE \
122 	}
123 #define tst_resm(a1, whatever...) \
124 	{ \
125 		printf("%s %2d %s : ", TCID, \
126 			 (a1 == TPASS)?++TST_CNT:0, \
127 			 (a1 == TPASS)?"PASS":"INFO"); \
128 		printf(whatever); \
129 		printf("\n"); \
130 	}
131 #endif
132 
test_socket(int domain,int type,int protocol)133 static inline int test_socket(int domain, int type, int protocol)
134 {
135 	int sk = socket(domain, type, protocol);
136 
137 	if (sk == -1) {
138 		if (errno == EAFNOSUPPORT)
139 			tst_brkm(TCONF | TERRNO, tst_exit, "socket(%i, %i, %i) not supported", domain,
140 					 type, protocol);
141 
142 		tst_brkm(TBROK | TERRNO, tst_exit, "socket()");
143 	}
144 
145 	return sk;
146 }
147 
test_bind(int sk,struct sockaddr * addr,socklen_t addrlen)148 static inline int test_bind(int sk, struct sockaddr *addr, socklen_t addrlen)
149 {
150 	int error = bind(sk, addr, addrlen);
151 
152 	if (error == -1)
153 		tst_brkm(TBROK | TERRNO, tst_exit, "bind()");
154 
155 	return error;
156 }
157 
test_bindx_add(int sk,struct sockaddr * addr,int count)158 static inline int test_bindx_add(int sk, struct sockaddr *addr, int count)
159 {
160 	int error = sctp_bindx(sk, addr, count, SCTP_BINDX_ADD_ADDR);
161 
162 	if (error == -1)
163 		tst_brkm(TBROK | TERRNO, tst_exit, "sctp_bindx()");
164 
165 	return error;
166 }
167 
test_listen(int sk,int backlog)168 static inline int test_listen(int sk, int backlog)
169 {
170 	int error = listen(sk, backlog);
171 
172 	if (error == -1)
173 		tst_brkm(TBROK | TERRNO, tst_exit, "listen()");
174 
175 	return error;
176 }
177 
test_connect(int sk,struct sockaddr * addr,socklen_t addrlen)178 static inline int test_connect(int sk, struct sockaddr *addr, socklen_t addrlen)
179 {
180 	int error = connect(sk, addr, addrlen);
181 
182 	if (error == -1)
183 		tst_brkm(TBROK | TERRNO, tst_exit, "connect()");
184 
185 	return error;
186 }
187 
test_connectx(int sk,struct sockaddr * addr,int count)188 static inline int test_connectx(int sk, struct sockaddr *addr, int count)
189 {
190 	int error = sctp_connectx(sk, addr, count, NULL);
191 
192 	if (error == -1)
193 		tst_brkm(TBROK | TERRNO, tst_exit, "connectx()");
194 
195 	return error;
196 }
197 
test_accept(int sk,struct sockaddr * addr,socklen_t * addrlen)198 static inline int test_accept(int sk, struct sockaddr *addr, socklen_t *addrlen)
199 {
200 	int error = accept(sk, addr, addrlen);
201 
202 	if (error == -1)
203 		tst_brkm(TBROK | TERRNO, tst_exit, "accept()");
204 
205 	return error;
206 }
207 
test_send(int sk,const void * msg,size_t len,int flags)208 static inline int test_send(int sk, const void *msg, size_t len, int flags)
209 {
210 	int error = send(sk, msg, len, flags);
211 
212 	if ((long)len != error)
213 		tst_brkm(TBROK | TERRNO, tst_exit, "send(): error: %d", error);
214 
215 	return error;
216 }
217 
test_sendto(int sk,const void * msg,size_t len,int flags,const struct sockaddr * to,socklen_t tolen)218 static inline int test_sendto(int sk, const void *msg, size_t len, int flags,
219 			      const struct sockaddr *to, socklen_t tolen)
220 {
221 	int error = sendto(sk, msg, len, flags, to, tolen);
222 
223 	if ((long)len != error)
224 		tst_brkm(TBROK | TERRNO, tst_exit, "sendto(): error: %d", error);
225 
226 	return error;
227 }
228 
test_sendmsg(int sk,const struct msghdr * msg,int flags,int msglen)229 static inline int test_sendmsg(int sk, const struct msghdr *msg, int flags,
230 			       int msglen)
231 {
232 	int error = sendmsg(sk, msg, flags);
233 
234 	if (msglen != error)
235 		tst_brkm(TBROK | TERRNO, tst_exit, "sendmsg(): error: %d", error);
236 
237 	return error;
238 }
239 
test_recv(int sk,void * buf,size_t len,int flags)240 static inline int test_recv(int sk, void *buf, size_t len, int flags)
241 {
242 	int error = recv(sk, buf, len, flags);
243 
244 	if (error == -1)
245 		tst_brkm(TBROK | TERRNO, tst_exit, "recv()");
246 
247 	return error;
248 }
249 
test_recvmsg(int sk,struct msghdr * msg,int flags)250 static inline int test_recvmsg(int sk, struct msghdr *msg, int flags)
251 {
252 	int error = recvmsg(sk, msg, flags);
253 
254 	if (error == -1)
255 		tst_brkm(TBROK | TERRNO, tst_exit, "recvmsg()");
256 
257 	return error;
258 }
259 
test_shutdown(int sk,int how)260 static inline int test_shutdown(int sk, int how)
261 {
262 	int error = shutdown(sk, how);
263 
264 	if (error == -1)
265 		tst_brkm(TBROK | TERRNO, tst_exit, "shutdown()");
266 
267 	return error;
268 }
269 
test_getsockopt(int sk,int optname,void * optval,socklen_t * optlen)270 static inline int test_getsockopt(int sk, int optname, void *optval,
271 				  socklen_t *optlen)
272 {
273 	int error = getsockopt(sk, SOL_SCTP, optname, optval, optlen);
274 
275 	if (error)
276 		tst_brkm(TBROK, tst_exit, "getsockopt(%d): %s", optname,
277 			 strerror(errno));
278 	return error;
279 }
280 
test_setsockopt(int sk,int optname,const void * optval,socklen_t optlen)281 static inline int test_setsockopt(int sk, int optname, const void *optval,
282 				  socklen_t optlen)
283 {
284 	int error = setsockopt(sk, SOL_SCTP, optname, optval, optlen);
285 
286 	if (error)
287 		tst_brkm(TBROK, tst_exit, "setsockopt(%d): %s", optname,
288 			 strerror(errno));
289 
290 	return error;
291 }
292 
test_sctp_peeloff(int sk,sctp_assoc_t assoc_id)293 static inline int test_sctp_peeloff(int sk, sctp_assoc_t assoc_id)
294 {
295 	int error = sctp_peeloff(sk, assoc_id);
296 
297 	if (error == -1)
298 		tst_brkm(TBROK | TERRNO, tst_exit, "sctp_peeloff()");
299 
300 	return error;
301 }
302 
test_sctp_sendmsg(int s,const void * msg,size_t len,struct sockaddr * to,socklen_t tolen,uint32_t ppid,uint32_t flags,uint16_t stream_no,uint32_t timetolive,uint32_t context)303 static inline int test_sctp_sendmsg(int s, const void *msg, size_t len,
304 				    struct sockaddr *to, socklen_t tolen,
305 				    uint32_t ppid, uint32_t flags,
306 				    uint16_t stream_no, uint32_t timetolive,
307 				    uint32_t context)
308 {
309 	int error = sctp_sendmsg(s, msg, len, to, tolen, ppid, flags, stream_no,
310 				 timetolive, context);
311 
312 	if (error != (long)len)
313 		tst_brkm(TBROK, tst_exit, "sctp_sendmsg: error:%d errno:%d",
314 			 error, errno);
315 
316 	return error;
317 }
318 
test_sctp_send(int s,const void * msg,size_t len,const struct sctp_sndrcvinfo * sinfo,int flags)319 static inline int test_sctp_send(int s, const void *msg, size_t len,
320 				 const struct sctp_sndrcvinfo *sinfo,
321 				 int flags)
322 {
323 	int error = sctp_send(s, msg, len, sinfo, flags);
324 
325 	if (error != (long)len)
326 		tst_brkm(TBROK, tst_exit, "sctp_send: error:%d errno:%d",
327 			 error, errno);
328 
329 	return error;
330 }
331 
test_sctp_recvmsg(int sk,void * msg,size_t len,struct sockaddr * from,socklen_t * fromlen,struct sctp_sndrcvinfo * sinfo,int * msg_flags)332 static inline int test_sctp_recvmsg(int sk, void *msg, size_t len,
333 				    struct sockaddr *from, socklen_t *fromlen,
334 				    struct sctp_sndrcvinfo *sinfo,
335 				    int *msg_flags)
336 {
337 	int error = sctp_recvmsg(sk, msg, len, from, fromlen, sinfo, msg_flags);
338 
339 	if (error == -1)
340 		tst_brkm(TBROK | TERRNO, tst_exit, "sctp_recvmsg()");
341 
342 	return error;
343 }
344 
test_malloc(size_t size)345 static inline void *test_malloc(size_t size)
346 {
347 	void *buf = malloc(size);
348 
349 	if (NULL == buf)
350 		tst_brkm(TBROK, tst_exit, "malloc failed");
351 
352 	return buf;
353 }
354 
355 void test_check_msg_notification(struct msghdr *, int, int, uint16_t, uint32_t);
356 void test_check_buf_notification(void *, int, int, int, uint16_t, uint32_t);
357 void test_check_msg_data(struct msghdr *, int, int, int, uint16_t, uint32_t);
358 void test_check_buf_data(void *, int, int, struct sctp_sndrcvinfo *, int, int,
359 			 uint16_t, uint32_t);
360 void *test_build_msg(int);
361 void test_enable_assoc_change(int);
362 void test_print_message(int sk, struct msghdr *msg, size_t msg_len);
363 int test_peer_addr(int sk, sctp_assoc_t asoc, sockaddr_storage_t *peers, int count);
364 
365 #endif /* __sctputil_h__ */
366