/*
 *	T T C P . C
 *
 * Test TCP connection.  Makes a connection on port 5001
 * and transfers fabricated buffers or data copied from stdin.
 *
 * Usable on Linux systems.
 *
 * Congestion selection requires a kernel that supports it.
 * Will probably be 2.6.13
 *
 * Modified for operation under 4.2BSD, 18 Dec 84
 *      T.C. Slattery, USNA
 * Minor improvements, Mike Muuss and Terry Slattery, 16-Oct-85.
 * Modified in 1989 at Silicon Graphics, Inc.
 *	catch SIGPIPE to be able to print stats when receiver has died 
 *	for tcp, don't look for sentinel during reads to allow small transfers
 *	increased default buffer size to 8K, nbuf to 2K to transfer 16MB
 *	moved default port to 5001, beyond IPPORT_USERRESERVED
 *	make sinkmode default because it is more popular, 
 *		-s now means don't sink/source 
 *	count number of read/write system calls to see effects of 
 *		blocking from full socket buffers
 *	for tcp, -D option turns off buffered writes (sets TCP_NODELAY sockopt)
 *	buffer alignment options, -A and -O
 *	print stats in a format that's a bit easier to use with grep & awk
 *	for SYSV, mimic BSD routines to use most of the existing timing code
 * Modified in 2005 by Arnaldo Carvalho de Melo
 * 	Added support for DCCP
 * 	Reorganised the code as a whole, using more standard types for
 * 	lib functions, etc
 * Modified in 2005 at University of Waikato by Ian McDonald
 * 	Moving towards ANSI C
 * 	Make Linux only (remove conditional compile)
 * 	Add support for choosing TCP congestion mechanism
 *
 * Distribution Status -
 *      Public Domain.  Distribution Unlimited.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/tfrc.h>
#include <arpa/inet.h>

#ifndef SOCK_DCCP
#define SOCK_DCCP 6
#endif

#ifndef SOL_DCCP
#define SOL_DCCP 269
#endif

#ifndef IPPROTO_DCCP
#define IPPROTO_DCCP 33
#endif

#ifndef DCCP_SOCKOPT_PACKET_SIZE
#define DCCP_SOCKOPT_PACKET_SIZE 1
#endif

#ifndef DCCP_SOCKOPT_SERVICE
#define DCCP_SOCKOPT_SERVICE 2
#endif

#ifndef DCCP_SOCKOPT_CCID_RX_INFO
#define DCCP_SOCKOPT_CCID_RX_INFO 128
#endif

#ifndef DCCP_SOCKOPT_CCID_TX_INFO
#define DCCP_SOCKOPT_CCID_TX_INFO 192
#endif

#ifndef TCP_CONGESTION
#define TCP_CONGESTION	13
#endif

struct addrinfo *hostinfo;
struct addrinfo hosthints;

int domain;
int fd;				/* fd of network socket */

int buflen = 8 * 1024;		/* length of buffer */
char *buf;			/* ptr to dynamic buffer */
int nbuf = 2 * 1024;		/* number of buffers to send in sinkmode */

int bufoffset = 0;		/* align buffer to this */
int bufalign = 16 * 1024;	/* modulo this */

int udp = 0;			/* 0 = tcp, !0 = udp */
int dccp = 0;
unsigned int dccp_services[32];
unsigned int dccp_services_nr;
int options = 0;		/* socket options */
int one = 1;			/* for 4.3 BSD style setsockopt() */
short port = 5001;		/* TCP port number */
char *host;			/* ptr to name of host */
int trans;			/* 0=receive, !0=transmit mode */
int sinkmode = 1;		/* 0=normal I/O, !0=sink/source mode */
int verbose = 0;		/* 0=print basic info, 1=print cpu rate, proc
				 * resource usage. */
int nodelay = 0;		/* set TCP_NODELAY socket option */
int b_flag = 0;			/* use mread() */
int cong = 0;			/* use a non-default TCP congestion algorithm */
char cong_name[25];		/* name of TCP congestion algorithm */

struct hostent *addr;
extern int errno;

