xref: /iperf/src/iperf_api.c (revision de289c95)
1 /*
2  * iperf, Copyright (c) 2014-2020, The Regents of the University of
3  * California, through Lawrence Berkeley National Laboratory (subject
4  * to receipt of any required approvals from the U.S. Dept. of
5  * Energy).  All rights reserved.
6  *
7  * If you have questions about your rights to use or distribute this
8  * software, please contact Berkeley Lab's Technology Transfer
9  * Department at [email protected].
10  *
11  * NOTICE.  This software is owned by the U.S. Department of Energy.
12  * As such, the U.S. Government has been granted for itself and others
13  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14  * worldwide license in the Software to reproduce, prepare derivative
15  * works, and perform publicly and display publicly.  Beginning five
16  * (5) years after the date permission to assert copyright is obtained
17  * from the U.S. Department of Energy, and subject to any subsequent
18  * five (5) year renewals, the U.S. Government is granted for itself
19  * and others acting on its behalf a paid-up, nonexclusive,
20  * irrevocable, worldwide license in the Software to reproduce,
21  * prepare derivative works, distribute copies to the public, perform
22  * publicly and display publicly, and to permit others to do so.
23  *
24  * This code is distributed under a BSD style license, see the LICENSE file
25  * for complete information.
26  */
27 #ifndef _GNU_SOURCE
28 # define _GNU_SOURCE
29 #endif
30 #define __USE_GNU
31 
32 #include "iperf_config.h"
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <getopt.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <unistd.h>
42 #include <assert.h>
43 #include <fcntl.h>
44 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netdb.h>
49 #ifdef HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
52 #include <netinet/tcp.h>
53 #include <sys/time.h>
54 #include <sys/resource.h>
55 #include <sys/mman.h>
56 #include <sys/stat.h>
57 #include <sched.h>
58 #include <setjmp.h>
59 #include <stdarg.h>
60 #include <math.h>
61 
62 #if defined(HAVE_CPUSET_SETAFFINITY)
63 #include <sys/param.h>
64 #include <sys/cpuset.h>
65 #endif /* HAVE_CPUSET_SETAFFINITY */
66 
67 #if defined(__CYGWIN__) || defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
68 #define CPU_SETSIZE __CPU_SETSIZE
69 #endif /* __CYGWIN__, _WIN32, _WIN64, __WINDOWS__ */
70 
71 #if defined(HAVE_SETPROCESSAFFINITYMASK)
72 #include <Windows.h>
73 #endif /* HAVE_SETPROCESSAFFINITYMASK */
74 
75 #include "net.h"
76 #include "iperf.h"
77 #include "iperf_api.h"
78 #include "iperf_udp.h"
79 #include "iperf_tcp.h"
80 #if defined(HAVE_SCTP_H)
81 #include "iperf_sctp.h"
82 #endif /* HAVE_SCTP_H */
83 #include "timer.h"
84 
85 #include "cjson.h"
86 #include "units.h"
87 #include "iperf_util.h"
88 #include "iperf_locale.h"
89 #include "version.h"
90 #if defined(HAVE_SSL)
91 #include <openssl/bio.h>
92 #include "iperf_auth.h"
93 #endif /* HAVE_SSL */
94 
95 /* Forwards. */
96 static int send_parameters(struct iperf_test *test);
97 static int get_parameters(struct iperf_test *test);
98 static int send_results(struct iperf_test *test);
99 static int get_results(struct iperf_test *test);
100 static int diskfile_send(struct iperf_stream *sp);
101 static int diskfile_recv(struct iperf_stream *sp);
102 static int JSON_write(int fd, cJSON *json);
103 static void print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams);
104 static cJSON *JSON_read(int fd);
105 
106 
107 /*************************** Print usage functions ****************************/
108 
109 void
110 usage()
111 {
112     fputs(usage_shortstr, stderr);
113 }
114 
115 
116 void
117 usage_long(FILE *f)
118 {
119     fprintf(f, usage_longstr, UDP_RATE / (1024*1024), DURATION, DEFAULT_TCP_BLKSIZE / 1024, DEFAULT_UDP_BLKSIZE);
120 }
121 
122 
123 void warning(const char *str)
124 {
125     fprintf(stderr, "warning: %s\n", str);
126 }
127 
128 
129 /************** Getter routines for some fields inside iperf_test *************/
130 
131 int
132 iperf_get_verbose(struct iperf_test *ipt)
133 {
134     return ipt->verbose;
135 }
136 
137 int
138 iperf_get_control_socket(struct iperf_test *ipt)
139 {
140     return ipt->ctrl_sck;
141 }
142 
143 int
144 iperf_get_control_socket_mss(struct iperf_test *ipt)
145 {
146     return ipt->ctrl_sck_mss;
147 }
148 
149 int
150 iperf_get_test_omit(struct iperf_test *ipt)
151 {
152     return ipt->omit;
153 }
154 
155 int
156 iperf_get_test_duration(struct iperf_test *ipt)
157 {
158     return ipt->duration;
159 }
160 
161 uint64_t
162 iperf_get_test_rate(struct iperf_test *ipt)
163 {
164     return ipt->settings->rate;
165 }
166 
167 uint64_t
168 iperf_get_test_bitrate_limit(struct iperf_test *ipt)
169 {
170     return ipt->settings->bitrate_limit;
171 }
172 
173 double
174 iperf_get_test_bitrate_limit_interval(struct iperf_test *ipt)
175 {
176     return ipt->settings->bitrate_limit_interval;
177 }
178 
179 int
180 iperf_get_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt)
181 {
182     return ipt->settings->bitrate_limit_stats_per_interval;
183 }
184 
185 uint64_t
186 iperf_get_test_fqrate(struct iperf_test *ipt)
187 {
188     return ipt->settings->fqrate;
189 }
190 
191 int
192 iperf_get_test_pacing_timer(struct iperf_test *ipt)
193 {
194     return ipt->settings->pacing_timer;
195 }
196 
197 uint64_t
198 iperf_get_test_bytes(struct iperf_test *ipt)
199 {
200     return (uint64_t) ipt->settings->bytes;
201 }
202 
203 uint64_t
204 iperf_get_test_blocks(struct iperf_test *ipt)
205 {
206     return (uint64_t) ipt->settings->blocks;
207 }
208 
209 int
210 iperf_get_test_burst(struct iperf_test *ipt)
211 {
212     return ipt->settings->burst;
213 }
214 
215 char
216 iperf_get_test_role(struct iperf_test *ipt)
217 {
218     return ipt->role;
219 }
220 
221 int
222 iperf_get_test_reverse(struct iperf_test *ipt)
223 {
224     return ipt->reverse;
225 }
226 
227 int
228 iperf_get_test_blksize(struct iperf_test *ipt)
229 {
230     return ipt->settings->blksize;
231 }
232 
233 FILE *
234 iperf_get_test_outfile (struct iperf_test *ipt)
235 {
236     return ipt->outfile;
237 }
238 
239 int
240 iperf_get_test_socket_bufsize(struct iperf_test *ipt)
241 {
242     return ipt->settings->socket_bufsize;
243 }
244 
245 double
246 iperf_get_test_reporter_interval(struct iperf_test *ipt)
247 {
248     return ipt->reporter_interval;
249 }
250 
251 double
252 iperf_get_test_stats_interval(struct iperf_test *ipt)
253 {
254     return ipt->stats_interval;
255 }
256 
257 int
258 iperf_get_test_num_streams(struct iperf_test *ipt)
259 {
260     return ipt->num_streams;
261 }
262 
263 int
264 iperf_get_test_timestamps(struct iperf_test *ipt)
265 {
266     return ipt->timestamps;
267 }
268 
269 const char *
270 iperf_get_test_timestamp_format(struct iperf_test *ipt)
271 {
272     return ipt->timestamp_format;
273 }
274 
275 int
276 iperf_get_test_repeating_payload(struct iperf_test *ipt)
277 {
278     return ipt->repeating_payload;
279 }
280 
281 int
282 iperf_get_test_server_port(struct iperf_test *ipt)
283 {
284     return ipt->server_port;
285 }
286 
287 char*
288 iperf_get_test_server_hostname(struct iperf_test *ipt)
289 {
290     return ipt->server_hostname;
291 }
292 
293 char*
294 iperf_get_test_template(struct iperf_test *ipt)
295 {
296     return ipt->tmp_template;
297 }
298 
299 int
300 iperf_get_test_protocol_id(struct iperf_test *ipt)
301 {
302     return ipt->protocol->id;
303 }
304 
305 int
306 iperf_get_test_json_output(struct iperf_test *ipt)
307 {
308     return ipt->json_output;
309 }
310 
311 char *
312 iperf_get_test_json_output_string(struct iperf_test *ipt)
313 {
314     return ipt->json_output_string;
315 }
316 
317 int
318 iperf_get_test_zerocopy(struct iperf_test *ipt)
319 {
320     return ipt->zerocopy;
321 }
322 
323 int
324 iperf_get_test_get_server_output(struct iperf_test *ipt)
325 {
326     return ipt->get_server_output;
327 }
328 
329 char
330 iperf_get_test_unit_format(struct iperf_test *ipt)
331 {
332     return ipt->settings->unit_format;
333 }
334 
335 char *
336 iperf_get_test_bind_address(struct iperf_test *ipt)
337 {
338     return ipt->bind_address;
339 }
340 
341 int
342 iperf_get_test_udp_counters_64bit(struct iperf_test *ipt)
343 {
344     return ipt->udp_counters_64bit;
345 }
346 
347 int
348 iperf_get_test_one_off(struct iperf_test *ipt)
349 {
350     return ipt->one_off;
351 }
352 
353 int
354 iperf_get_test_tos(struct iperf_test *ipt)
355 {
356     return ipt->settings->tos;
357 }
358 
359 char *
360 iperf_get_test_extra_data(struct iperf_test *ipt)
361 {
362     return ipt->extra_data;
363 }
364 
365 static const char iperf_version[] = IPERF_VERSION;
366 char *
367 iperf_get_iperf_version(void)
368 {
369     return (char*)iperf_version;
370 }
371 
372 int
373 iperf_get_test_no_delay(struct iperf_test *ipt)
374 {
375     return ipt->no_delay;
376 }
377 
378 int
379 iperf_get_test_connect_timeout(struct iperf_test *ipt)
380 {
381     return ipt->settings->connect_timeout;
382 }
383 
384 /************** Setter routines for some fields inside iperf_test *************/
385 
386 void
387 iperf_set_verbose(struct iperf_test *ipt, int verbose)
388 {
389     ipt->verbose = verbose;
390 }
391 
392 void
393 iperf_set_control_socket(struct iperf_test *ipt, int ctrl_sck)
394 {
395     ipt->ctrl_sck = ctrl_sck;
396 }
397 
398 void
399 iperf_set_test_omit(struct iperf_test *ipt, int omit)
400 {
401     ipt->omit = omit;
402 }
403 
404 void
405 iperf_set_test_duration(struct iperf_test *ipt, int duration)
406 {
407     ipt->duration = duration;
408 }
409 
410 void
411 iperf_set_test_reporter_interval(struct iperf_test *ipt, double reporter_interval)
412 {
413     ipt->reporter_interval = reporter_interval;
414 }
415 
416 void
417 iperf_set_test_stats_interval(struct iperf_test *ipt, double stats_interval)
418 {
419     ipt->stats_interval = stats_interval;
420 }
421 
422 void
423 iperf_set_test_state(struct iperf_test *ipt, signed char state)
424 {
425     ipt->state = state;
426 }
427 
428 void
429 iperf_set_test_blksize(struct iperf_test *ipt, int blksize)
430 {
431     ipt->settings->blksize = blksize;
432 }
433 
434 void
435 iperf_set_test_logfile(struct iperf_test *ipt, const char *logfile)
436 {
437     ipt->logfile = strdup(logfile);
438 }
439 
440 void
441 iperf_set_test_rate(struct iperf_test *ipt, uint64_t rate)
442 {
443     ipt->settings->rate = rate;
444 }
445 
446 void
447 iperf_set_test_bitrate_limit_maximum(struct iperf_test *ipt, uint64_t total_rate)
448 {
449     ipt->settings->bitrate_limit = total_rate;
450 }
451 
452 void
453 iperf_set_test_bitrate_limit_interval(struct iperf_test *ipt, uint64_t bitrate_limit_interval)
454 {
455     ipt->settings->bitrate_limit_interval = bitrate_limit_interval;
456 }
457 
458 void
459 iperf_set_test_bitrate_limit_stats_per_interval(struct iperf_test *ipt, uint64_t bitrate_limit_stats_per_interval)
460 {
461     ipt->settings->bitrate_limit_stats_per_interval = bitrate_limit_stats_per_interval;
462 }
463 
464 void
465 iperf_set_test_fqrate(struct iperf_test *ipt, uint64_t fqrate)
466 {
467     ipt->settings->fqrate = fqrate;
468 }
469 
470 void
471 iperf_set_test_pacing_timer(struct iperf_test *ipt, int pacing_timer)
472 {
473     ipt->settings->pacing_timer = pacing_timer;
474 }
475 
476 void
477 iperf_set_test_bytes(struct iperf_test *ipt, uint64_t bytes)
478 {
479     ipt->settings->bytes = (iperf_size_t) bytes;
480 }
481 
482 void
483 iperf_set_test_blocks(struct iperf_test *ipt, uint64_t blocks)
484 {
485     ipt->settings->blocks = (iperf_size_t) blocks;
486 }
487 
488 void
489 iperf_set_test_burst(struct iperf_test *ipt, int burst)
490 {
491     ipt->settings->burst = burst;
492 }
493 
494 void
495 iperf_set_test_server_port(struct iperf_test *ipt, int srv_port)
496 {
497     ipt->server_port = srv_port;
498 }
499 
500 void
501 iperf_set_test_socket_bufsize(struct iperf_test *ipt, int socket_bufsize)
502 {
503     ipt->settings->socket_bufsize = socket_bufsize;
504 }
505 
506 void
507 iperf_set_test_num_streams(struct iperf_test *ipt, int num_streams)
508 {
509     ipt->num_streams = num_streams;
510 }
511 
512 void
513 iperf_set_test_repeating_payload(struct iperf_test *ipt, int repeating_payload)
514 {
515     ipt->repeating_payload = repeating_payload;
516 }
517 
518 void
519 iperf_set_test_timestamps(struct iperf_test *ipt, int timestamps)
520 {
521     ipt->timestamps = timestamps;
522 }
523 
524 void
525 iperf_set_test_timestamp_format(struct iperf_test *ipt, const char *tf)
526 {
527     ipt->timestamp_format = strdup(tf);
528 }
529 
530 static void
531 check_sender_has_retransmits(struct iperf_test *ipt)
532 {
533     if (ipt->mode != RECEIVER && ipt->protocol->id == Ptcp && has_tcpinfo_retransmits())
534 	ipt->sender_has_retransmits = 1;
535     else
536 	ipt->sender_has_retransmits = 0;
537 }
538 
539 void
540 iperf_set_test_role(struct iperf_test *ipt, char role)
541 {
542     ipt->role = role;
543     if (!ipt->reverse) {
544         if (ipt->bidirectional)
545             ipt->mode = BIDIRECTIONAL;
546         else if (role == 'c')
547             ipt->mode = SENDER;
548         else if (role == 's')
549             ipt->mode = RECEIVER;
550     } else {
551         if (role == 'c')
552             ipt->mode = RECEIVER;
553         else if (role == 's')
554             ipt->mode = SENDER;
555     }
556     check_sender_has_retransmits(ipt);
557 }
558 
559 void
560 iperf_set_test_server_hostname(struct iperf_test *ipt, const char *server_hostname)
561 {
562     ipt->server_hostname = strdup(server_hostname);
563 }
564 
565 void
566 iperf_set_test_template(struct iperf_test *ipt, const char *tmp_template)
567 {
568     ipt->tmp_template = strdup(tmp_template);
569 }
570 
571 void
572 iperf_set_test_reverse(struct iperf_test *ipt, int reverse)
573 {
574     ipt->reverse = reverse;
575     if (!ipt->reverse) {
576         if (ipt->role == 'c')
577             ipt->mode = SENDER;
578         else if (ipt->role == 's')
579             ipt->mode = RECEIVER;
580     } else {
581         if (ipt->role == 'c')
582             ipt->mode = RECEIVER;
583         else if (ipt->role == 's')
584             ipt->mode = SENDER;
585     }
586     check_sender_has_retransmits(ipt);
587 }
588 
589 void
590 iperf_set_test_json_output(struct iperf_test *ipt, int json_output)
591 {
592     ipt->json_output = json_output;
593 }
594 
595 int
596 iperf_has_zerocopy( void )
597 {
598     return has_sendfile();
599 }
600 
601 void
602 iperf_set_test_zerocopy(struct iperf_test *ipt, int zerocopy)
603 {
604     ipt->zerocopy = (zerocopy && has_sendfile());
605 }
606 
607 void
608 iperf_set_test_get_server_output(struct iperf_test *ipt, int get_server_output)
609 {
610     ipt->get_server_output = get_server_output;
611 }
612 
613 void
614 iperf_set_test_unit_format(struct iperf_test *ipt, char unit_format)
615 {
616     ipt->settings->unit_format = unit_format;
617 }
618 
619 #if defined(HAVE_SSL)
620 void
621 iperf_set_test_client_username(struct iperf_test *ipt, const char *client_username)
622 {
623     ipt->settings->client_username = strdup(client_username);
624 }
625 
626 void
627 iperf_set_test_client_password(struct iperf_test *ipt, const char *client_password)
628 {
629     ipt->settings->client_password = strdup(client_password);
630 }
631 
632 void
633 iperf_set_test_client_rsa_pubkey(struct iperf_test *ipt, const char *client_rsa_pubkey_base64)
634 {
635     ipt->settings->client_rsa_pubkey = load_pubkey_from_base64(client_rsa_pubkey_base64);
636 }
637 
638 void
639 iperf_set_test_server_authorized_users(struct iperf_test *ipt, const char *server_authorized_users)
640 {
641     ipt->server_authorized_users = strdup(server_authorized_users);
642 }
643 
644 void
645 iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, const char *server_rsa_privkey_base64)
646 {
647     ipt->server_rsa_private_key = load_privkey_from_base64(server_rsa_privkey_base64);
648 }
649 #endif // HAVE_SSL
650 
651 void
652 iperf_set_test_bind_address(struct iperf_test *ipt, const char *bnd_address)
653 {
654     ipt->bind_address = strdup(bnd_address);
655 }
656 
657 void
658 iperf_set_test_udp_counters_64bit(struct iperf_test *ipt, int udp_counters_64bit)
659 {
660     ipt->udp_counters_64bit = udp_counters_64bit;
661 }
662 
663 void
664 iperf_set_test_one_off(struct iperf_test *ipt, int one_off)
665 {
666     ipt->one_off = one_off;
667 }
668 
669 void
670 iperf_set_test_tos(struct iperf_test *ipt, int tos)
671 {
672     ipt->settings->tos = tos;
673 }
674 
675 void
676 iperf_set_test_extra_data(struct iperf_test *ipt, const char *dat)
677 {
678     ipt->extra_data = strdup(dat);
679 }
680 
681 void
682 iperf_set_test_bidirectional(struct iperf_test* ipt, int bidirectional)
683 {
684     ipt->bidirectional = bidirectional;
685     if (bidirectional)
686         ipt->mode = BIDIRECTIONAL;
687     else
688         iperf_set_test_reverse(ipt, ipt->reverse);
689 }
690 
691 void
692 iperf_set_test_no_delay(struct iperf_test* ipt, int no_delay)
693 {
694     ipt->no_delay = no_delay;
695 }
696 
697 void
698 iperf_set_test_connect_timeout(struct iperf_test* ipt, int ct)
699 {
700     ipt->settings->connect_timeout = ct;
701 }
702 
703 
704 /********************** Get/set test protocol structure ***********************/
705 
706 struct protocol *
707 get_protocol(struct iperf_test *test, int prot_id)
708 {
709     struct protocol *prot;
710 
711     SLIST_FOREACH(prot, &test->protocols, protocols) {
712         if (prot->id == prot_id)
713             break;
714     }
715 
716     if (prot == NULL)
717         i_errno = IEPROTOCOL;
718 
719     return prot;
720 }
721 
722 int
723 set_protocol(struct iperf_test *test, int prot_id)
724 {
725     struct protocol *prot = NULL;
726 
727     SLIST_FOREACH(prot, &test->protocols, protocols) {
728         if (prot->id == prot_id) {
729             test->protocol = prot;
730 	    check_sender_has_retransmits(test);
731             return 0;
732         }
733     }
734 
735     i_errno = IEPROTOCOL;
736     return -1;
737 }
738 
739 
740 /************************** Iperf callback functions **************************/
741 
742 void
743 iperf_on_new_stream(struct iperf_stream *sp)
744 {
745     connect_msg(sp);
746 }
747 
748 void
749 iperf_on_test_start(struct iperf_test *test)
750 {
751     if (test->json_output) {
752 	cJSON_AddItemToObject(test->json_start, "test_start", iperf_json_printf("protocol: %s  num_streams: %d  blksize: %d  omit: %d  duration: %d  bytes: %d  blocks: %d  reverse: %d  tos: %d", test->protocol->name, (int64_t) test->num_streams, (int64_t) test->settings->blksize, (int64_t) test->omit, (int64_t) test->duration, (int64_t) test->settings->bytes, (int64_t) test->settings->blocks, test->reverse?(int64_t)1:(int64_t)0, (int64_t) test->settings->tos));
753     } else {
754 	if (test->verbose) {
755 	    if (test->settings->bytes)
756 		iperf_printf(test, test_start_bytes, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->bytes, test->settings->tos);
757 	    else if (test->settings->blocks)
758 		iperf_printf(test, test_start_blocks, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->settings->blocks, test->settings->tos);
759 	    else
760 		iperf_printf(test, test_start_time, test->protocol->name, test->num_streams, test->settings->blksize, test->omit, test->duration, test->settings->tos);
761 	}
762     }
763 }
764 
765 /* This converts an IPv6 string address from IPv4-mapped format into regular
766 ** old IPv4 format, which is easier on the eyes of network veterans.
767 **
768 ** If the v6 address is not v4-mapped it is left alone.
769 */
770 static void
771 mapped_v4_to_regular_v4(char *str)
772 {
773     char *prefix = "::ffff:";
774     int prefix_len;
775 
776     prefix_len = strlen(prefix);
777     if (strncmp(str, prefix, prefix_len) == 0) {
778 	int str_len = strlen(str);
779 	memmove(str, str + prefix_len, str_len - prefix_len + 1);
780     }
781 }
782 
783 void
784 iperf_on_connect(struct iperf_test *test)
785 {
786     time_t now_secs;
787     const char* rfc1123_fmt = "%a, %d %b %Y %H:%M:%S %Z";
788     char now_str[100];
789     char ipr[INET6_ADDRSTRLEN];
790     int port;
791     struct sockaddr_storage sa;
792     struct sockaddr_in *sa_inP;
793     struct sockaddr_in6 *sa_in6P;
794     socklen_t len;
795 
796     now_secs = time((time_t*) 0);
797     (void) strftime(now_str, sizeof(now_str), rfc1123_fmt, gmtime(&now_secs));
798     if (test->json_output)
799 	cJSON_AddItemToObject(test->json_start, "timestamp", iperf_json_printf("time: %s  timesecs: %d", now_str, (int64_t) now_secs));
800     else if (test->verbose)
801 	iperf_printf(test, report_time, now_str);
802 
803     if (test->role == 'c') {
804 	if (test->json_output)
805 	    cJSON_AddItemToObject(test->json_start, "connecting_to", iperf_json_printf("host: %s  port: %d", test->server_hostname, (int64_t) test->server_port));
806 	else {
807 	    iperf_printf(test, report_connecting, test->server_hostname, test->server_port);
808 	    if (test->reverse)
809 		iperf_printf(test, report_reverse, test->server_hostname);
810 	}
811     } else {
812         len = sizeof(sa);
813         getpeername(test->ctrl_sck, (struct sockaddr *) &sa, &len);
814         if (getsockdomain(test->ctrl_sck) == AF_INET) {
815 	    sa_inP = (struct sockaddr_in *) &sa;
816             inet_ntop(AF_INET, &sa_inP->sin_addr, ipr, sizeof(ipr));
817 	    port = ntohs(sa_inP->sin_port);
818         } else {
819 	    sa_in6P = (struct sockaddr_in6 *) &sa;
820             inet_ntop(AF_INET6, &sa_in6P->sin6_addr, ipr, sizeof(ipr));
821 	    port = ntohs(sa_in6P->sin6_port);
822         }
823 	mapped_v4_to_regular_v4(ipr);
824 	if (test->json_output)
825 	    cJSON_AddItemToObject(test->json_start, "accepted_connection", iperf_json_printf("host: %s  port: %d", ipr, (int64_t) port));
826 	else
827 	    iperf_printf(test, report_accepted, ipr, port);
828     }
829     if (test->json_output) {
830 	cJSON_AddStringToObject(test->json_start, "cookie", test->cookie);
831         if (test->protocol->id == SOCK_STREAM) {
832 	    if (test->settings->mss)
833 		cJSON_AddNumberToObject(test->json_start, "tcp_mss", test->settings->mss);
834 	    else {
835 		cJSON_AddNumberToObject(test->json_start, "tcp_mss_default", test->ctrl_sck_mss);
836 	    }
837         if (test->settings->rate)
838             cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate);
839         }
840     } else if (test->verbose) {
841         iperf_printf(test, report_cookie, test->cookie);
842         if (test->protocol->id == SOCK_STREAM) {
843             if (test->settings->mss)
844                 iperf_printf(test, "      TCP MSS: %d\n", test->settings->mss);
845             else {
846                 iperf_printf(test, "      TCP MSS: %d (default)\n", test->ctrl_sck_mss);
847             }
848         }
849         if (test->settings->rate)
850             iperf_printf(test, "      Target Bitrate: %"PRIu64"\n", test->settings->rate);
851     }
852 }
853 
854 void
855 iperf_on_test_finish(struct iperf_test *test)
856 {
857 }
858 
859 
860 /******************************************************************************/
861 
862 int
863 iperf_parse_arguments(struct iperf_test *test, int argc, char **argv)
864 {
865     static struct option longopts[] =
866     {
867         {"port", required_argument, NULL, 'p'},
868         {"format", required_argument, NULL, 'f'},
869         {"interval", required_argument, NULL, 'i'},
870         {"daemon", no_argument, NULL, 'D'},
871         {"one-off", no_argument, NULL, '1'},
872         {"verbose", no_argument, NULL, 'V'},
873         {"json", no_argument, NULL, 'J'},
874         {"version", no_argument, NULL, 'v'},
875         {"server", no_argument, NULL, 's'},
876         {"client", required_argument, NULL, 'c'},
877         {"udp", no_argument, NULL, 'u'},
878         {"bitrate", required_argument, NULL, 'b'},
879         {"bandwidth", required_argument, NULL, 'b'},
880 	{"server-bitrate-limit", required_argument, NULL, OPT_SERVER_BITRATE_LIMIT},
881         {"time", required_argument, NULL, 't'},
882         {"bytes", required_argument, NULL, 'n'},
883         {"blockcount", required_argument, NULL, 'k'},
884         {"length", required_argument, NULL, 'l'},
885         {"parallel", required_argument, NULL, 'P'},
886         {"reverse", no_argument, NULL, 'R'},
887         {"bidir", no_argument, NULL, OPT_BIDIRECTIONAL},
888         {"window", required_argument, NULL, 'w'},
889         {"bind", required_argument, NULL, 'B'},
890         {"cport", required_argument, NULL, OPT_CLIENT_PORT},
891         {"set-mss", required_argument, NULL, 'M'},
892         {"no-delay", no_argument, NULL, 'N'},
893         {"version4", no_argument, NULL, '4'},
894         {"version6", no_argument, NULL, '6'},
895         {"tos", required_argument, NULL, 'S'},
896         {"dscp", required_argument, NULL, OPT_DSCP},
897 	{"extra-data", required_argument, NULL, OPT_EXTRA_DATA},
898 #if defined(HAVE_FLOWLABEL)
899         {"flowlabel", required_argument, NULL, 'L'},
900 #endif /* HAVE_FLOWLABEL */
901         {"zerocopy", no_argument, NULL, 'Z'},
902         {"omit", required_argument, NULL, 'O'},
903         {"file", required_argument, NULL, 'F'},
904         {"repeating-payload", no_argument, NULL, OPT_REPEATING_PAYLOAD},
905         {"timestamps", optional_argument, NULL, OPT_TIMESTAMPS},
906 #if defined(HAVE_CPU_AFFINITY)
907         {"affinity", required_argument, NULL, 'A'},
908 #endif /* HAVE_CPU_AFFINITY */
909         {"title", required_argument, NULL, 'T'},
910 #if defined(HAVE_TCP_CONGESTION)
911         {"congestion", required_argument, NULL, 'C'},
912         {"linux-congestion", required_argument, NULL, 'C'},
913 #endif /* HAVE_TCP_CONGESTION */
914 #if defined(HAVE_SCTP_H)
915         {"sctp", no_argument, NULL, OPT_SCTP},
916         {"nstreams", required_argument, NULL, OPT_NUMSTREAMS},
917         {"xbind", required_argument, NULL, 'X'},
918 #endif
919 	{"pidfile", required_argument, NULL, 'I'},
920 	{"logfile", required_argument, NULL, OPT_LOGFILE},
921 	{"forceflush", no_argument, NULL, OPT_FORCEFLUSH},
922 	{"get-server-output", no_argument, NULL, OPT_GET_SERVER_OUTPUT},
923 	{"udp-counters-64bit", no_argument, NULL, OPT_UDP_COUNTERS_64BIT},
924  	{"no-fq-socket-pacing", no_argument, NULL, OPT_NO_FQ_SOCKET_PACING},
925 #if defined(HAVE_SSL)
926     {"username", required_argument, NULL, OPT_CLIENT_USERNAME},
927     {"rsa-public-key-path", required_argument, NULL, OPT_CLIENT_RSA_PUBLIC_KEY},
928     {"rsa-private-key-path", required_argument, NULL, OPT_SERVER_RSA_PRIVATE_KEY},
929     {"authorized-users-path", required_argument, NULL, OPT_SERVER_AUTHORIZED_USERS},
930 #endif /* HAVE_SSL */
931 	{"fq-rate", required_argument, NULL, OPT_FQ_RATE},
932 	{"pacing-timer", required_argument, NULL, OPT_PACING_TIMER},
933 	{"connect-timeout", required_argument, NULL, OPT_CONNECT_TIMEOUT},
934         {"debug", no_argument, NULL, 'd'},
935         {"help", no_argument, NULL, 'h'},
936         {NULL, 0, NULL, 0}
937     };
938     int flag;
939     int portno;
940     int blksize;
941     int server_flag, client_flag, rate_flag, duration_flag;
942     char *endptr;
943 #if defined(HAVE_CPU_AFFINITY)
944     char* comma;
945 #endif /* HAVE_CPU_AFFINITY */
946     char* slash;
947     struct xbind_entry *xbe;
948     double farg;
949 
950     blksize = 0;
951     server_flag = client_flag = rate_flag = duration_flag = 0;
952 #if defined(HAVE_SSL)
953     char *client_username = NULL, *client_rsa_public_key = NULL, *server_rsa_private_key = NULL;
954 #endif /* HAVE_SSL */
955 
956     while ((flag = getopt_long(argc, argv, "p:f:i:D1VJvsc:ub:t:n:k:l:P:Rw:B:M:N46S:L:ZO:F:A:T:C:dI:hX:", longopts, NULL)) != -1) {
957         switch (flag) {
958             case 'p':
959 		portno = atoi(optarg);
960 		if (portno < 1 || portno > 65535) {
961 		    i_errno = IEBADPORT;
962 		    return -1;
963 		}
964 		test->server_port = portno;
965                 break;
966             case 'f':
967 		if (!optarg) {
968 		    i_errno = IEBADFORMAT;
969 		    return -1;
970 		}
971 		test->settings->unit_format = *optarg;
972 		if (test->settings->unit_format == 'k' ||
973 		    test->settings->unit_format == 'K' ||
974 		    test->settings->unit_format == 'm' ||
975 		    test->settings->unit_format == 'M' ||
976 		    test->settings->unit_format == 'g' ||
977 		    test->settings->unit_format == 'G' ||
978 		    test->settings->unit_format == 't' ||
979 		    test->settings->unit_format == 'T') {
980 			break;
981 		}
982 		else {
983 		    i_errno = IEBADFORMAT;
984 		    return -1;
985 		}
986                 break;
987             case 'i':
988                 /* XXX: could potentially want separate stat collection and reporting intervals,
989                    but just set them to be the same for now */
990                 test->stats_interval = test->reporter_interval = atof(optarg);
991                 if ((test->stats_interval < MIN_INTERVAL || test->stats_interval > MAX_INTERVAL) && test->stats_interval != 0) {
992                     i_errno = IEINTERVAL;
993                     return -1;
994                 }
995                 break;
996             case 'D':
997 		test->daemon = 1;
998 		server_flag = 1;
999 	        break;
1000             case '1':
1001 		test->one_off = 1;
1002 		server_flag = 1;
1003 	        break;
1004             case 'V':
1005                 test->verbose = 1;
1006                 break;
1007             case 'J':
1008                 test->json_output = 1;
1009                 break;
1010             case 'v':
1011                 printf("%s (cJSON %s)\n%s\n%s\n", version, cJSON_Version(), get_system_info(),
1012 		       get_optional_features());
1013                 exit(0);
1014             case 's':
1015                 if (test->role == 'c') {
1016                     i_errno = IESERVCLIENT;
1017                     return -1;
1018                 }
1019 		iperf_set_test_role(test, 's');
1020                 break;
1021             case 'c':
1022                 if (test->role == 's') {
1023                     i_errno = IESERVCLIENT;
1024                     return -1;
1025                 }
1026 		iperf_set_test_role(test, 'c');
1027 		iperf_set_test_server_hostname(test, optarg);
1028                 break;
1029             case 'u':
1030                 set_protocol(test, Pudp);
1031 		client_flag = 1;
1032                 break;
1033             case OPT_SCTP:
1034 #if defined(HAVE_SCTP_H)
1035                 set_protocol(test, Psctp);
1036                 client_flag = 1;
1037                 break;
1038 #else /* HAVE_SCTP_H */
1039                 i_errno = IEUNIMP;
1040                 return -1;
1041 #endif /* HAVE_SCTP_H */
1042 
1043             case OPT_NUMSTREAMS:
1044 #if defined(linux) || defined(__FreeBSD__)
1045                 test->settings->num_ostreams = unit_atoi(optarg);
1046                 client_flag = 1;
1047 #else /* linux */
1048                 i_errno = IEUNIMP;
1049                 return -1;
1050 #endif /* linux */
1051             case 'b':
1052 		slash = strchr(optarg, '/');
1053 		if (slash) {
1054 		    *slash = '\0';
1055 		    ++slash;
1056 		    test->settings->burst = atoi(slash);
1057 		    if (test->settings->burst <= 0 ||
1058 		        test->settings->burst > MAX_BURST) {
1059 			i_errno = IEBURST;
1060 			return -1;
1061 		    }
1062 		}
1063                 test->settings->rate = unit_atof_rate(optarg);
1064 		rate_flag = 1;
1065 		client_flag = 1;
1066                 break;
1067             case OPT_SERVER_BITRATE_LIMIT:
1068 		slash = strchr(optarg, '/');
1069 		if (slash) {
1070 		    *slash = '\0';
1071 		    ++slash;
1072 		    test->settings->bitrate_limit_interval = atof(slash);
1073 		    if (test->settings->bitrate_limit_interval != 0 &&	/* Using same Max/Min limits as for Stats Interval */
1074 		        (test->settings->bitrate_limit_interval < MIN_INTERVAL || test->settings->bitrate_limit_interval > MAX_INTERVAL) ) {
1075 			i_errno = IETOTALINTERVAL;
1076 			return -1;
1077 		    }
1078 		}
1079 		test->settings->bitrate_limit = unit_atof_rate(optarg);
1080 		server_flag = 1;
1081 	        break;
1082             case 't':
1083                 test->duration = atoi(optarg);
1084                 if (test->duration > MAX_TIME) {
1085                     i_errno = IEDURATION;
1086                     return -1;
1087                 }
1088 		duration_flag = 1;
1089 		client_flag = 1;
1090                 break;
1091             case 'n':
1092                 test->settings->bytes = unit_atoi(optarg);
1093 		client_flag = 1;
1094                 break;
1095             case 'k':
1096                 test->settings->blocks = unit_atoi(optarg);
1097 		client_flag = 1;
1098                 break;
1099             case 'l':
1100                 blksize = unit_atoi(optarg);
1101 		client_flag = 1;
1102                 break;
1103             case 'P':
1104                 test->num_streams = atoi(optarg);
1105                 if (test->num_streams > MAX_STREAMS) {
1106                     i_errno = IENUMSTREAMS;
1107                     return -1;
1108                 }
1109 		client_flag = 1;
1110                 break;
1111             case 'R':
1112                 if (test->bidirectional) {
1113                     i_errno = IEREVERSEBIDIR;
1114                     return -1;
1115                 }
1116 		iperf_set_test_reverse(test, 1);
1117 		client_flag = 1;
1118                 break;
1119             case OPT_BIDIRECTIONAL:
1120                 if (test->reverse) {
1121                     i_errno = IEREVERSEBIDIR;
1122                     return -1;
1123                 }
1124                 iperf_set_test_bidirectional(test, 1);
1125                 client_flag = 1;
1126                 break;
1127             case 'w':
1128                 // XXX: This is a socket buffer, not specific to TCP
1129 		// Do sanity checks as double-precision floating point
1130 		// to avoid possible integer overflows.
1131                 farg = unit_atof(optarg);
1132                 if (farg > (double) MAX_TCP_BUFFER) {
1133                     i_errno = IEBUFSIZE;
1134                     return -1;
1135                 }
1136                 test->settings->socket_bufsize = (int) farg;
1137 		client_flag = 1;
1138                 break;
1139             case 'B':
1140                 test->bind_address = strdup(optarg);
1141                 break;
1142             case OPT_CLIENT_PORT:
1143 		portno = atoi(optarg);
1144 		if (portno < 1 || portno > 65535) {
1145 		    i_errno = IEBADPORT;
1146 		    return -1;
1147 		}
1148                 test->bind_port = portno;
1149                 break;
1150             case 'M':
1151                 test->settings->mss = atoi(optarg);
1152                 if (test->settings->mss > MAX_MSS) {
1153                     i_errno = IEMSS;
1154                     return -1;
1155                 }
1156 		client_flag = 1;
1157                 break;
1158             case 'N':
1159                 test->no_delay = 1;
1160 		client_flag = 1;
1161                 break;
1162             case '4':
1163                 test->settings->domain = AF_INET;
1164                 break;
1165             case '6':
1166                 test->settings->domain = AF_INET6;
1167                 break;
1168             case 'S':
1169                 test->settings->tos = strtol(optarg, &endptr, 0);
1170 		if (endptr == optarg ||
1171 		    test->settings->tos < 0 ||
1172 		    test->settings->tos > 255) {
1173 		    i_errno = IEBADTOS;
1174 		    return -1;
1175 		}
1176 		client_flag = 1;
1177                 break;
1178 	    case OPT_DSCP:
1179                 test->settings->tos = parse_qos(optarg);
1180 		if(test->settings->tos < 0) {
1181 			i_errno = IEBADTOS;
1182 			return -1;
1183 		}
1184 		client_flag = 1;
1185                 break;
1186 	    case OPT_EXTRA_DATA:
1187 		test->extra_data = strdup(optarg);
1188 		client_flag = 1;
1189 	        break;
1190             case 'L':
1191 #if defined(HAVE_FLOWLABEL)
1192                 test->settings->flowlabel = strtol(optarg, &endptr, 0);
1193 		if (endptr == optarg ||
1194 		    test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) {
1195                     i_errno = IESETFLOW;
1196                     return -1;
1197 		}
1198 		client_flag = 1;
1199 #else /* HAVE_FLOWLABEL */
1200                 i_errno = IEUNIMP;
1201                 return -1;
1202 #endif /* HAVE_FLOWLABEL */
1203                 break;
1204             case 'X':
1205 		xbe = (struct xbind_entry *)malloc(sizeof(struct xbind_entry));
1206                 if (!xbe) {
1207 		    i_errno = IESETSCTPBINDX;
1208                     return -1;
1209                 }
1210 	        memset(xbe, 0, sizeof(*xbe));
1211                 xbe->name = strdup(optarg);
1212                 if (!xbe->name) {
1213 		    i_errno = IESETSCTPBINDX;
1214                     return -1;
1215                 }
1216 		TAILQ_INSERT_TAIL(&test->xbind_addrs, xbe, link);
1217                 break;
1218             case 'Z':
1219                 if (!has_sendfile()) {
1220                     i_errno = IENOSENDFILE;
1221                     return -1;
1222                 }
1223                 test->zerocopy = 1;
1224 		client_flag = 1;
1225                 break;
1226             case OPT_REPEATING_PAYLOAD:
1227                 test->repeating_payload = 1;
1228                 client_flag = 1;
1229                 break;
1230             case OPT_TIMESTAMPS:
1231                 iperf_set_test_timestamps(test, 1);
1232 		if (optarg) {
1233 		    iperf_set_test_timestamp_format(test, optarg);
1234 		}
1235 		else {
1236 		    iperf_set_test_timestamp_format(test, TIMESTAMP_FORMAT);
1237 		}
1238                 break;
1239             case 'O':
1240                 test->omit = atoi(optarg);
1241                 if (test->omit < 0 || test->omit > 60) {
1242                     i_errno = IEOMIT;
1243                     return -1;
1244                 }
1245 		client_flag = 1;
1246                 break;
1247             case 'F':
1248                 test->diskfile_name = optarg;
1249                 break;
1250             case 'A':
1251 #if defined(HAVE_CPU_AFFINITY)
1252                 test->affinity = strtol(optarg, &endptr, 0);
1253                 if (endptr == optarg ||
1254 		    test->affinity < 0 || test->affinity > 1024) {
1255                     i_errno = IEAFFINITY;
1256                     return -1;
1257                 }
1258 		comma = strchr(optarg, ',');
1259 		if (comma != NULL) {
1260 		    test->server_affinity = atoi(comma+1);
1261 		    if (test->server_affinity < 0 || test->server_affinity > 1024) {
1262 			i_errno = IEAFFINITY;
1263 			return -1;
1264 		    }
1265 		    client_flag = 1;
1266 		}
1267 #else /* HAVE_CPU_AFFINITY */
1268                 i_errno = IEUNIMP;
1269                 return -1;
1270 #endif /* HAVE_CPU_AFFINITY */
1271                 break;
1272             case 'T':
1273                 test->title = strdup(optarg);
1274 		client_flag = 1;
1275                 break;
1276 	    case 'C':
1277 #if defined(HAVE_TCP_CONGESTION)
1278 		test->congestion = strdup(optarg);
1279 		client_flag = 1;
1280 #else /* HAVE_TCP_CONGESTION */
1281 		i_errno = IEUNIMP;
1282 		return -1;
1283 #endif /* HAVE_TCP_CONGESTION */
1284 		break;
1285 	    case 'd':
1286 		test->debug = 1;
1287 		break;
1288 	    case 'I':
1289 		test->pidfile = strdup(optarg);
1290 		server_flag = 1;
1291 	        break;
1292 	    case OPT_LOGFILE:
1293 		test->logfile = strdup(optarg);
1294 		break;
1295 	    case OPT_FORCEFLUSH:
1296 		test->forceflush = 1;
1297 		break;
1298 	    case OPT_GET_SERVER_OUTPUT:
1299 		test->get_server_output = 1;
1300 		client_flag = 1;
1301 		break;
1302 	    case OPT_UDP_COUNTERS_64BIT:
1303 		test->udp_counters_64bit = 1;
1304 		break;
1305 	    case OPT_NO_FQ_SOCKET_PACING:
1306 #if defined(HAVE_SO_MAX_PACING_RATE)
1307 		printf("Warning:  --no-fq-socket-pacing is deprecated\n");
1308 		test->settings->fqrate = 0;
1309 		client_flag = 1;
1310 #else /* HAVE_SO_MAX_PACING_RATE */
1311 		i_errno = IEUNIMP;
1312 		return -1;
1313 #endif
1314 		break;
1315 	    case OPT_FQ_RATE:
1316 #if defined(HAVE_SO_MAX_PACING_RATE)
1317 		test->settings->fqrate = unit_atof_rate(optarg);
1318 		client_flag = 1;
1319 #else /* HAVE_SO_MAX_PACING_RATE */
1320 		i_errno = IEUNIMP;
1321 		return -1;
1322 #endif
1323 		break;
1324 #if defined(HAVE_SSL)
1325         case OPT_CLIENT_USERNAME:
1326             client_username = strdup(optarg);
1327             break;
1328         case OPT_CLIENT_RSA_PUBLIC_KEY:
1329             client_rsa_public_key = strdup(optarg);
1330             break;
1331         case OPT_SERVER_RSA_PRIVATE_KEY:
1332             server_rsa_private_key = strdup(optarg);
1333             break;
1334         case OPT_SERVER_AUTHORIZED_USERS:
1335             test->server_authorized_users = strdup(optarg);
1336             break;
1337 #endif /* HAVE_SSL */
1338 	    case OPT_PACING_TIMER:
1339 		test->settings->pacing_timer = unit_atoi(optarg);
1340 		client_flag = 1;
1341 		break;
1342 	    case OPT_CONNECT_TIMEOUT:
1343 		test->settings->connect_timeout = unit_atoi(optarg);
1344 		client_flag = 1;
1345 		break;
1346 	    case 'h':
1347 		usage_long(stdout);
1348 		exit(0);
1349             default:
1350                 usage_long(stderr);
1351                 exit(1);
1352         }
1353     }
1354 
1355     /* Check flag / role compatibility. */
1356     if (test->role == 'c' && server_flag) {
1357         i_errno = IESERVERONLY;
1358         return -1;
1359     }
1360     if (test->role == 's' && client_flag) {
1361         i_errno = IECLIENTONLY;
1362         return -1;
1363     }
1364 
1365 #if defined(HAVE_SSL)
1366 
1367     if (test->role == 's' && (client_username || client_rsa_public_key)){
1368         i_errno = IECLIENTONLY;
1369         return -1;
1370     } else if (test->role == 'c' && (client_username || client_rsa_public_key) &&
1371         !(client_username && client_rsa_public_key)) {
1372         i_errno = IESETCLIENTAUTH;
1373         return -1;
1374     } else if (test->role == 'c' && (client_username && client_rsa_public_key)){
1375 
1376         char *client_password = NULL;
1377         size_t s;
1378         /* Need to copy env var, so we can do a common free */
1379         if ((client_password = getenv("IPERF3_PASSWORD")) != NULL)
1380              client_password = strdup(client_password);
1381         else if (iperf_getpass(&client_password, &s, stdin) < 0){
1382             i_errno = IESETCLIENTAUTH;
1383             return -1;
1384         }
1385         if (test_load_pubkey_from_file(client_rsa_public_key) < 0){
1386             i_errno = IESETCLIENTAUTH;
1387             return -1;
1388         }
1389 
1390         test->settings->client_username = client_username;
1391         test->settings->client_password = client_password;
1392         test->settings->client_rsa_pubkey = load_pubkey_from_file(client_rsa_public_key);
1393 	free(client_rsa_public_key);
1394 	client_rsa_public_key = NULL;
1395     }
1396 
1397     if (test->role == 'c' && (server_rsa_private_key || test->server_authorized_users)){
1398         i_errno = IESERVERONLY;
1399         return -1;
1400     } else if (test->role == 's' && (server_rsa_private_key || test->server_authorized_users) &&
1401         !(server_rsa_private_key && test->server_authorized_users)) {
1402          i_errno = IESETSERVERAUTH;
1403         return -1;
1404     } else if (test->role == 's' && server_rsa_private_key) {
1405         test->server_rsa_private_key = load_privkey_from_file(server_rsa_private_key);
1406         if (test->server_rsa_private_key == NULL){
1407             i_errno = IESETSERVERAUTH;
1408             return -1;
1409         }
1410 	free(server_rsa_private_key);
1411 	server_rsa_private_key = NULL;
1412     }
1413 
1414 #endif //HAVE_SSL
1415     if (blksize == 0) {
1416 	if (test->protocol->id == Pudp)
1417 	    blksize = 0;	/* try to dynamically determine from MSS */
1418 	else if (test->protocol->id == Psctp)
1419 	    blksize = DEFAULT_SCTP_BLKSIZE;
1420 	else
1421 	    blksize = DEFAULT_TCP_BLKSIZE;
1422     }
1423     if ((test->protocol->id != Pudp && blksize <= 0)
1424 	|| blksize > MAX_BLOCKSIZE) {
1425 	i_errno = IEBLOCKSIZE;
1426 	return -1;
1427     }
1428     if (test->protocol->id == Pudp &&
1429 	(blksize > 0 &&
1430 	    (blksize < MIN_UDP_BLOCKSIZE || blksize > MAX_UDP_BLOCKSIZE))) {
1431 	i_errno = IEUDPBLOCKSIZE;
1432 	return -1;
1433     }
1434     test->settings->blksize = blksize;
1435 
1436     if (!rate_flag)
1437 	test->settings->rate = test->protocol->id == Pudp ? UDP_RATE : 0;
1438 
1439     if ((test->settings->bytes != 0 || test->settings->blocks != 0) && ! duration_flag)
1440         test->duration = 0;
1441 
1442     /* Disallow specifying multiple test end conditions. The code actually
1443     ** works just fine without this prohibition. As soon as any one of the
1444     ** three possible end conditions is met, the test ends. So this check
1445     ** could be removed if desired.
1446     */
1447     if ((duration_flag && test->settings->bytes != 0) ||
1448         (duration_flag && test->settings->blocks != 0) ||
1449 	(test->settings->bytes != 0 && test->settings->blocks != 0)) {
1450         i_errno = IEENDCONDITIONS;
1451         return -1;
1452     }
1453 
1454     /* For subsequent calls to getopt */
1455 #ifdef __APPLE__
1456     optreset = 1;
1457 #endif
1458     optind = 0;
1459 
1460     if ((test->role != 'c') && (test->role != 's')) {
1461         i_errno = IENOROLE;
1462         return -1;
1463     }
1464 
1465     /* Set Total-rate average interval to multiplicity of State interval */
1466     if (test->settings->bitrate_limit_interval != 0) {
1467 	test->settings->bitrate_limit_stats_per_interval =
1468 	    (test->settings->bitrate_limit_interval <= test->stats_interval ?
1469 	    1 : round(test->settings->bitrate_limit_interval/test->stats_interval) );
1470     }
1471 
1472     /* Show warning if JSON output is used with explicit report format */
1473     if ((test->json_output) && (test->settings->unit_format != 'a')) {
1474         warning("Report format (-f) flag ignored with JSON output (-J)");
1475     }
1476 
1477     /* Show warning if JSON output is used with verbose or debug flags */
1478     if (test->json_output && test->verbose) {
1479         warning("Verbose output (-v) may interfere with JSON output (-J)");
1480     }
1481     if (test->json_output && test->debug) {
1482         warning("Debug output (-d) may interfere with JSON output (-J)");
1483     }
1484 
1485     return 0;
1486 }
1487 
1488 /*
1489  * Open the file specified by test->logfile and set test->outfile to its' FD.
1490  */
1491 int iperf_open_logfile(struct iperf_test *test)
1492 {
1493     test->outfile = fopen(test->logfile, "a+");
1494     if (test->outfile == NULL) {
1495         i_errno = IELOGFILE;
1496         return -1;
1497     }
1498 
1499     return 0;
1500 }
1501 
1502 int
1503 iperf_set_send_state(struct iperf_test *test, signed char state)
1504 {
1505     test->state = state;
1506     if (Nwrite(test->ctrl_sck, (char*) &state, sizeof(state), Ptcp) < 0) {
1507 	i_errno = IESENDMESSAGE;
1508 	return -1;
1509     }
1510     return 0;
1511 }
1512 
1513 void
1514 iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP)
1515 {
1516     struct iperf_time temp_time;
1517     double seconds;
1518     uint64_t bits_per_second;
1519 
1520     if (sp->test->done || sp->test->settings->rate == 0 || sp->test->settings->burst != 0)
1521         return;
1522     iperf_time_diff(&sp->result->start_time_fixed, nowP, &temp_time);
1523     seconds = iperf_time_in_secs(&temp_time);
1524     bits_per_second = sp->result->bytes_sent * 8 / seconds;
1525     if (bits_per_second < sp->test->settings->rate) {
1526         sp->green_light = 1;
1527         FD_SET(sp->socket, &sp->test->write_set);
1528     } else {
1529         sp->green_light = 0;
1530         FD_CLR(sp->socket, &sp->test->write_set);
1531     }
1532 }
1533 
1534 /* Verify that average traffic is not greater than the specifid limit */
1535 void
1536 iperf_check_total_rate(struct iperf_test *test, iperf_size_t last_interval_bytes_transferred)
1537 {
1538     double seconds;
1539     uint64_t bits_per_second;
1540     iperf_size_t total_bytes;
1541     int i;
1542 
1543     if (test->done || test->settings->bitrate_limit == 0)    // Continue only if check should be done
1544         return;
1545 
1546     /* Add last inetrval's transffered bytes to the array */
1547     if (++test->bitrate_limit_last_interval_index >= test->settings->bitrate_limit_stats_per_interval)
1548         test->bitrate_limit_last_interval_index = 0;
1549     test->bitrate_limit_intervals_traffic_bytes[test->bitrate_limit_last_interval_index] = last_interval_bytes_transferred;
1550 
1551     /* Ensure that enough stats periods passed to allow averaging throughput */
1552     test->bitrate_limit_stats_count += 1;
1553     if (test->bitrate_limit_stats_count < test->settings->bitrate_limit_stats_per_interval)
1554         return;
1555 
1556      /* Calculating total bytes traffic to be averaged */
1557     for (total_bytes = 0, i = 0; i < test->settings->bitrate_limit_stats_per_interval; i++) {
1558         total_bytes += test->bitrate_limit_intervals_traffic_bytes[i];
1559     }
1560 
1561     seconds = test->stats_interval * test->settings->bitrate_limit_stats_per_interval;
1562     bits_per_second = total_bytes * 8 / seconds;
1563     if (test->debug) {
1564         iperf_printf(test,"Interval %" PRIu64 " - throughput %" PRIu64 " bps (limit %" PRIu64 ")\n", test->bitrate_limit_stats_count, bits_per_second, test->settings->bitrate_limit);
1565     }
1566 
1567     if (bits_per_second  > test->settings->bitrate_limit) {
1568 	iperf_err(test, "Total throughput of %" PRIu64 " bps exceeded %" PRIu64 " bps limit", bits_per_second, test->settings->bitrate_limit);
1569 	test->bitrate_limit_exceeded = 1;
1570     }
1571 }
1572 
1573 int
1574 iperf_send(struct iperf_test *test, fd_set *write_setP)
1575 {
1576     register int multisend, r, streams_active;
1577     register struct iperf_stream *sp;
1578     struct iperf_time now;
1579 
1580     /* Can we do multisend mode? */
1581     if (test->settings->burst != 0)
1582         multisend = test->settings->burst;
1583     else if (test->settings->rate == 0)
1584         multisend = test->multisend;
1585     else
1586         multisend = 1;	/* nope */
1587 
1588     for (; multisend > 0; --multisend) {
1589 	if (test->settings->rate != 0 && test->settings->burst == 0)
1590 	    iperf_time_now(&now);
1591 	streams_active = 0;
1592 	SLIST_FOREACH(sp, &test->streams, streams) {
1593 	    if ((sp->green_light && sp->sender &&
1594 		 (write_setP == NULL || FD_ISSET(sp->socket, write_setP)))) {
1595 		if ((r = sp->snd(sp)) < 0) {
1596 		    if (r == NET_SOFTERROR)
1597 			break;
1598 		    i_errno = IESTREAMWRITE;
1599 		    return r;
1600 		}
1601 		streams_active = 1;
1602 		test->bytes_sent += r;
1603 		++test->blocks_sent;
1604 		iperf_check_throttle(sp, &now);
1605 		if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes)
1606 		    break;
1607 		if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks)
1608 		    break;
1609 	    }
1610 	}
1611 	if (!streams_active)
1612 	    break;
1613     }
1614     if (test->settings->burst != 0) {
1615 	iperf_time_now(&now);
1616 	SLIST_FOREACH(sp, &test->streams, streams)
1617 	    if (sp->sender)
1618 	        iperf_check_throttle(sp, &now);
1619     }
1620     if (write_setP != NULL)
1621 	SLIST_FOREACH(sp, &test->streams, streams)
1622 	    if (FD_ISSET(sp->socket, write_setP))
1623 		FD_CLR(sp->socket, write_setP);
1624 
1625     return 0;
1626 }
1627 
1628 int
1629 iperf_recv(struct iperf_test *test, fd_set *read_setP)
1630 {
1631     int r;
1632     struct iperf_stream *sp;
1633 
1634     SLIST_FOREACH(sp, &test->streams, streams) {
1635 	if (FD_ISSET(sp->socket, read_setP) && !sp->sender) {
1636 	    if ((r = sp->rcv(sp)) < 0) {
1637 		i_errno = IESTREAMREAD;
1638 		return r;
1639 	    }
1640 	    test->bytes_received += r;
1641 	    ++test->blocks_received;
1642 	    FD_CLR(sp->socket, read_setP);
1643 	}
1644     }
1645 
1646     return 0;
1647 }
1648 
1649 int
1650 iperf_init_test(struct iperf_test *test)
1651 {
1652     struct iperf_time now;
1653     struct iperf_stream *sp;
1654 
1655     if (test->protocol->init) {
1656         if (test->protocol->init(test) < 0)
1657             return -1;
1658     }
1659 
1660     /* Init each stream. */
1661     if (iperf_time_now(&now) < 0) {
1662 	i_errno = IEINITTEST;
1663 	return -1;
1664     }
1665     SLIST_FOREACH(sp, &test->streams, streams) {
1666 	sp->result->start_time = sp->result->start_time_fixed = now;
1667     }
1668 
1669     if (test->on_test_start)
1670         test->on_test_start(test);
1671 
1672     return 0;
1673 }
1674 
1675 static void
1676 send_timer_proc(TimerClientData client_data, struct iperf_time *nowP)
1677 {
1678     struct iperf_stream *sp = client_data.p;
1679 
1680     /* All we do here is set or clear the flag saying that this stream may
1681     ** be sent to.  The actual sending gets done in the send proc, after
1682     ** checking the flag.
1683     */
1684     iperf_check_throttle(sp, nowP);
1685 }
1686 
1687 int
1688 iperf_create_send_timers(struct iperf_test * test)
1689 {
1690     struct iperf_time now;
1691     struct iperf_stream *sp;
1692     TimerClientData cd;
1693 
1694     if (iperf_time_now(&now) < 0) {
1695 	i_errno = IEINITTEST;
1696 	return -1;
1697     }
1698     SLIST_FOREACH(sp, &test->streams, streams) {
1699         sp->green_light = 1;
1700 	if (test->settings->rate != 0 && sp->sender) {
1701 	    cd.p = sp;
1702 	    sp->send_timer = tmr_create(NULL, send_timer_proc, cd, test->settings->pacing_timer, 1);
1703 	    if (sp->send_timer == NULL) {
1704 		i_errno = IEINITTEST;
1705 		return -1;
1706 	    }
1707 	}
1708     }
1709     return 0;
1710 }
1711 
1712 #if defined(HAVE_SSL)
1713 int test_is_authorized(struct iperf_test *test){
1714     if ( !(test->server_rsa_private_key && test->server_authorized_users)) {
1715         return 0;
1716     }
1717 
1718     if (test->settings->authtoken){
1719         char *username = NULL, *password = NULL;
1720         time_t ts;
1721         int rc = decode_auth_setting(test->debug, test->settings->authtoken, test->server_rsa_private_key, &username, &password, &ts);
1722 	if (rc) {
1723 	    return -1;
1724 	}
1725         int ret = check_authentication(username, password, ts, test->server_authorized_users);
1726         if (ret == 0){
1727             iperf_printf(test, report_authentication_succeeded, username, ts);
1728             free(username);
1729             free(password);
1730             return 0;
1731         } else {
1732             iperf_printf(test, report_authentication_failed, username, ts);
1733             free(username);
1734             free(password);
1735             return -1;
1736         }
1737     }
1738     return -1;
1739 }
1740 #endif //HAVE_SSL
1741 
1742 /**
1743  * iperf_exchange_parameters - handles the param_Exchange part for client
1744  *
1745  */
1746 
1747 int
1748 iperf_exchange_parameters(struct iperf_test *test)
1749 {
1750     int s;
1751     int32_t err;
1752 
1753     if (test->role == 'c') {
1754 
1755         if (send_parameters(test) < 0)
1756             return -1;
1757 
1758     } else {
1759 
1760         if (get_parameters(test) < 0)
1761             return -1;
1762 
1763 #if defined(HAVE_SSL)
1764         if (test_is_authorized(test) < 0){
1765             if (iperf_set_send_state(test, SERVER_ERROR) != 0)
1766                 return -1;
1767             i_errno = IEAUTHTEST;
1768             err = htonl(i_errno);
1769             if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1770                 i_errno = IECTRLWRITE;
1771                 return -1;
1772             }
1773             return -1;
1774         }
1775 #endif //HAVE_SSL
1776 
1777         if ((s = test->protocol->listen(test)) < 0) {
1778 	        if (iperf_set_send_state(test, SERVER_ERROR) != 0)
1779                 return -1;
1780             err = htonl(i_errno);
1781             if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1782                 i_errno = IECTRLWRITE;
1783                 return -1;
1784             }
1785             err = htonl(errno);
1786             if (Nwrite(test->ctrl_sck, (char*) &err, sizeof(err), Ptcp) < 0) {
1787                 i_errno = IECTRLWRITE;
1788                 return -1;
1789             }
1790             return -1;
1791         }
1792 
1793         FD_SET(s, &test->read_set);
1794         test->max_fd = (s > test->max_fd) ? s : test->max_fd;
1795         test->prot_listener = s;
1796 
1797         // Send the control message to create streams and start the test
1798 	if (iperf_set_send_state(test, CREATE_STREAMS) != 0)
1799             return -1;
1800 
1801     }
1802 
1803     return 0;
1804 }
1805 
1806 /*************************************************************/
1807 
1808 int
1809 iperf_exchange_results(struct iperf_test *test)
1810 {
1811     if (test->role == 'c') {
1812         /* Send results to server. */
1813 	if (send_results(test) < 0)
1814             return -1;
1815         /* Get server results. */
1816         if (get_results(test) < 0)
1817             return -1;
1818     } else {
1819         /* Get client results. */
1820         if (get_results(test) < 0)
1821             return -1;
1822         /* Send results to client. */
1823 	if (send_results(test) < 0)
1824             return -1;
1825     }
1826     return 0;
1827 }
1828 
1829 /*************************************************************/
1830 
1831 static int
1832 send_parameters(struct iperf_test *test)
1833 {
1834     int r = 0;
1835     cJSON *j;
1836 
1837     j = cJSON_CreateObject();
1838     if (j == NULL) {
1839 	i_errno = IESENDPARAMS;
1840 	r = -1;
1841     } else {
1842 	if (test->protocol->id == Ptcp)
1843 	    cJSON_AddTrueToObject(j, "tcp");
1844 	else if (test->protocol->id == Pudp)
1845 	    cJSON_AddTrueToObject(j, "udp");
1846         else if (test->protocol->id == Psctp)
1847             cJSON_AddTrueToObject(j, "sctp");
1848 	cJSON_AddNumberToObject(j, "omit", test->omit);
1849 	if (test->server_affinity != -1)
1850 	    cJSON_AddNumberToObject(j, "server_affinity", test->server_affinity);
1851 	cJSON_AddNumberToObject(j, "time", test->duration);
1852 	if (test->settings->bytes)
1853 	    cJSON_AddNumberToObject(j, "num", test->settings->bytes);
1854 	if (test->settings->blocks)
1855 	    cJSON_AddNumberToObject(j, "blockcount", test->settings->blocks);
1856 	if (test->settings->mss)
1857 	    cJSON_AddNumberToObject(j, "MSS", test->settings->mss);
1858 	if (test->no_delay)
1859 	    cJSON_AddTrueToObject(j, "nodelay");
1860 	cJSON_AddNumberToObject(j, "parallel", test->num_streams);
1861 	if (test->reverse)
1862 	    cJSON_AddTrueToObject(j, "reverse");
1863 	if (test->bidirectional)
1864 	            cJSON_AddTrueToObject(j, "bidirectional");
1865 	if (test->settings->socket_bufsize)
1866 	    cJSON_AddNumberToObject(j, "window", test->settings->socket_bufsize);
1867 	if (test->settings->blksize)
1868 	    cJSON_AddNumberToObject(j, "len", test->settings->blksize);
1869 	if (test->settings->rate)
1870 	    cJSON_AddNumberToObject(j, "bandwidth", test->settings->rate);
1871 	if (test->settings->fqrate)
1872 	    cJSON_AddNumberToObject(j, "fqrate", test->settings->fqrate);
1873 	if (test->settings->pacing_timer)
1874 	    cJSON_AddNumberToObject(j, "pacing_timer", test->settings->pacing_timer);
1875 	if (test->settings->burst)
1876 	    cJSON_AddNumberToObject(j, "burst", test->settings->burst);
1877 	if (test->settings->tos)
1878 	    cJSON_AddNumberToObject(j, "TOS", test->settings->tos);
1879 	if (test->settings->flowlabel)
1880 	    cJSON_AddNumberToObject(j, "flowlabel", test->settings->flowlabel);
1881 	if (test->title)
1882 	    cJSON_AddStringToObject(j, "title", test->title);
1883 	if (test->extra_data)
1884 	    cJSON_AddStringToObject(j, "extra_data", test->extra_data);
1885 	if (test->congestion)
1886 	    cJSON_AddStringToObject(j, "congestion", test->congestion);
1887 	if (test->congestion_used)
1888 	    cJSON_AddStringToObject(j, "congestion_used", test->congestion_used);
1889 	if (test->get_server_output)
1890 	    cJSON_AddNumberToObject(j, "get_server_output", iperf_get_test_get_server_output(test));
1891 	if (test->udp_counters_64bit)
1892 	    cJSON_AddNumberToObject(j, "udp_counters_64bit", iperf_get_test_udp_counters_64bit(test));
1893 	if (test->repeating_payload)
1894 	    cJSON_AddNumberToObject(j, "repeating_payload", test->repeating_payload);
1895 #if defined(HAVE_SSL)
1896 	/* Send authentication parameters */
1897 	if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){
1898 	    int rc = encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken);
1899 
1900 	    if (rc) {
1901 		cJSON_Delete(j);
1902 		i_errno = IESENDPARAMS;
1903 		return -1;
1904 	    }
1905 
1906 	    cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken);
1907 	}
1908 #endif // HAVE_SSL
1909 	cJSON_AddStringToObject(j, "client_version", IPERF_VERSION);
1910 
1911 	if (test->debug) {
1912 	    char *str = cJSON_Print(j);
1913 	    printf("send_parameters:\n%s\n", str);
1914 	    cJSON_free(str);
1915 	}
1916 
1917 	if (JSON_write(test->ctrl_sck, j) < 0) {
1918 	    i_errno = IESENDPARAMS;
1919 	    r = -1;
1920 	}
1921 	cJSON_Delete(j);
1922     }
1923     return r;
1924 }
1925 
1926 /*************************************************************/
1927 
1928 static int
1929 get_parameters(struct iperf_test *test)
1930 {
1931     int r = 0;
1932     cJSON *j;
1933     cJSON *j_p;
1934 
1935     j = JSON_read(test->ctrl_sck);
1936     if (j == NULL) {
1937 	i_errno = IERECVPARAMS;
1938         r = -1;
1939     } else {
1940 	if (test->debug) {
1941             char *str;
1942             str = cJSON_Print(j);
1943             printf("get_parameters:\n%s\n", str );
1944             cJSON_free(str);
1945 	}
1946 
1947 	if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL)
1948 	    set_protocol(test, Ptcp);
1949 	if ((j_p = cJSON_GetObjectItem(j, "udp")) != NULL)
1950 	    set_protocol(test, Pudp);
1951         if ((j_p = cJSON_GetObjectItem(j, "sctp")) != NULL)
1952             set_protocol(test, Psctp);
1953 	if ((j_p = cJSON_GetObjectItem(j, "omit")) != NULL)
1954 	    test->omit = j_p->valueint;
1955 	if ((j_p = cJSON_GetObjectItem(j, "server_affinity")) != NULL)
1956 	    test->server_affinity = j_p->valueint;
1957 	if ((j_p = cJSON_GetObjectItem(j, "time")) != NULL)
1958 	    test->duration = j_p->valueint;
1959 	if ((j_p = cJSON_GetObjectItem(j, "num")) != NULL)
1960 	    test->settings->bytes = j_p->valueint;
1961 	if ((j_p = cJSON_GetObjectItem(j, "blockcount")) != NULL)
1962 	    test->settings->blocks = j_p->valueint;
1963 	if ((j_p = cJSON_GetObjectItem(j, "MSS")) != NULL)
1964 	    test->settings->mss = j_p->valueint;
1965 	if ((j_p = cJSON_GetObjectItem(j, "nodelay")) != NULL)
1966 	    test->no_delay = 1;
1967 	if ((j_p = cJSON_GetObjectItem(j, "parallel")) != NULL)
1968 	    test->num_streams = j_p->valueint;
1969 	if ((j_p = cJSON_GetObjectItem(j, "reverse")) != NULL)
1970 	    iperf_set_test_reverse(test, 1);
1971         if ((j_p = cJSON_GetObjectItem(j, "bidirectional")) != NULL)
1972             iperf_set_test_bidirectional(test, 1);
1973 	if ((j_p = cJSON_GetObjectItem(j, "window")) != NULL)
1974 	    test->settings->socket_bufsize = j_p->valueint;
1975 	if ((j_p = cJSON_GetObjectItem(j, "len")) != NULL)
1976 	    test->settings->blksize = j_p->valueint;
1977 	if ((j_p = cJSON_GetObjectItem(j, "bandwidth")) != NULL)
1978 	    test->settings->rate = j_p->valueint;
1979 	if ((j_p = cJSON_GetObjectItem(j, "fqrate")) != NULL)
1980 	    test->settings->fqrate = j_p->valueint;
1981 	if ((j_p = cJSON_GetObjectItem(j, "pacing_timer")) != NULL)
1982 	    test->settings->pacing_timer = j_p->valueint;
1983 	if ((j_p = cJSON_GetObjectItem(j, "burst")) != NULL)
1984 	    test->settings->burst = j_p->valueint;
1985 	if ((j_p = cJSON_GetObjectItem(j, "TOS")) != NULL)
1986 	    test->settings->tos = j_p->valueint;
1987 	if ((j_p = cJSON_GetObjectItem(j, "flowlabel")) != NULL)
1988 	    test->settings->flowlabel = j_p->valueint;
1989 	if ((j_p = cJSON_GetObjectItem(j, "title")) != NULL)
1990 	    test->title = strdup(j_p->valuestring);
1991 	if ((j_p = cJSON_GetObjectItem(j, "extra_data")) != NULL)
1992 	    test->extra_data = strdup(j_p->valuestring);
1993 	if ((j_p = cJSON_GetObjectItem(j, "congestion")) != NULL)
1994 	    test->congestion = strdup(j_p->valuestring);
1995 	if ((j_p = cJSON_GetObjectItem(j, "congestion_used")) != NULL)
1996 	    test->congestion_used = strdup(j_p->valuestring);
1997 	if ((j_p = cJSON_GetObjectItem(j, "get_server_output")) != NULL)
1998 	    iperf_set_test_get_server_output(test, 1);
1999 	if ((j_p = cJSON_GetObjectItem(j, "udp_counters_64bit")) != NULL)
2000 	    iperf_set_test_udp_counters_64bit(test, 1);
2001 	if ((j_p = cJSON_GetObjectItem(j, "repeating_payload")) != NULL)
2002 	    test->repeating_payload = 1;
2003 #if defined(HAVE_SSL)
2004 	if ((j_p = cJSON_GetObjectItem(j, "authtoken")) != NULL)
2005         test->settings->authtoken = strdup(j_p->valuestring);
2006 #endif //HAVE_SSL
2007 	if (test->mode && test->protocol->id == Ptcp && has_tcpinfo_retransmits())
2008 	    test->sender_has_retransmits = 1;
2009 	if (test->settings->rate)
2010 	    cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate);
2011 	cJSON_Delete(j);
2012     }
2013     return r;
2014 }
2015 
2016 /*************************************************************/
2017 
2018 static int
2019 send_results(struct iperf_test *test)
2020 {
2021     int r = 0;
2022     cJSON *j;
2023     cJSON *j_streams;
2024     struct iperf_stream *sp;
2025     cJSON *j_stream;
2026     int sender_has_retransmits;
2027     iperf_size_t bytes_transferred;
2028     int retransmits;
2029     struct iperf_time temp_time;
2030     double start_time, end_time;
2031 
2032     j = cJSON_CreateObject();
2033     if (j == NULL) {
2034 	i_errno = IEPACKAGERESULTS;
2035 	r = -1;
2036     } else {
2037 	cJSON_AddNumberToObject(j, "cpu_util_total", test->cpu_util[0]);
2038 	cJSON_AddNumberToObject(j, "cpu_util_user", test->cpu_util[1]);
2039 	cJSON_AddNumberToObject(j, "cpu_util_system", test->cpu_util[2]);
2040 	if ( test->mode == RECEIVER )
2041 	    sender_has_retransmits = -1;
2042 	else
2043 	    sender_has_retransmits = test->sender_has_retransmits;
2044 	cJSON_AddNumberToObject(j, "sender_has_retransmits", sender_has_retransmits);
2045 	if ( test->congestion_used ) {
2046 	    cJSON_AddStringToObject(j, "congestion_used", test->congestion_used);
2047 	}
2048 
2049 	/* If on the server and sending server output, then do this */
2050 	if (test->role == 's' && test->get_server_output) {
2051 	    if (test->json_output) {
2052 		/* Add JSON output */
2053 		cJSON_AddItemReferenceToObject(j, "server_output_json", test->json_top);
2054 	    }
2055 	    else {
2056 		/* Add textual output */
2057 		size_t buflen = 0;
2058 
2059 		/* Figure out how much room we need to hold the complete output string */
2060 		struct iperf_textline *t;
2061 		TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) {
2062 		    buflen += strlen(t->line);
2063 		}
2064 
2065 		/* Allocate and build it up from the component lines */
2066 		char *output = calloc(buflen + 1, 1);
2067 		TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) {
2068 		    strncat(output, t->line, buflen);
2069 		    buflen -= strlen(t->line);
2070 		}
2071 
2072 		cJSON_AddStringToObject(j, "server_output_text", output);
2073         free(output);
2074 	    }
2075 	}
2076 
2077 	j_streams = cJSON_CreateArray();
2078 	if (j_streams == NULL) {
2079 	    i_errno = IEPACKAGERESULTS;
2080 	    r = -1;
2081 	} else {
2082 	    cJSON_AddItemToObject(j, "streams", j_streams);
2083 	    SLIST_FOREACH(sp, &test->streams, streams) {
2084 		j_stream = cJSON_CreateObject();
2085 		if (j_stream == NULL) {
2086 		    i_errno = IEPACKAGERESULTS;
2087 		    r = -1;
2088 		} else {
2089 		    cJSON_AddItemToArray(j_streams, j_stream);
2090 		    bytes_transferred = sp->sender ? (sp->result->bytes_sent - sp->result->bytes_sent_omit) : sp->result->bytes_received;
2091 		    retransmits = (sp->sender && test->sender_has_retransmits) ? sp->result->stream_retrans : -1;
2092 		    cJSON_AddNumberToObject(j_stream, "id", sp->id);
2093 		    cJSON_AddNumberToObject(j_stream, "bytes", bytes_transferred);
2094 		    cJSON_AddNumberToObject(j_stream, "retransmits", retransmits);
2095 		    cJSON_AddNumberToObject(j_stream, "jitter", sp->jitter);
2096 		    cJSON_AddNumberToObject(j_stream, "errors", sp->cnt_error);
2097 		    cJSON_AddNumberToObject(j_stream, "packets", sp->packet_count);
2098 
2099 		    iperf_time_diff(&sp->result->start_time, &sp->result->start_time, &temp_time);
2100 		    start_time = iperf_time_in_secs(&temp_time);
2101 		    iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time);
2102 		    end_time = iperf_time_in_secs(&temp_time);
2103 		    cJSON_AddNumberToObject(j_stream, "start_time", start_time);
2104 		    cJSON_AddNumberToObject(j_stream, "end_time", end_time);
2105 
2106 		}
2107 	    }
2108 	    if (r == 0 && test->debug) {
2109                 char *str = cJSON_Print(j);
2110 		printf("send_results\n%s\n", str);
2111                 cJSON_free(str);
2112 	    }
2113 	    if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) {
2114 		i_errno = IESENDRESULTS;
2115 		r = -1;
2116 	    }
2117 	}
2118 	cJSON_Delete(j);
2119     }
2120     return r;
2121 }
2122 
2123 /*************************************************************/
2124 
2125 static int
2126 get_results(struct iperf_test *test)
2127 {
2128     int r = 0;
2129     cJSON *j;
2130     cJSON *j_cpu_util_total;
2131     cJSON *j_cpu_util_user;
2132     cJSON *j_cpu_util_system;
2133     cJSON *j_remote_congestion_used;
2134     cJSON *j_sender_has_retransmits;
2135     int result_has_retransmits;
2136     cJSON *j_streams;
2137     int n, i;
2138     cJSON *j_stream;
2139     cJSON *j_id;
2140     cJSON *j_bytes;
2141     cJSON *j_retransmits;
2142     cJSON *j_jitter;
2143     cJSON *j_errors;
2144     cJSON *j_packets;
2145     cJSON *j_server_output;
2146     cJSON *j_start_time, *j_end_time;
2147     int sid, cerror, pcount;
2148     double jitter;
2149     iperf_size_t bytes_transferred;
2150     int retransmits;
2151     struct iperf_stream *sp;
2152 
2153     j = JSON_read(test->ctrl_sck);
2154     if (j == NULL) {
2155 	i_errno = IERECVRESULTS;
2156         r = -1;
2157     } else {
2158 	j_cpu_util_total = cJSON_GetObjectItem(j, "cpu_util_total");
2159 	j_cpu_util_user = cJSON_GetObjectItem(j, "cpu_util_user");
2160 	j_cpu_util_system = cJSON_GetObjectItem(j, "cpu_util_system");
2161 	j_sender_has_retransmits = cJSON_GetObjectItem(j, "sender_has_retransmits");
2162 	if (j_cpu_util_total == NULL || j_cpu_util_user == NULL || j_cpu_util_system == NULL || j_sender_has_retransmits == NULL) {
2163 	    i_errno = IERECVRESULTS;
2164 	    r = -1;
2165 	} else {
2166 	    if (test->debug) {
2167                 char *str = cJSON_Print(j);
2168                 printf("get_results\n%s\n", str);
2169                 cJSON_free(str);
2170 	    }
2171 
2172 	    test->remote_cpu_util[0] = j_cpu_util_total->valuedouble;
2173 	    test->remote_cpu_util[1] = j_cpu_util_user->valuedouble;
2174 	    test->remote_cpu_util[2] = j_cpu_util_system->valuedouble;
2175 	    result_has_retransmits = j_sender_has_retransmits->valueint;
2176 	    if ( test->mode == RECEIVER ) {
2177 	        test->sender_has_retransmits = result_has_retransmits;
2178 	        test->other_side_has_retransmits = 0;
2179 	    }
2180 	    else if ( test->mode == BIDIRECTIONAL )
2181 	        test->other_side_has_retransmits = result_has_retransmits;
2182 
2183 	    j_streams = cJSON_GetObjectItem(j, "streams");
2184 	    if (j_streams == NULL) {
2185 		i_errno = IERECVRESULTS;
2186 		r = -1;
2187 	    } else {
2188 	        n = cJSON_GetArraySize(j_streams);
2189 		for (i=0; i<n; ++i) {
2190 		    j_stream = cJSON_GetArrayItem(j_streams, i);
2191 		    if (j_stream == NULL) {
2192 			i_errno = IERECVRESULTS;
2193 			r = -1;
2194 		    } else {
2195 			j_id = cJSON_GetObjectItem(j_stream, "id");
2196 			j_bytes = cJSON_GetObjectItem(j_stream, "bytes");
2197 			j_retransmits = cJSON_GetObjectItem(j_stream, "retransmits");
2198 			j_jitter = cJSON_GetObjectItem(j_stream, "jitter");
2199 			j_errors = cJSON_GetObjectItem(j_stream, "errors");
2200 			j_packets = cJSON_GetObjectItem(j_stream, "packets");
2201 			j_start_time = cJSON_GetObjectItem(j_stream, "start_time");
2202 			j_end_time = cJSON_GetObjectItem(j_stream, "end_time");
2203 			if (j_id == NULL || j_bytes == NULL || j_retransmits == NULL || j_jitter == NULL || j_errors == NULL || j_packets == NULL) {
2204 			    i_errno = IERECVRESULTS;
2205 			    r = -1;
2206 			} else {
2207 			    sid = j_id->valueint;
2208 			    bytes_transferred = j_bytes->valueint;
2209 			    retransmits = j_retransmits->valueint;
2210 			    jitter = j_jitter->valuedouble;
2211 			    cerror = j_errors->valueint;
2212 			    pcount = j_packets->valueint;
2213 			    SLIST_FOREACH(sp, &test->streams, streams)
2214 				if (sp->id == sid) break;
2215 			    if (sp == NULL) {
2216 				i_errno = IESTREAMID;
2217 				r = -1;
2218 			    } else {
2219 				if (sp->sender) {
2220 				    sp->jitter = jitter;
2221 				    sp->cnt_error = cerror;
2222 				    sp->peer_packet_count = pcount;
2223 				    sp->result->bytes_received = bytes_transferred;
2224 				    /*
2225 				     * We have to handle the possibilty that
2226 				     * start_time and end_time might not be
2227 				     * available; this is the case for older (pre-3.2)
2228 				     * servers.
2229 				     *
2230 				     * We need to have result structure members to hold
2231 				     * the both sides' start_time and end_time.
2232 				     */
2233 				    if (j_start_time && j_end_time) {
2234 					sp->result->receiver_time = j_end_time->valuedouble - j_start_time->valuedouble;
2235 				    }
2236 				    else {
2237 					sp->result->receiver_time = 0.0;
2238 				    }
2239 				} else {
2240 				    sp->peer_packet_count = pcount;
2241 				    sp->result->bytes_sent = bytes_transferred;
2242 				    sp->result->stream_retrans = retransmits;
2243 				    if (j_start_time && j_end_time) {
2244 					sp->result->sender_time = j_end_time->valuedouble - j_start_time->valuedouble;
2245 				    }
2246 				    else {
2247 					sp->result->sender_time = 0.0;
2248 				    }
2249 				}
2250 			    }
2251 			}
2252 		    }
2253 		}
2254 		/*
2255 		 * If we're the client and we're supposed to get remote results,
2256 		 * look them up and process accordingly.
2257 		 */
2258 		if (test->role == 'c' && iperf_get_test_get_server_output(test)) {
2259 		    /* Look for JSON.  If we find it, grab the object so it doesn't get deleted. */
2260 		    j_server_output = cJSON_DetachItemFromObject(j, "server_output_json");
2261 		    if (j_server_output != NULL) {
2262 			test->json_server_output = j_server_output;
2263 		    }
2264 		    else {
2265 			/* No JSON, look for textual output.  Make a copy of the text for later. */
2266 			j_server_output = cJSON_GetObjectItem(j, "server_output_text");
2267 			if (j_server_output != NULL) {
2268 			    test->server_output_text = strdup(j_server_output->valuestring);
2269 			}
2270 		    }
2271 		}
2272 	    }
2273 	}
2274 
2275 	j_remote_congestion_used = cJSON_GetObjectItem(j, "congestion_used");
2276 	if (j_remote_congestion_used != NULL) {
2277 	    test->remote_congestion_used = strdup(j_remote_congestion_used->valuestring);
2278 	}
2279 
2280 	cJSON_Delete(j);
2281     }
2282     return r;
2283 }
2284 
2285 /*************************************************************/
2286 
2287 static int
2288 JSON_write(int fd, cJSON *json)
2289 {
2290     uint32_t hsize, nsize;
2291     char *str;
2292     int r = 0;
2293 
2294     str = cJSON_PrintUnformatted(json);
2295     if (str == NULL)
2296 	r = -1;
2297     else {
2298 	hsize = strlen(str);
2299 	nsize = htonl(hsize);
2300 	if (Nwrite(fd, (char*) &nsize, sizeof(nsize), Ptcp) < 0)
2301 	    r = -1;
2302 	else {
2303 	    if (Nwrite(fd, str, hsize, Ptcp) < 0)
2304 		r = -1;
2305 	}
2306 	cJSON_free(str);
2307     }
2308     return r;
2309 }
2310 
2311 /*************************************************************/
2312 
2313 static cJSON *
2314 JSON_read(int fd)
2315 {
2316     uint32_t hsize, nsize;
2317     char *str;
2318     cJSON *json = NULL;
2319     int rc;
2320 
2321     /*
2322      * Read a four-byte integer, which is the length of the JSON to follow.
2323      * Then read the JSON into a buffer and parse it.  Return a parsed JSON
2324      * structure, NULL if there was an error.
2325      */
2326     if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) {
2327 	hsize = ntohl(nsize);
2328 	/* Allocate a buffer to hold the JSON */
2329 	str = (char *) calloc(sizeof(char), hsize+1);	/* +1 for trailing null */
2330 	if (str != NULL) {
2331 	    rc = Nread(fd, str, hsize, Ptcp);
2332 	    if (rc >= 0) {
2333 		/*
2334 		 * We should be reading in the number of bytes corresponding to the
2335 		 * length in that 4-byte integer.  If we don't the socket might have
2336 		 * prematurely closed.  Only do the JSON parsing if we got the
2337 		 * correct number of bytes.
2338 		 */
2339 		if (rc == hsize) {
2340 		    json = cJSON_Parse(str);
2341 		}
2342 		else {
2343 		    printf("WARNING:  Size of data read does not correspond to offered length\n");
2344 		}
2345 	    }
2346 	}
2347 	free(str);
2348     }
2349     return json;
2350 }
2351 
2352 /*************************************************************/
2353 /**
2354  * add_to_interval_list -- adds new interval to the interval_list
2355  */
2356 
2357 void
2358 add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_results * new)
2359 {
2360     struct iperf_interval_results *irp;
2361 
2362     irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results));
2363     memcpy(irp, new, sizeof(struct iperf_interval_results));
2364     TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries);
2365 }
2366 
2367 
2368 /************************************************************/
2369 
2370 /**
2371  * connect_msg -- displays connection message
2372  * denoting sender/receiver details
2373  *
2374  */
2375 
2376 void
2377 connect_msg(struct iperf_stream *sp)
2378 {
2379     char ipl[INET6_ADDRSTRLEN], ipr[INET6_ADDRSTRLEN];
2380     int lport, rport;
2381 
2382     if (getsockdomain(sp->socket) == AF_INET) {
2383         inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->local_addr)->sin_addr, ipl, sizeof(ipl));
2384 	mapped_v4_to_regular_v4(ipl);
2385         inet_ntop(AF_INET, (void *) &((struct sockaddr_in *) &sp->remote_addr)->sin_addr, ipr, sizeof(ipr));
2386 	mapped_v4_to_regular_v4(ipr);
2387         lport = ntohs(((struct sockaddr_in *) &sp->local_addr)->sin_port);
2388         rport = ntohs(((struct sockaddr_in *) &sp->remote_addr)->sin_port);
2389     } else {
2390         inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->local_addr)->sin6_addr, ipl, sizeof(ipl));
2391 	mapped_v4_to_regular_v4(ipl);
2392         inet_ntop(AF_INET6, (void *) &((struct sockaddr_in6 *) &sp->remote_addr)->sin6_addr, ipr, sizeof(ipr));
2393 	mapped_v4_to_regular_v4(ipr);
2394         lport = ntohs(((struct sockaddr_in6 *) &sp->local_addr)->sin6_port);
2395         rport = ntohs(((struct sockaddr_in6 *) &sp->remote_addr)->sin6_port);
2396     }
2397 
2398     if (sp->test->json_output)
2399         cJSON_AddItemToArray(sp->test->json_connected, iperf_json_printf("socket: %d  local_host: %s  local_port: %d  remote_host: %s  remote_port: %d", (int64_t) sp->socket, ipl, (int64_t) lport, ipr, (int64_t) rport));
2400     else
2401 	iperf_printf(sp->test, report_connected, sp->socket, ipl, lport, ipr, rport);
2402 }
2403 
2404 
2405 /**************************************************************************/
2406 
2407 struct iperf_test *
2408 iperf_new_test()
2409 {
2410     struct iperf_test *test;
2411 
2412     test = (struct iperf_test *) malloc(sizeof(struct iperf_test));
2413     if (!test) {
2414         i_errno = IENEWTEST;
2415         return NULL;
2416     }
2417     /* initialize everything to zero */
2418     memset(test, 0, sizeof(struct iperf_test));
2419 
2420     test->settings = (struct iperf_settings *) malloc(sizeof(struct iperf_settings));
2421     if (!test->settings) {
2422         free(test);
2423 	i_errno = IENEWTEST;
2424 	return NULL;
2425     }
2426     memset(test->settings, 0, sizeof(struct iperf_settings));
2427 
2428     test->bitrate_limit_intervals_traffic_bytes = (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL);
2429     if (!test->bitrate_limit_intervals_traffic_bytes) {
2430         free(test);
2431 	i_errno = IENEWTEST;
2432 	return NULL;
2433     }
2434     memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL));
2435 
2436     /* By default all output goes to stdout */
2437     test->outfile = stdout;
2438 
2439     return test;
2440 }
2441 
2442 /**************************************************************************/
2443 
2444 struct protocol *
2445 protocol_new(void)
2446 {
2447     struct protocol *proto;
2448 
2449     proto = malloc(sizeof(struct protocol));
2450     if(!proto) {
2451         return NULL;
2452     }
2453     memset(proto, 0, sizeof(struct protocol));
2454 
2455     return proto;
2456 }
2457 
2458 void
2459 protocol_free(struct protocol *proto)
2460 {
2461     free(proto);
2462 }
2463 
2464 /**************************************************************************/
2465 int
2466 iperf_defaults(struct iperf_test *testp)
2467 {
2468     struct protocol *tcp, *udp;
2469 #if defined(HAVE_SCTP_H)
2470     struct protocol *sctp;
2471 #endif /* HAVE_SCTP_H */
2472 
2473     testp->omit = OMIT;
2474     testp->duration = DURATION;
2475     testp->diskfile_name = (char*) 0;
2476     testp->affinity = -1;
2477     testp->server_affinity = -1;
2478     TAILQ_INIT(&testp->xbind_addrs);
2479 #if defined(HAVE_CPUSET_SETAFFINITY)
2480     CPU_ZERO(&testp->cpumask);
2481 #endif /* HAVE_CPUSET_SETAFFINITY */
2482     testp->title = NULL;
2483     testp->extra_data = NULL;
2484     testp->congestion = NULL;
2485     testp->congestion_used = NULL;
2486     testp->remote_congestion_used = NULL;
2487     testp->server_port = PORT;
2488     testp->ctrl_sck = -1;
2489     testp->prot_listener = -1;
2490     testp->other_side_has_retransmits = 0;
2491 
2492     testp->stats_callback = iperf_stats_callback;
2493     testp->reporter_callback = iperf_reporter_callback;
2494 
2495     testp->stats_interval = testp->reporter_interval = 1;
2496     testp->num_streams = 1;
2497 
2498     testp->settings->domain = AF_UNSPEC;
2499     testp->settings->unit_format = 'a';
2500     testp->settings->socket_bufsize = 0;    /* use autotuning */
2501     testp->settings->blksize = DEFAULT_TCP_BLKSIZE;
2502     testp->settings->rate = 0;
2503     testp->settings->bitrate_limit = 0;
2504     testp->settings->bitrate_limit_interval = 5;
2505     testp->settings->bitrate_limit_stats_per_interval = 0;
2506     testp->settings->fqrate = 0;
2507     testp->settings->pacing_timer = 1000;
2508     testp->settings->burst = 0;
2509     testp->settings->mss = 0;
2510     testp->settings->bytes = 0;
2511     testp->settings->blocks = 0;
2512     testp->settings->connect_timeout = -1;
2513     memset(testp->cookie, 0, COOKIE_SIZE);
2514 
2515     testp->multisend = 10;	/* arbitrary */
2516 
2517     /* Set up protocol list */
2518     SLIST_INIT(&testp->streams);
2519     SLIST_INIT(&testp->protocols);
2520 
2521     tcp = protocol_new();
2522     if (!tcp)
2523         return -1;
2524 
2525     tcp->id = Ptcp;
2526     tcp->name = "TCP";
2527     tcp->accept = iperf_tcp_accept;
2528     tcp->listen = iperf_tcp_listen;
2529     tcp->connect = iperf_tcp_connect;
2530     tcp->send = iperf_tcp_send;
2531     tcp->recv = iperf_tcp_recv;
2532     tcp->init = NULL;
2533     SLIST_INSERT_HEAD(&testp->protocols, tcp, protocols);
2534 
2535     udp = protocol_new();
2536     if (!udp) {
2537         protocol_free(tcp);
2538         return -1;
2539     }
2540 
2541     udp->id = Pudp;
2542     udp->name = "UDP";
2543     udp->accept = iperf_udp_accept;
2544     udp->listen = iperf_udp_listen;
2545     udp->connect = iperf_udp_connect;
2546     udp->send = iperf_udp_send;
2547     udp->recv = iperf_udp_recv;
2548     udp->init = iperf_udp_init;
2549     SLIST_INSERT_AFTER(tcp, udp, protocols);
2550 
2551     set_protocol(testp, Ptcp);
2552 
2553 #if defined(HAVE_SCTP_H)
2554     sctp = protocol_new();
2555     if (!sctp) {
2556         protocol_free(tcp);
2557         protocol_free(udp);
2558         return -1;
2559     }
2560 
2561     sctp->id = Psctp;
2562     sctp->name = "SCTP";
2563     sctp->accept = iperf_sctp_accept;
2564     sctp->listen = iperf_sctp_listen;
2565     sctp->connect = iperf_sctp_connect;
2566     sctp->send = iperf_sctp_send;
2567     sctp->recv = iperf_sctp_recv;
2568     sctp->init = iperf_sctp_init;
2569 
2570     SLIST_INSERT_AFTER(udp, sctp, protocols);
2571 #endif /* HAVE_SCTP_H */
2572 
2573     testp->on_new_stream = iperf_on_new_stream;
2574     testp->on_test_start = iperf_on_test_start;
2575     testp->on_connect = iperf_on_connect;
2576     testp->on_test_finish = iperf_on_test_finish;
2577 
2578     TAILQ_INIT(&testp->server_output_list);
2579 
2580     return 0;
2581 }
2582 
2583 
2584 /**************************************************************************/
2585 void
2586 iperf_free_test(struct iperf_test *test)
2587 {
2588     struct protocol *prot;
2589     struct iperf_stream *sp;
2590 
2591     /* Free streams */
2592     while (!SLIST_EMPTY(&test->streams)) {
2593         sp = SLIST_FIRST(&test->streams);
2594         SLIST_REMOVE_HEAD(&test->streams, streams);
2595         iperf_free_stream(sp);
2596     }
2597     if (test->server_hostname)
2598 	free(test->server_hostname);
2599     if (test->tmp_template)
2600 	free(test->tmp_template);
2601     if (test->bind_address)
2602 	free(test->bind_address);
2603     if (!TAILQ_EMPTY(&test->xbind_addrs)) {
2604         struct xbind_entry *xbe;
2605 
2606         while (!TAILQ_EMPTY(&test->xbind_addrs)) {
2607             xbe = TAILQ_FIRST(&test->xbind_addrs);
2608             TAILQ_REMOVE(&test->xbind_addrs, xbe, link);
2609             if (xbe->ai)
2610                 freeaddrinfo(xbe->ai);
2611             free(xbe->name);
2612             free(xbe);
2613         }
2614     }
2615 #if defined(HAVE_SSL)
2616 
2617     if (test->server_rsa_private_key)
2618       EVP_PKEY_free(test->server_rsa_private_key);
2619     test->server_rsa_private_key = NULL;
2620 
2621     free(test->settings->authtoken);
2622     test->settings->authtoken = NULL;
2623 
2624     free(test->settings->client_username);
2625     test->settings->client_username = NULL;
2626 
2627     free(test->settings->client_password);
2628     test->settings->client_password = NULL;
2629 
2630     if (test->settings->client_rsa_pubkey)
2631       EVP_PKEY_free(test->settings->client_rsa_pubkey);
2632     test->settings->client_rsa_pubkey = NULL;
2633 #endif /* HAVE_SSL */
2634 
2635     if (test->settings)
2636     free(test->settings);
2637     if (test->title)
2638 	free(test->title);
2639     if (test->extra_data)
2640 	free(test->extra_data);
2641     if (test->congestion)
2642 	free(test->congestion);
2643     if (test->congestion_used)
2644 	free(test->congestion_used);
2645     if (test->remote_congestion_used)
2646 	free(test->remote_congestion_used);
2647     if (test->timestamp_format)
2648 	free(test->timestamp_format);
2649     if (test->omit_timer != NULL)
2650 	tmr_cancel(test->omit_timer);
2651     if (test->timer != NULL)
2652 	tmr_cancel(test->timer);
2653     if (test->stats_timer != NULL)
2654 	tmr_cancel(test->stats_timer);
2655     if (test->reporter_timer != NULL)
2656 	tmr_cancel(test->reporter_timer);
2657 
2658     /* Free protocol list */
2659     while (!SLIST_EMPTY(&test->protocols)) {
2660         prot = SLIST_FIRST(&test->protocols);
2661         SLIST_REMOVE_HEAD(&test->protocols, protocols);
2662         free(prot);
2663     }
2664 
2665     if (test->server_output_text) {
2666 	free(test->server_output_text);
2667 	test->server_output_text = NULL;
2668     }
2669 
2670     if (test->json_output_string) {
2671 	free(test->json_output_string);
2672 	test->json_output_string = NULL;
2673     }
2674 
2675     /* Free output line buffers, if any (on the server only) */
2676     struct iperf_textline *t;
2677     while (!TAILQ_EMPTY(&test->server_output_list)) {
2678 	t = TAILQ_FIRST(&test->server_output_list);
2679 	TAILQ_REMOVE(&test->server_output_list, t, textlineentries);
2680 	free(t->line);
2681 	free(t);
2682     }
2683 
2684     /* sctp_bindx: do not free the arguments, only the resolver results */
2685     if (!TAILQ_EMPTY(&test->xbind_addrs)) {
2686         struct xbind_entry *xbe;
2687 
2688         TAILQ_FOREACH(xbe, &test->xbind_addrs, link) {
2689             if (xbe->ai) {
2690                 freeaddrinfo(xbe->ai);
2691                 xbe->ai = NULL;
2692             }
2693         }
2694     }
2695 
2696     /* Free interval's traffic array for avrage rate calculations */
2697     if (test->bitrate_limit_intervals_traffic_bytes != NULL)
2698         free(test->bitrate_limit_intervals_traffic_bytes);
2699 
2700     /* XXX: Why are we setting these values to NULL? */
2701     // test->streams = NULL;
2702     test->stats_callback = NULL;
2703     test->reporter_callback = NULL;
2704     free(test);
2705 }
2706 
2707 
2708 void
2709 iperf_reset_test(struct iperf_test *test)
2710 {
2711     struct iperf_stream *sp;
2712     int i;
2713 
2714     /* Free streams */
2715     while (!SLIST_EMPTY(&test->streams)) {
2716         sp = SLIST_FIRST(&test->streams);
2717         SLIST_REMOVE_HEAD(&test->streams, streams);
2718         iperf_free_stream(sp);
2719     }
2720     if (test->omit_timer != NULL) {
2721 	tmr_cancel(test->omit_timer);
2722 	test->omit_timer = NULL;
2723     }
2724     if (test->timer != NULL) {
2725 	tmr_cancel(test->timer);
2726 	test->timer = NULL;
2727     }
2728     if (test->stats_timer != NULL) {
2729 	tmr_cancel(test->stats_timer);
2730 	test->stats_timer = NULL;
2731     }
2732     if (test->reporter_timer != NULL) {
2733 	tmr_cancel(test->reporter_timer);
2734 	test->reporter_timer = NULL;
2735     }
2736     test->done = 0;
2737 
2738     SLIST_INIT(&test->streams);
2739 
2740     if (test->remote_congestion_used)
2741         free(test->remote_congestion_used);
2742     test->remote_congestion_used = NULL;
2743     test->role = 's';
2744     test->mode = RECEIVER;
2745     test->sender_has_retransmits = 0;
2746     set_protocol(test, Ptcp);
2747     test->omit = OMIT;
2748     test->duration = DURATION;
2749     test->server_affinity = -1;
2750 #if defined(HAVE_CPUSET_SETAFFINITY)
2751     CPU_ZERO(&test->cpumask);
2752 #endif /* HAVE_CPUSET_SETAFFINITY */
2753     test->state = 0;
2754 
2755     test->ctrl_sck = -1;
2756     test->prot_listener = -1;
2757 
2758     test->bytes_sent = 0;
2759     test->blocks_sent = 0;
2760 
2761     test->bytes_received = 0;
2762     test->blocks_received = 0;
2763 
2764     test->other_side_has_retransmits = 0;
2765 
2766     test->bitrate_limit_stats_count = 0;
2767     test->bitrate_limit_last_interval_index = 0;
2768     test->bitrate_limit_exceeded = 0;
2769 
2770     for (i = 0; i < MAX_INTERVAL; i++)
2771         test->bitrate_limit_intervals_traffic_bytes[i] = 0;
2772 
2773     test->reverse = 0;
2774     test->bidirectional = 0;
2775     test->no_delay = 0;
2776 
2777     FD_ZERO(&test->read_set);
2778     FD_ZERO(&test->write_set);
2779 
2780     test->num_streams = 1;
2781     test->settings->socket_bufsize = 0;
2782     test->settings->blksize = DEFAULT_TCP_BLKSIZE;
2783     test->settings->rate = 0;
2784     test->settings->burst = 0;
2785     test->settings->mss = 0;
2786     test->settings->tos = 0;
2787 
2788 #if defined(HAVE_SSL)
2789     if (test->settings->authtoken) {
2790         free(test->settings->authtoken);
2791         test->settings->authtoken = NULL;
2792     }
2793     if (test->settings->client_username) {
2794         free(test->settings->client_username);
2795         test->settings->client_username = NULL;
2796     }
2797     if (test->settings->client_password) {
2798         free(test->settings->client_password);
2799         test->settings->client_password = NULL;
2800     }
2801     if (test->settings->client_rsa_pubkey) {
2802         EVP_PKEY_free(test->settings->client_rsa_pubkey);
2803         test->settings->client_rsa_pubkey = NULL;
2804     }
2805 #endif /* HAVE_SSL */
2806 
2807     memset(test->cookie, 0, COOKIE_SIZE);
2808     test->multisend = 10;	/* arbitrary */
2809     test->udp_counters_64bit = 0;
2810     if (test->title) {
2811 	free(test->title);
2812 	test->title = NULL;
2813     }
2814     if (test->extra_data) {
2815 	free(test->extra_data);
2816 	test->extra_data = NULL;
2817     }
2818 
2819     /* Free output line buffers, if any (on the server only) */
2820     struct iperf_textline *t;
2821     while (!TAILQ_EMPTY(&test->server_output_list)) {
2822 	t = TAILQ_FIRST(&test->server_output_list);
2823 	TAILQ_REMOVE(&test->server_output_list, t, textlineentries);
2824 	free(t->line);
2825 	free(t);
2826     }
2827 }
2828 
2829 
2830 /* Reset all of a test's stats back to zero.  Called when the omitting
2831 ** period is over.
2832 */
2833 void
2834 iperf_reset_stats(struct iperf_test *test)
2835 {
2836     struct iperf_time now;
2837     struct iperf_stream *sp;
2838     struct iperf_stream_result *rp;
2839 
2840     test->bytes_sent = 0;
2841     test->blocks_sent = 0;
2842     iperf_time_now(&now);
2843     SLIST_FOREACH(sp, &test->streams, streams) {
2844 	sp->omitted_packet_count = sp->packet_count;
2845         sp->omitted_cnt_error = sp->cnt_error;
2846         sp->omitted_outoforder_packets = sp->outoforder_packets;
2847 	sp->jitter = 0;
2848 	rp = sp->result;
2849         rp->bytes_sent_omit = rp->bytes_sent;
2850         rp->bytes_received = 0;
2851         rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
2852 	if (test->sender_has_retransmits == 1) {
2853 	    struct iperf_interval_results ir; /* temporary results structure */
2854 	    save_tcpinfo(sp, &ir);
2855 	    rp->stream_prev_total_retrans = get_total_retransmits(&ir);
2856 	}
2857 	rp->stream_retrans = 0;
2858 	rp->start_time = now;
2859     }
2860 }
2861 
2862 
2863 /**************************************************************************/
2864 
2865 /**
2866  * Gather statistics during a test.
2867  * This function works for both the client and server side.
2868  */
2869 void
2870 iperf_stats_callback(struct iperf_test *test)
2871 {
2872     struct iperf_stream *sp;
2873     struct iperf_stream_result *rp = NULL;
2874     struct iperf_interval_results *irp, temp;
2875     struct iperf_time temp_time;
2876     iperf_size_t total_interval_bytes_transferred = 0;
2877 
2878     temp.omitted = test->omitting;
2879     SLIST_FOREACH(sp, &test->streams, streams) {
2880         rp = sp->result;
2881 	temp.bytes_transferred = sp->sender ? rp->bytes_sent_this_interval : rp->bytes_received_this_interval;
2882 
2883         // Total bytes transferred this interval
2884 	total_interval_bytes_transferred += rp->bytes_sent_this_interval + rp->bytes_received_this_interval;
2885 
2886 	irp = TAILQ_LAST(&rp->interval_results, irlisthead);
2887         /* result->end_time contains timestamp of previous interval */
2888         if ( irp != NULL ) /* not the 1st interval */
2889             memcpy(&temp.interval_start_time, &rp->end_time, sizeof(struct iperf_time));
2890         else /* or use timestamp from beginning */
2891             memcpy(&temp.interval_start_time, &rp->start_time, sizeof(struct iperf_time));
2892         /* now save time of end of this interval */
2893         iperf_time_now(&rp->end_time);
2894         memcpy(&temp.interval_end_time, &rp->end_time, sizeof(struct iperf_time));
2895         iperf_time_diff(&temp.interval_start_time, &temp.interval_end_time, &temp_time);
2896         temp.interval_duration = iperf_time_in_secs(&temp_time);
2897 	if (test->protocol->id == Ptcp) {
2898 	    if ( has_tcpinfo()) {
2899 		save_tcpinfo(sp, &temp);
2900 		if (test->sender_has_retransmits == 1) {
2901 		    long total_retrans = get_total_retransmits(&temp);
2902 		    temp.interval_retrans = total_retrans - rp->stream_prev_total_retrans;
2903 		    rp->stream_retrans += temp.interval_retrans;
2904 		    rp->stream_prev_total_retrans = total_retrans;
2905 
2906 		    temp.snd_cwnd = get_snd_cwnd(&temp);
2907 		    if (temp.snd_cwnd > rp->stream_max_snd_cwnd) {
2908 			rp->stream_max_snd_cwnd = temp.snd_cwnd;
2909 		    }
2910 
2911 		    temp.rtt = get_rtt(&temp);
2912 		    if (temp.rtt > rp->stream_max_rtt) {
2913 			rp->stream_max_rtt = temp.rtt;
2914 		    }
2915 		    if (rp->stream_min_rtt == 0 ||
2916 			temp.rtt < rp->stream_min_rtt) {
2917 			rp->stream_min_rtt = temp.rtt;
2918 		    }
2919 		    rp->stream_sum_rtt += temp.rtt;
2920 		    rp->stream_count_rtt++;
2921 
2922 		    temp.rttvar = get_rttvar(&temp);
2923 		    temp.pmtu = get_pmtu(&temp);
2924 		}
2925 	    }
2926 	} else {
2927 	    if (irp == NULL) {
2928 		temp.interval_packet_count = sp->packet_count;
2929 		temp.interval_outoforder_packets = sp->outoforder_packets;
2930 		temp.interval_cnt_error = sp->cnt_error;
2931 	    } else {
2932 		temp.interval_packet_count = sp->packet_count - irp->packet_count;
2933 		temp.interval_outoforder_packets = sp->outoforder_packets - irp->outoforder_packets;
2934 		temp.interval_cnt_error = sp->cnt_error - irp->cnt_error;
2935 	    }
2936 	    temp.packet_count = sp->packet_count;
2937 	    temp.jitter = sp->jitter;
2938 	    temp.outoforder_packets = sp->outoforder_packets;
2939 	    temp.cnt_error = sp->cnt_error;
2940 	}
2941         add_to_interval_list(rp, &temp);
2942         rp->bytes_sent_this_interval = rp->bytes_received_this_interval = 0;
2943     }
2944 
2945     /* Verify that total server's throughput is not above specified limit */
2946     if (test->role == 's') {
2947 	iperf_check_total_rate(test, total_interval_bytes_transferred);
2948     }
2949 }
2950 
2951 /**
2952  * Print intermediate results during a test (interval report).
2953  * Uses print_interval_results to print the results for each stream,
2954  * then prints an interval summary for all streams in this
2955  * interval.
2956  */
2957 static void
2958 iperf_print_intermediate(struct iperf_test *test)
2959 {
2960     struct iperf_stream *sp = NULL;
2961     struct iperf_interval_results *irp;
2962     struct iperf_time temp_time;
2963     cJSON *json_interval;
2964     cJSON *json_interval_streams;
2965 
2966     int lower_mode, upper_mode;
2967     int current_mode;
2968 
2969     /*
2970      * Due to timing oddities, there can be cases, especially on the
2971      * server side, where at the end of a test there is a fairly short
2972      * interval with no data transferred.  This could caused by
2973      * the control and data flows sharing the same path in the network,
2974      * and having the control messages for stopping the test being
2975      * queued behind the data packets.
2976      *
2977      * We'd like to try to omit that last interval when it happens, to
2978      * avoid cluttering data and output with useless stuff.
2979      * So we're going to try to ignore very short intervals (less than
2980      * 10% of the interval time) that have no data.
2981      */
2982     int interval_ok = 0;
2983     SLIST_FOREACH(sp, &test->streams, streams) {
2984 	irp = TAILQ_LAST(&sp->result->interval_results, irlisthead);
2985 	if (irp) {
2986 	    iperf_time_diff(&irp->interval_start_time, &irp->interval_end_time, &temp_time);
2987 	    double interval_len = iperf_time_in_secs(&temp_time);
2988 	    if (test->debug) {
2989 		printf("interval_len %f bytes_transferred %" PRIu64 "\n", interval_len, irp->bytes_transferred);
2990 	    }
2991 
2992 	    /*
2993 	     * If the interval is at least 10% the normal interval
2994 	     * length, or if there were actual bytes transferrred,
2995 	     * then we want to keep this interval.
2996 	     */
2997 	    if (interval_len >= test->stats_interval * 0.10 ||
2998 		irp->bytes_transferred > 0) {
2999 		interval_ok = 1;
3000 		if (test->debug) {
3001 		    printf("interval forces keep\n");
3002 		}
3003 	    }
3004 	}
3005     }
3006     if (!interval_ok) {
3007 	if (test->debug) {
3008 	    printf("ignoring short interval with no data\n");
3009 	}
3010 	return;
3011     }
3012 
3013     if (test->json_output) {
3014         json_interval = cJSON_CreateObject();
3015 	if (json_interval == NULL)
3016 	    return;
3017 	cJSON_AddItemToArray(test->json_intervals, json_interval);
3018         json_interval_streams = cJSON_CreateArray();
3019 	if (json_interval_streams == NULL)
3020 	    return;
3021 	cJSON_AddItemToObject(json_interval, "streams", json_interval_streams);
3022     } else {
3023         json_interval = NULL;
3024         json_interval_streams = NULL;
3025     }
3026 
3027     /*
3028      * We must to sum streams separately.
3029      * For bidirectional mode we must to display
3030      * information about sender and receiver streams.
3031      * For client side we must handle sender streams
3032      * firstly and receiver streams for server side.
3033      * The following design allows us to do this.
3034      */
3035 
3036     if (test->mode == BIDIRECTIONAL) {
3037         if (test->role == 'c') {
3038             lower_mode = -1;
3039             upper_mode = 0;
3040         } else {
3041             lower_mode = 0;
3042             upper_mode = 1;
3043         }
3044     } else {
3045         lower_mode = test->mode;
3046         upper_mode = lower_mode;
3047     }
3048 
3049 
3050     for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) {
3051         char ubuf[UNIT_LEN];
3052         char nbuf[UNIT_LEN];
3053         char mbuf[UNIT_LEN];
3054         char zbuf[] = "          ";
3055 
3056         iperf_size_t bytes = 0;
3057         double bandwidth;
3058         int retransmits = 0;
3059         double start_time, end_time;
3060 
3061         int total_packets = 0, lost_packets = 0;
3062         double avg_jitter = 0.0, lost_percent;
3063         int stream_must_be_sender = current_mode * current_mode;
3064 
3065         /*  Print stream role just for bidirectional mode. */
3066 
3067         if (test->mode == BIDIRECTIONAL) {
3068             sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S");
3069         } else {
3070             mbuf[0] = '\0';
3071             zbuf[0] = '\0';
3072         }
3073 
3074         SLIST_FOREACH(sp, &test->streams, streams) {
3075             if (sp->sender == stream_must_be_sender) {
3076                 print_interval_results(test, sp, json_interval_streams);
3077                 /* sum up all streams */
3078                 irp = TAILQ_LAST(&sp->result->interval_results, irlisthead);
3079                 if (irp == NULL) {
3080                     iperf_err(test,
3081                             "iperf_print_intermediate error: interval_results is NULL");
3082                     return;
3083                 }
3084                 bytes += irp->bytes_transferred;
3085                 if (test->protocol->id == Ptcp) {
3086                     if (test->sender_has_retransmits == 1) {
3087                         retransmits += irp->interval_retrans;
3088                     }
3089                 } else {
3090                     total_packets += irp->interval_packet_count;
3091                     lost_packets += irp->interval_cnt_error;
3092                     avg_jitter += irp->jitter;
3093                 }
3094             }
3095         }
3096 
3097         /* next build string with sum of all streams */
3098         if (test->num_streams > 1 || test->json_output) {
3099             sp = SLIST_FIRST(&test->streams); /* reset back to 1st stream */
3100             /* Only do this of course if there was a first stream */
3101             if (sp) {
3102 	    irp = TAILQ_LAST(&sp->result->interval_results, irlisthead);    /* use 1st stream for timing info */
3103 
3104 	    unit_snprintf(ubuf, UNIT_LEN, (double) bytes, 'A');
3105 	    bandwidth = (double) bytes / (double) irp->interval_duration;
3106 	    unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3107 
3108 	    iperf_time_diff(&sp->result->start_time,&irp->interval_start_time, &temp_time);
3109 	    start_time = iperf_time_in_secs(&temp_time);
3110 	    iperf_time_diff(&sp->result->start_time,&irp->interval_end_time, &temp_time);
3111 	    end_time = iperf_time_in_secs(&temp_time);
3112                 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3113                     if (test->sender_has_retransmits == 1 && stream_must_be_sender) {
3114                         /* Interval sum, TCP with retransmits. */
3115                         if (test->json_output)
3116                             cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  retransmits: %d  omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) retransmits, irp->omitted, stream_must_be_sender)); /* XXX irp->omitted or test->omitting? */
3117                         else
3118                             iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, end_time, ubuf, nbuf, retransmits, irp->omitted?report_omitted:""); /* XXX irp->omitted or test->omitting? */
3119                     } else {
3120                         /* Interval sum, TCP without retransmits. */
3121                         if (test->json_output)
3122                             cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, test->omitting, stream_must_be_sender));
3123                         else
3124                             iperf_printf(test, report_sum_bw_format, mbuf, start_time, end_time, ubuf, nbuf, test->omitting?report_omitted:"");
3125                     }
3126                 } else {
3127                     /* Interval sum, UDP. */
3128                     if (stream_must_be_sender) {
3129                         if (test->json_output)
3130                             cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  packets: %d  omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (int64_t) total_packets, test->omitting, stream_must_be_sender));
3131                         else
3132                             iperf_printf(test, report_sum_bw_udp_sender_format, mbuf, start_time, end_time, ubuf, nbuf, zbuf, total_packets, test->omitting?report_omitted:"");
3133                     } else {
3134                         avg_jitter /= test->num_streams;
3135                         if (total_packets > 0) {
3136                             lost_percent = 100.0 * lost_packets / total_packets;
3137                         }
3138                         else {
3139                             lost_percent = 0.0;
3140                         }
3141                         if (test->json_output)
3142                             cJSON_AddItemToObject(json_interval, "sum", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  jitter_ms: %f  lost_packets: %d  packets: %d  lost_percent: %f  omitted: %b sender: %b", (double) start_time, (double) end_time, (double) irp->interval_duration, (int64_t) bytes, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, test->omitting, stream_must_be_sender));
3143                         else
3144                             iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, end_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, total_packets, lost_percent, test->omitting?report_omitted:"");
3145                     }
3146                 }
3147             }
3148         }
3149     }
3150 }
3151 
3152 /**
3153  * Print overall summary statistics at the end of a test.
3154  */
3155 static void
3156 iperf_print_results(struct iperf_test *test)
3157 {
3158 
3159     cJSON *json_summary_streams = NULL;
3160 
3161     int lower_mode, upper_mode;
3162     int current_mode;
3163 
3164     int tmp_sender_has_retransmits = test->sender_has_retransmits;
3165 
3166     /* print final summary for all intervals */
3167 
3168     if (test->json_output) {
3169         json_summary_streams = cJSON_CreateArray();
3170 	if (json_summary_streams == NULL)
3171 	    return;
3172 	cJSON_AddItemToObject(test->json_end, "streams", json_summary_streams);
3173     } else {
3174 	iperf_printf(test, "%s", report_bw_separator);
3175 	if (test->verbose)
3176 	    iperf_printf(test, "%s", report_summary);
3177 	if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3178 	    if (test->sender_has_retransmits || test->other_side_has_retransmits) {
3179 	        if (test->bidirectional)
3180 	            iperf_printf(test, "%s", report_bw_retrans_header_bidir);
3181 	        else
3182 	            iperf_printf(test, "%s", report_bw_retrans_header);
3183 	    }
3184 	    else {
3185 	        if (test->bidirectional)
3186 	            iperf_printf(test, "%s", report_bw_header_bidir);
3187 	        else
3188 	            iperf_printf(test, "%s", report_bw_header);
3189 	    }
3190 	} else {
3191 	    if (test->bidirectional)
3192 	        iperf_printf(test, "%s", report_bw_udp_header_bidir);
3193 	    else
3194 	        iperf_printf(test, "%s", report_bw_udp_header);
3195 	}
3196     }
3197 
3198     /*
3199      * We must to sum streams separately.
3200      * For bidirectional mode we must to display
3201      * information about sender and receiver streams.
3202      * For client side we must handle sender streams
3203      * firstly and receiver streams for server side.
3204      * The following design allows us to do this.
3205      */
3206 
3207     if (test->mode == BIDIRECTIONAL) {
3208         if (test->role == 'c') {
3209             lower_mode = -1;
3210             upper_mode = 0;
3211         } else {
3212             lower_mode = 0;
3213             upper_mode = 1;
3214         }
3215     } else {
3216         lower_mode = test->mode;
3217         upper_mode = lower_mode;
3218     }
3219 
3220 
3221     for (current_mode = lower_mode; current_mode <= upper_mode; ++current_mode) {
3222         cJSON *json_summary_stream = NULL;
3223         int total_retransmits = 0;
3224         int total_packets = 0, lost_packets = 0;
3225         int sender_packet_count = 0, receiver_packet_count = 0; /* for this stream, this interval */
3226         int sender_total_packets = 0, receiver_total_packets = 0; /* running total */
3227         char ubuf[UNIT_LEN];
3228         char nbuf[UNIT_LEN];
3229         struct stat sb;
3230         char sbuf[UNIT_LEN];
3231         struct iperf_stream *sp = NULL;
3232         iperf_size_t bytes_sent, total_sent = 0;
3233         iperf_size_t bytes_received, total_received = 0;
3234         double start_time, end_time = 0.0, avg_jitter = 0.0, lost_percent = 0.0;
3235         double sender_time = 0.0, receiver_time = 0.0;
3236     struct iperf_time temp_time;
3237         double bandwidth;
3238 
3239         char mbuf[UNIT_LEN];
3240         int stream_must_be_sender = current_mode * current_mode;
3241 
3242 
3243         /*  Print stream role just for bidirectional mode. */
3244 
3245         if (test->mode == BIDIRECTIONAL) {
3246             sprintf(mbuf, "[%s-%s]", stream_must_be_sender?"TX":"RX", test->role == 'c'?"C":"S");
3247         } else {
3248             mbuf[0] = '\0';
3249         }
3250 
3251         /* Get sender_has_retransmits for each sender side (client and server) */
3252         if (test->mode == BIDIRECTIONAL && stream_must_be_sender)
3253             test->sender_has_retransmits = tmp_sender_has_retransmits;
3254         else if (test->mode == BIDIRECTIONAL && !stream_must_be_sender)
3255             test->sender_has_retransmits = test->other_side_has_retransmits;
3256 
3257         start_time = 0.;
3258         sp = SLIST_FIRST(&test->streams);
3259 
3260         /*
3261          * If there is at least one stream, then figure out the length of time
3262          * we were running the tests and print out some statistics about
3263          * the streams.  It's possible to not have any streams at all
3264          * if the client got interrupted before it got to do anything.
3265          *
3266          * Also note that we try to keep seperate values for the sender
3267          * and receiver ending times.  Earlier iperf (3.1 and earlier)
3268          * servers didn't send that to the clients, so in this case we fall
3269          * back to using the client's ending timestamp.  The fallback is
3270          * basically emulating what iperf 3.1 did.
3271          */
3272 
3273         if (sp) {
3274     iperf_time_diff(&sp->result->start_time, &sp->result->end_time, &temp_time);
3275     end_time = iperf_time_in_secs(&temp_time);
3276         if (sp->sender) {
3277             sp->result->sender_time = end_time;
3278             if (sp->result->receiver_time == 0.0) {
3279                 sp->result->receiver_time = sp->result->sender_time;
3280             }
3281         }
3282         else {
3283             sp->result->receiver_time = end_time;
3284             if (sp->result->sender_time == 0.0) {
3285                 sp->result->sender_time = sp->result->receiver_time;
3286             }
3287         }
3288         sender_time = sp->result->sender_time;
3289         receiver_time = sp->result->receiver_time;
3290         SLIST_FOREACH(sp, &test->streams, streams) {
3291             if (sp->sender == stream_must_be_sender) {
3292                 if (test->json_output) {
3293                     json_summary_stream = cJSON_CreateObject();
3294                     if (json_summary_stream == NULL)
3295                         return;
3296                     cJSON_AddItemToArray(json_summary_streams, json_summary_stream);
3297                 }
3298 
3299                 bytes_sent = sp->result->bytes_sent - sp->result->bytes_sent_omit;
3300                 bytes_received = sp->result->bytes_received;
3301                 total_sent += bytes_sent;
3302                 total_received += bytes_received;
3303 
3304                 if (sp->sender) {
3305                     sender_packet_count = sp->packet_count;
3306                     receiver_packet_count = sp->peer_packet_count;
3307                 }
3308                 else {
3309                     sender_packet_count = sp->peer_packet_count;
3310                     receiver_packet_count = sp->packet_count;
3311                 }
3312 
3313                 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3314                     if (test->sender_has_retransmits) {
3315                         total_retransmits += sp->result->stream_retrans;
3316                     }
3317                 } else {
3318                     /*
3319                      * Running total of the total number of packets.  Use the sender packet count if we
3320                      * have it, otherwise use the receiver packet count.
3321                      */
3322                     int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count;
3323                     total_packets += (packet_count - sp->omitted_packet_count);
3324                     sender_total_packets += (sender_packet_count - sp->omitted_packet_count);
3325                     receiver_total_packets += (receiver_packet_count - sp->omitted_packet_count);
3326                     lost_packets += (sp->cnt_error - sp->omitted_cnt_error);
3327                     avg_jitter += sp->jitter;
3328                 }
3329 
3330                 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_sent, 'A');
3331                 if (sender_time > 0.0) {
3332                     bandwidth = (double) bytes_sent / (double) sender_time;
3333                 }
3334                 else {
3335                     bandwidth = 0.0;
3336                 }
3337                 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3338                 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3339                     if (test->sender_has_retransmits) {
3340                         /* Sender summary, TCP and SCTP with retransmits. */
3341                         if (test->json_output)
3342                             cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  retransmits: %d  max_snd_cwnd:  %d  max_rtt:  %d  min_rtt:  %d  mean_rtt:  %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (int64_t) sp->result->stream_retrans, (int64_t) sp->result->stream_max_snd_cwnd, (int64_t) sp->result->stream_max_rtt, (int64_t) sp->result->stream_min_rtt, (int64_t) ((sp->result->stream_count_rtt == 0) ? 0 : sp->result->stream_sum_rtt / sp->result->stream_count_rtt), stream_must_be_sender));
3343                         else
3344                             if (test->role == 's' && !sp->sender) {
3345                                 if (test->verbose)
3346                                     iperf_printf(test, report_sender_not_available_format, sp->socket);
3347                             }
3348                             else {
3349                                 iperf_printf(test, report_bw_retrans_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, sp->result->stream_retrans, report_sender);
3350                             }
3351                     } else {
3352                         /* Sender summary, TCP and SCTP without retransmits. */
3353                         if (test->json_output)
3354                             cJSON_AddItemToObject(json_summary_stream, "sender", iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8,  stream_must_be_sender));
3355                         else
3356                             if (test->role == 's' && !sp->sender) {
3357                                 if (test->verbose)
3358                                     iperf_printf(test, report_sender_not_available_format, sp->socket);
3359                             }
3360                             else {
3361                                 iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, report_sender);
3362                             }
3363                     }
3364                 } else {
3365                     /* Sender summary, UDP. */
3366                     if (sender_packet_count - sp->omitted_packet_count > 0) {
3367                         lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (sender_packet_count - sp->omitted_packet_count);
3368                     }
3369                     else {
3370                         lost_percent = 0.0;
3371                     }
3372                     if (test->json_output) {
3373                         /*
3374                          * For hysterical raisins, we only emit one JSON
3375                          * object for the UDP summary, and it contains
3376                          * information for both the sender and receiver
3377                          * side.
3378                          *
3379                          * The JSON format as currently defined only includes one
3380                          * value for the number of packets.  We usually want that
3381                          * to be the sender's value (how many packets were sent
3382                          * by the sender).  However this value might not be
3383                          * available on the receiver in certain circumstances
3384                          * specifically on the server side for a normal test or
3385                          * the client side for a reverse-mode test.  If this
3386                          * is the case, then use the receiver's count of packets
3387                          * instead.
3388                          */
3389                         int packet_count = sender_packet_count ? sender_packet_count : receiver_packet_count;
3390                         cJSON_AddItemToObject(json_summary_stream, "udp", iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  jitter_ms: %f  lost_packets: %d  packets: %d  lost_percent: %f  out_of_order: %d sender: %b", (int64_t) sp->socket, (double) start_time, (double) sender_time, (double) sender_time, (int64_t) bytes_sent, bandwidth * 8, (double) sp->jitter * 1000.0, (int64_t) (sp->cnt_error - sp->omitted_cnt_error), (int64_t) (packet_count - sp->omitted_packet_count), (double) lost_percent, (int64_t) (sp->outoforder_packets - sp->omitted_outoforder_packets), stream_must_be_sender));
3391                     }
3392                     else {
3393                         /*
3394                          * Due to ordering of messages on the control channel,
3395                          * the server cannot report on client-side summary
3396                          * statistics.  If we're the server, omit one set of
3397                          * summary statistics to avoid giving meaningless
3398                          * results.
3399                          */
3400                         if (test->role == 's' && !sp->sender) {
3401                             if (test->verbose)
3402                                 iperf_printf(test, report_sender_not_available_format, sp->socket);
3403                         }
3404                         else {
3405                             iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, (sender_packet_count - sp->omitted_packet_count), (double) 0, report_sender);
3406                         }
3407                         if ((sp->outoforder_packets - sp->omitted_outoforder_packets) > 0)
3408                           iperf_printf(test, report_sum_outoforder, mbuf, start_time, sender_time, (sp->outoforder_packets - sp->omitted_outoforder_packets));
3409                     }
3410                 }
3411 
3412                 if (sp->diskfile_fd >= 0) {
3413                     if (fstat(sp->diskfile_fd, &sb) == 0) {
3414                         /* In the odd case that it's a zero-sized file, say it was all transferred. */
3415                         int percent_sent = 100, percent_received = 100;
3416                         if (sb.st_size > 0) {
3417                             percent_sent = (int) ( ( (double) bytes_sent / (double) sb.st_size ) * 100.0 );
3418                             percent_received = (int) ( ( (double) bytes_received / (double) sb.st_size ) * 100.0 );
3419                         }
3420                         unit_snprintf(sbuf, UNIT_LEN, (double) sb.st_size, 'A');
3421                         if (test->json_output)
3422                             cJSON_AddItemToObject(json_summary_stream, "diskfile", iperf_json_printf("sent: %d  received: %d  size: %d  percent_sent: %d  percent_received: %d  filename: %s", (int64_t) bytes_sent, (int64_t) bytes_received, (int64_t) sb.st_size, (int64_t) percent_sent, (int64_t) percent_received, test->diskfile_name));
3423                         else
3424                             if (stream_must_be_sender) {
3425                                 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_sent, test->diskfile_name);
3426                             }
3427                             else {
3428                                 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A');
3429                                 iperf_printf(test, report_diskfile, ubuf, sbuf, percent_received, test->diskfile_name);
3430                             }
3431                     }
3432                 }
3433 
3434                 unit_snprintf(ubuf, UNIT_LEN, (double) bytes_received, 'A');
3435                 if (receiver_time > 0) {
3436                     bandwidth = (double) bytes_received / (double) receiver_time;
3437                 }
3438                 else {
3439                     bandwidth = 0.0;
3440                 }
3441                 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3442                 if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3443                     /* Receiver summary, TCP and SCTP */
3444                     if (test->json_output)
3445                         cJSON_AddItemToObject(json_summary_stream, "receiver", iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f sender: %b", (int64_t) sp->socket, (double) start_time, (double) receiver_time, (double) end_time, (int64_t) bytes_received, bandwidth * 8, stream_must_be_sender));
3446                     else
3447                         if (test->role == 's' && sp->sender) {
3448                             if (test->verbose)
3449                                 iperf_printf(test, report_receiver_not_available_format, sp->socket);
3450                         }
3451                         else {
3452                             iperf_printf(test, report_bw_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver);
3453                         }
3454                 }
3455                 else {
3456                     /*
3457                      * Receiver summary, UDP.  Note that JSON was emitted with
3458                      * the sender summary, so we only deal with human-readable
3459                      * data here.
3460                      */
3461                     if (! test->json_output) {
3462                         if (receiver_packet_count - sp->omitted_packet_count > 0) {
3463                             lost_percent = 100.0 * (sp->cnt_error - sp->omitted_cnt_error) / (receiver_packet_count - sp->omitted_packet_count);
3464                         }
3465                         else {
3466                             lost_percent = 0.0;
3467                         }
3468 
3469                         if (test->role == 's' && sp->sender) {
3470                             if (test->verbose)
3471                                 iperf_printf(test, report_receiver_not_available_format, sp->socket);
3472                         }
3473                         else {
3474                             iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, start_time, receiver_time, ubuf, nbuf, sp->jitter * 1000.0, (sp->cnt_error - sp->omitted_cnt_error), (receiver_packet_count - sp->omitted_packet_count), lost_percent, report_receiver);
3475                         }
3476                     }
3477                 }
3478             }
3479         }
3480         }
3481 
3482         if (test->num_streams > 1 || test->json_output) {
3483             unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A');
3484             /* If no tests were run, arbitrarily set bandwidth to 0. */
3485             if (sender_time > 0.0) {
3486                 bandwidth = (double) total_sent / (double) sender_time;
3487             }
3488             else {
3489                 bandwidth = 0.0;
3490             }
3491             unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3492             if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3493                 if (test->sender_has_retransmits) {
3494                     /* Summary sum, TCP with retransmits. */
3495                     if (test->json_output)
3496                         cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  retransmits: %d sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, (int64_t) total_retransmits, stream_must_be_sender));
3497                     else
3498                         if (test->role == 's' && !stream_must_be_sender) {
3499                             if (test->verbose)
3500                                 iperf_printf(test, report_sender_not_available_summary_format, "SUM");
3501                         }
3502                         else {
3503                           iperf_printf(test, report_sum_bw_retrans_format, mbuf, start_time, sender_time, ubuf, nbuf, total_retransmits, report_sender);
3504                         }
3505                 } else {
3506                     /* Summary sum, TCP without retransmits. */
3507                     if (test->json_output)
3508                         cJSON_AddItemToObject(test->json_end, "sum_sent", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f sender: %b", (double) start_time, (double) sender_time, (double) sender_time, (int64_t) total_sent, bandwidth * 8, stream_must_be_sender));
3509                     else
3510                         if (test->role == 's' && !stream_must_be_sender) {
3511                             if (test->verbose)
3512                                 iperf_printf(test, report_sender_not_available_summary_format, "SUM");
3513                         }
3514                         else {
3515                             iperf_printf(test, report_sum_bw_format, mbuf, start_time, sender_time, ubuf, nbuf, report_sender);
3516                         }
3517                 }
3518                 unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A');
3519                 /* If no tests were run, set received bandwidth to 0 */
3520                 if (receiver_time > 0.0) {
3521                     bandwidth = (double) total_received / (double) receiver_time;
3522                 }
3523                 else {
3524                     bandwidth = 0.0;
3525                 }
3526                 unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3527                 if (test->json_output)
3528                     cJSON_AddItemToObject(test->json_end, "sum_received", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_received, bandwidth * 8, stream_must_be_sender));
3529                 else
3530                     if (test->role == 's' && stream_must_be_sender) {
3531                         if (test->verbose)
3532                             iperf_printf(test, report_receiver_not_available_summary_format, "SUM");
3533                     }
3534                     else {
3535                         iperf_printf(test, report_sum_bw_format, mbuf, start_time, receiver_time, ubuf, nbuf, report_receiver);
3536                     }
3537             } else {
3538                 /* Summary sum, UDP. */
3539                 avg_jitter /= test->num_streams;
3540                 /* If no packets were sent, arbitrarily set loss percentage to 0. */
3541                 if (total_packets > 0) {
3542                     lost_percent = 100.0 * lost_packets / total_packets;
3543                 }
3544                 else {
3545                     lost_percent = 0.0;
3546                 }
3547                 if (test->json_output)
3548                     cJSON_AddItemToObject(test->json_end, "sum", iperf_json_printf("start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  jitter_ms: %f  lost_packets: %d  packets: %d  lost_percent: %f sender: %b", (double) start_time, (double) receiver_time, (double) receiver_time, (int64_t) total_sent, bandwidth * 8, (double) avg_jitter * 1000.0, (int64_t) lost_packets, (int64_t) total_packets, (double) lost_percent, stream_must_be_sender));
3549                 else {
3550                     /*
3551                      * On the client we have both sender and receiver overall summary
3552                      * stats.  On the server we have only the side that was on the
3553                      * server.  Output whatever we have.
3554                      */
3555                     if (! (test->role == 's' && !stream_must_be_sender) ) {
3556                         unit_snprintf(ubuf, UNIT_LEN, (double) total_sent, 'A');
3557                         iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, sender_time, ubuf, nbuf, 0.0, 0, sender_total_packets, 0.0, "sender");
3558                     }
3559                     if (! (test->role == 's' && stream_must_be_sender) ) {
3560 
3561                         unit_snprintf(ubuf, UNIT_LEN, (double) total_received, 'A');
3562                         /* Compute received bandwidth. */
3563                         if (end_time > 0.0) {
3564                             bandwidth = (double) total_received / (double) receiver_time;
3565                         }
3566                         else {
3567                             bandwidth = 0.0;
3568                         }
3569                         unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3570                         iperf_printf(test, report_sum_bw_udp_format, mbuf, start_time, receiver_time, ubuf, nbuf, avg_jitter * 1000.0, lost_packets, receiver_total_packets, lost_percent, "receiver");
3571                     }
3572                 }
3573             }
3574         }
3575 
3576         if (test->json_output && current_mode == upper_mode) {
3577             cJSON_AddItemToObject(test->json_end, "cpu_utilization_percent", iperf_json_printf("host_total: %f  host_user: %f  host_system: %f  remote_total: %f  remote_user: %f  remote_system: %f", (double) test->cpu_util[0], (double) test->cpu_util[1], (double) test->cpu_util[2], (double) test->remote_cpu_util[0], (double) test->remote_cpu_util[1], (double) test->remote_cpu_util[2]));
3578             if (test->protocol->id == Ptcp) {
3579                 char *snd_congestion = NULL, *rcv_congestion = NULL;
3580                 if (stream_must_be_sender) {
3581                     snd_congestion = test->congestion_used;
3582                     rcv_congestion = test->remote_congestion_used;
3583                 }
3584                 else {
3585                     snd_congestion = test->remote_congestion_used;
3586                     rcv_congestion = test->congestion_used;
3587                 }
3588                 if (snd_congestion) {
3589                     cJSON_AddStringToObject(test->json_end, "sender_tcp_congestion", snd_congestion);
3590                 }
3591                 if (rcv_congestion) {
3592                     cJSON_AddStringToObject(test->json_end, "receiver_tcp_congestion", rcv_congestion);
3593                 }
3594             }
3595         }
3596         else {
3597             if (test->verbose) {
3598                 if (stream_must_be_sender) {
3599                     if (test->bidirectional) {
3600                         iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3601                         iperf_printf(test, report_cpu, report_local, !stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, !stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3602                     } else
3603                         iperf_printf(test, report_cpu, report_local, stream_must_be_sender?report_sender:report_receiver, test->cpu_util[0], test->cpu_util[1], test->cpu_util[2], report_remote, stream_must_be_sender?report_receiver:report_sender, test->remote_cpu_util[0], test->remote_cpu_util[1], test->remote_cpu_util[2]);
3604                 }
3605                 if (test->protocol->id == Ptcp) {
3606                     char *snd_congestion = NULL, *rcv_congestion = NULL;
3607                     if (stream_must_be_sender) {
3608                         snd_congestion = test->congestion_used;
3609                         rcv_congestion = test->remote_congestion_used;
3610                     }
3611                     else {
3612                         snd_congestion = test->remote_congestion_used;
3613                         rcv_congestion = test->congestion_used;
3614                     }
3615                     if (snd_congestion) {
3616                         iperf_printf(test, "snd_tcp_congestion %s\n", snd_congestion);
3617                     }
3618                     if (rcv_congestion) {
3619                         iperf_printf(test, "rcv_tcp_congestion %s\n", rcv_congestion);
3620                     }
3621                 }
3622             }
3623 
3624             /* Print server output if we're on the client and it was requested/provided */
3625             if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) {
3626                 if (test->json_server_output) {
3627 		    char *str = cJSON_Print(test->json_server_output);
3628                     iperf_printf(test, "\nServer JSON output:\n%s\n", str);
3629 		    cJSON_free(str);
3630                     cJSON_Delete(test->json_server_output);
3631                     test->json_server_output = NULL;
3632                 }
3633                 if (test->server_output_text) {
3634                     iperf_printf(test, "\nServer output:\n%s\n", test->server_output_text);
3635                     test->server_output_text = NULL;
3636                 }
3637             }
3638         }
3639     }
3640 
3641     /* Set real sender_has_retransmits for current side */
3642     if (test->mode == BIDIRECTIONAL)
3643         test->sender_has_retransmits = tmp_sender_has_retransmits;
3644 }
3645 
3646 /**************************************************************************/
3647 
3648 /**
3649  * Main report-printing callback.
3650  * Prints results either during a test (interval report only) or
3651  * after the entire test has been run (last interval report plus
3652  * overall summary).
3653  */
3654 void
3655 iperf_reporter_callback(struct iperf_test *test)
3656 {
3657     switch (test->state) {
3658         case TEST_RUNNING:
3659         case STREAM_RUNNING:
3660             /* print interval results for each stream */
3661             iperf_print_intermediate(test);
3662             break;
3663         case TEST_END:
3664         case DISPLAY_RESULTS:
3665             iperf_print_intermediate(test);
3666             iperf_print_results(test);
3667             break;
3668     }
3669 
3670 }
3671 
3672 /**
3673  * Print the interval results for one stream.
3674  * This function needs to know about the overall test so it can determine the
3675  * context for printing headers, separators, etc.
3676  */
3677 static void
3678 print_interval_results(struct iperf_test *test, struct iperf_stream *sp, cJSON *json_interval_streams)
3679 {
3680     char ubuf[UNIT_LEN];
3681     char nbuf[UNIT_LEN];
3682     char cbuf[UNIT_LEN];
3683     char mbuf[UNIT_LEN];
3684     char zbuf[] = "          ";
3685     double st = 0., et = 0.;
3686     struct iperf_time temp_time;
3687     struct iperf_interval_results *irp = NULL;
3688     double bandwidth, lost_percent;
3689 
3690     if (test->mode == BIDIRECTIONAL) {
3691         sprintf(mbuf, "[%s-%s]", sp->sender?"TX":"RX", test->role == 'c'?"C":"S");
3692     } else {
3693         mbuf[0] = '\0';
3694         zbuf[0] = '\0';
3695     }
3696 
3697     irp = TAILQ_LAST(&sp->result->interval_results, irlisthead); /* get last entry in linked list */
3698     if (irp == NULL) {
3699 	iperf_err(test, "print_interval_results error: interval_results is NULL");
3700         return;
3701     }
3702     if (!test->json_output) {
3703 	/* First stream? */
3704 	if (sp == SLIST_FIRST(&test->streams)) {
3705 	    /* It it's the first interval, print the header;
3706 	    ** else if there's more than one stream, print the separator;
3707 	    ** else nothing.
3708 	    */
3709 	    if (iperf_time_compare(&sp->result->start_time, &irp->interval_start_time) == 0) {
3710 		if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3711 		    if (test->sender_has_retransmits == 1) {
3712 		        if (test->bidirectional)
3713 		            iperf_printf(test, "%s", report_bw_retrans_cwnd_header_bidir);
3714 		        else
3715 		            iperf_printf(test, "%s", report_bw_retrans_cwnd_header);
3716 		    }
3717 		    else {
3718 	                if (test->bidirectional)
3719 	                    iperf_printf(test, "%s", report_bw_header_bidir);
3720 	                else
3721 	                    iperf_printf(test, "%s", report_bw_header);
3722 	            }
3723 		} else {
3724 		    if (test->mode == SENDER) {
3725 		        iperf_printf(test, "%s", report_bw_udp_sender_header);
3726 		    } else if (test->mode == RECEIVER){
3727 		        iperf_printf(test, "%s", report_bw_udp_header);
3728 		    } else {
3729 		        /* BIDIRECTIONAL */
3730 		        iperf_printf(test, "%s", report_bw_udp_header_bidir);
3731 		    }
3732 		}
3733 	    } else if (test->num_streams > 1)
3734 		iperf_printf(test, "%s", report_bw_separator);
3735 	}
3736     }
3737 
3738     unit_snprintf(ubuf, UNIT_LEN, (double) (irp->bytes_transferred), 'A');
3739     if (irp->interval_duration > 0.0) {
3740 	bandwidth = (double) irp->bytes_transferred / (double) irp->interval_duration;
3741     }
3742     else {
3743 	bandwidth = 0.0;
3744     }
3745     unit_snprintf(nbuf, UNIT_LEN, bandwidth, test->settings->unit_format);
3746 
3747     iperf_time_diff(&sp->result->start_time, &irp->interval_start_time, &temp_time);
3748     st = iperf_time_in_secs(&temp_time);
3749     iperf_time_diff(&sp->result->start_time, &irp->interval_end_time, &temp_time);
3750     et = iperf_time_in_secs(&temp_time);
3751 
3752     if (test->protocol->id == Ptcp || test->protocol->id == Psctp) {
3753 	if (test->sender_has_retransmits == 1 && sp->sender) {
3754 	    /* Interval, TCP with retransmits. */
3755 	    if (test->json_output)
3756 		cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  retransmits: %d  snd_cwnd:  %d  rtt:  %d  rttvar: %d  pmtu: %d  omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_retrans, (int64_t) irp->snd_cwnd, (int64_t) irp->rtt, (int64_t) irp->rttvar, (int64_t) irp->pmtu, irp->omitted, sp->sender));
3757 	    else {
3758 		unit_snprintf(cbuf, UNIT_LEN, irp->snd_cwnd, 'A');
3759 		iperf_printf(test, report_bw_retrans_cwnd_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->interval_retrans, cbuf, irp->omitted?report_omitted:"");
3760 	    }
3761 	} else {
3762 	    /* Interval, TCP without retransmits. */
3763 	    if (test->json_output)
3764 		cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, irp->omitted, sp->sender));
3765 	    else
3766 		iperf_printf(test, report_bw_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->omitted?report_omitted:"");
3767 	}
3768     } else {
3769 	/* Interval, UDP. */
3770 	if (sp->sender) {
3771 	    if (test->json_output)
3772 		cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  packets: %d  omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (int64_t) irp->interval_packet_count, irp->omitted, sp->sender));
3773 	    else
3774 		iperf_printf(test, report_bw_udp_sender_format, sp->socket, mbuf, st, et, ubuf, nbuf, zbuf, irp->interval_packet_count, irp->omitted?report_omitted:"");
3775 	} else {
3776 	    if (irp->interval_packet_count > 0) {
3777 		lost_percent = 100.0 * irp->interval_cnt_error / irp->interval_packet_count;
3778 	    }
3779 	    else {
3780 		lost_percent = 0.0;
3781 	    }
3782 	    if (test->json_output)
3783 		cJSON_AddItemToArray(json_interval_streams, iperf_json_printf("socket: %d  start: %f  end: %f  seconds: %f  bytes: %d  bits_per_second: %f  jitter_ms: %f  lost_packets: %d  packets: %d  lost_percent: %f  omitted: %b sender: %b", (int64_t) sp->socket, (double) st, (double) et, (double) irp->interval_duration, (int64_t) irp->bytes_transferred, bandwidth * 8, (double) irp->jitter * 1000.0, (int64_t) irp->interval_cnt_error, (int64_t) irp->interval_packet_count, (double) lost_percent, irp->omitted, sp->sender));
3784 	    else
3785 		iperf_printf(test, report_bw_udp_format, sp->socket, mbuf, st, et, ubuf, nbuf, irp->jitter * 1000.0, irp->interval_cnt_error, irp->interval_packet_count, lost_percent, irp->omitted?report_omitted:"");
3786 	}
3787     }
3788 
3789     if (test->logfile || test->forceflush)
3790         iflush(test);
3791 }
3792 
3793 /**************************************************************************/
3794 void
3795 iperf_free_stream(struct iperf_stream *sp)
3796 {
3797     struct iperf_interval_results *irp, *nirp;
3798 
3799     /* XXX: need to free interval list too! */
3800     munmap(sp->buffer, sp->test->settings->blksize);
3801     close(sp->buffer_fd);
3802     if (sp->diskfile_fd >= 0)
3803 	close(sp->diskfile_fd);
3804     for (irp = TAILQ_FIRST(&sp->result->interval_results); irp != NULL; irp = nirp) {
3805         nirp = TAILQ_NEXT(irp, irlistentries);
3806         free(irp);
3807     }
3808     free(sp->result);
3809     if (sp->send_timer != NULL)
3810 	tmr_cancel(sp->send_timer);
3811     free(sp);
3812 }
3813 
3814 /**************************************************************************/
3815 struct iperf_stream *
3816 iperf_new_stream(struct iperf_test *test, int s, int sender)
3817 {
3818     struct iperf_stream *sp;
3819     int ret = 0;
3820 
3821     char template[1024];
3822     if (test->tmp_template) {
3823         snprintf(template, sizeof(template) / sizeof(char), "%s", test->tmp_template);
3824     } else {
3825         //find the system temporary dir *unix, windows, cygwin support
3826         char* tempdir = getenv("TMPDIR");
3827         if (tempdir == 0){
3828             tempdir = getenv("TEMP");
3829         }
3830         if (tempdir == 0){
3831             tempdir = getenv("TMP");
3832         }
3833         if (tempdir == 0){
3834             tempdir = "/tmp";
3835         }
3836         snprintf(template, sizeof(template) / sizeof(char), "%s/iperf3.XXXXXX", tempdir);
3837     }
3838 
3839     sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream));
3840     if (!sp) {
3841         i_errno = IECREATESTREAM;
3842         return NULL;
3843     }
3844 
3845     memset(sp, 0, sizeof(struct iperf_stream));
3846 
3847     sp->sender = sender;
3848     sp->test = test;
3849     sp->settings = test->settings;
3850     sp->result = (struct iperf_stream_result *) malloc(sizeof(struct iperf_stream_result));
3851     if (!sp->result) {
3852         free(sp);
3853         i_errno = IECREATESTREAM;
3854         return NULL;
3855     }
3856 
3857     memset(sp->result, 0, sizeof(struct iperf_stream_result));
3858     TAILQ_INIT(&sp->result->interval_results);
3859 
3860     /* Create and randomize the buffer */
3861     sp->buffer_fd = mkstemp(template);
3862     if (sp->buffer_fd == -1) {
3863         i_errno = IECREATESTREAM;
3864         free(sp->result);
3865         free(sp);
3866         return NULL;
3867     }
3868     if (unlink(template) < 0) {
3869         i_errno = IECREATESTREAM;
3870         free(sp->result);
3871         free(sp);
3872         return NULL;
3873     }
3874     if (ftruncate(sp->buffer_fd, test->settings->blksize) < 0) {
3875         i_errno = IECREATESTREAM;
3876         free(sp->result);
3877         free(sp);
3878         return NULL;
3879     }
3880     sp->buffer = (char *) mmap(NULL, test->settings->blksize, PROT_READ|PROT_WRITE, MAP_PRIVATE, sp->buffer_fd, 0);
3881     if (sp->buffer == MAP_FAILED) {
3882         i_errno = IECREATESTREAM;
3883         free(sp->result);
3884         free(sp);
3885         return NULL;
3886     }
3887 
3888     /* Set socket */
3889     sp->socket = s;
3890 
3891     sp->snd = test->protocol->send;
3892     sp->rcv = test->protocol->recv;
3893 
3894     if (test->diskfile_name != (char*) 0) {
3895 	sp->diskfile_fd = open(test->diskfile_name, sender ? O_RDONLY : (O_WRONLY|O_CREAT|O_TRUNC), S_IRUSR|S_IWUSR);
3896 	if (sp->diskfile_fd == -1) {
3897 	    i_errno = IEFILE;
3898             munmap(sp->buffer, sp->test->settings->blksize);
3899             free(sp->result);
3900             free(sp);
3901 	    return NULL;
3902 	}
3903         sp->snd2 = sp->snd;
3904 	sp->snd = diskfile_send;
3905 	sp->rcv2 = sp->rcv;
3906 	sp->rcv = diskfile_recv;
3907     } else
3908         sp->diskfile_fd = -1;
3909 
3910     /* Initialize stream */
3911     if (test->repeating_payload)
3912         fill_with_repeating_pattern(sp->buffer, test->settings->blksize);
3913     else
3914         ret = readentropy(sp->buffer, test->settings->blksize);
3915 
3916     if ((ret < 0) || (iperf_init_stream(sp, test) < 0)) {
3917         close(sp->buffer_fd);
3918         munmap(sp->buffer, sp->test->settings->blksize);
3919         free(sp->result);
3920         free(sp);
3921         return NULL;
3922     }
3923     iperf_add_stream(test, sp);
3924 
3925     return sp;
3926 }
3927 
3928 /**************************************************************************/
3929 int
3930 iperf_init_stream(struct iperf_stream *sp, struct iperf_test *test)
3931 {
3932     socklen_t len;
3933     int opt;
3934 
3935     len = sizeof(struct sockaddr_storage);
3936     if (getsockname(sp->socket, (struct sockaddr *) &sp->local_addr, &len) < 0) {
3937         i_errno = IEINITSTREAM;
3938         return -1;
3939     }
3940     len = sizeof(struct sockaddr_storage);
3941     if (getpeername(sp->socket, (struct sockaddr *) &sp->remote_addr, &len) < 0) {
3942         i_errno = IEINITSTREAM;
3943         return -1;
3944     }
3945 
3946     /* Set IP TOS */
3947     if ((opt = test->settings->tos)) {
3948         if (getsockdomain(sp->socket) == AF_INET6) {
3949 #ifdef IPV6_TCLASS
3950             if (setsockopt(sp->socket, IPPROTO_IPV6, IPV6_TCLASS, &opt, sizeof(opt)) < 0) {
3951                 i_errno = IESETCOS;
3952                 return -1;
3953             }
3954 #else
3955             i_errno = IESETCOS;
3956             return -1;
3957 #endif
3958         } else {
3959             if (setsockopt(sp->socket, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) < 0) {
3960                 i_errno = IESETTOS;
3961                 return -1;
3962             }
3963         }
3964     }
3965 
3966     return 0;
3967 }
3968 
3969 /**************************************************************************/
3970 void
3971 iperf_add_stream(struct iperf_test *test, struct iperf_stream *sp)
3972 {
3973     int i;
3974     struct iperf_stream *n, *prev;
3975 
3976     if (SLIST_EMPTY(&test->streams)) {
3977         SLIST_INSERT_HEAD(&test->streams, sp, streams);
3978         sp->id = 1;
3979     } else {
3980         // for (n = test->streams, i = 2; n->next; n = n->next, ++i);
3981         i = 2;
3982         SLIST_FOREACH(n, &test->streams, streams) {
3983             prev = n;
3984             ++i;
3985         }
3986         SLIST_INSERT_AFTER(prev, sp, streams);
3987         sp->id = i;
3988     }
3989 }
3990 
3991 /* This pair of routines gets inserted into the snd/rcv function pointers
3992 ** when there's a -F flag. They handle the file stuff and call the real
3993 ** snd/rcv functions, which have been saved in snd2/rcv2.
3994 **
3995 ** The advantage of doing it this way is that in the much more common
3996 ** case of no -F flag, there is zero extra overhead.
3997 */
3998 
3999 static int
4000 diskfile_send(struct iperf_stream *sp)
4001 {
4002     int r;
4003     static int rtot;
4004 
4005     /* if needed, read enough data from the disk to fill up the buffer */
4006     if (sp->diskfile_left < sp->test->settings->blksize && !sp->test->done) {
4007 	r = read(sp->diskfile_fd, sp->buffer, sp->test->settings->blksize -
4008 		 sp->diskfile_left);
4009 	rtot += r;
4010 	if (sp->test->debug) {
4011 	    printf("read %d bytes from file, %d total\n", r, rtot);
4012 	    if (r != sp->test->settings->blksize - sp->diskfile_left)
4013 		printf("possible eof\n");
4014 	}
4015 	/* If there's no data left in the file or in the buffer, we're done */
4016 	if (r == 0 && sp->diskfile_left == 0) {
4017 	    sp->test->done = 1;
4018 	    if (sp->test->debug)
4019 		printf("done\n");
4020 	}
4021     }
4022 
4023     r = sp->snd2(sp);
4024     if (r < 0) {
4025 	return r;
4026     }
4027     /*
4028      * Compute how much data is in the buffer but didn't get sent.
4029      * If there are bytes that got left behind, slide them to the
4030      * front of the buffer so they can hopefully go out on the next
4031      * pass.
4032      */
4033     sp->diskfile_left = sp->test->settings->blksize - r;
4034     if (sp->diskfile_left && sp->diskfile_left < sp->test->settings->blksize) {
4035 	memcpy(sp->buffer,
4036 	       sp->buffer + (sp->test->settings->blksize - sp->diskfile_left),
4037 	       sp->diskfile_left);
4038 	if (sp->test->debug)
4039 	    printf("Shifting %d bytes by %d\n", sp->diskfile_left, (sp->test->settings->blksize - sp->diskfile_left));
4040     }
4041     return r;
4042 }
4043 
4044 static int
4045 diskfile_recv(struct iperf_stream *sp)
4046 {
4047     int r;
4048 
4049     r = sp->rcv2(sp);
4050     if (r > 0) {
4051 	(void) write(sp->diskfile_fd, sp->buffer, r);
4052 	(void) fsync(sp->diskfile_fd);
4053     }
4054     return r;
4055 }
4056 
4057 
4058 void
4059 iperf_catch_sigend(void (*handler)(int))
4060 {
4061 #ifdef SIGINT
4062     signal(SIGINT, handler);
4063 #endif
4064 #ifdef SIGTERM
4065     signal(SIGTERM, handler);
4066 #endif
4067 #ifdef SIGHUP
4068     signal(SIGHUP, handler);
4069 #endif
4070 }
4071 
4072 /**
4073  * Called as a result of getting a signal.
4074  * Depending on the current state of the test (and the role of this
4075  * process) compute and report one more set of ending statistics
4076  * before cleaning up and exiting.
4077  */
4078 void
4079 iperf_got_sigend(struct iperf_test *test)
4080 {
4081     /*
4082      * If we're the client, or if we're a server and running a test,
4083      * then dump out the accumulated stats so far.
4084      */
4085     if (test->role == 'c' ||
4086       (test->role == 's' && test->state == TEST_RUNNING)) {
4087 
4088 	test->done = 1;
4089 	cpu_util(test->cpu_util);
4090 	test->stats_callback(test);
4091 	test->state = DISPLAY_RESULTS; /* change local state only */
4092 	if (test->on_test_finish)
4093 	    test->on_test_finish(test);
4094 	test->reporter_callback(test);
4095     }
4096 
4097     if (test->ctrl_sck >= 0) {
4098 	test->state = (test->role == 'c') ? CLIENT_TERMINATE : SERVER_TERMINATE;
4099 	(void) Nwrite(test->ctrl_sck, (char*) &test->state, sizeof(signed char), Ptcp);
4100     }
4101     i_errno = (test->role == 'c') ? IECLIENTTERM : IESERVERTERM;
4102     iperf_errexit(test, "interrupt - %s", iperf_strerror(i_errno));
4103 }
4104 
4105 /* Try to write a PID file if requested, return -1 on an error. */
4106 int
4107 iperf_create_pidfile(struct iperf_test *test)
4108 {
4109     if (test->pidfile) {
4110 	int fd;
4111 	char buf[8];
4112 
4113 	/* See if the file already exists and we can read it. */
4114 	fd = open(test->pidfile, O_RDONLY, 0);
4115 	if (fd >= 0) {
4116 	    if (read(fd, buf, sizeof(buf) - 1) >= 0) {
4117 
4118 		/* We read some bytes, see if they correspond to a valid PID */
4119 		pid_t pid;
4120 		pid = atoi(buf);
4121 		if (pid > 0) {
4122 
4123 		    /* See if the process exists. */
4124 		    if (kill(pid, 0) == 0) {
4125 			/*
4126 			 * Make sure not to try to delete existing PID file by
4127 			 * scribbling over the pathname we'd use to refer to it.
4128 			 * Then exit with an error.
4129 			 */
4130 			free(test->pidfile);
4131 			test->pidfile = NULL;
4132 			iperf_errexit(test, "Another instance of iperf3 appears to be running");
4133 		    }
4134 		}
4135 	    }
4136 	}
4137 
4138 	/*
4139 	 * File didn't exist, we couldn't read it, or it didn't correspond to
4140 	 * a running process.  Try to create it.
4141 	 */
4142 	fd = open(test->pidfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR);
4143 	if (fd < 0) {
4144 	    return -1;
4145 	}
4146 	snprintf(buf, sizeof(buf), "%d", getpid()); /* no trailing newline */
4147 	if (write(fd, buf, strlen(buf) + 1) < 0) {
4148 	    return -1;
4149 	}
4150 	if (close(fd) < 0) {
4151 	    return -1;
4152 	};
4153     }
4154     return 0;
4155 }
4156 
4157 /* Get rid of a PID file, return -1 on error. */
4158 int
4159 iperf_delete_pidfile(struct iperf_test *test)
4160 {
4161     if (test->pidfile) {
4162 	if (unlink(test->pidfile) < 0) {
4163 	    return -1;
4164 	}
4165     }
4166     return 0;
4167 }
4168 
4169 int
4170 iperf_json_start(struct iperf_test *test)
4171 {
4172     test->json_top = cJSON_CreateObject();
4173     if (test->json_top == NULL)
4174         return -1;
4175     test->json_start = cJSON_CreateObject();
4176     if (test->json_start == NULL)
4177         return -1;
4178     cJSON_AddItemToObject(test->json_top, "start", test->json_start);
4179     test->json_connected = cJSON_CreateArray();
4180     if (test->json_connected == NULL)
4181         return -1;
4182     cJSON_AddItemToObject(test->json_start, "connected", test->json_connected);
4183     test->json_intervals = cJSON_CreateArray();
4184     if (test->json_intervals == NULL)
4185         return -1;
4186     cJSON_AddItemToObject(test->json_top, "intervals", test->json_intervals);
4187     test->json_end = cJSON_CreateObject();
4188     if (test->json_end == NULL)
4189         return -1;
4190     cJSON_AddItemToObject(test->json_top, "end", test->json_end);
4191     return 0;
4192 }
4193 
4194 int
4195 iperf_json_finish(struct iperf_test *test)
4196 {
4197     if (test->title)
4198 	cJSON_AddStringToObject(test->json_top, "title", test->title);
4199     if (test->extra_data)
4200 	cJSON_AddStringToObject(test->json_top, "extra_data", test->extra_data);
4201     /* Include server output */
4202     if (test->json_server_output) {
4203 	cJSON_AddItemToObject(test->json_top, "server_output_json", test->json_server_output);
4204     }
4205     if (test->server_output_text) {
4206 	cJSON_AddStringToObject(test->json_top, "server_output_text", test->server_output_text);
4207     }
4208     test->json_output_string = cJSON_Print(test->json_top);
4209     if (test->json_output_string == NULL)
4210         return -1;
4211     fprintf(test->outfile, "%s\n", test->json_output_string);
4212     iflush(test);
4213     cJSON_free(test->json_output_string);
4214     test->json_output_string = NULL;
4215     cJSON_Delete(test->json_top);
4216     test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL;
4217     return 0;
4218 }
4219 
4220 
4221 /* CPU affinity stuff - Linux, FreeBSD, and Windows only. */
4222 
4223 int
4224 iperf_setaffinity(struct iperf_test *test, int affinity)
4225 {
4226 #if defined(HAVE_SCHED_SETAFFINITY)
4227     cpu_set_t cpu_set;
4228 
4229     CPU_ZERO(&cpu_set);
4230     CPU_SET(affinity, &cpu_set);
4231     if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) {
4232 	i_errno = IEAFFINITY;
4233         return -1;
4234     }
4235     return 0;
4236 #elif defined(HAVE_CPUSET_SETAFFINITY)
4237     cpuset_t cpumask;
4238 
4239     if(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
4240                           sizeof(cpuset_t), &test->cpumask) != 0) {
4241         i_errno = IEAFFINITY;
4242         return -1;
4243     }
4244 
4245     CPU_ZERO(&cpumask);
4246     CPU_SET(affinity, &cpumask);
4247 
4248     if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1,
4249                           sizeof(cpuset_t), &cpumask) != 0) {
4250         i_errno = IEAFFINITY;
4251         return -1;
4252     }
4253     return 0;
4254 #elif defined(HAVE_SETPROCESSAFFINITYMASK)
4255 	HANDLE process = GetCurrentProcess();
4256 	DWORD_PTR processAffinityMask = 1 << affinity;
4257 
4258 	if (SetProcessAffinityMask(process, processAffinityMask) == 0) {
4259 		i_errno = IEAFFINITY;
4260 		return -1;
4261 	}
4262 	return 0;
4263 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4264     i_errno = IEAFFINITY;
4265     return -1;
4266 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4267 }
4268 
4269 int
4270 iperf_clearaffinity(struct iperf_test *test)
4271 {
4272 #if defined(HAVE_SCHED_SETAFFINITY)
4273     cpu_set_t cpu_set;
4274     int i;
4275 
4276     CPU_ZERO(&cpu_set);
4277     for (i = 0; i < CPU_SETSIZE; ++i)
4278 	CPU_SET(i, &cpu_set);
4279     if (sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != 0) {
4280 	i_errno = IEAFFINITY;
4281         return -1;
4282     }
4283     return 0;
4284 #elif defined(HAVE_CPUSET_SETAFFINITY)
4285     if(cpuset_setaffinity(CPU_LEVEL_WHICH,CPU_WHICH_PID, -1,
4286                           sizeof(cpuset_t), &test->cpumask) != 0) {
4287         i_errno = IEAFFINITY;
4288         return -1;
4289     }
4290     return 0;
4291 #elif defined(HAVE_SETPROCESSAFFINITYMASK)
4292 	HANDLE process = GetCurrentProcess();
4293 	DWORD_PTR processAffinityMask;
4294 	DWORD_PTR lpSystemAffinityMask;
4295 
4296 	if (GetProcessAffinityMask(process, &processAffinityMask, &lpSystemAffinityMask) == 0
4297 			|| SetProcessAffinityMask(process, lpSystemAffinityMask) == 0) {
4298 		i_errno = IEAFFINITY;
4299 		return -1;
4300 	}
4301 	return 0;
4302 #else /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4303     i_errno = IEAFFINITY;
4304     return -1;
4305 #endif /* neither HAVE_SCHED_SETAFFINITY nor HAVE_CPUSET_SETAFFINITY nor HAVE_SETPROCESSAFFINITYMASK */
4306 }
4307 
4308 char iperf_timestr[100];
4309 
4310 int
4311 iperf_printf(struct iperf_test *test, const char* format, ...)
4312 {
4313     va_list argp;
4314     int r = -1;
4315     time_t now;
4316     struct tm *ltm = NULL;
4317     char *ct = NULL;
4318 
4319     /* Timestamp if requested */
4320     if (iperf_get_test_timestamps(test)) {
4321 	time(&now);
4322 	ltm = localtime(&now);
4323 	strftime(iperf_timestr, sizeof(iperf_timestr), iperf_get_test_timestamp_format(test), ltm);
4324 	ct = iperf_timestr;
4325     }
4326 
4327     /*
4328      * There are roughly two use cases here.  If we're the client,
4329      * want to print stuff directly to the output stream.
4330      * If we're the sender we might need to buffer up output to send
4331      * to the client.
4332      *
4333      * This doesn't make a whole lot of difference except there are
4334      * some chunks of output on the client (on particular the whole
4335      * of the server output with --get-server-output) that could
4336      * easily exceed the size of the line buffer, but which don't need
4337      * to be buffered up anyway.
4338      */
4339     if (test->role == 'c') {
4340 	if (ct) {
4341 	    fprintf(test->outfile, "%s", ct);
4342 	}
4343 	if (test->title)
4344 	    fprintf(test->outfile, "%s:  ", test->title);
4345 	va_start(argp, format);
4346 	r = vfprintf(test->outfile, format, argp);
4347 	va_end(argp);
4348     }
4349     else if (test->role == 's') {
4350 	char linebuffer[1024];
4351 	int i = 0;
4352 	if (ct) {
4353 	    i = sprintf(linebuffer, "%s", ct);
4354 	}
4355 	va_start(argp, format);
4356 	r = vsnprintf(linebuffer + i, sizeof(linebuffer), format, argp);
4357 	va_end(argp);
4358 	fprintf(test->outfile, "%s", linebuffer);
4359 
4360 	if (test->role == 's' && iperf_get_test_get_server_output(test)) {
4361 	    struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline));
4362 	    l->line = strdup(linebuffer);
4363 	    TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries);
4364 	}
4365     }
4366     return r;
4367 }
4368 
4369 int
4370 iflush(struct iperf_test *test)
4371 {
4372     return fflush(test->outfile);
4373 }
4374