Debian Codegraph @

src/apps/sctp_status.c

raw
1 /* SCTP kernel reference Implementation
2 * (C) Copyright Fujitsu Ltd. 2008, 2009
3 *
4 * The SCTP reference implementation is free software;
5 * you can redistribute it and/or modify it under the terms of
6 * the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * The SCTP reference implementation is distributed in the hope that it
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * ************************
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU CC; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 * Please send any bug reports or fixes you make to the
22 * email address(es):
23 * lksctp developers <[email protected]>
24 *
25 * Or submit a bug report through the following website:
26 * https://github.com/sctp/lksctp-tools/
27 *
28 * Any bugs reported to us we will try to fix... any fixes shared will
29 * be incorporated into the next SCTP release.
30 *
31 * Written or modified by:
32 * Wei Yongjun <[email protected]>
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <arpa/inet.h>
40 #include <net/if.h>
41 #include <netinet/sctp.h>
42 #include <signal.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <netdb.h>
47 #include <string.h>
48
49 #define DEFAULT_SEC 0
50 #define DEFAULT_USEC 5000
51
52 #define REALLY_BIG 65536
53
54 #define SERVER 0
55 #define CLIENT 1
56 #define NOT_DEFINED 666
57
58 #define DEBUG_NONE 0
59 #define DEBUG_MIN 1
60 #define DEBUG_MAX 2
61
62 #define ORDER_PATTERN_UNORDERED 0
63 #define ORDER_PATTERN_ORDERED 1
64 #define ORDER_PATTERN_ALTERNATE 2
65 #define ORDER_PATTERN_RANDOM 3
66
67 #define STREAM_PATTERN_SEQUENTIAL 0
68 #define STREAM_PATTERN_RANDOM 1
69
70 #define MAX_BIND_RETRYS 10
71 #define BIG_REPEAT 1000000
72 #define REPEAT 10
73
74 #define DEFAULT_MAX_WINDOW 32768
75 #define DEFAULT_MIN_WINDOW 1500
76
77 #define MSG_CNT 10
78
79 #define DEBUG_PRINT(level, print_this...) \
80 { \
81 if (debug_level >= level) { \
82 fprintf(stdout, print_this); \
83 fflush(stdout); \
84 } \
85 } /* DEBUG_PRINT */
86
87 char *local_host = NULL;
88 int local_port = 0;
89 char *remote_host = NULL;
90 int remote_port = 0;
91 struct sockaddr_storage s_rem, s_loc;
92 int r_len, l_len;
93 int size_arg = 0;
94 int debug_level = DEBUG_NONE;
95 int order_pattern = ORDER_PATTERN_UNORDERED;
96 int order_state = 0;
97 int stream_pattern = STREAM_PATTERN_SEQUENTIAL;
98 int stream_state = 0;
99 int repeat = REPEAT;
100 int repeat_count = 0;
101 int max_msgsize = DEFAULT_MAX_WINDOW;
102 int msg_cnt = MSG_CNT;
103 int drain = 0;
104 int max_stream = 0;
105 int gsk = -1;
106 int period = 1;
107 char *statusfile = NULL;
108
109 void printstatus(int sk);
110 void sighandler(int signo);
111 void settimerhandle(void);
112 void usage(char *argv0);
113 void start_test(int role);
114
115 unsigned char msg[] = "012345678901234567890123456789012345678901234567890";
116
117 /* Convenience structure to determine space needed for cmsg. */
118 typedef union {
119 struct sctp_initmsg init;
120 struct sctp_sndrcvinfo sndrcvinfo;
121 } _sctp_cmsg_data_t;
122
123 int main(int argc, char *argv[]) {
124 int c, role = NOT_DEFINED;
125 char *interface = NULL;
126 struct sockaddr_in *t_addr;
127 struct sockaddr_in6 *t_addr6;
128
129 /* Parse the arguments. */
130 while ((c = getopt(argc, argv, ":H:L:P:h:p:c:d:lm:sx:X:o:M:Di:I:f:")) >= 0 ) {
131 switch (c) {
132 case 'H':
133 local_host = optarg;
134 break;
135 case 'P':
136 local_port = atoi(optarg);
137 break;
138 case 'h':
139 remote_host = optarg;
140 break;
141 case 'p':
142 remote_port = atoi(optarg);
143 break;
144 case 'l':
145 if (role != NOT_DEFINED) {
146 printf("%s: only -s or -l\n", argv[0]);
147 usage(argv[0]);
148 exit(1);
149 }
150 role = SERVER;
151 break;
152 case 's':
153 if (role != NOT_DEFINED) {
154 printf("%s: only -s or -l\n", argv[0]);
155 usage(argv[0]);
156 exit(1);
157 }
158 role = CLIENT;
159 break;
160 case 'D':
161 drain = 1;
162 break;
163 case 'd':
164 debug_level = atoi(optarg);
165 if (debug_level < DEBUG_NONE
166 || debug_level > DEBUG_MAX) {
167 usage(argv[0]);
168 exit(1);
169 }
170 break;
171 case 'I':
172 period = atoi(optarg);
173 if (period < 0) {
174 usage(argv[0]);
175 exit(1);
176 }
177 break;
178 case 'x':
179 repeat = atoi(optarg);
180 if (!repeat) {
181 repeat = BIG_REPEAT;
182 }
183 break;
184 case 'X':
185 msg_cnt = atoi(optarg);
186 if ((msg_cnt <= 0) || (msg_cnt > MSG_CNT)) {
187 usage(argv[0]);
188 exit(1);
189 }
190 break;
191 case 'c':
192 size_arg = atoi(optarg);
193 if (size_arg < 0) {
194 usage(argv[0]);
195 exit(1);
196 }
197
198 break;
199 case 'o':
200 order_pattern = atoi(optarg);
201 if (order_pattern < ORDER_PATTERN_UNORDERED
202 || order_pattern > ORDER_PATTERN_RANDOM ) {
203 usage(argv[0]);
204 exit(1);
205 }
206 break;
207 case 'M':
208 max_stream = atoi(optarg);
209 if (max_stream < 0
210 || max_stream >= (1<<16)) {
211 usage(argv[0]);
212 exit(1);
213 }
214 break;
215 case 'm':
216 max_msgsize = atoi(optarg);
217 break;
218 case 'i':
219 interface = optarg;
220 break;
221 case 'f':
222 statusfile = optarg;
223 break;
224 case '?':
225 default:
226 usage(argv[0]);
227 exit(0);
228 }
229 } /* while() */
230
231 if (NOT_DEFINED == role) {
232 usage(argv[0]);
233 exit(1);
234 }
235
236 if (SERVER == role && NULL == local_host && remote_host != NULL) {
237 fprintf(stderr, "%s: Server needs local address, "
238 "not remote address\n", argv[0]);
239 usage(argv[0]);
240 exit(1);
241 }
242 if (CLIENT == role && NULL == remote_host) {
243 fprintf(stderr, "%s: Client needs at least remote address "
244 "& port\n", argv[0]);
245 usage(argv[0]);
246 exit(1);
247 }
248
249 if (optind < argc) {
250 fprintf(stderr, "%s: non-option arguments are illegal: ", argv[0]);
251 while (optind < argc)
252 fprintf(stderr, "%s ", argv[optind++]);
253 fprintf (stderr, "\n");
254 usage(argv[0]);
255 exit(1);
256 }
257
258 if (remote_host != NULL && remote_port != 0) {
259 struct addrinfo *res;
260 int error;
261 char *host_s, *serv_s;
262
263 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
264 fprintf(stderr, "\n*** host_s malloc failed!!! ***\n");
265 exit(1);
266 }
267 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
268 fprintf(stderr, "\n*** serv_s malloc failed!!! ***\n");
269 exit(1);
270 }
271
272 error = getaddrinfo(remote_host, 0, NULL, &res);
273 if (error) {
274 printf("%s.\n", gai_strerror(error));
275 usage(argv[0]);
276 exit(1);
277 }
278
279 switch (res->ai_family) {
280 case AF_INET:
281 t_addr = (struct sockaddr_in *)&s_rem;
282
283 memcpy(t_addr, res->ai_addr,
284 res->ai_addrlen);
285 t_addr->sin_family = res->ai_family;
286 t_addr->sin_port = htons(remote_port);
287
288 r_len = res->ai_addrlen;
289 #ifdef __FreeBSD__
290 t_addr->sin_len = r_len;
291 #endif
292 break;
293 case AF_INET6:
294 t_addr6 = (struct sockaddr_in6 *)&s_rem;
295
296 memcpy(t_addr6, res->ai_addr,
297 res->ai_addrlen);
298 t_addr6->sin6_family = res->ai_family;
299 t_addr6->sin6_port = htons(remote_port);
300 if (interface)
301 t_addr6->sin6_scope_id = if_nametoindex(interface);
302
303 r_len = res->ai_addrlen;
304
305 #ifdef __FreeBSD__
306 t_addr6->sin6_len = r_len;
307 #endif
308 break;
309 }
310
311 getnameinfo((struct sockaddr *)&s_rem, r_len, host_s,
312 NI_MAXHOST, serv_s, NI_MAXSERV, NI_NUMERICHOST);
313
314 DEBUG_PRINT(DEBUG_MAX, "remote:addr=%s, port=%s, family=%d\n",
315 host_s, serv_s, res->ai_family);
316
317 free(host_s);
318 free(serv_s);
319 freeaddrinfo(res);
320 }
321
322 if (local_host != NULL) {
323 struct addrinfo *res;
324 int error;
325 char *host_s, *serv_s;
326 struct sockaddr_in *t_addr;
327 struct sockaddr_in6 *t_addr6;
328
329 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
330 fprintf(stderr, "\n*** host_s malloc failed!!! ***\n");
331 exit(1);
332 }
333 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
334 fprintf(stderr, "\n*** serv_s malloc failed!!! ***\n");
335 exit(1);
336 }
337
338 if (strcmp(local_host, "0") == 0)
339 local_host = "0.0.0.0";
340
341 error = getaddrinfo(local_host, 0, NULL, &res);
342 if (error) {
343 printf("%s.\n", gai_strerror(error));
344 usage(argv[0]);
345 exit(1);
346 }
347
348 switch (res->ai_family) {
349 case AF_INET:
350 t_addr = (struct sockaddr_in *)&s_loc;
351
352 memcpy(t_addr, res->ai_addr,
353 res->ai_addrlen);
354 t_addr->sin_family = res->ai_family;
355 t_addr->sin_port = htons(local_port);
356
357 l_len = res->ai_addrlen;
358 #ifdef __FreeBSD__
359 t_addr->sin_len = l_len;
360 #endif
361 break;
362 case AF_INET6:
363 t_addr6 = (struct sockaddr_in6 *)&s_loc;
364
365 memcpy(t_addr6, res->ai_addr,
366 res->ai_addrlen);
367 t_addr6->sin6_family = res->ai_family;
368 t_addr6->sin6_port = htons(local_port);
369 if (interface)
370 t_addr6->sin6_scope_id = if_nametoindex(interface);
371
372 l_len = res->ai_addrlen;
373
374 #ifdef __FreeBSD__
375 t_addr6->sin6_len = l_len;
376 #endif
377 break;
378 }
379
380 error = getnameinfo((struct sockaddr *)&s_loc, l_len, host_s,
381 NI_MAXHOST, serv_s, NI_MAXSERV, NI_NUMERICHOST);
382
383 if (error)
384 printf("%s..\n", gai_strerror(error));
385
386 DEBUG_PRINT(DEBUG_MAX, "local:addr=%s, port=%s, family=%d\n",
387 host_s, serv_s, res->ai_family);
388
389 free(host_s);
390 free(serv_s);
391 freeaddrinfo(res);
392 }
393
394 /* Let the testing begin. */
395 start_test(role);
396
397 return 0;
398 }
399
400 int bind_r(int sk, struct sockaddr_storage *saddr) {
401 int error = 0, i = 0;
402 char *host_s, *serv_s;
403
404 if ((host_s = malloc(NI_MAXHOST)) == NULL) {
405 fprintf(stderr, "\n\t\t*** host_s malloc failed!!! ***\n");
406 exit(1);
407 }
408 if ((serv_s = malloc(NI_MAXSERV)) == NULL) {
409 fprintf(stderr, "\n\t\t*** serv_s malloc failed!!! ***\n");
410 exit(1);
411 }
412
413 do {
414 if (i > 0) sleep(1); /* sleep a while before new try... */
415
416 error = getnameinfo((struct sockaddr *)saddr, l_len, host_s,
417 NI_MAXHOST, serv_s, NI_MAXSERV,
418 NI_NUMERICHOST);
419
420 if (error)
421 printf("%s\n", gai_strerror(error));
422
423 DEBUG_PRINT(DEBUG_MIN,
424 "\tbind(sk=%d, [a:%s,p:%s]) -- attempt %d/%d\n",
425 sk, host_s, serv_s, i+1, MAX_BIND_RETRYS);
426
427 error = bind(sk, (struct sockaddr *)saddr, l_len);
428
429 if (error != 0) {
430 if( errno != EADDRINUSE ) {
431 fprintf(stderr, "\n\n\t\t***bind: can "
432 "not bind to %s:%s: %s ****\n",
433 host_s, serv_s, strerror(errno));
434 exit(1);
435 }
436 }
437 i++;
438 if (i >= MAX_BIND_RETRYS) {
439 fprintf(stderr, "Maximum bind() attempts. "
440 "Die now...\n\n");
441 exit(1);
442 }
443 } while (error < 0 && i < MAX_BIND_RETRYS);
444
445 free(host_s);
446 free(serv_s);
447 return 0;
448 } /* bind_r() */
449
450 int listen_r(int sk, int listen_count) {
451 int error = 0;
452
453 DEBUG_PRINT(DEBUG_MIN, "\tlisten(sk=%d,backlog=%d)\n",
454 sk, listen_count);
455
456 /* Mark sk as being able to accept new associations */
457 error = listen(sk, 1);
458 if (error != 0) {
459 fprintf(stderr, "\n\n\t\t*** listen: %s ***\n\n\n", strerror(errno));
460 exit(1);
461 }
462
463 return 0;
464 } /* listen_r() */
465
466 int accept_r(int sk){
467 socklen_t len = 0;
468
469 DEBUG_PRINT(DEBUG_MIN, "\taccept(sk=%d)\n", sk);
470
471 gsk = accept(sk, NULL, &len);
472 if (gsk < 0) {
473 fprintf(stderr, "\n\n\t\t*** accept: %s ***\n\n\n", strerror(errno));
474 exit(1);
475 }
476
477 return 0;
478 } /* accept_r() */
479
480 int connect_r(int sk, const struct sockaddr *serv_addr, socklen_t addrlen) {
481 int error = 0;
482
483 DEBUG_PRINT(DEBUG_MIN, "\tconnect(sk=%d)\n", sk);
484
485 /* Mark sk as being able to accept new associations */
486 error = connect(sk, serv_addr, addrlen);
487 if (error != 0) {
488 fprintf(stderr, "\n\n\t\t*** connect: %s ***\n\n\n",
489 strerror(errno));
490 exit(1);
491 }
492
493 gsk = sk;
494
495 return 0;
496 } /* connect_r() */
497
498 int close_r(int sk) {
499 int error = 0;
500
501 DEBUG_PRINT(DEBUG_MIN, "\tclose(sk=%d)\n",sk);
502
503 error = close(sk);
504 if (error != 0) {
505 fprintf(stderr, "\n\n\t\t*** close: %s ***\n\n",
506 strerror(errno));
507 exit(1);
508 }
509 fflush(stdout);
510 return 0;
511 } /* close_r() */
512
513 int receive_r(int sk)
514 {
515 int error = 0;
516 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
517 struct iovec iov;
518 struct msghdr inmessage;
519
520 /* Initialize inmessage with enough space for DATA... */
521 memset(&inmessage, 0, sizeof(inmessage));
522 if ((iov.iov_base = malloc(REALLY_BIG)) == NULL) {
523 fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n");
524 exit(1);
525 }
526 iov.iov_len = REALLY_BIG;
527 inmessage.msg_iov = &iov;
528 inmessage.msg_iovlen = 1;
529 /* or a control message. */
530 inmessage.msg_control = incmsg;
531 inmessage.msg_controllen = sizeof(incmsg);
532
533 /* Get the messages sent */
534 while (1) {
535 DEBUG_PRINT(DEBUG_MIN, "\trecvmsg(sk=%d) ", sk);
536
537 error = recvmsg(sk, &inmessage, MSG_WAITALL);
538 if (error < 0 && errno != EAGAIN) {
539 fprintf(stderr, "\n\t\t*** recvmsg: %s ***\n\n",
540 strerror(errno));
541 fflush(stdout);
542 close(sk);
543 free(iov.iov_base);
544 exit(1);
545 } else if (error == 0) {
546 printf("\n\t\trecvmsg() returned 0 !!!!\n");
547 fflush(stdout);
548 }
549
550 if(MSG_NOTIFICATION & inmessage.msg_flags)
551 continue; /* got a notification... */
552
553 inmessage.msg_control = incmsg;
554 inmessage.msg_controllen = sizeof(incmsg);
555 iov.iov_len = REALLY_BIG;
556 break;
557 }
558
559 free(iov.iov_base);
560 return 0;
561 } /* receive_r () */
562
563 void server(int sk) {
564 int i;
565
566 if (max_msgsize > DEFAULT_MAX_WINDOW) {
567 if (setsockopt(sk, IPPROTO_SCTP, SO_RCVBUF, &max_msgsize,
568 sizeof(max_msgsize)) < 0) {
569 perror("setsockopt(SO_RCVBUF)");
570 exit(1);
571 }
572 }
573
574 for (i = 0; i < msg_cnt; i++) {
575 receive_r(sk);
576 DEBUG_PRINT(DEBUG_MIN, "count %d\n", i+1);
577 }
578 } /* server() */
579
580 void * build_msg(int len) {
581 int i = len - 1;
582 int n;
583 char *msg_buf, *p;
584
585 msg_buf = malloc(len);
586 if (NULL == msg_buf) {
587 fprintf(stderr, "\n\t\t*** malloc not enough memory!!! ***\n");
588 exit(1);
589 }
590 p = msg_buf;
591
592 do {
593 n = ((i > 50)?50:i);
594 memcpy(p, msg, ((i > 50)?50:i));
595 p += n;
596 i -= n;
597 } while (i > 0);
598
599 msg_buf[len-1] = '\0';
600
601 return(msg_buf);
602
603 } /* build_msg() */
604
605 int send_r(int sk, int stream, int order, int send_size, int assoc_i) {
606 int error = 0;
607 struct msghdr outmsg;
608 struct iovec iov;
609 char *message = NULL;
610 int msglen = 0;
611 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
612 struct cmsghdr *cmsg;
613 struct sctp_sndrcvinfo *sinfo;
614
615 if (send_size > 0) {
616 message = build_msg(send_size);
617 msglen = strlen(message) + 1;
618 iov.iov_base = message;
619 iov.iov_len = msglen;
620 } else {
621 exit(1);
622 }
623
624 outmsg.msg_name = &s_rem;
625 outmsg.msg_namelen = sizeof(struct sockaddr_storage);
626 outmsg.msg_iov = &iov;
627 outmsg.msg_iovlen = 1;
628 outmsg.msg_control = outcmsg;
629 outmsg.msg_controllen = sizeof(outcmsg);
630 outmsg.msg_flags = 0;
631
632 cmsg = CMSG_FIRSTHDR(&outmsg);
633 cmsg->cmsg_level = IPPROTO_SCTP;
634 cmsg->cmsg_type = SCTP_SNDRCV;
635 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
636
637 outmsg.msg_controllen = cmsg->cmsg_len;
638 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
639 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
640 sinfo->sinfo_ppid = rand();
641 sinfo->sinfo_stream = stream;
642 sinfo->sinfo_flags = 0;
643 if (!order)
644 sinfo->sinfo_flags = SCTP_UNORDERED;
645
646 DEBUG_PRINT(DEBUG_MIN, "\tsendmsg(sk=%d, assoc=%d) %4d bytes.\n",
647 sk, assoc_i, send_size);
648 DEBUG_PRINT(DEBUG_MAX, "\t SNDRCV");
649 if (DEBUG_MAX == debug_level) {
650 printf("(stream=%u ", sinfo->sinfo_stream);
651 printf("flags=0x%x ", sinfo->sinfo_flags);
652 printf("ppid=%u)\n", sinfo->sinfo_ppid);
653 }
654
655 /* Send to our neighbor. */
656 error = sendmsg(sk, &outmsg, MSG_WAITALL);
657 if (error != msglen) {
658 fprintf(stderr, "\n\t\t*** sendmsg: %s ***\n\n",
659 strerror(errno));
660 fflush(stdout);
661 exit(1);
662 }
663
664 if (send_size > 0) free(message);
665 return 0;
666 } /* send_r() */
667
668 int next_order(int state, int pattern)
669 {
670 switch (pattern){
671 case ORDER_PATTERN_UNORDERED:
672 state = 0;
673 break;
674 case ORDER_PATTERN_ORDERED:
675 state = 1;
676 break;
677 case ORDER_PATTERN_ALTERNATE:
678 state = state ? 0 : 1;
679 break;
680 case ORDER_PATTERN_RANDOM:
681 state = rand() % 2;
682 break;
683 }
684
685 return state;
686 }
687
688 int next_stream(int state, int pattern)
689 {
690 switch (pattern){
691 case STREAM_PATTERN_RANDOM:
692 state = rand() % max_stream;
693 break;
694 case STREAM_PATTERN_SEQUENTIAL:
695 state = state + 1;
696 if (state >= max_stream)
697 state = 0;
698 break;
699 }
700
701 return state;
702 }
703
704 int next_msg_size(int msg_cnt)
705 {
706 int msg_size;
707
708 if (size_arg) {
709 msg_size = size_arg;
710 } else {
711 msg_size = (rand() % max_msgsize) + 1;
712 }
713
714 return msg_size;
715
716 } /* next_msg_size() */
717
718 void client(int sk) {
719 int msg_size;
720 int i;
721
722 for (i = 0; i < msg_cnt; i++) {
723 msg_size = next_msg_size(i);
724 order_state = next_order(order_state, order_pattern);
725 stream_state = next_stream(stream_state, stream_pattern);
726
727 if (send_r(sk, stream_state, order_state, msg_size, 0) < 0) {
728 close(sk);
729 break;
730 }
731
732 /* The sender is echoing so do discard the echoed data. */
733 if (drain && ((i + 1) % period == 0)) {
734 receive_r(sk);
735 }
736 }
737 } /* client() */
738
739 void start_test(int role) {
740 int sk, pid, ret;
741 int i = 0;
742
743 DEBUG_PRINT(DEBUG_NONE, "\nStarting tests...\n");
744
745 repeat_count = repeat;
746
747 DEBUG_PRINT(DEBUG_MIN, "\tsocket(SOCK_STREAM, IPPROTO_SCTP)");
748
749 if ((sk = socket(s_loc.ss_family, SOCK_STREAM, IPPROTO_SCTP)) < 0 ) {
750 fprintf(stderr, "\n\n\t\t*** socket: failed to create"
751 " socket: %s ***\n", strerror(errno));
752 exit(1);
753 }
754 DEBUG_PRINT(DEBUG_MIN, " -> sk=%d\n", sk);
755
756 bind_r(sk, &s_loc);
757
758 if (role == SERVER) {
759 listen_r(sk, 1);
760 accept_r(sk);
761 } else {
762 if (max_stream > 0) {
763 struct sctp_initmsg initmsg;
764
765 memset(&initmsg, 0, sizeof(initmsg));
766 initmsg.sinit_num_ostreams = max_stream;
767 initmsg.sinit_max_instreams = max_stream;
768 initmsg.sinit_max_attempts = 3;
769
770 ret = setsockopt(sk, IPPROTO_SCTP, SCTP_INITMSG,
771 &initmsg, sizeof(initmsg));
772 if (ret < 0) {
773 perror("setsockopt(SCTP_INITMSG)");
774 exit(0);
775 }
776 }
777
778 connect_r(sk, (struct sockaddr *)&s_rem, r_len);
779 }
780
781 if ((pid = fork()) == 0) {
782 settimerhandle();
783 printstatus(gsk);
784 while(1);
785 } else {
786 if (!debug_level) {
787 printf(" ");
788 }
789
790 for(i = 0; i < repeat_count; i++) {
791
792 if (role == SERVER) {
793 DEBUG_PRINT(DEBUG_NONE, "Server: Receiving packets.(%d/%d)\n",
794 i+1, repeat_count);
795 server(gsk);
796 } else {
797 DEBUG_PRINT(DEBUG_NONE, "Client: Sending packets.(%d/%d)\n",
798 i+1, repeat_count);
799 client(sk);
800 }
801
802 fflush(stdout);
803 }
804
805 if (role == SERVER) close_r(gsk);
806 close_r(sk);
807 }
808 } /* start_test() */
809
810 void settimerhandle(void) {
811 struct sigaction act;
812 struct itimerval interval;
813
814 act.sa_handler = sighandler;
815 act.sa_flags = 0;
816 sigemptyset(&act.sa_mask);
817 sigaction(SIGPROF, &act, NULL);
818
819 interval.it_value.tv_sec = DEFAULT_SEC;
820 interval.it_value.tv_usec = DEFAULT_USEC;
821 interval.it_interval = interval.it_value;
822
823 setitimer(ITIMER_PROF, &interval, NULL);
824 }
825
826 void usage(char *argv0) {
827 fprintf(stderr, "\nusage:\n");
828 fprintf(stderr, " server:\n");
829 fprintf(stderr, " %8s -H local-addr -P local-port -l [-d level] [-x]\n"
830 "\t [-L num-ports] [-S num-ports]\n"
831 "\t [-a assoc-pattern]\n"
832 "\t [-i interface]\n"
833 "\t [-f status-file]\n",
834 argv0);
835 fprintf(stderr, "\n");
836 fprintf(stderr, " client:\n");
837 fprintf(stderr, " %8s -H local-addr -P local-port -h remote-addr\n"
838 "\t -p remote-port -s [-c case ] [-d level]\n"
839 "\t [-x repeat] [-o order-pattern] ream-pattern]\n"
840 "\t [-M max-stream]\n"
841 "\t [-m max-msgsize]\n"
842 "\t [-L num-ports] [-S num-ports]\n"
843 "\t [-i interface]\n"
844 "\t [-f status-file]\n",
845 argv0);
846 fprintf(stderr, "\n");
847 fprintf(stderr, "\t-c value = Packets of specifed size.\n");
848 fprintf(stderr, "\t-m msgsize(1500-65515, default value 32768)\n");
849 fprintf(stderr, "\t-x number of repeats\n");
850 fprintf(stderr, "\t-X number of messages\n");
851 fprintf(stderr, "\t-o order-pattern\n");
852 fprintf(stderr, "\t 0 = all unordered(default) \n");
853 fprintf(stderr, "\t 1 = all ordered \n");
854 fprintf(stderr, "\t 2 = alternating \n");
855 fprintf(stderr, "\t 3 = random\n");
856 fprintf(stderr, "\t-M max-stream (default value 0)\n");
857 fprintf(stderr, "\t-D drain. If in client mode do a read following send.\n");
858 fprintf(stderr, "\t-I receive after <n> times of send, default value 1.\n");
859 fprintf(stderr, "\n");
860 fflush(stderr);
861
862 } /* usage() */
863
864 void sighandler(int signo) {
865 DEBUG_PRINT(DEBUG_MAX, "timeout sig\n");
866 printstatus(gsk);
867 }
868
869 char* get_sstat_state(int state) {
870 switch(state) {
871 case SCTP_EMPTY:
872 return "EMPTY";
873 case SCTP_CLOSED:
874 return "CLOSED";
875 case SCTP_COOKIE_WAIT:
876 return "COOKIE_WAIT";
877 case SCTP_COOKIE_ECHOED:
878 return "COOKIE_ECHOED";
879 case SCTP_ESTABLISHED:
880 return "ESTABLISHED";
881 case SCTP_SHUTDOWN_PENDING:
882 return "SHUTDOWN_PENDING";
883 case SCTP_SHUTDOWN_SENT:
884 return "SHUTDOWN_SENT";
885 case SCTP_SHUTDOWN_RECEIVED:
886 return "SHUTDOWN_RECEIVED";
887 case SCTP_SHUTDOWN_ACK_SENT:
888 return "SHUTDOWN_ACK_SENT";
889 default:
890 return "UNKNOW";
891 }
892 }
893
894 void printstatus(int sk) {
895 static int cwnd = 0;
896 static int count = 0;
897 struct sctp_status status;
898 socklen_t optlen;
899 FILE * fp;
900 const char *state_to_str[] = {
901 [SCTP_INACTIVE] = "INACTIVE",
902 [SCTP_PF] = "PF",
903 [SCTP_ACTIVE] = "ACTIVE",
904 [SCTP_UNCONFIRMED] = "UNCONFIRMED",
905 };
906
907 optlen = sizeof(struct sctp_status);
908 if(getsockopt(sk, IPPROTO_SCTP, SCTP_STATUS, &status, &optlen) < 0) {
909 fprintf(stderr, "Error getting status: %s.\n", strerror(errno));
910 exit(1);
911 }
912
913 if (statusfile != NULL) {
914 if (count == 0)
915 unlink(statusfile);
916
917 if((fp = fopen(statusfile, "a+")) == NULL) {
918 perror("fopen");
919 exit(1);
920 }
921 } else
922 fp = stdout;
923
924 if (count == 0)
925 fprintf(fp, "NO. ASSOC-ID STATE RWND UNACKDATA PENDDATA INSTRMS OUTSTRMS "
926 "FRAG-POINT SPINFO-STATE SPINFO-CWDN SPINFO-SRTT SPINFO-RTO SPINFO-MTU\n");
927
928 if (cwnd != status.sstat_primary.spinfo_cwnd) {
929 count++;
930
931 fprintf(fp, "%-3d %-8d %-17s %-8d %-9d %-8d %-7d %-8d %-10d %-12s %-11d %-11d %-10d %d\n", count,
932 status.sstat_assoc_id, get_sstat_state(status.sstat_state),
933 status.sstat_rwnd, status.sstat_unackdata, status.sstat_penddata,
934 status.sstat_instrms, status.sstat_outstrms, status.sstat_fragmentation_point,
935 state_to_str[status.sstat_primary.spinfo_state],
936 status.sstat_primary.spinfo_cwnd, status.sstat_primary.spinfo_srtt,
937 status.sstat_primary.spinfo_rto, status.sstat_primary.spinfo_mtu);
938 }
939
940 cwnd = status.sstat_primary.spinfo_cwnd;
941
942 fflush(fp);
943
944 if (fp != stdout)
945 fclose(fp);
946
947 if (status.sstat_primary.spinfo_state != SCTP_ACTIVE) {
948 close_r(sk);
949 exit(1);
950 }
951 }