static char usage[] = "\
Usage: ttcp -t [-options] host [ < in ]\n\
       ttcp -r [-options > out]\n\
Common options:\n\
	-l##	length of bufs read from or written to network (default 8192)\n\
	-6	use IPv6\n\
	-u	use UDP instead of TCP\n\
	-c##	use DCCP instead of TCP (service(s) must be specified)\n\
	-p##	port number to send to or listen at (default 5001)\n\
	-s	-t: don't source a pattern to network, get data from stdin\n\
		-r: don't sink (discard), print data on stdout\n\
	-A	align the start of buffers to this modulus (default 16384)\n\
	-O	start buffers at this offset from the modulus (default 0)\n\
	-v	verbose: print more statistics\n\
	-d	set SO_DEBUG socket option\n\
	-C##	use TCP congestion algorithm ##\n\
Options specific to -t:\n\
	-n##	number of source bufs written to network (default 2048)\n\
	-D	don't buffer TCP writes (sets TCP_NODELAY socket option)\n\
Options specific to -r:\n\
	-B	for -s, only output full blocks as specified by -l (for TAR)\n\
";

static char stats[128];
static ssize_t nbytes;		/* bytes on net */
static unsigned long nr_calls;	/* # of I/O system calls */
static double cput, realt;	/* user, real time (seconds) */
static struct timeval time0;	/* Time at which timing started */
static struct rusage ru0;	/* Resource utilization at the start */

static void pattern(char *cp, int cnt)
{
	char c = 0;

	while (cnt-- > 0) {
		while (!isprint((c & 0x7F)))
			c++;
		*cp++ = (c++ & 0x7F);
	}
}

#define END(x)	{while(*x) x++;}

static void psecs(long l, char *cp)
{
	int i;

	i = l / 3600;
	if (i) {
		sprintf(cp, "%d:", i);
		END(cp);
		i = l % 3600;
		sprintf(cp, "%d%d", (i / 60) / 10, (i / 60) % 10);
		END(cp);
	} else {
		i = l;
		sprintf(cp, "%d", i / 60);
		END(cp);
	}
	i %= 60;
	*cp++ = ':';
	sprintf(cp, "%d%d", i / 10, i % 10);
}

/* Crude hack to support SOCK_DCCP */
static int xgetaddrinfo(const char *node, const char *service,
			struct addrinfo *hints, struct addrinfo **res)
{
	int socktype = 0, rc;

	if (hints) {
		socktype = hints->ai_socktype;
		if (socktype == SOCK_DCCP) {
			hints->ai_socktype = SOCK_STREAM;
			hints->ai_protocol = IPPROTO_TCP;
		}
	}
	rc = getaddrinfo(node, service, hints, res);

	if (rc == 0 && socktype == SOCK_DCCP) {
		(*res)->ai_socktype = SOCK_DCCP;
		(*res)->ai_protocol = IPPROTO_DCCP;
	}

	return rc;
}

static void tvadd(struct timeval *tsum, struct timeval *t0, struct timeval *t1)
{
	tsum->tv_sec = t0->tv_sec + t1->tv_sec;
	tsum->tv_usec = t0->tv_usec + t1->tv_usec;
	while (tsum->tv_usec > 1000000)
		tsum->tv_sec++, tsum->tv_usec -= 1000000;
}

static void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0)
{
	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
	while (tdiff->tv_usec < 0)
		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
}

static void prep_timer(void)
{
	gettimeofday(&time0, NULL);
	getrusage(RUSAGE_SELF, &ru0);
}

static void prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
		    struct timeval *b, char *outp)
{
	struct timeval tdiff;
	time_t t;
	char *cp;
	int i;
	int ms;

