📄 ttcpv.c
字号:
/* * 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 4.2, 4.3, and 4.1a systems by defining one of * BSD42 BSD43 (BSD41a) * Machines using System V with BSD sockets should define SYSV. * * 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 * * Distribution Status - * Public Domain. Distribution Unlimited. */#include <tk/tkernel.h>#include <renesas_tcpip.h>#include <renesas_tcpip_common.h>#include <netinet/tcp.h>#include "config.h"#define atoi(s) (int)strtol((s), (char **)0, 10)int fd, fd2;#if !defined(_STD_SH7727_)int domain, fromlen;struct sockaddr_in sinme, sinhim, frominet;#endifstruct hostent *addr;#if defined(ORG)char lbuf[64*1024 + 16*1024], *buf=0;#elif defined(_MIC_M32104_) || defined(_MIC_M32192_)char *lbuf = BUF_ADDR, *buf=0;#endifint udp = 0; /* 0 = tcp, !0 = udp */int options = 0; /* socket options */int one = 1; /* for 4.3 BSD style setsockopt() */int b_flag = 0; /* use mread() */unsigned long nbytes = 0; /* bytes on net */unsigned long numCalls = 0; /* # of I/O system calls */#ifdef ORG_NAMEextern struct hostent *gethostbyname();#elseextern struct hostent *unix_gethostbyname();#endifvoid prep_timer();long read_timer();long realt; /* real time (mili seconds) */void err(char *);void mes(char *, int);longttcp(trans,nodelay,nbuf,buflen,sinkmode,port,bufalign,bufoffset,host,m_flag)int trans; /* >0: client(sender), =0: server(receiver) */int nodelay; /* set option TCP_NODELAY */int nbuf; /* the number of buffer (default=2KB) */int buflen; /* buffer size for reading/writing each times (default=8KB) */#ifdef OLDint sinkmode; /* No used */#elseint sinkmode; /* >0: udp, =0: tcp */#endifshort port; /* my/dst port number (default=5001) */int bufalign; /* buffer alignment (default=16KB) */int bufoffset; /* buffer offset */char *host; /* dst IP addr like "127.0.0.1" */int m_flag; /* read requested bytes by calling read() multiple times */{#if !defined(ORG) && !defined(_MIC_M32104_) && !defined(_MIC_M32192_) char lbuf[64*1024 +4], *buf=0;#endif unsigned long addr_tmp; int verbose=1; register int cnt;#if defined(_STD_SH7727_) int domain, fromlen; struct sockaddr_in sinme, sinhim, frominet;#endif if (trans > 0) trans = 1; else trans = 0; if (nodelay != 0) nodelay = 1; if (nbuf == 0) nbuf = 2048; if (buflen == 0) buflen = 8*1024;#ifdef OLD if (sinkmode != 0) sinkmode = 1;#else if (sinkmode != 0) udp = 1;#endif if (port == 0) port = 5001; if (bufalign == 0) bufalign = 16*1024; if (m_flag != 0) b_flag = 1; memset((char *)&sinhim, 0, sizeof(sinhim)); memset((char *)&sinme, 0, sizeof(sinme)); memset((char *)&frominet, 0, sizeof(frominet)); if (trans) { if (atoi(host) > 0) { /* Numeric */ sinhim.sin_family = AF_INET;#if defined(cray) addr_tmp = inet_addr(host); sinhim.sin_addr = addr_tmp;#else sinhim.sin_addr.s_addr = inet_addr(host);#endif }#ifdef ORG else {#ifdef ORG_NAME if ((addr=gethostbyname(host)) == NULL)#else if ((addr=unix_gethostbyname(host)) == NULL)#endif err("bad hostname"); sinhim.sin_family = addr->h_addrtype; memcpy((char *)&addr_tmp,addr->h_addr,addr->h_length);#if defined(cray) sinhim.sin_addr = addr_tmp;#else sinhim.sin_addr.s_addr = addr_tmp;#endif cray }#endif /* ORG */ sinhim.sin_port = htons(port); sinme.sin_port = 0; /* free choice */ } else { /* rcvr */ sinme.sin_port = htons(port); } buf = &lbuf[0]; if (bufalign != 0) buf += ((bufalign - ((int)buf % bufalign) + bufoffset) % bufalign);#ifdef ORG_NAME if ((fd = socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0)) < 0)#else if ((fd = unix_socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0)) < 0)#endif err("socket"); mes("socket",trans);#ifdef ORG_NAME if (bind(fd, (struct sockaddr *)&sinme, sizeof(sinme)) < 0)#else if (unix_bind(fd, (struct sockaddr *)&sinme, sizeof(sinme)) < 0)#endif err("bind"); if (!udp) { if (trans) { /* We are the client if transmitting */ if (options) {#ifdef ORG_NAME if (setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)#else if (unix_setsockopt(fd, SOL_SOCKET, options, (char *)&one, sizeof(one)) < 0)#endif err("setsockopt"); } if (nodelay) {#ifdef ORG_NAME if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)#else if (unix_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0)#endif err("setsockopt: nodelay"); mes("nodelay",trans); }#ifdef ORG_NAME if (connect(fd, (struct sockaddr *)&sinhim, sizeof(sinhim) ) < 0)#else if (unix_connect(fd, (struct sockaddr *)&sinhim, sizeof(sinhim) ) < 0)#endif err("connect"); mes("connect",trans); } else { /* otherwise, we are the server and * should listen for the connections */#ifdef ORG_NAME listen(fd, 0);#else unix_listen(fd, 0);#endif if (options) {#ifdef ORG_NAME if (setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one)) < 0)#else if (unix_setsockopt(fd, SOL_SOCKET, options, (char *)&one, sizeof(one)) < 0)#endif err("setsockopt"); } fromlen = sizeof(frominet); domain = AF_INET; fd2 = fd;#ifdef ORG_NAME if ((fd=accept(fd, (struct sockaddr *)&frominet, &fromlen) ) < 0)#else if ((fd=unix_accept(fd, (struct sockaddr *)&frominet, &fromlen) ) < 0)#endif err("accept");#ifdef ORG { struct sockaddr_in peer; int peerlen = sizeof(peer); memset((char *)&peer, 0, sizeof(peer));#ifdef ORG_NAME if (getpeername(fd, (struct sockaddr *)&peer, &peerlen) < 0) {#else if (unix_getpeername(fd, (struct sockaddr *)&peer, &peerlen) < 0) {#endif err("getpeername"); } }#endif /* ORG */ } } /* if(!udp) */#if defined(KPR_ON) /* get start time */ prep_timer();#endif errno = 0; if (trans) { 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;#if defined(KPR_ON) /* get real start time */ prep_timer();#endif } else { nbytes += cnt; } } } else { while ((cnt=Nread(fd, buf, buflen)) > 0) { nbytes += cnt; } } } if (errno) err("IO");#if defined(KPR_ON) /* get end time */ (void)read_timer();#endif 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 */ }#ifdef ORG_NAME close(fd);#else unix_close(fd);#endif if (!udp && !trans) {#ifdef ORG_NAME close(fd2);#else unix_close(fd2);#endif }#if defined(KPR_ON) if (trans) printf("Mtest- : [%s send] length = %d number = %d\n", udp?"UDP":"TCP", buflen, nbytes/buflen); else printf("Mtest- : [%s receive] length = %d number = %d\n", udp?"UDP":"TCP", buflen, nbytes/buflen);#endif // return ((nbytes * 1000)/realt); /* bytes/sec */ return ((nbytes/realt) * 1000); /* bytes/sec */}voiderr(s)char *s;{#if defined(KPR_ON) printf("errno=%d\n",errno);#endif return;}voidmes(s,trans)char *s;int trans;{#if defined(KPR_ON) printf("ttcp%s: %s\n", trans?"-t":"-r", s);#endif return;}pattern(cp, cnt)register char *cp;register int cnt;{ register char c; c = 0; while (cnt-- > 0) { while (!isprint((c & 0x7F))) c++; *cp++ = (c++ & 0x7F); }}static long tim_val;/* * P R E P _ T I M E R */voidprep_timer(){#if defined(T_KERNEL) SYSTIM pk_tim;#endif#if defined(T_KERNEL) tk_get_tim(&pk_tim); tim_val = pk_tim.lo; /* mili seconds */#endif}/* * R E A D _ T I M E R * */longread_timer(){ long timedol;#if defined(T_KERNEL) SYSTIM pk_tim;#endif#if defined(T_KERNEL) tk_get_tim(&pk_tim); timedol = pk_tim.lo; /* mili seconds */ realt = timedol - tim_val; /* realt is msec */#endif return (realt);}/* * N R E A D */Nread(fd, buf, count)int fd;char *buf;int count;{ struct sockaddr_in from; int len = sizeof(from); register int cnt; if (udp) {#ifdef ORG_NAME cnt = recvfrom(fd, buf, count, 0, (struct sockaddr *)&from, &len);#else cnt = unix_recvfrom(fd, buf, count, 0, (struct sockaddr *)&from, &len);#endif numCalls++; } else { if (b_flag) cnt = mread(fd, buf, count); /* fill buf */ else {#ifdef ORG_NAME cnt = read(fd, buf, count);#else cnt = unix_read(fd, buf, count);#endif numCalls++; } } return (cnt);}/* * N W R I T E */Nwrite(fd, buf, count)int fd;char *buf;int count;{ register int cnt;#if defined(_STD_SH7727_) struct sockaddr_in sinhim;#endif if (udp) {again:#ifdef ORG_NAME cnt = sendto(fd, buf, count, 0, (struct sockaddr *)&sinhim, sizeof(sinhim));#else cnt = unix_sendto(fd, buf, count, 0, (struct sockaddr *)&sinhim, sizeof(sinhim));#endif numCalls++; if (cnt < 0 && errno == ENOBUFS) { errno = 0; goto again; } } else {#ifdef ORG_NAME cnt = write(fd, buf, count);#else cnt = unix_write(fd, buf, count);#endif numCalls++; } return (cnt);}/* * 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. */intmread(fd, bufp, n)int fd;register char *bufp;unsigned n;{ register unsigned count = 0; register int nread; do {#ifdef ORG_NAME nread = read(fd, bufp, n - count);#else nread = unix_read(fd, bufp, n - count);#endif numCalls++; if (nread < 0) { return (-1); } if (nread == 0) return ((int)count); count += (unsigned)nread; bufp += nread; } while (count < n); return ((int)count);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -