Debian Codegraph @

src/apps/sctp_test.c

raw symbols SCIP
1 /* SCTP kernel Implementation
2 * (C) Copyright IBM Corp. 2001, 2003
3 * Copyright (c) 1999 Cisco
4 * Copyright (c) 1999, 2000, 2001 Motorola
5 * Copyright (c) 2001-2002 Nokia
6 * Copyright (c) 2001 La Monte H.P. Yarroll
7 *
8 * This is a userspace test application for the SCTP kernel
9 * implementation.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it
17 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
18 * ^^^^^^^^^^^^^^^^^^^^^^^^
19 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 * See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with GNU CC; see the file COPYING. If not, write to
24 * the Free Software Foundation, 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
26 *
27 * Please send any bug reports or fixes you make to the
28 * email address(es):
29 * lksctp developers <[email protected]>
30 *
31 * Or submit a bug report through the following website:
32 * https://github.com/sctp/lksctp-tools/
33 *
34 * Written or modified by:
35 * Hui Huang <[email protected]>
36 * Sridhar Samudrala <[email protected]>
37 * Jon Grimm <[email protected]>
38 * Daisy Chang <[email protected]>
39 * Ryan Layer <[email protected]>
40 */
41
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <time.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/uio.h>
50 #include <netinet/in.h>
51 #include <errno.h>
52 #include <netinet/sctp.h>
53 #include <string.h>
54 #include <arpa/inet.h>
55 #include <sys/wait.h>
56 #include <sys/param.h>
57 #include <fcntl.h>
58 #include <netdb.h>
59 #include <net/if.h>
60
61 #include <sys/resource.h>
62
63
64 #define REALLY_BIG 65536
65
66 #define SERVER 0
67 #define CLIENT 1
68 #define MIXED 2
69 #define NOT_DEFINED 666
70
71 #define REPEAT 10
72 #define BIG_REPEAT 1000000
73 #define MAX_BIND_RETRYS 10
74 #define BODYSIZE 10
75 #define MSG_CNT 10 /* If this is changed the msg_sizes array
76 needs to be modified accordingly. */
77
78 #define DEFAULT_MAX_WINDOW 32768
79 #define DEFAULT_MIN_WINDOW 1500
80
81 #define DEBUG_NONE 0
82 #define DEBUG_MIN 1
83 #define DEBUG_MAX 2
84
85 #define STREAM_PATTERN_SEQUENTIAL 0
86 #define STREAM_PATTERN_RANDOM 1
87
88 #define ORDER_PATTERN_UNORDERED 0
89 #define ORDER_PATTERN_ORDERED 1
90 #define ORDER_PATTERN_ALTERNATE 2
91 #define ORDER_PATTERN_RANDOM 3
92
93 #define ASSOC_PATTERN_SEQUENTIAL 0
94 #define ASSOC_PATTERN_RANDOM 1
95
96 #define NCASES 6
97 #define MAX_POLL_SKS 256
98
99 #define DEBUG_PRINT(level, print_this...) \
100 { \
101 if (debug_level >= level) { \
102 fprintf(stdout, print_this); \
103 fflush(stdout); \
104 } \
105 } /* DEBUG_PRINT */
106
107 /* Convenience structure to determine space needed for cmsg. */
108 typedef union {
109 struct sctp_initmsg init;
110 struct sctp_sndrcvinfo sndrcvinfo;
111 } _sctp_cmsg_data_t;
112
113 #ifdef __FreeBSD__
114 typedef union {
115 int raw;
116 struct sctp_initmsg init;
117 struct sctp_sndrcvinfo sndrcv;
118 } sctp_cmsg_data_t;
119 #endif
120
121 #define CMSG_SPACE_INITMSG (CMSG_SPACE(sizeof(struct sctp_initmsg)))
122 #define CMSG_SPACE_SNDRCV (CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)))
123
124 typedef struct {
125 int rem_port;
126 int order_state;
127 int stream_state;
128 int msg_cnt;
129 int msg_sent;
130 int cycle;
131 } _assoc_state;
132
133 typedef struct {
134 int sk;
135 int assoc_i;
136 _assoc_state *assoc_state;
137 } _poll_sks;
138
139 char *local_host = NULL;
140 int local_port = 0;
141 char *remote_host = NULL;
142 int remote_port = 0;
143 /* struct sockaddr_in s_rem, s_loc; */
144 struct sockaddr_storage s_rem, s_loc;
145 int r_len, l_len;
146 int test_case = 0;
147 int size_arg = 0;
148 int xflag = 0;
149 int debug_level = DEBUG_MAX;
150 int do_exit = 1;
151 int stream_pattern = STREAM_PATTERN_SEQUENTIAL;
152 int stream_state = 0;
153 int order_pattern = ORDER_PATTERN_UNORDERED;
154 int order_state = 0;
155 int max_stream = 0;
156 int seed = 0;
157 int max_msgsize = DEFAULT_MAX_WINDOW;
158 int timetolive = 0;
159 int assoc_pattern = ASSOC_PATTERN_SEQUENTIAL;
160 int socket_type = SOCK_SEQPACKET;
161 int repeat_count = 0;
162 int listeners = 0;
163 int tosend = 0;
164 _poll_sks poll_sks[MAX_POLL_SKS];
165 int repeat = REPEAT;
166 int msg_cnt = MSG_CNT;
167 int drain = 0;
168 int role = NOT_DEFINED;
169 struct sockaddr *bindx_add_addrs = NULL;
170 int bindx_add_count = 0;
171 struct sockaddr *connectx_addrs = NULL;
172 int connectx_count = 0;
173 int if_index = 0;
174
175 unsigned char msg[] = "012345678901234567890123456789012345678901234567890";
176
177 static int msg_sizes[NCASES][MSG_CNT] =
178 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
179 {1452, 2904, 4356, 1452, 2904, 4356, 1452, 2904, 4356, 1452},
180 {1453, 1453, 1453, 1453, 1453, 1453, 1453, 1453, 1453, 1453},
181 {1, 1453, 32768, 1, 1453, 32768, 1, 1453, 32768, 1},
182 {1, 1000, 2000, 3000, 5000, 10000, 15000, 20000, 25000, 32768},
183 {32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768},
184 };
185
186 static const char *sac_state_tbl[] = {
187 "COMMUNICATION_UP",
188 "COMMUNICATION_LOST",
189 "RESTART",
190 "SHUTDOWN_COMPLETE",
191 "CANT_START_ASSOCICATION"
192 };
193
194 void usage(char *argv0)
195 {
196 fprintf(stderr, "\nusage:\n");
197 fprintf(stderr, " server:\n");
198 fprintf(stderr, " %8s -H local-addr -P local-port -l [-d level] [-x]\n"
199 "\t [-L num-ports] [-S num-ports]\n"
200 "\t [-a assoc-pattern]\n"
201 "\t [-i interface]\n",
202 argv0);
203 fprintf(stderr, "\n");
204 fprintf(stderr, " client:\n");
205 fprintf(stderr, " %8s -H local-addr -P local-port -h remote-addr\n"
206 "\t -p remote-port -s [-c case ] [-d level]\n"
207 "\t [-x repeat] [-o order-pattern] [-t stream-pattern]\n"
208 "\t [-M max-stream] [-r rand-seed]\n"
209 "\t [-m max-msgsize]\n"
210 "\t [-L num-ports] [-S num-ports]\n"
211 "\t [-a assoc-pattern]\n"
212 "\t [-i interface]\n",
213 argv0);
214 fprintf(stderr, "\n");
215 fprintf(stderr, "\t-a assoc_pattern in the mixed mode\n");
216 fprintf(stderr, "\t 0 = sequential ascending(default)\n");
217 fprintf(stderr, "\t 1 = random\n");
218 fprintf(stderr, "\t-d debug\n");
219 fprintf(stderr, "\t 0 = none\n");
220 fprintf(stderr, "\t 1 = min(default)\n");
221 fprintf(stderr, "\t 2 = max\n");
222 fprintf(stderr, "\t-c testcase\n");
223 fprintf(stderr, "\t 0 = 1 byte packets.\n");
224 fprintf(stderr, "\t 1 = Sequence of multiples of 1452 byte packets.\n");
225 fprintf(stderr, "\t (1452 is fragmentation point for an i/f with ");
226 fprintf(stderr, "1500 as mtu.)\n");
227 fprintf(stderr, "\t 2 = 1453 byte packets.\n");
228 fprintf(stderr, "\t (min. size at which fragmentation occurs\n");
229 fprintf(stderr, "\t for an i/f with 1500 as mtu.)\n");
230 fprintf(stderr, "\t 3 = Sequence of 1, 1453, 32768 byte packets.\n");
231 fprintf(stderr, "\t 4 = Sequence of following size packets.\n");
232 fprintf(stderr, "\t (1, 1000, 2000, 3000, 5000, 10000,");
233 fprintf(stderr, "15000, 20000, 25000, 32768)\n");
234 fprintf(stderr, "\t 5 = 32768 byte packets.\n");
235 fprintf(stderr, "\t (default max receive window size.)\n");
236 fprintf(stderr, "\t 6 = random size packets.\n");
237 fprintf(stderr, "\t -ve value = Packets of specifed size.\n");
238 fprintf(stderr, "\t-m max msgsize for option -c 6 (1500-65515, default value 32768)\n");
239 fprintf(stderr, "\t-x number of repeats\n");
240 fprintf(stderr, "\t-o order-pattern\n");
241 fprintf(stderr, "\t 0 = all unordered(default) \n");
242 fprintf(stderr, "\t 1 = all ordered \n");
243 fprintf(stderr, "\t 2 = alternating \n");
244 fprintf(stderr, "\t 3 = random\n");
245 fprintf(stderr, "\t-t stream-pattern\n");
246 fprintf(stderr, "\t 0 = sequential ascending(default)\n");
247 fprintf(stderr, "\t 1 = random\n");
248 fprintf(stderr, "\t-M max-stream (default value 0)\n");
249 fprintf(stderr, "\t-r seed (default 0, use time())\n");
250 fprintf(stderr, "\t-L num-ports (default value 0). Run the mixed mode\n");
251 fprintf(stderr, "\t-S num-ports (default value 0). Run the mixed mode\n");
252 fprintf(stderr, "\t-D drain. If in client mode do a read following send.\n");
253 fprintf(stderr, "\t-T use SOCK_STREAM tcp-style sockets.\n");
254 fprintf(stderr, "\t-B add the specified address(es) as additional bind\n");
255 fprintf(stderr, "\t addresses of the local socket. Multiple addresses can\n");
256 fprintf(stderr, "\t be specified by using this argument multiple times.\n");
257 fprintf(stderr, "\t For example, '-B 10.0.0.1 -B 20.0.0.2'.\n");
258 fprintf(stderr, "\t In case of IPv6 linklocal address, interface name can be set in following way \n");
259 fprintf(stderr, "\t For example, '-B fe80::f8c3:b77f:698e:4506%%eth2'.\n");
260 fprintf(stderr, "\t-C use the specified address(es) for connection to the\n");
261 fprintf(stderr, "\t peer socket. Multiple addresses can be specified by\n");
262 fprintf(stderr, "\t using this argument multiple times.\n");
263 fprintf(stderr, "\t For example, '-C 10.0.0.1 -C 20.0.0.2'.\n");
264 fprintf(stderr, "\t This option is incompatible with the -h option.\n");
265 fprintf(stderr, "\t-O time to live (default value 0)\n");
266 fprintf(stderr, "\n");
267 fflush(stderr);
268
269 } /* usage() */
270
271 void *
272 build_msg(int len)
273 {
274 int i = len - 1;
275 int n;
276 char *msg_buf, *p;
277
278 msg_buf = malloc(len);
279 if (NULL == msg_buf) {
280 fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n");
281 exit(1);
282 }
283 p = msg_buf;
284
285 do {
286 n = ((i > 50)?50:i);
287 memcpy(p, msg, ((i > 50)?50:i));
288 p += n;
289 i -= n;
290 } while (i > 0);
291
292 msg_buf[len-1] = '\0';
293
294 return(msg_buf);
295
296 } /* build_msg() */
297
298 static int
299 print_cmsg(int type, sctp_cmsg_data_t *data)
300 {
301 switch(type) {
302 case SCTP_INIT:
303 DEBUG_PRINT(DEBUG_MAX, "\tINIT\n");
304 if (DEBUG_MAX == debug_level) {
305 printf("\t\tsinit_num_ostreams=%d ",
306 data->init.sinit_num_ostreams);
307 printf("sinit_max_instreams=%d ",
308 data->init.sinit_max_instreams);
309 printf("sinit_max_attempts=%d ",
310 data->init.sinit_max_attempts);
311 printf("sinit_max_init_timeo=%d\n",
312 data->init.sinit_max_init_timeo);
313 }
314 break;
315 case SCTP_SNDRCV:
316 DEBUG_PRINT(DEBUG_MAX, "\t SNDRCV");
317 if (DEBUG_MAX == debug_level) {
318 printf("(stream=%u ", data->sndrcv.sinfo_stream);
319 printf("ssn=%u ", data->sndrcv.sinfo_ssn);
320 printf("tsn=%u ", data->sndrcv.sinfo_tsn);
321 printf("flags=0x%x ", data->sndrcv.sinfo_flags);
322 printf("ppid=%u\n", data->sndrcv.sinfo_ppid);
323 printf("cumtsn=%u\n", data->sndrcv.sinfo_cumtsn);
324 }
325 break;
326 default:
327 DEBUG_PRINT(DEBUG_MIN, "\tUnknown type: %d\n", type);
328 break;
329 }
330 fflush(stdout);
331 return 0;
332
333 } /* print_cmsg() */
334
335 /* This function prints the message. */
336 static int
337 print_message(const int sk, struct msghdr *msg, size_t msg_len) {
338 struct cmsghdr *scmsg;
339 sctp_cmsg_data_t *data;
340 int i;
341
342 if (!(MSG_NOTIFICATION & msg->msg_flags)) {
343 int index = 0;
344
345 DEBUG_PRINT(DEBUG_MIN, "Data %zu bytes.", msg_len);
346 DEBUG_PRINT(DEBUG_MAX, " First %zu bytes: ",
347 (msg_len < BODYSIZE)?msg_len:BODYSIZE);
348 /* Make sure that everything is printable and that we
349 * are NUL terminated...
350 */
351 while ( msg_len > 0 ) {
352 char *text, tmptext[BODYSIZE];
353 int len;
354
355 memset(tmptext, 0x0, BODYSIZE);
356
357 text = msg->msg_iov[index].iov_base;
358 len = msg->msg_iov[index].iov_len;
359
360 if (msg_len == 1 && text[0] == 0) {
361 DEBUG_PRINT(DEBUG_MIN, "<empty> text[0]=%d",
362 text[0]);
363 break;
364 }
365
366 if ( len > msg_len ) {
367 /* text[(len = msg_len) - 1] = '\0'; */
368 text[(len = msg_len)] = '\0';
369 }
370
371 if ( (msg_len -= len) > 0 ) { index++; }
372
373 for (i = 0; i < len - 1; ++i) {
374 if (!isprint(text[i])) text[i] = '.';
375 }
376
377 strncpy(tmptext, text, BODYSIZE);
378 tmptext[BODYSIZE-1] = '\0';
379
380 DEBUG_PRINT(DEBUG_MAX, "%s", tmptext);
381 }
382
383 DEBUG_PRINT(DEBUG_MIN, "\n");
384 fflush(stdout);
385 } else { /* if(we have notification) */
386 struct sctp_assoc_change *sac;
387 struct sctp_send_failed *ssf;
388 struct sctp_paddr_change *spc;
389 struct sctp_remote_error *sre;
390 union sctp_notification *snp;
391 char addrbuf[INET6_ADDRSTRLEN];
392 const char *ap;
393 struct sockaddr_in *sin;
394 struct sockaddr_in6 *sin6;
395 int index = 0;
396
397 snp = (union sctp_notification *)msg->msg_iov[index].iov_base;
398
399 DEBUG_PRINT(DEBUG_MIN, "Notification:");
400
401 switch (snp->sn_header.sn_type) {
402 case SCTP_ASSOC_CHANGE:
403 sac = &snp->sn_assoc_change;
404 DEBUG_PRINT(DEBUG_MIN,
405 " SCTP_ASSOC_CHANGE(%s)\n",
406 sac_state_tbl[sac->sac_state]);
407 DEBUG_PRINT(DEBUG_MAX,
408 "\t\t(assoc_change: state=%hu, "
409 "error=%hu, instr=%hu "
410 "outstr=%hu)\n",
411 sac->sac_state, sac->sac_error,
412 sac->sac_inbound_streams,
413 sac->sac_outbound_streams);
414 break;
415 case SCTP_PEER_ADDR_CHANGE:
416 spc = &snp->sn_paddr_change;
417 DEBUG_PRINT(DEBUG_MIN,
418 " SCTP_PEER_ADDR_CHANGE\n");
419 if (spc->spc_aaddr.ss_family == AF_INET) {
420 sin = (struct sockaddr_in *)
421 &spc->spc_aaddr;
422 ap = inet_ntop(AF_INET, &sin->sin_addr,
423 addrbuf,
424 INET6_ADDRSTRLEN);
425 } else {
426 sin6 = (struct sockaddr_in6 *)
427 &spc->spc_aaddr;
428 ap = inet_ntop(AF_INET6,
429 &sin6->sin6_addr,
430 addrbuf,
431 INET6_ADDRSTRLEN);
432 }
433 DEBUG_PRINT(DEBUG_MAX,
434 "\t\t(peer_addr_change: %s "
435 "state=%d, error=%d)\n",
436 ap, spc->spc_state,
437 spc->spc_error);
438 break;
439 case SCTP_SEND_FAILED:
440 ssf = &snp->sn_send_failed;
441 DEBUG_PRINT(DEBUG_MIN,
442 " SCTP_SEND_FAILED\n");
443 DEBUG_PRINT(DEBUG_MAX,
444 "\t\t(sendfailed: len=%hu "
445 "err=%d)\n",
446 ssf->ssf_length, ssf->ssf_error);
447 break;
448 case SCTP_REMOTE_ERROR:
449 sre = &snp->sn_remote_error;
450 DEBUG_PRINT(DEBUG_MIN,
451 " SCTP_REMOTE_ERROR\n");
452 DEBUG_PRINT(DEBUG_MAX,
453 "\t\t(remote_error: err=%hu)\n",
454 ntohs(sre->sre_error));
455 break;
456 case SCTP_SHUTDOWN_EVENT:
457 DEBUG_PRINT(DEBUG_MIN,
458 " SCTP_SHUTDOWN_EVENT\n");
459 break;
460 default:
461 DEBUG_PRINT(DEBUG_MIN,
462 " Unknown type: %hu\n",
463 snp->sn_header.sn_type);
464 break;
465 }
466
467 fflush(stdout);
468 return 1;
469
470 } /* notification received */
471
472 for (scmsg = CMSG_FIRSTHDR(msg);
473 scmsg != NULL;
474 scmsg = CMSG_NXTHDR(msg, scmsg)) {
475
476 data = (sctp_cmsg_data_t *)CMSG_DATA(scmsg);
477 if (debug_level) print_cmsg(scmsg->cmsg_type, data);
478 }
479
480
481 fflush(stdout);
482 return 0;
483
484 } /* print_message() */
485
486 struct sockaddr *
487 append_addr(const char *parm, struct sockaddr *addrs, int *ret_count)
488 {
489 struct sockaddr *new_addrs = NULL;
490 struct addrinfo hints, *res, *rp;
491 void *aptr;
492 struct sockaddr *sa_addr;
493 struct sockaddr_in *b4ap;
494 struct sockaddr_in6 *b6ap;
495 int j;
496 int orig_count = *ret_count;
497 int count = orig_count;
498 char *ipaddr = strdup(parm);
499 char *ifname;
500 int ifindex = 0;
501
502 if (!ipaddr)
503 return NULL;
504
505 /* check the interface. */
506 ifname = strchr(ipaddr,'%');
507 if (ifname) {
508 *ifname=0;
509 ifname++;
510 ifindex = if_nametoindex(ifname);
511 if (!ifindex) {
512 fprintf(stderr, "bad interface name: %s\n", ifname);
513 goto finally;
514 }
515 }
516
517 memset(&hints, 0, sizeof(struct addrinfo));
518 hints.ai_family = AF_UNSPEC;
519 hints.ai_protocol = IPPROTO_SCTP;
520 if (getaddrinfo(ipaddr, NULL, &hints, &res) != 0) {
521 fprintf(stderr, "bad hostname: %s\n", ipaddr);
522 goto finally;
523 }
524
525 for (rp = res; rp != NULL; rp = rp->ai_next)
526 count++;
527
528 /* Expand memory for the new addresses. Assume all the addresses
529 * are v6 addresses.
530 */
531 new_addrs = (struct sockaddr *)
532 realloc(addrs, sizeof(struct sockaddr_in6) * count);
533
534 if (NULL == new_addrs) {
535 count = *ret_count;
536 goto finally;
537 }
538
539 /* Skip the existing addresses. */
540 aptr = new_addrs;
541 for (j = 0; j < orig_count; j++) {
542 sa_addr = (struct sockaddr *)aptr;
543 switch(sa_addr->sa_family) {
544 case AF_INET:
545 aptr += sizeof(struct sockaddr_in);
546 break;
547 case AF_INET6:
548 aptr += sizeof(struct sockaddr_in6);
549 break;
550 default:
551 count = orig_count;
552 goto finally;
553 }
554 }
555
556 /* Put the new addresses away. */
557 for (rp = res; rp != NULL; rp = rp->ai_next) {
558 b4ap = (struct sockaddr_in *)aptr;
559 b6ap = (struct sockaddr_in6 *)aptr;
560 bcopy(rp->ai_addr, aptr, rp->ai_addrlen);
561 b4ap->sin_port = htons(local_port); /* equal to b6ap.v6.sin6_port */
562 if (rp->ai_family == AF_INET6) {
563 b6ap->sin6_scope_id = if_index;
564 if (!ifindex)
565 b6ap->sin6_scope_id = ifindex;
566 }
567 aptr += rp->ai_addrlen;
568 }
569
570 finally:
571 free(ipaddr);
572 *ret_count = count;
573
574 return new_addrs;
575
576 } /* append_addr() */
577
578 int socket_r(void)
579 {
580 struct sctp_event_subscribe subscribe;
581 int sk, error;
582
583 DEBUG_PRINT(DEBUG_MIN, "\tsocket(%s, IPPROTO_SCTP)",
584 (socket_type == SOCK_SEQPACKET) ? "SOCK_SEQPACKET" : "SOCK_STREAM");
585
586 if ((sk = socket(s_loc.ss_family, socket_type, IPPROTO_SCTP)) < 0 ) {
587 if (do_exit) {
588 fprintf(stderr, "\n\n\t\t*** socket: failed to create"
589 " socket: %s ***\n",
590 strerror(errno));
591 exit(1);
592 } else {
593 return -1;
594 }
595 }
596 DEBUG_PRINT(DEBUG_MIN, " -> sk=%d\n", sk);
597
598 memset(&subscribe, 0, sizeof(subscribe));
599 subscribe.sctp_data_io_event = 1;
600 subscribe.sctp_association_event = 1;
601 error = setsockopt(sk, SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
602 sizeof(subscribe));
603 if (error) {
604 fprintf(stderr, "SCTP_EVENTS: error: %d\n", error);
605 exit(1);
606 }
607 if (max_stream > 0) {
608 struct sctp_initmsg initmsg;
609 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
610 initmsg.sinit_num_ostreams = max_stream;
611 error = setsockopt(sk, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(struct sctp_initmsg));
612 if (error) {
613 fprintf(stderr, "SCTP_INITMSG: error: %d\n", error);
614 exit(1);
615 }
616 }
617 return sk;
618
619 } /* socket_r() */
620
621 int bind_r(int sk, struct sockaddr_storage *saddr)
622 {
623 int error = 0, i = 0;
624 char *host_s, *serv_s;
625
626 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
627 fprintf(stderr, "\n\t\t*** host_s malloc failed!!! ***\n");
628 exit(1);
629 }
630 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
631 fprintf(stderr, "\n\t\t*** serv_s malloc failed!!! ***\n");
632 exit(1);
633 }
634
635 do {
636 if (i > 0) sleep(1); /* sleep a while before new try... */
637
638 error = getnameinfo((struct sockaddr *)saddr, l_len, host_s,
639 NI_MAXHOST, serv_s, NI_MAXSERV,
640 NI_NUMERICHOST);
641
642 if (error)
643 printf("%s\n", gai_strerror(error));
644
645 DEBUG_PRINT(DEBUG_MIN,
646 "\tbind(sk=%d, [a:%s,p:%s]) -- attempt %d/%d\n",
647 sk, host_s, serv_s, i+1, MAX_BIND_RETRYS);
648
649 error = bind(sk, (struct sockaddr *)saddr, l_len);
650
651 if (error != 0) {
652 if( errno != EADDRINUSE ) {
653 if (do_exit) {
654 fprintf(stderr, "\n\n\t\t***bind: can "
655 "not bind to %s:%s: %s ****\n",
656 host_s, serv_s,
657 strerror(errno));
658 exit(1);
659 } else {
660 return -1;
661 }
662 }
663 }
664 i++;
665 if (i >= MAX_BIND_RETRYS) {
666 fprintf(stderr, "Maximum bind() attempts. "
667 "Die now...\n\n");
668 exit(1);
669 }
670 } while (error < 0 && i < MAX_BIND_RETRYS);
671
672 free(host_s);
673 free(serv_s);
674 return 0;
675
676 } /* bind_r() */
677
678 int
679 bindx_r(int sk, struct sockaddr *addrs, int count, int flag)
680 {
681 int error;
682 int i;
683 struct sockaddr *sa_addr;
684 void *aptr;
685
686 /* Set the port in every address. */
687 aptr = addrs;
688 for (i = 0; i < count; i++) {
689 sa_addr = (struct sockaddr *)aptr;
690
691 switch(sa_addr->sa_family) {
692 case AF_INET:
693 ((struct sockaddr_in *)sa_addr)->sin_port =
694 htons(local_port);
695 aptr += sizeof(struct sockaddr_in);
696 break;
697 case AF_INET6:
698 ((struct sockaddr_in6 *)sa_addr)->sin6_port =
699 htons(local_port);
700 aptr += sizeof(struct sockaddr_in6);
701 break;
702 default:
703 fprintf(stderr, "Invalid address family\n");
704 exit(1);
705 }
706 }
707
708 error = sctp_bindx(sk, addrs, count, flag);
709 if (error != 0) {
710 fprintf(stderr, "\n\n\t\t***bindx_r: error adding addrs:"
711 " %s. ***\n", strerror(errno));
712 exit(1);
713 }
714
715 return 0;
716
717 } /* bindx_r() */
718
719 int listen_r(int sk, int listen_count)
720 {
721 int error = 0;
722
723 DEBUG_PRINT(DEBUG_MIN, "\tlisten(sk=%d,backlog=%d)\n",
724 sk, listen_count);
725
726 /* Mark sk as being able to accept new associations */
727 error = listen(sk, listen_count);
728 if (error != 0) {
729 if (do_exit) {
730 fprintf(stderr, "\n\n\t\t*** listen: %s ***\n\n\n",
731 strerror(errno));
732 exit(1);
733 }
734 else return -1;
735 }
736 return 0;
737
738 } /* listen_r() */
739
740 int accept_r(int sk){
741 socklen_t len = 0;
742 int subsk;
743
744 DEBUG_PRINT(DEBUG_MIN, "\taccept(sk=%d)\n", sk);
745
746 subsk = accept(sk, NULL, &len);
747 if (subsk < 0) {
748 fprintf(stderr, "\n\n\t\t*** accept: %s ***\n\n\n", strerror(errno));
749 exit(1);
750 }
751
752 return subsk;
753 } /* accept_r() */
754
755 int connect_r(int sk, const struct sockaddr *serv_addr, socklen_t addrlen)
756 {
757 int error = 0;
758
759 DEBUG_PRINT(DEBUG_MIN, "\tconnect(sk=%d)\n", sk);
760
761 /* Mark sk as being able to accept new associations */
762 error = connect(sk, serv_addr, addrlen);
763 if (error != 0) {
764 if (do_exit) {
765 fprintf(stderr, "\n\n\t\t*** connect: %s ***\n\n\n",
766 strerror(errno));
767 exit(1);
768 }
769 else return -1;
770 }
771 return 0;
772
773 } /* connect_r() */
774
775 int connectx_r(int sk, struct sockaddr *addrs, int count)
776 {
777 int error;
778 int i;
779 struct sockaddr *sa_addr;
780 void *aptr;
781
782 /* Set the port in every address. */
783 aptr = addrs;
784 for (i = 0; i < count; i++) {
785 sa_addr = (struct sockaddr *)aptr;
786
787 switch(sa_addr->sa_family) {
788 case AF_INET:
789 ((struct sockaddr_in *)sa_addr)->sin_port =
790 htons(remote_port);
791 aptr += sizeof(struct sockaddr_in);
792 break;
793 case AF_INET6:
794 ((struct sockaddr_in6 *)sa_addr)->sin6_port =
795 htons(remote_port);
796 aptr += sizeof(struct sockaddr_in6);
797 break;
798 default:
799 fprintf(stderr, "Invalid address family\n");
800 exit(1);
801 }
802 }
803
804 error = sctp_connectx(sk, addrs, count, NULL);
805 if (error != 0) {
806 fprintf(stderr, "\n\n\t\t*** connectx_r: error connecting"
807 " to addrs: %s ***\n", strerror(errno));
808 exit(1);
809 }
810
811 return 0;
812
813 } /* connectx_r() */
814
815 int receive_r(int sk, int once)
816 {
817 int recvsk = sk, i = 0, error = 0, ret = -1;
818 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
819 struct iovec iov;
820 struct msghdr inmessage;
821
822 /* Initialize inmessage with enough space for DATA... */
823 memset(&inmessage, 0, sizeof(inmessage));
824 if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
825 fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n");
826 exit(1);
827 }
828 iov.iov_len = REALLY_BIG;
829 inmessage.msg_iov = &iov;
830 inmessage.msg_iovlen = 1;
831 /* or a control message. */
832 inmessage.msg_control = incmsg;
833 inmessage.msg_controllen = sizeof(incmsg);
834
835 /* Get the messages sent */
836 while (1) {
837
838 if (recvsk == sk && socket_type == SOCK_STREAM &&
839 role == SERVER)
840 recvsk = accept_r(sk);
841
842 DEBUG_PRINT(DEBUG_MIN, "\trecvmsg(sk=%d) ", sk);
843
844 error = recvmsg(recvsk, &inmessage, MSG_WAITALL);
845 if (error < 0 && errno != EAGAIN) {
846 if (errno == ENOTCONN && socket_type == SOCK_STREAM &&
847 role == SERVER) {
848 printf("No association is present now!!\n");
849 close(recvsk);
850 recvsk = sk;
851 continue;
852 }
853
854 fprintf(stderr, "\n\t\t*** recvmsg: %s ***\n\n",
855 strerror(errno));
856 fflush(stdout);
857 if (do_exit) exit(1);
858 else goto error_out;
859 }
860 else if (error == 0) {
861 if (socket_type == SOCK_STREAM && role == SERVER) {
862 printf("No association is present now!!\n");
863 close(recvsk);
864 recvsk = sk;
865 continue;
866 }
867 printf("\n\t\trecvmsg() returned 0 !!!!\n");
868 fflush(stdout);
869 }
870
871 if (print_message(recvsk, &inmessage, error) > 0)
872 continue; /* got a notification... */
873
874 inmessage.msg_control = incmsg;
875 inmessage.msg_controllen = sizeof(incmsg);
876 iov.iov_len = REALLY_BIG;
877 i++;
878 if (once)
879 break;
880 }
881
882 ret = 0;
883
884 error_out:
885 if (recvsk != sk)
886 close(recvsk);
887 if (ret)
888 close(sk);
889 free(iov.iov_base);
890 return ret;
891 } /* receive_r () */
892
893 int next_order(int state, int pattern)
894 {
895 switch (pattern){
896 case ORDER_PATTERN_UNORDERED:
897 state = 0;
898 break;
899 case ORDER_PATTERN_ORDERED:
900 state = 1;
901 break;
902 case ORDER_PATTERN_ALTERNATE:
903 state = state ? 0 : 1;
904 break;
905 case ORDER_PATTERN_RANDOM:
906 state = rand() % 2;
907 break;
908 }
909
910 return state;
911 }
912
913 int next_stream(int state, int pattern)
914 {
915 switch (pattern){
916 case STREAM_PATTERN_RANDOM:
917 state = rand() % (max_stream == 0 ? 1 : max_stream);
918 break;
919 case STREAM_PATTERN_SEQUENTIAL:
920 state = state + 1;
921 if (state >= max_stream)
922 state = 0;
923 break;
924 }
925
926 return state;
927 }
928
929 int next_msg_size(int msg_cnt)
930 {
931 int msg_size;
932
933 if (size_arg) {
934 msg_size = size_arg;
935 } else if (test_case < NCASES) {
936 msg_size = msg_sizes[test_case][msg_cnt];
937 } else {
938 msg_size = (rand() % max_msgsize) + 1;
939 }
940
941 return msg_size;
942
943 } /* next_msg_size() */
944
945 int next_assoc(int i, int state, int pattern)
946 {
947 int j;
948 int found = 0;
949 _assoc_state *as;
950
951
952 switch (pattern){
953 case ASSOC_PATTERN_RANDOM:
954 state = rand() % tosend;
955 break;
956 case ASSOC_PATTERN_SEQUENTIAL:
957 state = state + 1;
958 if (state >= tosend)
959 state = 0;
960 break;
961 }
962
963 as = poll_sks[i].assoc_state;
964 j = state;
965 do {
966 if (as[j].msg_sent < repeat_count) {
967 found = 1;
968 break;
969 }
970 if (++j >= tosend) {
971 j = 0;
972 }
973 } while (j != state);
974
975 if (found) {
976 return j;
977 } else {
978 return -1;
979 }
980
981 } /* next_assoc() */
982
983 int send_r(int sk, int stream, int order, int send_size, int assoc_i)
984 {
985 int error = 0;
986 struct msghdr outmsg;
987 struct iovec iov;
988 char *message = NULL;
989 int msglen = 0;
990 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
991 struct cmsghdr *cmsg;
992 struct sctp_sndrcvinfo *sinfo;
993
994 if (send_size > 0) {
995 message = build_msg(send_size);
996 msglen = strlen(message) + 1;
997 iov.iov_base = message;
998 iov.iov_len = msglen;
999 }
1000 else {
1001 if (do_exit) {
1002 exit(1);
1003 } else {
1004 goto error_out;
1005 }
1006 }
1007
1008 outmsg.msg_name = &s_rem;
1009 outmsg.msg_namelen = sizeof(struct sockaddr_storage);
1010 outmsg.msg_iov = &iov;
1011 outmsg.msg_iovlen = 1;
1012 outmsg.msg_control = outcmsg;
1013 outmsg.msg_controllen = sizeof(outcmsg);
1014 outmsg.msg_flags = 0;
1015
1016 cmsg = CMSG_FIRSTHDR(&outmsg);
1017 cmsg->cmsg_level = IPPROTO_SCTP;
1018 cmsg->cmsg_type = SCTP_SNDRCV;
1019 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1020
1021 outmsg.msg_controllen = cmsg->cmsg_len;
1022 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1023 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
1024 sinfo->sinfo_ppid = rand();
1025 sinfo->sinfo_stream = stream;
1026 sinfo->sinfo_flags = 0;
1027 if (!order)
1028 sinfo->sinfo_flags = SCTP_UNORDERED;
1029 if (timetolive)
1030 sinfo->sinfo_timetolive = timetolive;
1031
1032 DEBUG_PRINT(DEBUG_MIN, "\tsendmsg(sk=%d, assoc=%d) %4d bytes.\n",
1033 sk, assoc_i, send_size);
1034 DEBUG_PRINT(DEBUG_MAX, "\t SNDRCV");
1035 if (DEBUG_MAX == debug_level) {
1036 printf("(stream=%u ", sinfo->sinfo_stream);
1037 printf("flags=0x%x ", sinfo->sinfo_flags);
1038 printf("ppid=%u\n", sinfo->sinfo_ppid);
1039 }
1040
1041 /* Send to our neighbor. */
1042 error = sendmsg(sk, &outmsg, MSG_WAITALL);
1043 if (error != msglen) {
1044 fprintf(stderr, "\n\t\t*** sendmsg: %s ***\n\n",
1045 strerror(errno));
1046 fflush(stdout);
1047
1048 if (do_exit) {
1049 exit(1);
1050 } else {
1051 if (!drain)
1052 goto error_out;
1053 }
1054 }
1055
1056 if (send_size > 0) free(message);
1057 return 0;
1058 error_out:
1059 if (send_size > 0) free(message);
1060 return -1;
1061
1062 } /* send_r() */
1063
1064 int close_r(int sk)
1065 {
1066 int error = 0;
1067
1068 DEBUG_PRINT(DEBUG_MIN, "\tclose(sk=%d)\n",sk);
1069
1070 error = close(sk);
1071 if (error != 0) {
1072 if (do_exit) {
1073 fprintf(stderr, "\n\n\t\t*** close: %s ***\n\n",
1074 strerror(errno));
1075 exit(1);
1076 } else {
1077 return -1;
1078 }
1079 }
1080 fflush(stdout);
1081 return 0;
1082
1083 } /* close_r() */
1084
1085 void
1086 server(int sk)
1087 {
1088 if (max_msgsize > DEFAULT_MAX_WINDOW) {
1089 if (setsockopt(sk, SOL_SOCKET, SO_RCVBUF, &max_msgsize,
1090 sizeof(max_msgsize)) < 0) {
1091 perror("setsockopt(SO_RCVBUF)");
1092 exit(1);
1093 }
1094 }
1095
1096 receive_r(sk, 0);
1097
1098 } /* server() */
1099
1100 void
1101 client(int sk)
1102 {
1103 int msg_size;
1104 int i;
1105
1106 for (i = 0; i < msg_cnt; i++) {
1107
1108 msg_size = next_msg_size(i);
1109 order_state = next_order(order_state, order_pattern);
1110 stream_state = next_stream(stream_state, stream_pattern);
1111
1112 if (send_r(sk, stream_state, order_state, msg_size, 0) < 0)
1113 break;
1114 /* The sender is echoing so do discard the echoed data. */
1115 if (drain) {
1116 receive_r(sk, 1);
1117 }
1118 }
1119 } /* client() */
1120
1121 void
1122 mixed_mode_test(void)
1123 {
1124 int error, i, j, max_fd, sks, size;
1125 int assoc_i, n_msg_size, n_order, n_stream;
1126 int done = 0;
1127 fd_set *ibitsp = NULL, *obitsp = NULL, *xbitsp = NULL;
1128 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
1129 struct iovec iov;
1130 struct msghdr inmessage;
1131 _assoc_state *as;
1132
1133
1134 /* Set up the listeners. If listeners is 0, set up one socket for
1135 * transmitting only.
1136 */
1137 iov.iov_base = NULL;
1138 max_fd = -1;
1139 sks = (0 == listeners) ? 1 : listeners;
1140 memset(poll_sks, 0, sks * sizeof(_poll_sks));
1141
1142 for (i = 0; i < sks; i++) {
1143 poll_sks[i].sk = socket_r();
1144
1145 if (s_loc.ss_family == AF_INET6)
1146 ( (struct sockaddr_in6 *)&s_loc)->sin6_port =
1147 htons(local_port + i);
1148 else
1149 ( (struct sockaddr_in *)&s_loc)->sin_port =
1150 htons(local_port + i);
1151
1152 bind_r(poll_sks[i].sk, &s_loc);
1153 if (listeners) {
1154 listen_r(poll_sks[i].sk, 100);
1155 }
1156 if (max_msgsize > DEFAULT_MAX_WINDOW) {
1157 if (setsockopt(poll_sks[i].sk, SOL_SOCKET, SO_RCVBUF,
1158 &max_msgsize, sizeof(max_msgsize)) < 0) {
1159 perror("setsockopt(SO_RCVBUF)");
1160 exit(1);
1161 }
1162 }
1163
1164 if (tosend) {
1165 if ((poll_sks[i].assoc_state = (_assoc_state *)malloc(
1166 sizeof(_assoc_state) * tosend)) == NULL) {
1167 printf("Can't allocate memory.\n");
1168 goto clean_up;
1169 }
1170 memset(poll_sks[i].assoc_state, 0,
1171 sizeof(_assoc_state) * tosend);
1172 }
1173
1174 if (poll_sks[i].sk > max_fd) {
1175 max_fd = poll_sks[i].sk;
1176 }
1177 }
1178
1179 size = howmany(max_fd + 1, NFDBITS) * sizeof(fd_mask);
1180 if ((ibitsp = (fd_set *)malloc(size)) == NULL) {
1181 printf("Can't allocate memory.\n");
1182 goto clean_up;
1183 }
1184 if ((obitsp = (fd_set *)malloc(size)) == NULL) {
1185 printf("Can't allocate memory.\n");
1186 goto clean_up;
1187 }
1188 if ((xbitsp = (fd_set *)malloc(size)) == NULL) {
1189 printf("Can't allocate memory.\n");
1190 goto clean_up;
1191 }
1192
1193 memset(ibitsp, 0, size);
1194 memset(obitsp, 0, size);
1195 memset(xbitsp, 0, size);
1196
1197
1198 /* Initialize inmessage with enough space for DATA... */
1199 memset(&inmessage, 0, sizeof(inmessage));
1200 if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
1201 fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n");
1202 goto clean_up;
1203 }
1204 iov.iov_len = REALLY_BIG;
1205 inmessage.msg_iov = &iov;
1206 inmessage.msg_iovlen = 1;
1207 /* or a control message. */
1208 inmessage.msg_control = incmsg;
1209 inmessage.msg_controllen = sizeof(incmsg);
1210
1211 /* Set up the remote port number per association for output. */
1212 for (i = 0; i < sks; i++) {
1213 as = poll_sks[i].assoc_state;
1214 for (j = 0; j < tosend; j++) {
1215 as[j].rem_port = remote_port + j;
1216 }
1217 }
1218
1219 while (!done) {
1220
1221 for (i = 0; i < sks; i++) {
1222 FD_SET(poll_sks[i].sk, ibitsp);
1223 FD_SET(poll_sks[i].sk, obitsp);
1224 FD_SET(poll_sks[i].sk, xbitsp);
1225 }
1226 if ((error = select(max_fd + 1, ibitsp, obitsp, xbitsp,
1227 (struct timeval *)0)) < 0) {
1228 fprintf(stderr, "\n\t\t*** select() failed ");
1229 fprintf(stderr, "with error: %s\n\n",
1230 strerror(errno));
1231 fflush(stdout);
1232 goto clean_up;
1233 }
1234
1235 for (i = 0; i < sks; i++) {
1236 /* Is there anything to read from the socket? */
1237 if (listeners && FD_ISSET(poll_sks[i].sk, ibitsp)) {
1238
1239 FD_CLR(poll_sks[i].sk, ibitsp);
1240 error = recvmsg(poll_sks[i].sk, &inmessage,
1241 MSG_WAITALL);
1242 if (error < 0) {
1243 fprintf(stderr,
1244 "\n\t\t*** recvmsg: %s ***\n\n",
1245 strerror(errno));
1246 fflush(stdout);
1247 goto clean_up;
1248 }
1249 else if (error == 0) {
1250 printf("\n\t\trecvmsg() returned ");
1251 printf("0 !!!!\n");
1252 fflush(stdout);
1253 }
1254
1255 print_message(poll_sks[i].sk, &inmessage,
1256 error);
1257
1258 inmessage.msg_control = incmsg;
1259 inmessage.msg_controllen = sizeof(incmsg);
1260 iov.iov_len = REALLY_BIG;
1261 }
1262
1263 /* Is this socket writeable? */
1264 if (tosend && FD_ISSET(poll_sks[i].sk, obitsp)) {
1265
1266 FD_CLR(poll_sks[i].sk, obitsp);
1267
1268 /* Pick an association. */
1269 assoc_i = next_assoc(i, poll_sks[i].assoc_i,
1270 assoc_pattern);
1271 if (assoc_i < 0) {
1272 /* No work to do on any associations.
1273 * We are probably done. */
1274 if (!listeners) {
1275 done = 1;
1276 }
1277 continue;
1278 }
1279 poll_sks[i].assoc_i = assoc_i;
1280
1281 as = poll_sks[i].assoc_state;
1282 n_msg_size = next_msg_size(as[assoc_i].msg_cnt);
1283 n_order = as[assoc_i].order_state =
1284 next_order(as[assoc_i].order_state,
1285 order_pattern);
1286 n_stream = as[assoc_i].stream_state =
1287 next_stream(as[assoc_i].stream_state,
1288 stream_pattern);
1289
1290 /* Set the destination port. */
1291 if (s_rem.ss_family == AF_INET6)
1292 ( (struct sockaddr_in6 *)&s_rem)->
1293 sin6_port =
1294 htons(as[assoc_i].rem_port);
1295 else
1296 ( (struct sockaddr_in *)&s_rem)->
1297 sin_port =
1298 htons(as[assoc_i].rem_port);
1299
1300 /* Send a message thru the association. */
1301 if (send_r(poll_sks[i].sk, n_stream, n_order,
1302 n_msg_size, assoc_i) < 0) {
1303 /* Don't increment counter if there
1304 * is a problem of sending.
1305 */
1306 continue;
1307 }
1308
1309 /* Increment counters. */
1310 if (++as[assoc_i].msg_cnt >= MSG_CNT) {
1311 as[assoc_i].msg_cnt = 0;
1312 }
1313 if (++as[assoc_i].msg_sent >=
1314 repeat_count) {
1315 fprintf(stderr, "Association #%d in ",
1316 assoc_i);
1317 fprintf(stderr, "sk=%d has ",
1318 poll_sks[i].sk);
1319 fprintf(stderr, "completed %d msg as ",
1320 as[assoc_i].msg_sent);
1321 fprintf(stderr, "cycle %d.\n",
1322 ++as[assoc_i].cycle);
1323
1324 /* In the mixed mode, -x not only
1325 * specify the longer repeat cycle,
1326 * but it also mean to run the test
1327 * forever.
1328 */
1329 if (xflag) {
1330 as[assoc_i].msg_sent = 0;
1331 }
1332
1333 }
1334
1335 }
1336 }
1337 }
1338
1339 clean_up:
1340 for (i = 0; i < sks; i++) {
1341 close(poll_sks[i].sk);
1342 if (poll_sks[i].assoc_state) {
1343 free(poll_sks[i].assoc_state);
1344 }
1345 }
1346
1347 if (ibitsp) free(ibitsp);
1348 if (obitsp) free(obitsp);
1349 if (xbitsp) free(xbitsp);
1350
1351 if (iov.iov_base) free(iov.iov_base);
1352
1353 } /* mixed_mode_test() */
1354
1355 void start_test(int role)
1356 {
1357 int sk;
1358 int i = 0;
1359
1360 DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n");
1361
1362 repeat_count = repeat;
1363
1364
1365 if (MIXED == role) {
1366 repeat_count = repeat_count * msg_cnt; /* Repeat per assoc. */
1367 mixed_mode_test();
1368 return;
1369 }
1370
1371 sk = socket_r();
1372 if (sk < 0) {
1373 DEBUG_PRINT(DEBUG_NONE, "\nSocket create err %d\n", errno);
1374 return;
1375 }
1376
1377 if (bind_r(sk, &s_loc) == -1) {
1378 DEBUG_PRINT(DEBUG_NONE, "\nSocket bind err %d\n", errno);
1379 close(sk);
1380 return;
1381 }
1382
1383 /* Do we need to do bindx() to add any additional addresses? */
1384 if (bindx_add_addrs)
1385 bindx_r(sk, bindx_add_addrs, bindx_add_count,
1386 SCTP_BINDX_ADD_ADDR);
1387
1388 if (role == SERVER) {
1389 listen_r(sk, 100);
1390 } else {
1391 if (socket_type == SOCK_STREAM && connectx_count == 0)
1392 connect_r(sk, (struct sockaddr *)&s_rem, r_len);
1393
1394 if (connectx_count != 0)
1395 connectx_r(sk, connectx_addrs, connectx_count);
1396 }
1397
1398 if (!debug_level) {
1399 printf(" ");
1400 }
1401
1402 for(i = 0; i < repeat_count; i++) {
1403
1404 if (role == SERVER) {
1405 DEBUG_PRINT(DEBUG_NONE,
1406 "Server: Receiving packets.\n");
1407 server(sk);
1408 } else {
1409 DEBUG_PRINT(DEBUG_NONE,
1410 "Client: Sending packets.(%d/%d)\n",
1411 i+1, repeat_count);
1412 client(sk);
1413 }
1414
1415 fflush(stdout);
1416 }
1417
1418 close_r(sk);
1419
1420 } /* start_test() */
1421
1422 int
1423 main(int argc, char *argv[])
1424 {
1425 int c;
1426 char *interface = NULL;
1427 struct sockaddr_in *t_addr;
1428 struct sockaddr_in6 *t_addr6;
1429 struct sockaddr *tmp_addrs = NULL;
1430
1431 /* Parse the arguments. */
1432 while ((c = getopt(argc, argv, ":H:L:P:S:a:h:p:c:d:lm:sx:X:o:t:M:r:w:Di:TB:C:O:")) >= 0 ) {
1433
1434 switch (c) {
1435 case 'H':
1436 local_host = optarg;
1437 break;
1438 case 'L':
1439 role = MIXED;
1440 listeners = atoi(optarg);
1441 if (listeners > MAX_POLL_SKS) {
1442 usage(argv[0]);
1443 exit(1);
1444 }
1445 break;
1446 case 'P':
1447 local_port = atoi(optarg);
1448 break;
1449 case 'S':
1450 role = MIXED;
1451 tosend = atoi(optarg);
1452 if (tosend > MAX_POLL_SKS) {
1453 usage(argv[0]);
1454 exit(1);
1455 }
1456 break;
1457 case 'a':
1458 assoc_pattern = atoi(optarg);
1459 if (assoc_pattern < ASSOC_PATTERN_SEQUENTIAL
1460 || assoc_pattern > ASSOC_PATTERN_RANDOM ) {
1461 usage(argv[0]);
1462 exit(1);
1463 }
1464 break;
1465 case 'h':
1466 remote_host = optarg;
1467 break;
1468 case 'D':
1469 drain = 1;
1470 do_exit = 0;
1471 break;
1472 case 'p':
1473 remote_port = atoi(optarg);
1474 break;
1475 case 's':
1476 if (role != NOT_DEFINED) {
1477 printf("%s: only -s or -l\n", argv[0]);
1478 usage(argv[0]);
1479 exit(1);
1480 }
1481 role = CLIENT;
1482 break;
1483 case 'l':
1484 if (role != NOT_DEFINED) {
1485 printf("%s: only -s or -l\n", argv[0]);
1486 usage(argv[0]);
1487 exit(1);
1488 }
1489 role = SERVER;
1490 break;
1491 case 'd':
1492 debug_level = atoi(optarg);
1493 if (debug_level < DEBUG_NONE
1494 || debug_level > DEBUG_MAX) {
1495 usage(argv[0]);
1496 exit(1);
1497 }
1498 break;
1499 case 'x':
1500 repeat = atoi(optarg);
1501 if (!repeat) {
1502 xflag = 1;
1503 repeat = BIG_REPEAT;
1504 }
1505 break;
1506 case 'X':
1507 msg_cnt = atoi(optarg);
1508 if ((msg_cnt <= 0) || (msg_cnt > MSG_CNT)) {
1509 usage(argv[0]);
1510 exit(1);
1511 }
1512 break;
1513 case 'c':
1514 test_case = atoi(optarg);
1515 if (test_case > NCASES) {
1516 usage(argv[0]);
1517 exit(1);
1518 }
1519 if (test_case < 0) {
1520 size_arg = -test_case;
1521 }
1522
1523 break;
1524 case 'o':
1525 order_pattern = atoi(optarg);
1526 if (order_pattern < ORDER_PATTERN_UNORDERED
1527 || order_pattern > ORDER_PATTERN_RANDOM ) {
1528 usage(argv[0]);
1529 exit(1);
1530 }
1531 break;
1532 case 'O':
1533 timetolive = atoi(optarg);
1534 if (timetolive < 0) {
1535 usage(argv[0]);
1536 exit(1);
1537 }
1538 break;
1539 case 't':
1540 stream_pattern = atoi(optarg);
1541 if (stream_pattern < STREAM_PATTERN_SEQUENTIAL
1542 || stream_pattern > STREAM_PATTERN_RANDOM ) {
1543 usage(argv[0]);
1544 exit(1);
1545 }
1546 break;
1547 case 'M':
1548 max_stream = atoi(optarg);
1549 if (max_stream < 0
1550 || max_stream >= (1<<16)) {
1551 usage(argv[0]);
1552 exit(1);
1553 }
1554 break;
1555 case 'r':
1556 seed = atoi(optarg);
1557 break;
1558 case 'm':
1559 max_msgsize = atoi(optarg);
1560 #if 0
1561 if ((max_msgsize < DEFAULT_MIN_WINDOW) ||
1562 (max_msgsize > 65515)) {
1563 usage(argv[0]);
1564 exit(1);
1565 }
1566 #endif
1567 break;
1568 case 'i':
1569 interface = optarg;
1570 if_index = if_nametoindex(interface);
1571 if (!if_index) {
1572 printf("Interface %s unknown\n", interface);
1573 exit(1);
1574 }
1575 break;
1576 case 'T':
1577 socket_type = SOCK_STREAM;
1578 break;
1579 case 'B':
1580 tmp_addrs = append_addr(optarg, bindx_add_addrs,
1581 &bindx_add_count);
1582 if (NULL == tmp_addrs) {
1583 fprintf(stderr, "No memory to add ");
1584 fprintf(stderr, "%s\n", optarg);
1585 exit(1);
1586 }
1587 bindx_add_addrs = tmp_addrs;
1588 break;
1589 case 'C':
1590 tmp_addrs = append_addr(optarg, connectx_addrs,
1591 &connectx_count);
1592 if (NULL == tmp_addrs) {
1593 fprintf(stderr, "No memory to add ");
1594 fprintf(stderr, "%s\n", optarg);
1595 exit(1);
1596 }
1597 connectx_addrs = tmp_addrs;
1598 break;
1599 case '?':
1600 default:
1601 usage(argv[0]);
1602 exit(0);
1603 }
1604 } /* while() */
1605
1606 if (NOT_DEFINED == role) {
1607 usage(argv[0]);
1608 exit(1);
1609 }
1610
1611
1612 if (SERVER == role && NULL == local_host && remote_host != NULL) {
1613 fprintf (stderr, "%s: Server needs local address, "
1614 "not remote address\n", argv[0]);
1615 usage(argv[0]);
1616 exit(1);
1617 }
1618 if (CLIENT == role && NULL == remote_host && connectx_count == 0) {
1619 fprintf (stderr, "%s: Client needs at least remote address "
1620 "& port\n", argv[0]);
1621 usage(argv[0]);
1622 exit(1);
1623 }
1624 if (MIXED == role) {
1625 if (listeners && NULL == local_host) {
1626 fprintf (stderr, "%s: Servers need local address\n",
1627 argv[0]);
1628 usage(argv[0]);
1629 exit(1);
1630 }
1631 if (tosend && NULL == remote_host) {
1632 fprintf (stderr, "%s: Clients need remote address ",
1633 argv[0]);
1634 fprintf (stderr, "& port\n");
1635 usage(argv[0]);
1636 exit(1);
1637 }
1638 }
1639
1640 if (optind < argc) {
1641 fprintf(stderr, "%s: non-option arguments are illegal: ",
1642 argv[0]);
1643 while (optind < argc)
1644 fprintf(stderr, "%s ", argv[optind++]);
1645 fprintf (stderr, "\n");
1646 usage(argv[0]);
1647 exit(1);
1648 }
1649
1650 if (remote_host != NULL && connectx_count != 0) {
1651 fprintf(stderr, "%s: You can not provide both -h and -C options.\n",
1652 argv[0]);
1653 usage(argv[0]);
1654 exit(1);
1655 }
1656
1657 if (remote_host != NULL && remote_port != 0) {
1658 struct addrinfo *res;
1659 int error;
1660 char *host_s, *serv_s;
1661
1662 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
1663 fprintf(stderr, "\n*** host_s malloc failed!!! ***\n");
1664 exit(1);
1665 }
1666 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
1667 fprintf(stderr, "\n*** serv_s malloc failed!!! ***\n");
1668 exit(1);
1669 }
1670
1671 error = getaddrinfo(remote_host, 0, NULL, &res);
1672 if (error) {
1673 printf("%s.\n", gai_strerror(error));
1674 usage(argv[0]);
1675 exit(1);
1676 }
1677
1678 switch (res->ai_family) {
1679 case AF_INET:
1680 t_addr = (struct sockaddr_in *)&s_rem;
1681
1682 memcpy(t_addr, res->ai_addr,
1683 res->ai_addrlen);
1684 t_addr->sin_family = res->ai_family;
1685 t_addr->sin_port = htons(remote_port);
1686
1687 r_len = res->ai_addrlen;
1688
1689 #ifdef __FreeBSD__
1690 t_addr->sin_len = r_len;
1691 #endif
1692 break;
1693 case AF_INET6:
1694
1695 t_addr6 = (struct sockaddr_in6 *)&s_rem;
1696
1697 memcpy(t_addr6, res->ai_addr,
1698 res->ai_addrlen);
1699 t_addr6->sin6_family = res->ai_family;
1700 t_addr6->sin6_port = htons(remote_port);
1701 if (interface)
1702 t_addr6->sin6_scope_id =
1703 if_nametoindex(interface);
1704
1705 r_len = res->ai_addrlen;
1706
1707 #ifdef __FreeBSD__
1708 t_addr6->sin6_len = r_len;
1709 #endif
1710 break;
1711 }
1712
1713 getnameinfo((struct sockaddr *)&s_rem, r_len, host_s,
1714 NI_MAXHOST, serv_s, NI_MAXSERV, NI_NUMERICHOST);
1715
1716 DEBUG_PRINT(DEBUG_MAX, "remote:addr=%s, port=%s, family=%d\n",
1717 host_s, serv_s, res->ai_family);
1718
1719 free(host_s);
1720 free(serv_s);
1721 freeaddrinfo(res);
1722 }
1723
1724 if (connectx_count != 0) {
1725 switch (connectx_addrs->sa_family) {
1726 case AF_INET:
1727 t_addr = (struct sockaddr_in *)&s_rem;
1728 r_len = sizeof(struct sockaddr_in);
1729 memcpy(t_addr, connectx_addrs, r_len);
1730 t_addr->sin_port = htons(remote_port);
1731 break;
1732 case AF_INET6:
1733 t_addr6 = (struct sockaddr_in6 *)&s_rem;
1734 r_len = sizeof(struct sockaddr_in6);
1735 memcpy(t_addr6, connectx_addrs, r_len);
1736 t_addr6->sin6_port = htons(remote_port);
1737 break;
1738 }
1739 }
1740
1741 if (local_host != NULL) {
1742 struct addrinfo *res;
1743 int error;
1744 char *host_s, *serv_s;
1745 struct sockaddr_in *t_addr;
1746 struct sockaddr_in6 *t_addr6;
1747
1748 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
1749 fprintf(stderr, "\n*** host_s malloc failed!!! ***\n");
1750 exit(1);
1751 }
1752 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
1753 fprintf(stderr, "\n*** serv_s malloc failed!!! ***\n");
1754 exit(1);
1755 }
1756
1757 if (strcmp(local_host, "0") == 0)
1758 local_host = "0.0.0.0";
1759
1760 error = getaddrinfo(local_host, 0, NULL, &res);
1761 if (error) {
1762 printf("%s.\n", gai_strerror(error));
1763 usage(argv[0]);
1764 exit(1);
1765 }
1766
1767 switch (res->ai_family) {
1768 case AF_INET:
1769 t_addr = (struct sockaddr_in *)&s_loc;
1770 memcpy(t_addr, res->ai_addr,
1771 res->ai_addrlen);
1772 t_addr->sin_family = res->ai_family;
1773 t_addr->sin_port = htons(local_port);
1774
1775 l_len = res->ai_addrlen;
1776
1777 #ifdef __FreeBSD__
1778 t_addr->sin_len = l_len;
1779 #endif
1780 break;
1781 case AF_INET6:
1782 t_addr6 = (struct sockaddr_in6 *)&s_loc;
1783
1784 memcpy(t_addr6, res->ai_addr,
1785 res->ai_addrlen);
1786 t_addr6->sin6_family = res->ai_family;
1787 t_addr6->sin6_port = htons(local_port);
1788 if (interface)
1789 t_addr6->sin6_scope_id =
1790 if_nametoindex(interface);
1791
1792 l_len = res->ai_addrlen;
1793
1794 #ifdef __FreeBSD__
1795 t_addr6->sin6_len = l_len;
1796 #endif
1797 break;
1798 }
1799
1800 error = getnameinfo((struct sockaddr *)&s_loc, l_len, host_s,
1801 NI_MAXHOST, serv_s, NI_MAXSERV, NI_NUMERICHOST);
1802
1803 if (error)
1804 printf("%s..\n", gai_strerror(error));
1805
1806 DEBUG_PRINT(DEBUG_MAX, "local:addr=%s, port=%s, family=%d\n",
1807 host_s, serv_s, res->ai_family);
1808
1809 free(host_s);
1810 free(serv_s);
1811 freeaddrinfo(res);
1812 }
1813
1814
1815 /* A half-hearted attempt to seed rand() */
1816 if (seed == 0 ) {
1817 seed = time(0);
1818 DEBUG_PRINT(DEBUG_NONE, "seed = %d\n", seed);
1819 }
1820
1821 srand(seed);
1822
1823 /* Let the testing begin. */
1824 start_test(role);
1825
1826 return 0;
1827
1828 } /* main() */