	t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
	    (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
	    (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
	    (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
	ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;

	cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw";
	for (; *cp; cp++) {
		if (*cp != '%')
			*outp++ = *cp;
		else if (cp[1])
			switch (*++cp) {

			case 'U':
				tvsub(&tdiff, &r1->ru_utime, &r0->ru_utime);
				sprintf(outp, "%ld.%01ld", tdiff.tv_sec,
					tdiff.tv_usec / 100000);
				END(outp);
				break;

			case 'S':
				tvsub(&tdiff, &r1->ru_stime, &r0->ru_stime);
				sprintf(outp, "%ld.%01ld", tdiff.tv_sec,
					tdiff.tv_usec / 100000);
				END(outp);
				break;

			case 'E':
				psecs(ms / 100, outp);
				END(outp);
				break;

			case 'P':
				sprintf(outp, "%d%%",
					(int)(t * 100 / ((ms ? ms : 1))));
				END(outp);
				break;

			case 'W':
				i = r1->ru_nswap - r0->ru_nswap;
				sprintf(outp, "%d", i);
				END(outp);
				break;

			case 'X':
				sprintf(outp, "%ld",
					t ==
					0 ? 0 : (r1->ru_ixrss -
						 r0->ru_ixrss) / t);
				END(outp);
				break;

			case 'D':
				sprintf(outp, "%ld", t == 0 ? 0 :
					(r1->ru_idrss + r1->ru_isrss -
					 (r0->ru_idrss + r0->ru_isrss)) / t);
				END(outp);
				break;

			case 'K':
				sprintf(outp, "%ld", t == 0 ? 0 :
					((r1->ru_ixrss + r1->ru_isrss +
					  r1->ru_idrss) - (r0->ru_ixrss +
							   r0->ru_idrss +
							   r0->ru_isrss)) / t);
				END(outp);
				break;

			case 'M':
				sprintf(outp, "%ld", r1->ru_maxrss / 2);
				END(outp);
				break;

			case 'F':
				sprintf(outp, "%ld",
					r1->ru_majflt - r0->ru_majflt);
				END(outp);
				break;

			case 'R':
				sprintf(outp, "%ld",
					r1->ru_minflt - r0->ru_minflt);
				END(outp);
				break;

			case 'I':
				sprintf(outp, "%ld",
					r1->ru_inblock - r0->ru_inblock);
				END(outp);
				break;

			case 'O':
				sprintf(outp, "%ld",
					r1->ru_oublock - r0->ru_oublock);
				END(outp);
				break;
			case 'C':
				sprintf(outp, "%ld+%ld",
					r1->ru_nvcsw - r0->ru_nvcsw,
					r1->ru_nivcsw - r0->ru_nivcsw);
				END(outp);
				break;
			}
	}
	*outp = '\0';
}

static void delay(const suseconds_t us)
{
	struct timeval tv;

	tv.tv_sec = 0;
	tv.tv_usec = us;
	select(1, NULL, NULL, NULL, &tv);
}

/*
 *			M R E A D
 *
 * This function performs the function of a read(II) but will
 * call read(II) multiple times in order to get the requested
 * number of characters.  This can be necessary because
 * network connections don't deliver data with the same
 * grouping as it is written with.  Written by Robert S. Miles, BRL.
 */
ssize_t mread(int fd, char *bufp, ssize_t n)
{
	ssize_t count = 0;

	do {
		const ssize_t nread = read(fd, bufp, n - count);
		nr_calls++;
		if (nread < 0) {
			perror("ttcp_mread");
			return -1;
		}
		if (nread == 0)
			break;
		count += nread;
		bufp += nread;
	} while (count < n);

	return count;
}

/*
 *			N R E A D
 */
ssize_t Nread(int fd, char *buf, int count)
{
	struct sockaddr from;
	socklen_t len = sizeof(from);
	ssize_t cnt;

	if (udp) {
		cnt = recvfrom(fd, buf, count, 0, &from, &len);
		nr_calls++;
	} else if (b_flag)
		cnt = mread(fd, buf, count);	/* fill buf */
	else {
		cnt = read(fd, buf, count);
		nr_calls++;
		if (dccp && verbose) {
			static struct tfrc_rx_info tfrc_last;
			struct tfrc_rx_info tfrc;
			struct timeval now, onow;
			int len = sizeof(tfrc);

			if (getsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_CCID_RX_INFO,
				       &tfrc, &len))
				goto out;
			gettimeofday(&now, NULL);
			tvsub(&onow, &now, &time0);
			if (memcmp(&tfrc, &tfrc_last, len)) {
				printf("%ld.%06ld %u %u %u\n",
				       onow.tv_sec, onow.tv_usec,
				       tfrc.tfrcrx_x_recv,
				       tfrc.tfrcrx_rtt,
				       tfrc.tfrcrx_p);
				tfrc_last = tfrc;
			}
		}
	}
out:
	return cnt;
}

/*
 *			N W R I T E
 */
ssize_t Nwrite(int fd, char *buf, int count)
{
	ssize_t cnt;
	if (udp) {
again:
		cnt = sendto(fd, buf, count, 0,
			     hostinfo->ai_addr, hostinfo->ai_addrlen);
		nr_calls++;
		if (cnt < 0 && errno == ENOBUFS) {
			delay(18000);
			errno = 0;
			goto again;
		}
	} else {
again_dccp:
		cnt = write(fd, buf, count);
		if (dccp && cnt < 0 && errno == EAGAIN) {
			errno = 0;
			goto again_dccp;
		}
		nr_calls++;
		if (dccp && verbose) {
			static struct tfrc_tx_info tfrc_last;
			struct tfrc_tx_info tfrc;
			struct timeval now, onow;
			int len = sizeof(tfrc);

			if (getsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_CCID_TX_INFO,
				       &tfrc, &len))
				goto out;
			gettimeofday(&now, NULL);
			tvsub(&onow, &now, &time0);
			if (memcmp(&tfrc, &tfrc_last, len)) {
				printf("%ld.%06ld %u %u %u\n",
				       onow.tv_sec, onow.tv_usec,
				       tfrc.tfrctx_x_recv,
				       tfrc.tfrctx_rtt,
				       tfrc.tfrctx_p);
				tfrc_last = tfrc;
			}
		}
	}
out:
	return cnt;
}

void sigpipe(int dummy)
{
}

void err(const char *s)
{
	fprintf(stderr, "ttcp-%c: ", trans ? 't' : 'r');
	perror(s);
	fprintf(stderr, "errno=%d\n", errno);
	exit(1);
}

void mes(const char *s)
{
	fprintf(stderr, "ttcp-%c: %s\n", trans ? 't' : 'r', s);
}

static double read_timer(char *str, int len)
{
	struct timeval timedol;
	struct rusage ru1;
	struct timeval td;
	struct timeval tend, tstart;
	char line[132];

	getrusage(RUSAGE_SELF, &ru1);
	gettimeofday(&timedol, (struct timezone *)0);
	prusage(&ru0, &ru1, &timedol, &time0, line);
	(void)strncpy(str, line, len);

	/* Get real time */
	tvsub(&td, &timedol, &time0);
	realt = td.tv_sec + ((double)td.tv_usec) / 1000000;

	/* Get CPU time (user+sys) */
	tvadd(&tend, &ru1.ru_utime, &ru1.ru_stime);
	tvadd(&tstart, &ru0.ru_utime, &ru0.ru_stime);
	tvsub(&td, &tend, &tstart);
	cput = td.tv_sec + ((double)td.tv_usec) / 1000000;
	if (cput < 0.00001)
		cput = 0.00001;
	return (cput);
}

unsigned int decode_dccp_service(const char *service)
{
	unsigned int val;

	if (strlen(service) == 4 && isalpha(service[0]))
		val = (((unsigned int)service[0]) << 24) |
		      (((unsigned int)service[1]) << 16) |
		      (((unsigned int)service[2]) << 8) |
		       ((unsigned int)service[3]);
	else
		val = atoi(service);
	return htonl(val);
}

static const char *family_used(void)
{
	switch (hostinfo->ai_family) {
	case AF_INET:	return "inet";
	case AF_INET6:	return "inet6";
	}
	return "unknown";
}

static const char *protocol_used(void)
{
	switch (hostinfo->ai_protocol) {
	case IPPROTO_TCP:  return "tcp";
	case IPPROTO_UDP:  return "udp";
	case IPPROTO_DCCP: return "dccp";
	}
	return "unknown";
}

int main(int argc, char **argv)
{
	int rc;

	if (argc < 2)
		goto usage;

	hosthints.ai_family   = AF_INET;
	hosthints.ai_socktype = SOCK_STREAM;
	hosthints.ai_protocol = IPPROTO_TCP;

	argv++;
	argc--;
	while (argc > 0 && argv[0][0] == '-') {
		switch (argv[0][1]) {
		case '6':
			hosthints.ai_family = AF_INET6;
			break;
		case 'B':
			b_flag = 1;
			break;
		case 't':
			trans = 1;
			break;
		case 'r':
			trans = 0;
			break;
		case 'd':
			options |= SO_DEBUG;
			break;
		case 'D':
			nodelay = 1;
			break;
		case 'n':
			nbuf = atoi(&argv[0][2]);
			break;
		case 'l':
			buflen = atoi(&argv[0][2]);
			break;
		case 's':
			sinkmode = 0;	/* sink/source data */
			break;
		case 'p':
			port = atoi(&argv[0][2]);
			break;
		case 'u':
			udp = 1;
			hosthints.ai_socktype = SOCK_DGRAM;
			hosthints.ai_protocol = IPPROTO_UDP;
			break;
		case 'c': {
			char *service = &argv[0][2];
			unsigned int nr = 0;
			char *sep = strchr(service, ',');

			while ((sep = strchr(service, ',')) != NULL) {
				*sep = '\0';
				dccp_services[nr++] = decode_dccp_service(service);
				*sep = ',';
				service = sep + 1;
			}

			dccp_services[nr++] = decode_dccp_service(service);
			if (dccp_services[0] != htonl(-1))
				dccp_services_nr = nr;
			dccp = 1;

			hosthints.ai_socktype = SOCK_DCCP;
			hosthints.ai_protocol = IPPROTO_DCCP;
		}
			break;
		case 'v':
			verbose = 1;
			break;
		case 'A':
			bufalign = atoi(&argv[0][2]);
			break;
		case 'O':
			bufoffset = atoi(&argv[0][2]);
			break;
		case 'C':
			cong = 1;
			strncpy(cong_name, &argv[0][2], 24);
			break;
		default:
			goto usage;
		}
		argv++;
		argc--;
	}

	if (trans) {
		if (argc != 1)
			goto usage;

		host = argv[0];
	} else
		hosthints.ai_flags = AI_PASSIVE;

	rc = xgetaddrinfo(host, "5001", &hosthints, &hostinfo);
	if (rc != 0) {
		fprintf(stderr, "error using getaddrinfo: %s\n", gai_strerror(rc));
		return 1;
	}

	if (udp && buflen < 5)
		buflen = 5;	/* send more than the sentinel size */

	buf = malloc(buflen + bufalign);
	if (buf == NULL)
		err("malloc");

	if (bufalign != 0)
		buf += (bufalign - ((int)buf % bufalign) + bufoffset) % bufalign;

	if (trans)
		fprintf(stdout,
			"ttcp-t: buflen=%d, nbuf=%d, align=%d/+%d, port=%d  %s(%s)  -> %s\n",
			buflen, nbuf, bufalign, bufoffset, port,
			protocol_used(), family_used(), argv[0]);
	else
		fprintf(stdout,
			"ttcp-r: buflen=%d, nbuf=%d, align=%d/+%d, port=%d  %s(%s)\n",
			buflen, nbuf, bufalign, bufoffset, port,
			protocol_used(), family_used());

	fd = socket(hostinfo->ai_family, hostinfo->ai_socktype, hostinfo->ai_protocol);
	if (fd < 0)
		err("socket");
	mes("socket");

	if (!trans && bind(fd, hostinfo->ai_addr, hostinfo->ai_addrlen) < 0)
		err("bind");

	if (cong) {
		struct protoent *p = getprotobyname("tcp");

		if (p != NULL && setsockopt(fd, p->p_proto, TCP_CONGESTION,
					    cong_name,
					    strlen(cong_name) + 1) < 0)
			err("setsockopt: cong");
		mes("cong");
	}

	if (dccp) {
		if (setsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_PACKET_SIZE,
			       &buflen, sizeof(buflen)) < 0)
			err("setsockopt");
		if (dccp_services_nr > 0 &&
		    setsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE,
			       dccp_services,
			       dccp_services_nr * sizeof(int)) < 0)
			err("setsockopt");
	}

	if (!udp) {
		signal(SIGPIPE, sigpipe);
		if (trans) {
			/* We are the client if transmitting */
			if (options) {
				if (setsockopt(fd, SOL_SOCKET, options, &one,
					       sizeof(one)) < 0)
					err("setsockopt");
			}
			if (nodelay) {
				struct protoent *p;
				p = getprotobyname("tcp");
				if (p && setsockopt(fd, p->p_proto, TCP_NODELAY,
						    &one, sizeof(one)) < 0)
					err("setsockopt: nodelay");
				mes("nodelay");
			}

			if (connect(fd, hostinfo->ai_addr, hostinfo->ai_addrlen) < 0)
				err("connect");
			mes("connect");
		} else {
			/* otherwise, we are the server and 
			 * should listen for the connections
			 */
			listen(fd, 0);	/* allow a queue of 0 */
			if (options) {
				if (setsockopt(fd, SOL_SOCKET, options,
					       &one, sizeof(one)) < 0)
					err("setsockopt");
			}
			domain = AF_INET;
			fd = accept(fd, hostinfo->ai_addr, &hostinfo->ai_addrlen);
			if (fd < 0)
				err("accept");
			else {
				char peer[1024];

				rc = getnameinfo(hostinfo->ai_addr,
						 hostinfo->ai_addrlen,
						 peer, sizeof(peer), NULL, 0, 0);
				if (rc != 0) {
					fprintf(stderr, "error using getnameinfo: %s\n",
						gai_strerror(rc));
					return 1;
				}

				fprintf(stderr, "ttcp-r: accept from %s\n", peer);
			}
		}
	}
	prep_timer();
	errno = 0;
	if (sinkmode) {
		int cnt;
		if (trans) {
			pattern(buf, buflen);
			if (udp)
				Nwrite(fd, buf, 4);	/* rcvr start */
			while (nbuf-- && Nwrite(fd, buf, buflen) == buflen)
				nbytes += buflen;
			if (udp)
				Nwrite(fd, buf, 4);	/* rcvr end */
		} else if (udp) {
			while ((cnt = Nread(fd, buf, buflen)) > 0) {
				static int going = 0;
				if (cnt <= 4) {
					if (going)
						break;	/* "EOF" */
					going = 1;
					prep_timer();
				} else
					nbytes += cnt;
			}
		} else
			while ((cnt = Nread(fd, buf, buflen)) > 0) {
				nbytes += cnt;
				//printf("nbytes = %ld",nbytes);
			}
	} else {
		ssize_t cnt;
		if (trans) {
			while ((cnt = read(0, buf, buflen)) > 0 &&
			       Nwrite(fd, buf, cnt) == cnt)
				nbytes += cnt;
		} else
			while ((cnt = Nread(fd, buf, buflen)) > 0 &&
			       write(1, buf, cnt) == cnt)
				nbytes += cnt;
	}
	if (errno)
		err("IO");

	read_timer(stats, sizeof(stats));
	if (udp && trans) {
		Nwrite(fd, buf, 4);	/* rcvr end */
		Nwrite(fd, buf, 4);	/* rcvr end */
		Nwrite(fd, buf, 4);	/* rcvr end */
		Nwrite(fd, buf, 4);	/* rcvr end */
	}
	if (cput <= 0.0)
		cput = 0.001;
	if (realt <= 0.0)
		realt = 0.001;

	fprintf(stdout,
		"ttcp%s: %zd bytes in %.2f real seconds = %.2f KB/sec +++\n",
		trans ? "-t" : "-r",
		nbytes, realt, ((double)nbytes) / realt / 1024);

	if (verbose)
		fprintf(stdout,
			"ttcp%s: %zd bytes in %.2f CPU seconds = %.2f KB/cpu sec\n",
			trans ? "-t" : "-r",
			nbytes, cput, ((double)nbytes) / cput / 1024);

	fprintf(stdout,
		"ttcp%s: %ld I/O calls, msec/call = %.2f, calls/sec = %.2f\n",
		trans ? "-t" : "-r",
		nr_calls,
		1024.0 * realt / ((double)nr_calls),
		((double)nr_calls) / realt);
	fprintf(stdout, "ttcp%s: %s\n", trans ? "-t" : "-r", stats);
	if (verbose)
		fprintf(stdout,
			"ttcp%s: buffer address %#x\n",
			trans ? "-t" : "-r", (unsigned int)buf);
	exit(0);
usage:
	fprintf(stderr, usage);
	exit(1);
}
