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