ttcp.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. /*
  2. * T T C P . C
  3. *
  4. * Test TCP connection. Makes a connection on port 5010
  5. * and transfers fabricated buffers or data copied from stdin.
  6. *
  7. * Usable on 4.2, 4.3, and 4.1a systems by defining one of
  8. * BSD42 BSD43 (BSD41a)
  9. * Machines using System V with BSD sockets should define SYSV.
  10. *
  11. * Modified for operation under 4.2BSD, 18 Dec 84
  12. * T.C. Slattery, USNA
  13. * Minor improvements, Mike Muuss and Terry Slattery, 16-Oct-85.
  14. * Modified in 1989 at Silicon Graphics, Inc.
  15. * catch SIGPIPE to be able to print stats when receiver has died
  16. * for tcp, don't look for sentinel during reads to allow small transfers
  17. * increased default buffer size to 8K, nbuf to 2K to transfer 16MB
  18. * moved default port to 5001, beyond IPPORT_USERRESERVED
  19. * make sinkmode default because it is more popular,
  20. * -s now means don't sink/source
  21. * count number of read/write system calls to see effects of
  22. * blocking from full socket buffers
  23. * for tcp, -D option turns off buffered writes (sets TCP_NODELAY sockopt)
  24. * buffer alignment options, -A and -O
  25. * print stats in a format that's a bit easier to use with grep & awk
  26. * for SYSV, mimic BSD routines to use most of the existing timing code
  27. * Modified by Steve Miller of the University of Maryland, College Park
  28. * -b sets the socket buffer size (SO_SNDBUF/SO_RCVBUF)
  29. * Modified Sept. 1989 at Silicon Graphics, Inc.
  30. * restored -s sense at request of tcs@brl
  31. * Modified Oct. 1991 at Silicon Graphics, Inc.
  32. * use getopt(3) for option processing, add -f and -T options.
  33. * SGI IRIX 3.3 and 4.0 releases don't need #define SYSV.
  34. *
  35. * Modified --> Nov 1996 at CERN (Daniel DAVIDS)
  36. * printout of the Socket-Buffer-Sizes
  37. * configured for HP-UX 9000 OS
  38. * configured for Windows NT OS
  39. * Modified Dec 1996 at CERN (Jacques ROCHEZ)
  40. * severe cleanup
  41. * addaptation to the gcc compiler (ANSI)
  42. * configured for Lynx OS
  43. * automatic format for the rate display (G/M/K/)bytes/sec
  44. * added log (L) and more help (h) options.
  45. * Modified May 1997 at CERN (Jacques ROCHEZ)
  46. * removed the mes() function following err() function.
  47. * changed the default port to 5010
  48. * Modified jul 1997 at CERN (Jacques ROCHEZ)
  49. * adapted the timing calculation in microseconds
  50. * addapted the code for Vsisual C++ under NT4.0
  51. * Modified aug 1997 at CERN (Jacques ROCHEZ)
  52. * initialise to 0 the variables nbytes, numcalls
  53. * moved the buffer pre-load outside the measured timed area
  54. * Distribution Status -
  55. * Public Domain. Distribution Unlimited.
  56. * Modified jun 1998 at Epigram
  57. * - print hash marks to indicate prograss on transmitting side
  58. * - sleep between bursts of buffers
  59. * - set number of buffers/burst
  60. * Modified jul 1998 at Epigram
  61. * - send random size buffers
  62. * Modified jan 1999 at Epigram (WES)
  63. * - added start and end patterns to UDP start and end packets
  64. * - added handling of missed start, end, or data packets for UDP tests
  65. */
  66. #ifndef lint
  67. static char RCSid[] = "ttcp.c $- CERN Revision: 3.8 (dev level) -$";
  68. #endif
  69. static char VersDate[] = "Epigram 15-jul-98";
  70. /* system dependent setting
  71. * ------------------------
  72. * uname -a,gcc -v a.c are the tools used
  73. *
  74. * Platform/OS #define MACRO predefined
  75. * ----------- ------- ---------------------------------------------------
  76. * SunOS OS BSD43 __unix__ __sun__ __sparc__
  77. * SUN Solaris SYSV __unix__ __sun__ __sparc__ __svr4__
  78. * SGI-IRIX < 3.3 SYSV set as #define sgi
  79. * HP-UX 9000 SYSV __unix__ __hpux__ __hp9k8__
  80. * OSF/1 V3.2 SYSV __unix__ __osf__ __alpha__
  81. * OSF/1 V4.0 SYSV __unix__ __osf__ __alpha__ _CFE
  82. * LynxOS SYSV __unix__ __lynx__ __powerpc__
  83. * Windows NT SYSV __WINNT__ __i386__ __M_COFF__
  84. * AIX SYSV _AIX _AIX32 _POWER _IBMR2 _ARCH_PWR
  85. * Micosoft Visual C++ compiler under WindowNT 4.0
  86. * Windows NT _WINDOWS WIN32
  87. * Unix BSD 41a BSD41a
  88. * 42 BSD42
  89. * 43 BSD43
  90. * Machines using System V with BSD sockets should define SYSV.
  91. *
  92. * Compiler commands
  93. * -----------------
  94. * LynxOS : gcc -c ttcp.c -o ttcp.o | gcc -o ttcp -O ttcp.o -lnetinet -lc_p -lc
  95. */
  96. /* -------------attempt to set an automatic UNIX OS family detection -------*/
  97. #if defined(__hp9k8__) || defined(__osf__) || defined(__srv4__)
  98. #define SYSV
  99. #endif
  100. #if defined(__lynx__)
  101. #define SYSV
  102. #endif
  103. /* for solaris (__srv4__) the parameters SYSV is already set */
  104. /* version A.09.01 'key words' differs from A.09.05 A */
  105. #if defined(__hpux)
  106. #define __hpux__
  107. #endif
  108. #if defined(__sun__)&&!defined(__srv4__)
  109. #define BSD43 /* changed by BSD42 if necessary */
  110. #endif
  111. #if defined(_WIN32) /* Window NT 4.0 compiled with VisualC++ 4 */
  112. #define __NTVIS__
  113. #define SYSV
  114. #endif
  115. #if defined(__FreeBSD__)
  116. #define BSD43
  117. #endif
  118. #if defined(__linux__)
  119. #define BSD43
  120. #endif
  121. /*--------------------------------------------------------------------------*/
  122. #if !defined(UNDER_CE)
  123. #include <signal.h>
  124. #include <ctype.h>
  125. #include <errno.h>
  126. #include <sys/types.h>
  127. #endif
  128. #if defined(SYSV)
  129. #if defined(__osf__)
  130. #include <sys/socket.h>
  131. #include <netinet/in.h>
  132. #include <netinet/tcp.h>
  133. #include <arpa/inet.h>
  134. #include <netdb.h>
  135. #include <sys/time.h> /* struct timeval */
  136. #include <sys/resource.h> /* definition of struct rusage */
  137. #else /* else of __osf__ */
  138. #if defined(__NTVIS__)
  139. #include <winsock2.h>
  140. #include <windows.h> /* required for all Windows applications */
  141. #include <ws2tcpip.h>
  142. #include <memory.h>
  143. #include <time.h>
  144. #if !defined(UNDER_CE)
  145. #include <sys\timeb.h>
  146. #include <fcntl.h>
  147. #endif
  148. #include <stdlib.h>
  149. #include <io.h>
  150. struct rusage { struct timeval ru_utime, ru_stime; };
  151. #define RUSAGE_SELF 0
  152. #else /* else of __NTVIS__ */
  153. #if defined(__lynx__)
  154. #include <socket.h> /* located in /usr/include/..... */
  155. #include <netinet/in.h>
  156. #include <netinet/tcp.h>
  157. #include <arpa/inet.h>
  158. #include <netdb.h>
  159. #include <time.h>
  160. #include <resource.h> /* definition of struct rusage */
  161. #include <sys/times.h>
  162. #define RUSAGE_SELF 0
  163. #include <conf.h> /* definition of TICKSPERSEC (HZ) */
  164. #include <sys/param.h>
  165. #else /* else of __Lynx__ */
  166. #if defined(__svr4__)
  167. #include <sys/socket.h>
  168. #include <sys/uio.h>
  169. #include <netinet/in.h>
  170. #include <netinet/tcp.h>
  171. #include <arpa/inet.h>
  172. #include <netdb.h>
  173. #include <sys/time.h> /* struct timeval */
  174. #include <sys/resource.h> /* definition of struct rusage */
  175. #include <sys/times.h>
  176. #define RUSAGE_SELF 0
  177. #include <sys/param.h>
  178. #else /* else of __svr4__ all SYSV cases except those mentionned before */
  179. #include <sys/socket.h>
  180. #include <netinet/in.h>
  181. #include <netinet/tcp.h>
  182. #include <arpa/inet.h>
  183. #include <netdb.h>
  184. #include <sys/time.h> /* struct timeval */
  185. #include <sys/resource.h> /* definition of struct rusage */
  186. #include <sys/times.h>
  187. #define RUSAGE_SELF 0
  188. #include <sys/param.h>
  189. #endif /* __svr4__ */
  190. #endif /* __lynx__ */
  191. #endif /* __NTVIS__ */
  192. #endif /* __osf__ */
  193. #else /* else of SYSV it is a BSD OS */
  194. #include <stdlib.h>
  195. #include <string.h>
  196. #include <sys/socket.h>
  197. #include <netinet/in.h>
  198. #include <netinet/tcp.h>
  199. #include <arpa/inet.h>
  200. #include <netdb.h>
  201. #if defined(__linux__)
  202. #include <time.h> /* struct timeval */
  203. #else
  204. #include <sys/time.h> /* struct timeval */
  205. #endif
  206. #include <sys/resource.h> /* definition of struct rusage */
  207. #include <unistd.h> /* for usleep() - henry */
  208. #define SOCKET_ERROR -1
  209. #endif /* SYSV */
  210. #if defined(__NTVIS__)
  211. #if defined(_DEBUG) /* usual debug symbol for VC++ */
  212. #define DEBUG 1
  213. #endif
  214. void usleep(unsigned int microseconds);
  215. #define bcopy(a,b,n) memcpy((b), (a), (n))
  216. #define bzero(a,n) memset((a), 0, (n))
  217. #ifndef _GETOPT_
  218. #define _GETOPT_
  219. int getopt(int argc, char **argv, char *optstring);
  220. extern char *optarg; // returned arg to go with this option
  221. extern int optind; // index to next argv element to process
  222. extern int opterr; // should error messages be printed?
  223. extern int optopt; //
  224. #define BADCH ('?')
  225. #endif // _GETOPT
  226. /* get option letter from argument vector */
  227. int
  228. opterr = 1, // should error messages be printed?
  229. optind = 1, // index into parent argv vector
  230. optopt; // character checked for validity
  231. char *optarg; // argument associated with option
  232. #define EMSG ""
  233. char *progname; // may also be defined elsewhere
  234. #endif /*__NTVIS__*/
  235. /* sockaddr_in == file server address structure
  236. *
  237. * Socket address, internet style. declared in : /netinet/in.h
  238. * struct sockaddr_in {short sin_family;
  239. * u_short sin_port;
  240. * struct in_addr sin_addr;
  241. * char sin_zero[8];
  242. * };
  243. *
  244. * Structure used by kernel to store most addresses. declared in ./sys/socket.h
  245. * struct sockaddr{u_short sa_family; address family
  246. * char sa_data[14]; up to 14 bytes of direct address
  247. * };
  248. * PS : sin stand for "socket internet number"
  249. */
  250. #ifndef __NTVIS__
  251. #define FAR
  252. #endif
  253. #if defined(__CONST_SOCKADDR_ARG)
  254. #define SOCKADDR_CAST (__CONST_SOCKADDR_ARG)
  255. #elif defined(__lynx__) || defined(__sun__) || defined(_AIX) || defined(__FreeBSD__) || defined(__NTVIS__)
  256. #define SOCKADDR_CAST (struct sockaddr FAR *)
  257. #else
  258. #define SOCKADDR_CAST
  259. #endif
  260. #if defined(__sun__)
  261. struct sockaddr_in sockaddr; /* done in ./X11/Xdmcp.h */
  262. #endif
  263. struct sockaddr_in sinme; /* is the socket struct. in the local host */
  264. struct sockaddr_in sinhim; /* is the socket struc. in the remote host */
  265. #if defined(__lynx__) || defined(__svr4__) || defined(_AIX)
  266. struct sockaddr frominet;
  267. #else
  268. struct sockaddr_in frominet;
  269. #endif /* __lynx__ */
  270. int domain, fromlen;
  271. #if !defined(__NTVIS__)
  272. #define SOCKET int
  273. #endif /* __NTVIS__ */
  274. SOCKET fd; /* fd of network socket */
  275. #if !defined(__lynx__)
  276. extern int errno;
  277. #endif
  278. #include <stdio.h>
  279. FILE *fplog = NULL; /* file pointer for the log file */
  280. char logfile[100]; /* file name for the log */
  281. static char logfile_head[] ="ttcp_log"; /* header name for the log */
  282. int buflen = 8 * 1024; /* length of buffer */
  283. char *buf; /* ptr to dynamic buffer */
  284. int nbuf = 2 * 1024; /* number of buffers to send in sinkmode */
  285. #define START_PKT_LEN 4
  286. #define START_PATTERN_LEN 4
  287. #define END_PKT_LEN 8
  288. #define END_PATTERN_LEN 4
  289. #define MIN_UDP_LEN 5
  290. char start_pattern[START_PATTERN_LEN] = {1, 2, 3, 4};
  291. char end_pattern[END_PATTERN_LEN] = {2, 2, 2, 2};
  292. int bufoffset = 0; /* align buffer to this */
  293. int bufalign = 16*1024; /* modulo this */
  294. int udp = 0; /* 0 = tcp, !0 = udp */
  295. int options = 0; /* socket options */
  296. int one = 1; /* for 4.3 BSD style setsockopt() */
  297. short port = 5010; /* TCP port number */
  298. char *host; /* ptr to name of host */
  299. int rndm = 0; /* 0="normal", !0=random data */
  300. int trans; /* 0=receive, !0=transmit mode */
  301. int timeout; /* read timeout in millisec */
  302. int debug = 0; /* 0=No-Debug, 1=Debug-Set-On */
  303. int sinkmode = 0; /* 0=normal I/O, !0=sink/source mode */
  304. int verbose = 0; /* 0=print basic info, 1=print cpu rate,
  305. * proc resource usage. */
  306. int nodelay = 0; /* set TCP_NODELAY socket option */
  307. int pri = 0; /* link layer priority */
  308. int b_flag = 0; /* use mread() */
  309. int log_cnt = 0; /* append result to a log */
  310. int sockbufsize = 0; /* socket buffer size to use */
  311. char fmt = 'A'; /* output format:
  312. * b = bits, B = bytes,
  313. * k = kilobits, K = kilobytes,
  314. * m = megabits, M = megabytes,
  315. * g = gigabits, G = gigabytes,
  316. * A = automatic Xbytes (default) */
  317. int touchdata = 0; /* access data after reading */
  318. int seq_info = 0; /* report out of order seq nums */
  319. int hash = 0; /* print hash marks for each buffer */
  320. int udpsleep = 0; /* sleep in between udp buffers */
  321. int burstn = 1; /* number of buffers per burst */
  322. int bufmin = -1; /* minimum buffer size to use when
  323. sending random-size buffers */
  324. unsigned int seed = 1; /* seed for random number generator
  325. used for random buffer lengths */
  326. int no_start = 0; /* skip the start frames for UDP */
  327. int no_data = 0; /* skip all data frames for UDP */
  328. int no_end = 0; /* skip the end frames for UDP */
  329. double nbytes; /* bytes on net */
  330. unsigned long numCalls; /* # of I/O system calls */
  331. struct hostent *addr;
  332. extern int optind;
  333. extern char *optarg;
  334. #if defined(UNDER_CE)
  335. static int errno;
  336. static char *weekday[] ={"Sun", "Mon","Tues", "Wed", "Thurs", "Fri", "Sat"};
  337. static char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
  338. #define perror printf
  339. #endif
  340. /*--------------------------------------------------------------------------*/
  341. static struct timeval time0; /* Time at which timing started */
  342. static struct timeval time1;
  343. static struct rusage ru0; /* Resource utilization at the start */
  344. static struct rusage ru1;
  345. static struct tm *tms; /* system time structure */
  346. /*-----------Prototype functions definitions -------------------------------*/
  347. /*--------------------------------------------------------------------------*/
  348. /* ANSI input/output functions (stdio.h) */
  349. #if defined(__lynx__)
  350. int getopt(int, char**, char*);
  351. int gettimeofday(struct timeval *tp, struct timezone *tzp);
  352. #else
  353. #if defined(__svr4__)
  354. #else
  355. #if defined(_AIX)
  356. #else
  357. #if defined(__hpux__)
  358. #else
  359. #if defined(__NTVIS__)
  360. #else
  361. #if defined(BSD42) || defined(BSD43)
  362. #else
  363. int printf( char*, ...);
  364. int fprintf(FILE*,char*, ...);
  365. void perror(char*);
  366. int getopt(int, char**, char*);
  367. int gettimeofday(struct timeval *tp, struct timezone *tzp);
  368. #endif /* BSD42 || BSD43 */
  369. #endif /* __NTVIS__ */
  370. #endif /* __hpux__ */
  371. #endif /* _AIX */
  372. #endif /* __svr4__ */
  373. #endif /* __lynx__ */
  374. int main(int argc, char* argv[]);
  375. #if ( (!(defined(BSD42))) && (!(defined(BSD43))) && (!(defined(__NTVIS__))) )
  376. int read(int, char*, int);
  377. int write(int, char*, int);
  378. int close(int);
  379. #endif /* !(BSD42) && !(BSD43) */
  380. int fclose(FILE *stream);
  381. #if !defined(BSD43) && !defined(__NTVIS__)
  382. void bzero(char*,int);
  383. void bcopy(char*, char*, int);
  384. char strncpy(char *s1,char *s2,size_t n);
  385. int atoi(char*);
  386. int malloc(int);
  387. #endif
  388. /* ANSI socket functions prototype /sys/socket.h */
  389. #if defined(__lynx__)
  390. int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
  391. #else
  392. #if defined(__svr4__) && !defined(sparc)
  393. /* informations in : /usr/include/sys/socket.h */
  394. int socket(int, int, int);
  395. int connect(int, struct sockaddr *, int);
  396. int bind(int, struct sockaddr *, int);
  397. int listen(int, int);
  398. int accept(int, struct sockaddr *, int *);
  399. int sendto(int, const char *, int, int, const struct sockaddr *, int);
  400. int recvfrom(int, char *, int, int, struct sockaddr *, int *);
  401. int getpeername(int, struct sockaddr *, int *);
  402. int getsockopt(int, int, int, char *, int *);
  403. int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
  404. #else
  405. #if defined(_AIX)
  406. int select(unsigned long, void *, void *, void *, struct timeval *);
  407. #else
  408. #if defined(__hpux__)
  409. int getrusage(int who,struct rusage *rusage);
  410. #else
  411. #if defined(__NTVIS__)
  412. #else
  413. #if defined(BSD42) || defined(BSD43)
  414. #else
  415. int socket(int, int, int);
  416. int connect(int s,struct sockaddr_in *name, int namelen);
  417. int bind(int s,struct sockaddr *name,int namelen);
  418. int listen(int, int);
  419. int accept(int, struct sockaddr_in *, int *);
  420. int sendto(int, char *, int, int, struct sockaddr_in *, int);
  421. int recvfrom(int, char *, int, int, struct sockaddr_in *, int *);
  422. int getpeername(int, struct sockaddr *, int *);
  423. int setsockopt(int, int, int, char *, int);
  424. int getsockopt(int, int, int, char*, int*);
  425. int select(int, fd_set*, fd_set*, fd_set*, struct timeval*);
  426. #endif /* BSD42 || BSD43 */
  427. #endif /* __hpux__ */
  428. #endif /* _AIX */
  429. #endif /* __svr4__ */
  430. #endif /* __lynx__ */
  431. #endif /* __NTVIS__ */
  432. /* ttcp prototype functions */
  433. void udp_rcv_test(void);
  434. double calc_cpu_time(struct rusage *r0, struct rusage *r1);
  435. double calc_real_time(struct timeval *t0, struct timeval *t1);
  436. int getrusage(int who,struct rusage *rusage);
  437. void sockets_err(char*);
  438. void err(char *);
  439. void mes(char *);
  440. void pattern(char *, int);
  441. int Nread(SOCKET, void *, int);
  442. int Nwrite(int, void *, int);
  443. void delay(int);
  444. int mread(int, char *,unsigned);
  445. char *outfmt(char format, double b);
  446. void prep_timer(void);
  447. void read_timer(void);
  448. void result_summary(void);
  449. void prusage(struct rusage*, struct rusage*,
  450. struct timeval*, struct timeval*);
  451. void tvadd(struct timeval *tsum, struct timeval *t0,struct timeval *t1);
  452. void tvsub(struct timeval *tdiff, struct timeval *t1, struct timeval *t0);
  453. void psecs(int);
  454. void open_log(void);
  455. void close_log(void);
  456. void do_Usage(void);
  457. void do_help(void);
  458. /*--------------------------------------------------------------------------*/
  459. #if !defined(__NTVIS__)
  460. void
  461. sigpipe()
  462. {;
  463. }
  464. #endif
  465. /*--------------------------------------------------------------------------*/
  466. /*--------------------------------------------------------------------------*/
  467. int
  468. main(int argc, char **argv)
  469. {
  470. unsigned long addr_tmp;
  471. int c,k;
  472. int sockbufsndsize,sockbufrcvsize;
  473. int sockbuflen;
  474. struct sockaddr_in peer;
  475. int peerlen = sizeof(peer);
  476. #if defined(__NTVIS__)
  477. extern char *optarg;
  478. WSADATA WSAData;
  479. WSAStartup(MAKEWORD(1,1), &WSAData);
  480. #endif /* __NTVIS__ */
  481. if (argc < 2) { do_Usage(); exit(1); }
  482. while (1) {
  483. while ((c=getopt(argc, argv, "hidrstuvxHVBDTLb:f:l:n:p:A:O:S:N:P:R:I:w:")) != -1) {
  484. switch (c) {
  485. case 'w':
  486. timeout = atoi(optarg);
  487. break;
  488. case 'I':
  489. seed = atoi(optarg);
  490. break;
  491. case 'N':
  492. burstn = atoi(optarg);
  493. break;
  494. case 'P':
  495. #if defined(__linux__)
  496. pri = atoi(optarg);
  497. break;
  498. #else
  499. fprintf(stderr, "ttcp: -P option not supported on this OS\n");
  500. exit(1);
  501. #endif
  502. case 'R':
  503. bufmin = atoi(optarg);
  504. break;
  505. case 'S':
  506. udpsleep = atoi(optarg);
  507. if (udpsleep < 0) udpsleep = 0;
  508. /* convert msec arg to usec for usleep, minimum 10ms */
  509. udpsleep = udpsleep * 1000;
  510. break;
  511. case 'H':
  512. hash = 1;
  513. break;
  514. case 'V':
  515. fprintf(stdout,"%s %s\n" , RCSid , VersDate );
  516. exit(0);
  517. case 'B':
  518. b_flag = 1;
  519. break;
  520. case 'L':
  521. log_cnt = 1;
  522. break;
  523. case 'h':
  524. do_help();
  525. exit(1);
  526. break;
  527. case 't':
  528. trans = 1;
  529. break;
  530. case 'r':
  531. trans = 0;
  532. break;
  533. case 'x':
  534. rndm = 1;
  535. break;
  536. case 'd':
  537. options |= SO_DEBUG;
  538. break;
  539. case 'D':
  540. #ifdef TCP_NODELAY
  541. nodelay = 1;
  542. #else
  543. fprintf(stderr,
  544. "ttcp: -D option ignored: TCP_NODELAY socket option not supported\n");
  545. #endif
  546. break;
  547. case 'n':
  548. nbuf = atoi(optarg);
  549. break;
  550. case 'l':
  551. buflen = atoi(optarg);
  552. break;
  553. case 's':
  554. sinkmode = !sinkmode;
  555. break;
  556. case 'p':
  557. port = atoi(optarg);
  558. break;
  559. case 'u':
  560. udp = 1;
  561. break;
  562. case 'v':
  563. verbose = 1;
  564. break;
  565. case 'A':
  566. bufalign = atoi(optarg);
  567. break;
  568. case 'O':
  569. bufoffset = atoi(optarg);
  570. break;
  571. case 'b':
  572. #if defined(SO_SNDBUF) || defined(SO_RCVBUF)
  573. sockbufsize = atoi(optarg);
  574. #else
  575. fprintf(stderr,
  576. "ttcp: -b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported\n");
  577. #endif
  578. break;
  579. case 'f':
  580. fmt = *optarg;
  581. break;
  582. case 'T':
  583. touchdata = 1;
  584. break;
  585. case 'i':
  586. seq_info = 1;
  587. break;
  588. default:
  589. {do_Usage(); exit(1);}
  590. }/*switch */
  591. }/* while getopt() */
  592. argc -= optind;
  593. argv += optind;
  594. optind = 0;
  595. /* check for '--' args */
  596. if (argc == 0) {
  597. /* no more args */
  598. break; /* while (1) */
  599. #if defined(DEBUG)
  600. } else if (!strcmp(argv[0], "--nostart")) {
  601. no_start = 1;
  602. argc--; argv++;
  603. } else if (!strcmp(argv[0], "--nodata")) {
  604. no_data = 1;
  605. argc--; argv++;
  606. } else if (!strcmp(argv[0], "--noend")) {
  607. no_end = 1;
  608. argc--; argv++;
  609. } else if (!strcmp(argv[0], "--debug")) {
  610. debug = 1;
  611. argc--; argv++;
  612. #endif /* DEBUG */
  613. } else if (!strncmp(argv[0], "--", 2)) {
  614. fprintf(stderr, "ttcp: illegal option: %s\n", argv[0]);
  615. do_Usage(); exit(1);
  616. } else {
  617. /* the arg was not a '--' arg */
  618. break; /* while (1) */
  619. }
  620. } /* while (1) */
  621. /* ----------------------- main part ----------------------- */
  622. #if defined(__NTVIS__) && !defined(UNDER_CE)
  623. /* Set "stdin" to have binary mode: */
  624. if (_setmode(_fileno(stdin), _O_BINARY) == -1)
  625. perror("%s: Error setting stdin to binary mode");
  626. /* Set "stdout" to have binary mode: */
  627. if (_setmode(_fileno(stdout), _O_BINARY) == -1)
  628. perror("%s: Error setting stdout to binary mode");
  629. #endif /* __NTVIS__ */
  630. if (log_cnt) open_log();
  631. /* input error checking */
  632. if (burstn > nbuf) {
  633. fprintf(stderr, "ttcp: buffers per burst must be less than or equal to "
  634. "total number of buffers\n");
  635. exit(1);
  636. }
  637. if (bufmin < -1) {
  638. fprintf(stderr, "ttcp: minimum buffer size specified with -R option must "
  639. "be non-negative\n");
  640. exit(1);
  641. }
  642. if (buflen > 65535) {
  643. fprintf(stderr, "ttcp: maximum buffer size specified with -l option must "
  644. "be <= 65536\n");
  645. exit(1);
  646. }
  647. if (bufmin > buflen) {
  648. fprintf(stderr, "ttcp: minimum buffer size specified with -R option must "
  649. "be less than or equal to (maximum) buffer size specified with -l\n");
  650. exit(1);
  651. }
  652. /* buffer allocation */
  653. if (udp && buflen < MIN_UDP_LEN)
  654. buflen = MIN_UDP_LEN; /* send more than the sentinel size */
  655. if ( (buf = (char *)malloc(buflen+bufalign)) == (char *)NULL)
  656. err("malloc");
  657. if (bufalign != 0)
  658. buf +=(bufalign - ((int)buf % bufalign) + bufoffset) % bufalign;
  659. fprintf(stderr,"ttcp%s: buflen=%d, nbuf=%d, align=%d/%d, port=%d\n",
  660. trans?"-t":"-r",buflen, nbuf, bufalign, bufoffset, port);
  661. if (log_cnt)fprintf(fplog," %6d %6d %6d %6d %4d",
  662. buflen, nbuf, bufalign, bufoffset, port);
  663. /* preload the buffer for the transmit condition */
  664. pattern( buf, buflen );
  665. /* seed the random number generator */
  666. if ((bufmin != -1) || (rndm != 0)) {
  667. #if defined(__NTVIS__)
  668. srand(seed);
  669. #else /* ! __NTVIS__ */
  670. srandom(seed);
  671. #endif /* __NTVIS__ */
  672. }
  673. if(trans)
  674. {/* xmitr */
  675. if (argc == 0) {
  676. fprintf(stderr, "ttcp: missing destination host arg\n");
  677. do_Usage(); exit(1);
  678. }
  679. memset((char *)&sinhim, 0, sizeof(sinhim));
  680. host = argv[0];
  681. if (atoi(host) > 0 )
  682. {/* Numeric */
  683. sinhim.sin_family = AF_INET;
  684. sinhim.sin_addr.s_addr = inet_addr(host);
  685. }
  686. else
  687. {if ((addr=gethostbyname(host)) == NULL) sockets_err("bad hostname");
  688. sinhim.sin_family = addr->h_addrtype;
  689. memcpy((char*)&addr_tmp, addr->h_addr, addr->h_length);
  690. sinhim.sin_addr.s_addr = addr_tmp;
  691. }
  692. sinhim.sin_port = htons(port);
  693. sinme.sin_port = 0; /* free choice */
  694. }
  695. else
  696. {/* rcvr */
  697. sinme.sin_port = htons(port);
  698. }
  699. #if defined(__NTVIS__)
  700. sinme.sin_family = AF_INET;
  701. #endif
  702. fd = socket(AF_INET, udp?SOCK_DGRAM:SOCK_STREAM, 0);
  703. #if defined(__NTVIS__)
  704. if (fd == INVALID_SOCKET) sockets_err("socket");
  705. #else
  706. if (fd < 0) sockets_err("socket");
  707. #endif
  708. if (verbose) {
  709. char *label = trans?"ttcp-t":"ttcp-r";
  710. #if defined(UNDER_CE)
  711. SYSTEMTIME SystemTime;
  712. char time_str[30];
  713. GetLocalTime(&SystemTime);
  714. sprintf(time_str, "%s %s %d %02d:%02d:%02d %d\n", weekday[SystemTime.wDayOfWeek], month[SystemTime.wMonth - 1],
  715. SystemTime.wDay, SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wYear);
  716. #else
  717. time_t now;
  718. char *time_str;
  719. time(&now);
  720. time_str = ctime(&now);
  721. #endif
  722. fprintf(stdout,"%s: start time %s", label, time_str);
  723. fprintf(stdout,"%s: File-Descriptor 0x%x Opened\n", label, fd );
  724. }
  725. #if defined(__NTVIS__)
  726. if (bind(fd, (struct sockaddr FAR *)&sinme, sizeof(sinme)) == SOCKET_ERROR)
  727. sockets_err("bind");
  728. #else
  729. if (bind(fd, SOCKADDR_CAST &sinme, sizeof(sinme)) < 0)
  730. sockets_err("bind");
  731. #endif /* __NTVIS__ */
  732. #if defined(SO_SNDBUF) || defined(SO_RCVBUF)
  733. if (sockbufsize)
  734. {
  735. #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
  736. if (trans)
  737. {if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsize,
  738. sizeof sockbufsize) < 0)
  739. sockets_err("setsockopt: sndbuf");
  740. }
  741. else
  742. {if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize,
  743. sizeof sockbufsize) < 0)
  744. sockets_err("setsockopt: rcvbuf");
  745. }
  746. #else
  747. if (trans)
  748. {if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsize,
  749. sizeof sockbufsize) < 0)
  750. sockets_err("setsockopt: sndbuf");
  751. }
  752. else
  753. {if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufsize,
  754. sizeof sockbufsize) < 0)
  755. sockets_err("setsockopt: rcvbuf");
  756. }
  757. #endif /* __lynx__ __sun__ __NTVIS__ */
  758. }
  759. else
  760. {/*
  761. ** Added by Daniel Davids to Know Socket-Buffer-Sizes
  762. */
  763. sockbuflen = sizeof sockbufsndsize;
  764. #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
  765. getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsndsize,
  766. &sockbuflen);
  767. sockbuflen = sizeof sockbufrcvsize;
  768. getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufrcvsize,
  769. &sockbuflen);
  770. #else
  771. getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sockbufsndsize, &sockbuflen);
  772. sockbuflen = sizeof sockbufrcvsize;
  773. getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sockbufrcvsize, &sockbuflen);
  774. #endif /* __lynx__ __sun__ __NTVIS__ */
  775. sockbufsize = ( sockbufsndsize + sockbufrcvsize ) / 2;
  776. if ( sockbufsndsize != sockbufrcvsize )
  777. {fprintf(stderr, "sockbufsndsize=%d, ", sockbufsndsize );
  778. fprintf(stderr, "sockbufrcvsize=%d, ", sockbufrcvsize );
  779. }
  780. }
  781. #endif /* defined(SO_SNDBUF) || defined(SO_RCVBUF) */
  782. if (sockbufsize) fprintf(stderr, "sockbufsize=%d, \n", sockbufsize);
  783. if (log_cnt)
  784. {if (sockbufsize)fprintf(fplog," %6d",sockbufsize);
  785. else fprintf(fplog," 0");
  786. }
  787. #if defined(__linux__)
  788. if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (char *)&pri, sizeof pri) < 0)
  789. sockets_err("setsockopt: priority");
  790. #endif
  791. if (trans) fprintf(stderr, "# %s sender -> %s #\n", udp?"udp":"tcp", host);
  792. else fprintf(stderr, "# %s receiver #\n", udp?"udp":"tcp");
  793. if (!udp)
  794. {
  795. #if !defined(__NTVIS__)
  796. signal(SIGPIPE, sigpipe);
  797. #endif /* !__NTVIS__ */
  798. if (trans)
  799. {/* We are the client if transmitting */
  800. if (options)
  801. {
  802. #if defined(BSD42)
  803. if( setsockopt(fd, SOL_SOCKET, options, 0, 0) < 0)
  804. #else /* BSD43 */
  805. if( setsockopt(fd, SOL_SOCKET, options, (char *)&one, sizeof(one)) < 0)
  806. #endif /* BDS42 */
  807. sockets_err("setsockopt");
  808. }
  809. #ifdef TCP_NODELAY
  810. if (nodelay)
  811. {struct protoent *p;
  812. p = getprotobyname("tcp");
  813. #if defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
  814. if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
  815. (char *) &one, sizeof(one)) < 0)
  816. #else
  817. if( p && setsockopt(fd, p->p_proto, TCP_NODELAY,
  818. &one, sizeof(one)) < 0)
  819. #endif /* __lynx__ __sun__ __NTVIS__ */
  820. sockets_err("setsockopt: nodelay");
  821. }
  822. #endif /* TCP_NODELAY */
  823. if (connect(fd, SOCKADDR_CAST &sinhim, sizeof(sinhim) ) < 0)
  824. sockets_err("connect");
  825. }/* if (trans) */
  826. else
  827. {/* otherwise, we are the server and should listen for connections */
  828. errno = 0;
  829. #if defined(sgi) || ( defined(__osf__) && !defined(_CFE) )
  830. if( listen(fd,1) <0 )
  831. #else
  832. if( listen(fd,0) <0 ) /* allow a queue of 0 */
  833. #endif
  834. sockets_err("listen");
  835. if(options) {
  836. int err;
  837. #if defined(BSD42)
  838. err = setsockopt(fd, SOL_SOCKET, options, 0, 0);
  839. #elif defined(__lynx__) || defined(__sun__) || defined(__NTVIS__)
  840. err = setsockopt(fd, SOL_SOCKET, options, (char *) &one, sizeof(one));
  841. #else
  842. err = setsockopt(fd, SOL_SOCKET, options, &one, sizeof(one));
  843. #endif /* BSD42 */
  844. if (err < 0)
  845. sockets_err("setsockopt");
  846. }
  847. fromlen = sizeof(frominet);
  848. domain = AF_INET;
  849. if (timeout > 0) {
  850. fd_set readfds, exceptfds;
  851. struct timeval tv_timeout;
  852. int n;
  853. tv_timeout.tv_sec = timeout/1000;
  854. tv_timeout.tv_usec = (timeout%1000)*1000;
  855. FD_ZERO(&readfds);
  856. FD_ZERO(&exceptfds);
  857. FD_SET(fd, &readfds);
  858. FD_SET(fd, &exceptfds);
  859. n = select( fd+1, &readfds, NULL, &exceptfds, &tv_timeout );
  860. if (n == 0 || n == SOCKET_ERROR) {
  861. sockets_err("select listen");
  862. return 0;
  863. }
  864. }
  865. if((fd=accept(fd, SOCKADDR_CAST &frominet, &fromlen) ) < 0)
  866. sockets_err("accept");
  867. if (getpeername(fd, SOCKADDR_CAST &peer, &peerlen) < 0)
  868. sockets_err("getpeername");
  869. fprintf(stderr,"ttcp-r: accept from %s\n",inet_ntoa(peer.sin_addr));
  870. } /* otherwise we are ... */
  871. }
  872. prep_timer();
  873. errno = 0;
  874. nbytes = 0.0;
  875. numCalls = 0;
  876. {
  877. register int cnt,multi;
  878. char *pb;
  879. int nb = 0;
  880. unsigned long rbuflen = 0;
  881. multi = nbuf;
  882. if (trans) {
  883. #if !defined(BSD43)
  884. if(udp) {
  885. int err;
  886. int nochecksum_opt = 0;
  887. err = setsockopt(fd, IPPROTO_UDP, UDP_NOCHECKSUM,
  888. (char *)&nochecksum_opt, sizeof(nochecksum_opt));
  889. if (err < 0)
  890. sockets_err("setsockopt");
  891. }
  892. #endif
  893. if(udp && !no_start) {
  894. int start_count = 2;
  895. char start_pkt[START_PKT_LEN];
  896. memset(start_pkt, 0, START_PKT_LEN);
  897. memcpy(start_pkt, start_pattern, START_PATTERN_LEN);
  898. while (start_count-- > 0) {
  899. (void)Nwrite( fd, start_pkt, START_PKT_LEN ); /* rcvr start */
  900. if (udpsleep) {
  901. usleep(udpsleep);
  902. /* clear errno (see man page for errno(3), definition of
  903. EINTR. usleep() uses SIGCONT? ) */
  904. if (errno == EINTR) errno = 0;
  905. } /* udpsleep */
  906. }
  907. }
  908. /* initial seqence num for UDP */
  909. if (udp) buf[0] = 0;
  910. k = burstn;
  911. if (sinkmode) {
  912. while (multi-- > 0 && !no_data) {
  913. if (bufmin == -1) {
  914. rbuflen = buflen;
  915. } else {
  916. /* set rbuflen to a random value evenly distributed in
  917. [bufmin, buflen]. As long as buflen is < 2^16, we can
  918. fit the calculation in 32 bits */
  919. #if defined(__NTVIS__)
  920. rbuflen = (( (unsigned long)rand() *
  921. ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
  922. #else /* ! __NTVIS__ */
  923. rbuflen = (( ((unsigned long)random() >> 15) *
  924. ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
  925. #endif /* __NTVIS__ */
  926. }
  927. if (rndm) {
  928. unsigned int i, j, l;
  929. unsigned long data;
  930. if (udp)
  931. l = 1;
  932. else
  933. l = 0;
  934. for (i = l; i < rbuflen; /* null */) {
  935. #if defined(__NTVIS__)
  936. data = (unsigned long)rand();
  937. #else /* ! __NTVIS__ */
  938. data = (unsigned long)random();
  939. #endif /* __NTVIS__ */
  940. /*fprintf(stderr, "%08x\n",data);*/ /* DEBUG */
  941. /* The NT rand() function returns only 16 useful bits! */
  942. for (j = 0; (j < 2/*4*/) && (i < rbuflen) ; j++) {
  943. buf[i++] = (unsigned char)(data & 0xff);
  944. data >>= 8;
  945. }
  946. }
  947. }
  948. if ((cnt=Nwrite(fd,buf,rbuflen)) != (int)rbuflen)
  949. sockets_err("error from the socket write");
  950. /* increment sequence num if UDP */
  951. if (udp) buf[0] = (char)(((unsigned char)(buf[0])+1) & 0xFF);
  952. if (debug)
  953. fprintf(stdout,"ttcp-t: %5d | %d Bytes Written in %d write commands \n",
  954. ++nb, cnt, nbuf );
  955. nbytes += rbuflen;
  956. /* hashes */
  957. if (hash) {
  958. fprintf(stderr,"#");
  959. }
  960. /* Check for the end of a burst */
  961. if (--k <= 0) {
  962. k = burstn;
  963. /* sleep to avoid overrunning slower receivers - henry */
  964. if (udpsleep) {
  965. usleep(udpsleep);
  966. /* clear errno (see man page for errno(3), definition of
  967. EINTR. usleep() uses SIGCONT? ) */
  968. if (errno == EINTR) errno = 0;
  969. } /* udpsleep */
  970. }
  971. } /* while */
  972. } /* if (sinkmode) */
  973. else
  974. {
  975. nbuf = 0;
  976. if (bufmin == -1) {
  977. rbuflen = buflen;
  978. } else {
  979. /* set rbuflen to a random value evenly distributed in
  980. [bufmin, buflen]. As long as buflen is < 2^16, we can
  981. fit the calculation in 32 bits */
  982. #if defined(__NTVIS__)
  983. rbuflen = (( (unsigned long)rand() *
  984. ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
  985. #else /* ! __NTVIS__ */
  986. rbuflen = (( ((unsigned long)random() >> 15) *
  987. ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
  988. #endif /* __NTVIS__ */
  989. }
  990. if (udp)
  991. pb = &(buf[1]);
  992. else
  993. pb = &(buf[0]);
  994. #if !defined(__NTVIS__) /* else case specific to WINNT */
  995. while((cnt=read(0,pb,rbuflen)) > 0)
  996. #else /* __NTVIS__ */
  997. while((cnt=(int)fread(pb,1,rbuflen,stdin)) > 0)
  998. #endif /* __NTVIS__ */
  999. {
  1000. if (udp)
  1001. multi = cnt+1; /* don't include seq. num. in count of data */
  1002. else
  1003. multi = cnt;
  1004. if ((cnt=Nwrite(fd,buf,multi)) != (int)multi)
  1005. sockets_err("error from the socket write");
  1006. /* increment seqence num if UDP */
  1007. if (udp) buf[0] = (char)(((unsigned char)(buf[0])+1) & 0xFF);
  1008. nbuf++;
  1009. if (debug)
  1010. fprintf(stdout,"ttcp-t: %5d | %d Bytes Written in %d write commands \n",
  1011. ++nb, cnt, nbuf );
  1012. nbytes += multi;
  1013. /* hashes */
  1014. if (hash) {
  1015. fprintf(stderr,"#");
  1016. }
  1017. /* Check for the end of a burst */
  1018. if (--k <= 0) {
  1019. k = burstn;
  1020. /* sleep to avoid overrunning slower receivers - henry */
  1021. if (udpsleep) {
  1022. usleep(udpsleep);
  1023. /* clear errno (see man page for errno(3), definition of
  1024. EINTR. usleep() uses SIGCONT? ) */
  1025. if (errno == EINTR) errno = 0;
  1026. } /* udpsleep */
  1027. }
  1028. if (bufmin == -1) {
  1029. rbuflen = buflen;
  1030. } else {
  1031. /* set rbuflen to a random value evenly distributed in
  1032. [bufmin, buflen]. As long as buflen is < 2^16, we can
  1033. fit the calculation in 32 bits */
  1034. #if defined(__NTVIS__)
  1035. rbuflen = (( (unsigned long)rand() *
  1036. ((unsigned long)(buflen-bufmin+1)) ) >> 15) + bufmin;
  1037. #else /* ! __NTVIS__ */
  1038. rbuflen = (( ((unsigned long)random() >> 15) *
  1039. ((unsigned long)(buflen-bufmin+1)) ) >> 16) + bufmin;
  1040. #endif /* __NTVIS__ */
  1041. }
  1042. } /* while */
  1043. } /* if (sinkmode) */
  1044. if (hash) {
  1045. fprintf(stderr,"\n");
  1046. fflush(stderr);
  1047. }
  1048. #if defined(UNDER_CE)
  1049. errno = WSAGetLastError();
  1050. #endif
  1051. if (!errno) {
  1052. read_timer();
  1053. if(udp && !no_end) {
  1054. int end_count = 4;
  1055. int data_count = nbuf;
  1056. char end_pkt[END_PKT_LEN];
  1057. unsigned char* net_byte = &end_pkt[END_PATTERN_LEN];
  1058. memset(end_pkt, 0, END_PKT_LEN);
  1059. memcpy(end_pkt, end_pattern, END_PATTERN_LEN);
  1060. net_byte[3] = data_count & 0xFF; data_count >>= 8;
  1061. net_byte[2] = data_count & 0xFF; data_count >>= 8;
  1062. net_byte[1] = data_count & 0xFF; data_count >>= 8;
  1063. net_byte[0] = data_count & 0xFF;
  1064. while (end_count-- > 0) {
  1065. Nwrite( fd, end_pkt, END_PKT_LEN ); /* rcvr end */
  1066. if (udpsleep && end_count>0) usleep(udpsleep);
  1067. }
  1068. }
  1069. result_summary();
  1070. }
  1071. } /* trans */
  1072. else
  1073. {
  1074. if (udp) {
  1075. udp_rcv_test();
  1076. }
  1077. else
  1078. {/* not udp received transfer */
  1079. while ((cnt=Nread(fd,buf,buflen)) > 0)
  1080. {if(debug)fprintf(stdout,
  1081. "ttcp%s: %5d | %d Bytes Read\n",
  1082. trans?"-t":"-r", ++nb, cnt );
  1083. nbytes += cnt;
  1084. if (!sinkmode) {
  1085. #if !defined(__NTVIS__) /* else case specific to WINNT */
  1086. if (write(1,buf,cnt) != cnt) err("write");
  1087. #else /* __NTVIS__ */
  1088. if ((int)fwrite(buf,1,cnt,stdout) != cnt) err("fwrite");
  1089. #endif /* __NTVIS__ */
  1090. }
  1091. }
  1092. #if defined(UNDER_CE)
  1093. errno = WSAGetLastError();
  1094. #endif
  1095. if (!errno) {
  1096. read_timer();
  1097. result_summary();
  1098. }
  1099. }
  1100. }
  1101. }
  1102. if(errno) err("IO");
  1103. #if defined(__NTVIS__)
  1104. closesocket ( fd );
  1105. #else
  1106. close ( fd );
  1107. #endif /* __NTVIS__ */
  1108. if (verbose) fprintf(stdout,"ttcp%s: File-Descriptor fd 0x%x Closed\n" ,
  1109. trans?"-t":"-r", fd );
  1110. if (log_cnt) close_log();
  1111. fprintf(stderr,"ttcp done.\n");
  1112. fflush(stdout);
  1113. exit(0);
  1114. }
  1115. /*--------------------------------------------------------------------------*/
  1116. void
  1117. udp_rcv_test(void)
  1118. {
  1119. enum {START_WAIT, DATA_WAIT, DATA_RCV, END_TEST} rcv_state;
  1120. enum {START_PKT_TYPE, END_PKT_TYPE, DATA_PKT_TYPE} pkt_type;
  1121. int cnt;
  1122. int nbuffers = 0;
  1123. unsigned int start_pkt_cnt = 0;
  1124. unsigned int end_pkt_cnt = 0;
  1125. unsigned int data_pkt_cnt = 0;
  1126. unsigned int expected_pkt_cnt = 0;
  1127. unsigned char seq = 0;
  1128. unsigned char buf_seq;
  1129. rcv_state = START_WAIT;
  1130. while (rcv_state != END_TEST && (cnt=Nread(fd,buf,buflen)) > 0) {
  1131. /* Determine the type of packet received */
  1132. if (!memcmp(buf, start_pattern, START_PATTERN_LEN)) {
  1133. pkt_type = START_PKT_TYPE;
  1134. start_pkt_cnt++;
  1135. } else if (!memcmp(buf, end_pattern, END_PATTERN_LEN)) {
  1136. /* read the expected data packet count from the end packet */
  1137. unsigned char* net_byte = buf;
  1138. net_byte += END_PATTERN_LEN;
  1139. expected_pkt_cnt = ((net_byte[0] << 24) + (net_byte[1] << 16) +
  1140. (net_byte[2] << 8) + net_byte[3]);
  1141. pkt_type = END_PKT_TYPE;
  1142. end_pkt_cnt++;
  1143. } else {
  1144. data_pkt_cnt++;
  1145. pkt_type = DATA_PKT_TYPE;
  1146. }
  1147. if (rcv_state == START_WAIT) {
  1148. /* Wait until we see a vaild start packet */
  1149. if (pkt_type == START_PKT_TYPE) {
  1150. prep_timer();
  1151. rcv_state = DATA_WAIT;
  1152. } else if (pkt_type == DATA_PKT_TYPE) {
  1153. fprintf(stderr, "ERROR: Missed UDP start packet.\n");
  1154. prep_timer();
  1155. rcv_state = DATA_RCV;
  1156. } else if (pkt_type == END_PKT_TYPE) {
  1157. fprintf(stderr, "ERROR: Found UDP end packet before start packet or data.\n");
  1158. rcv_state = END_TEST;
  1159. }
  1160. } else if (rcv_state == DATA_WAIT) {
  1161. /* Skip any extra start packets until we see data */
  1162. if (pkt_type == START_PKT_TYPE) {
  1163. prep_timer();
  1164. } else if (pkt_type == DATA_PKT_TYPE) {
  1165. rcv_state = DATA_RCV;
  1166. } else if (pkt_type == END_PKT_TYPE) {
  1167. fprintf(stderr, "ERROR: Found UDP end packet without receiving "
  1168. "any data packets.\n");
  1169. rcv_state = END_TEST;
  1170. }
  1171. } else { /* DATA_RCV */
  1172. /* Collect data packets until we see a vaild end packet */
  1173. if (pkt_type == START_PKT_TYPE) {
  1174. /* We missed the end packets and now a new test is
  1175. * starting. Report the results of this test then start
  1176. * another. */
  1177. read_timer();
  1178. result_summary();
  1179. fprintf(stderr, "ERROR: Found UDP start packet while receiving data.\n"
  1180. "ERROR: Expected more data packets or an end packet.\n");
  1181. nbytes = 0.0;
  1182. nbuffers = 0;
  1183. numCalls = 0;
  1184. prep_timer();
  1185. rcv_state = DATA_WAIT;
  1186. } else if (pkt_type == DATA_PKT_TYPE) {
  1187. /* loop in this state */
  1188. } else if (pkt_type == END_PKT_TYPE) {
  1189. /* we used to print results here but now we do when the loop ends */
  1190. rcv_state = END_TEST;
  1191. }
  1192. }
  1193. /* tally data packets the same way from whatever state we are in */
  1194. if (pkt_type == DATA_PKT_TYPE) {
  1195. if (debug)
  1196. fprintf(stderr, "ttcp-r: %5d | %d Bytes Read\n", ++nbuffers, cnt);
  1197. nbytes += cnt;
  1198. if (seq_info) {
  1199. /* check seqence num */
  1200. buf_seq = (unsigned char)(buf[0]);
  1201. if (buf_seq != seq) {
  1202. fprintf(stderr, "ERROR: Out of sequence. "
  1203. "Buffer %u, seq %u, expected %u\n",
  1204. data_pkt_cnt, buf_seq, seq);
  1205. seq = buf_seq;
  1206. }
  1207. seq++;
  1208. }
  1209. if (!sinkmode) {
  1210. #if !defined(__NTVIS__) /* else case specific to WINNT */
  1211. if (write(1,&(buf[1]),cnt-1) != cnt-1) err("write");
  1212. #else /* __NTVIS__ */
  1213. if ((int)fwrite(&(buf[1]),1,cnt-1,stdout) != cnt-1) err("fwrite");
  1214. #endif /* __NTVIS__ */
  1215. }
  1216. }
  1217. } /* end-while */
  1218. /* normal end of test */
  1219. read_timer();
  1220. result_summary();
  1221. fprintf(stderr, "ttcp-r: packets %u data, %u start, %u end\n",
  1222. data_pkt_cnt, start_pkt_cnt, end_pkt_cnt);
  1223. if (expected_pkt_cnt != 0) {
  1224. int lost_cnt = expected_pkt_cnt - data_pkt_cnt;
  1225. fprintf(stderr, "ttcp-r: %g%% (%u/%u) packet loss\n",
  1226. 100.0 * (double)lost_cnt/(double)expected_pkt_cnt,
  1227. lost_cnt, expected_pkt_cnt);
  1228. }
  1229. }
  1230. /*--------------------------------------------------------------------------*/
  1231. /* calc_cpu_time(): Time difference, in usecs, of the combined user and
  1232. * sys times of the given start and end usage info */
  1233. double
  1234. calc_cpu_time(
  1235. struct rusage *r0,
  1236. struct rusage *r1
  1237. )
  1238. {
  1239. double time;
  1240. time_t ms;
  1241. struct timeval tdiff, tend, tstart;
  1242. /* total user delta time + total system delta time */
  1243. tvadd( &tend, &r1->ru_utime, &r1->ru_stime ); /* user + sys time @ end */
  1244. tvadd( &tstart, &r0->ru_utime, &r0->ru_stime ); /* user + sys time @ start*/
  1245. tvsub( &tdiff, &tend, &tstart );
  1246. /* useconds */
  1247. time = ((double)tdiff.tv_sec)*1e6 + (double)tdiff.tv_usec;
  1248. /* debug mseconds */
  1249. ms = tdiff.tv_sec*(time_t)1000 + tdiff.tv_usec/(time_t)1000;
  1250. if (ms != (time_t)(time/1000.0)) {
  1251. fprintf(stderr, "ERROR: time calc mismatch of msec (%d) to usec (%f)\n",
  1252. (int)ms, time/1000.0);
  1253. }
  1254. return time;
  1255. }
  1256. /*--------------------------------------------------------------------------*/
  1257. /* calc_real_time(): Time difference in usecs of the given times */
  1258. double
  1259. calc_real_time(
  1260. struct timeval *t0,
  1261. struct timeval *t1
  1262. )
  1263. {
  1264. double time;
  1265. time_t ms;
  1266. struct timeval tdiff;
  1267. tvsub(&tdiff, t1 ,t0);
  1268. /* useconds */
  1269. time = ((double)tdiff.tv_sec)*1e6 + (double)tdiff.tv_usec;
  1270. /* debug mseconds */
  1271. ms = tdiff.tv_sec*(time_t)1000 + tdiff.tv_usec/(time_t)1000;
  1272. if (ms != (time_t)(time/1000.0)) {
  1273. fprintf(stderr, "ERROR: time calc mismatch of msec (%d) to usec (%f)\n",
  1274. (int)ms, time/1000.0);
  1275. }
  1276. return time;
  1277. }
  1278. /*--------------------------------------------------------------------------*/
  1279. void
  1280. result_summary(void)
  1281. {
  1282. char* label = trans ? "ttcp-t" : "ttcp-r";
  1283. double cput = calc_cpu_time(&ru0, &ru1);
  1284. double realt = calc_real_time(&time0, &time1);
  1285. double t_min;
  1286. double bytes_per_sec;
  1287. /* lower end boundary conditions */
  1288. t_min = 10.0; /* 10 usec */
  1289. #if defined(__NTVIS__)
  1290. t_min = 1000.0; /* 1 msec */
  1291. #endif
  1292. if (cput <= t_min) { /* value in usec */
  1293. cput = t_min;
  1294. fprintf(stderr,"%s: cpu time too short set at %.0f usec, NOT accurate result.\n",
  1295. label,t_min);
  1296. }
  1297. if (realt <= t_min) { /* value in usec */
  1298. realt = t_min;
  1299. fprintf(stderr,"%s: real time too short, set at %.0f usec, NOT accurate result.\n",
  1300. label,t_min);
  1301. }
  1302. bytes_per_sec = (nbytes/realt)*1e6;
  1303. fprintf(stderr,"%s: %.0f bytes in %.06f real seconds = %s/sec +++\n",
  1304. label, nbytes, realt/1e6, outfmt(fmt, bytes_per_sec));
  1305. if (verbose)
  1306. fprintf(stderr,"%s: %.0f bytes in %.06f cpu seconds = %s/cpu sec\n",
  1307. label, nbytes,cput/1e6, outfmt(fmt, (nbytes/cput)*1e6));
  1308. fprintf(stderr,"%s: %ld I/O calls, %.3f msec(real)/call, %.3f msec(cpu)/call\n",
  1309. label, numCalls,
  1310. (numCalls>0)?(realt/(double)numCalls)/1000.0:0.0,
  1311. (numCalls>0)?(cput /(double)numCalls)/1000.0:0.0);
  1312. fprintf(stderr,"%s: ", label);
  1313. prusage(&ru0, &ru1, &time0, &time1);
  1314. fprintf(stderr,"\n");
  1315. if (verbose)
  1316. printf("%s: buffer address %#x\n", label, (unsigned int)buf);
  1317. if (fplog) {
  1318. struct timeval tdiff;
  1319. /* User time */
  1320. tvsub(&tdiff, &ru1.ru_utime, &ru0.ru_utime);
  1321. fprintf(fplog," %ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
  1322. /* System time */
  1323. tvsub(&tdiff, &ru1.ru_stime, &ru0.ru_stime);
  1324. fprintf(fplog," %ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
  1325. /* Percent of cput usage */
  1326. fprintf(fplog," %.1f", 100.0 * (cput/realt));
  1327. /* validity of real time mesurment */
  1328. fprintf(fplog, (realt == t_min)?" 0":" 1");
  1329. /* bytes, seconds, MBytes/sec, IO calls */
  1330. fprintf(fplog," %10.0f %4.06f %4.3f %6ld",
  1331. nbytes, realt/1e6, bytes_per_sec/(1024.0*1024.0), numCalls);
  1332. }
  1333. }
  1334. /*--------------------------------------------------------------------------*/
  1335. void
  1336. sockets_err(char* s)
  1337. {
  1338. #if defined(__NTVIS__)
  1339. int err = WSAGetLastError();
  1340. char* prefix = trans?"ttcp-t":"ttcp-r";
  1341. fprintf(stderr,"%s: %s\n", prefix, s);
  1342. fprintf(stderr,"%s: errno=%d\n", prefix, err);
  1343. exit(1);
  1344. #else
  1345. err(s);
  1346. #endif /* __NTVIS__ */
  1347. }
  1348. /*--------------------------------------------------------------------------*/
  1349. void
  1350. err(char *s)
  1351. {
  1352. char* prefix = trans?"ttcp-t":"ttcp-r";
  1353. fprintf(stderr,"%s: ", prefix);
  1354. perror(s);
  1355. fprintf(stderr,"%s: errno=%d\n", prefix, errno);
  1356. exit(1);
  1357. }
  1358. /*--------------------------------------------------------------------------*/
  1359. void
  1360. mes(char *s)
  1361. {
  1362. fprintf(stderr,"ttcp%s: %s\n", trans?"-t":"-r", s);
  1363. }
  1364. /*--------------------------------------------------------------------------*/
  1365. void
  1366. pattern( char *cp, int cnt )
  1367. {
  1368. register char c;
  1369. register int cnt1;
  1370. cnt1 = cnt;
  1371. c = 0;
  1372. while( cnt1-- > 0 ) {
  1373. while( !isprint((c&0x7F)) ) c++;
  1374. *cp++ = (c++&0x7F);
  1375. }
  1376. }
  1377. /*--------------------------------------------------------------------------*/
  1378. char *
  1379. outfmt(char format, double b)
  1380. {
  1381. static char obuf[50];
  1382. double giga = 1024.0 * 1024.0 * 1024.0;
  1383. double mega = 1024.0 * 1024.0;
  1384. double kilo = 1024.0;
  1385. if (format == 'A') {
  1386. if (b >= giga)
  1387. format = 'G';
  1388. else if (b >= mega)
  1389. format = 'M';
  1390. else if (b >= kilo)
  1391. format = 'K';
  1392. else
  1393. format = 'B';
  1394. }
  1395. switch (format) {
  1396. case 'G':
  1397. sprintf(obuf, "%.3f GB", b / giga);
  1398. break;
  1399. case 'M':
  1400. sprintf(obuf, "%.3f MB", b / mega);
  1401. break;
  1402. case 'K':
  1403. sprintf(obuf, "%.3f KB", b / kilo);
  1404. break;
  1405. case 'B':
  1406. sprintf(obuf, "%4f B", b);
  1407. break;
  1408. case 'g':
  1409. sprintf(obuf, "%.3f Gbit", b * 8.0 / giga);
  1410. break;
  1411. case 'm':
  1412. sprintf(obuf, "%.3f Mbit", b * 8.0 / mega);
  1413. break;
  1414. case 'k':
  1415. sprintf(obuf, "%.3f Kbit", b * 8.0 / kilo);
  1416. break;
  1417. case 'b':
  1418. sprintf(obuf, "%4f b", b * 8.0);
  1419. break;
  1420. default:
  1421. sprintf(obuf, "default..........");
  1422. }
  1423. return obuf;
  1424. }
  1425. /*--------------------------------------------------------------------------*/
  1426. #if defined(SYSV)
  1427. /*ARGSUSED*/
  1428. #if defined(__osf__)
  1429. /* getrusage defined in the system lib */
  1430. #else
  1431. #if defined(__lynx__)
  1432. /* getrusage defined in the system lib */
  1433. #else
  1434. #if defined(__sun__)
  1435. /* getrusage defined in the system lib */
  1436. #else
  1437. int
  1438. getrusage(ignored, ru)
  1439. int ignored;
  1440. register struct rusage *ru;
  1441. {
  1442. #if defined(__NTVIS__)
  1443. HANDLE phd;
  1444. FILETIME CreateTime, ExitTime, KernelTime, UserTime;
  1445. SYSTEMTIME SysTime;
  1446. #if defined(UNDER_CE)
  1447. phd = GetCurrentThread();
  1448. if( GetThreadTimes(phd, &CreateTime, &ExitTime, &KernelTime, &UserTime)
  1449. != TRUE)
  1450. #else
  1451. phd = GetCurrentProcess();
  1452. if( GetProcessTimes(phd, &CreateTime, &ExitTime, &KernelTime, &UserTime)
  1453. != TRUE)
  1454. #endif
  1455. {ru->ru_stime.tv_sec = 0;
  1456. ru->ru_stime.tv_usec = 0;
  1457. ru->ru_utime.tv_sec = 0;
  1458. ru->ru_utime.tv_usec = 0;
  1459. }
  1460. else
  1461. {
  1462. (void) FileTimeToSystemTime(&KernelTime, &SysTime);
  1463. /*
  1464. * fprintf(stdout,
  1465. * "System sec=%d, msec=%d\n", SysTime.wSecond, SysTime.wMilliseconds);
  1466. */
  1467. ru->ru_stime.tv_sec = SysTime.wSecond;
  1468. ru->ru_stime.tv_usec = SysTime.wMilliseconds * 1000;
  1469. (void) FileTimeToSystemTime(&UserTime, &SysTime);
  1470. /*
  1471. * fprintf(stdout,
  1472. * " User sec=%d, msec=%d\n", SysTime.wSecond, SysTime.wMilliseconds);
  1473. */
  1474. ru->ru_utime.tv_sec = SysTime.wSecond;
  1475. ru->ru_utime.tv_usec = SysTime.wMilliseconds * 1000;
  1476. }
  1477. #else /* __NTVIS__ */
  1478. struct tms buftime;
  1479. times(&buftime);
  1480. /* Assumption: HZ <= 2147 (LONG_MAX/1000000) */
  1481. /* info : in lynxOS HZ is called TICKSPERSEC (<conf.h>) */
  1482. ru->ru_stime.tv_sec = buftime.tms_stime / HZ;
  1483. ru->ru_stime.tv_usec = ((buftime.tms_stime % HZ) * 1000000) / HZ;
  1484. ru->ru_utime.tv_sec = buftime.tms_utime / HZ;
  1485. ru->ru_utime.tv_usec = ((buftime.tms_utime % HZ) * 1000000) / HZ;
  1486. #endif /* __NTVIS__ */
  1487. return(0);
  1488. } /* static getrusage */
  1489. #endif /* __sun__ */
  1490. #endif /* __lynx__ */
  1491. #endif /* __osf__ */
  1492. #endif /* SYSV */
  1493. /*--------------------------------------------------------------------------*/
  1494. #if defined(SYSV)
  1495. #if defined(__hpux__) || defined(_AIX) || defined(__sun__)
  1496. /* gettimeofday defined in the system lib */
  1497. #else
  1498. # if defined (__osf__) ||defined (__lynx__)
  1499. /* gettimeofday defined in the system lib */
  1500. #else
  1501. /*ARGSUSED*/
  1502. static
  1503. gettimeofday(tp, zp)
  1504. struct timeval *tp;
  1505. struct timezone *zp;
  1506. {
  1507. #if defined(__NTVIS__)
  1508. #if defined(UNDER_CE)
  1509. SYSTEMTIME SystemTime;
  1510. GetLocalTime(&SystemTime);
  1511. tp->tv_sec = SystemTime.wSecond;
  1512. tp->tv_usec = SystemTime.wMilliseconds * 1000;
  1513. #else
  1514. struct _timeb timeptr;
  1515. _ftime(&timeptr);
  1516. tp->tv_sec = timeptr.time;
  1517. tp->tv_usec = timeptr.millitm * 1000;
  1518. #endif
  1519. #else /* all cases */
  1520. tp->tv_sec = time(0);
  1521. tp->tv_usec = 0;
  1522. #endif /* __NTVIS__ */
  1523. return(1);
  1524. } /* static gettimeofday */
  1525. #endif /*__osf__ || __lynx__ */
  1526. #endif /* __hpux__ || _AIX || __sun__ || __osf__*/
  1527. #endif /* SYSV */
  1528. /*--------------------------------------------------------------------------*/
  1529. /*
  1530. * P R E P _ T I M E R
  1531. */
  1532. void
  1533. prep_timer(void)
  1534. {
  1535. gettimeofday(&time0, (struct timezone *)0);
  1536. getrusage(RUSAGE_SELF, &ru0);
  1537. }
  1538. /*--------------------------------------------------------------------------*/
  1539. /*
  1540. * R E A D _ T I M E R
  1541. *
  1542. */
  1543. void
  1544. read_timer(void)
  1545. {
  1546. getrusage(RUSAGE_SELF, &ru1);
  1547. gettimeofday(&time1, (struct timezone *)0);
  1548. }
  1549. /*--------------------------------------------------------------------------*/
  1550. /* Print the process usage calculated from timers values extracted
  1551. * before and after the transfer execution.
  1552. */
  1553. void
  1554. prusage(
  1555. struct rusage *r0, struct rusage *r1,
  1556. struct timeval *t0, struct timeval *t1
  1557. )
  1558. {
  1559. struct timeval tdiff;
  1560. int t, ms;
  1561. register char *cp;
  1562. double cput = calc_cpu_time(r0, r1);
  1563. double realt = calc_real_time(t0, t1);
  1564. /* t == total user delta time + total system delta time */
  1565. if (debug)
  1566. {
  1567. printf("timers : end startup\n");
  1568. printf("user (sec) : %9ld %9ld\n",r1->ru_utime.tv_sec,
  1569. r0->ru_utime.tv_sec);
  1570. printf("user (usec): %9ld %9ld\n",r1->ru_utime.tv_usec,
  1571. r0->ru_utime.tv_usec);
  1572. printf("sys (sec) : %9ld %9ld\n",r1->ru_stime.tv_sec,
  1573. r0->ru_stime.tv_sec);
  1574. printf("sys (usec): %9ld %9ld\n",r1->ru_stime.tv_usec,
  1575. r0->ru_stime.tv_usec);
  1576. printf("time (sec) : %9ld %9ld\n",t1->tv_sec,t0->tv_sec);
  1577. printf("time (usec): %9ld %9ld\n",t1->tv_usec,t0->tv_usec);
  1578. }
  1579. /* for the AIX debug, most counters are outside a good range
  1580. printf(" r0 r1\n");
  1581. printf("ru_ixrss %20ld %20ld \n", r0->ru_ixrss ,r1->ru_ixrss );
  1582. printf("ru_idrss %20ld %20ld \n", r0->ru_idrss ,r1->ru_idrss );
  1583. printf("ru_isrss %20ld %20ld \n", r0->ru_isrss ,r1->ru_isrss );
  1584. printf("ru_minflt %20ld %20ld \n", r0->ru_minflt ,r1->ru_minflt );
  1585. printf("ru_majflt %20ld %20ld \n", r0->ru_majflt ,r1->ru_majflt );
  1586. printf("ru_nswap %20ld %20ld \n", r0->ru_nswap ,r1->ru_nswap );
  1587. printf("ru_inblock %20ld %20ld \n", r0->ru_inblock ,r1->ru_inblock );
  1588. printf("ru_oublock %20ld %20ld \n", r0->ru_oublock ,r1->ru_oublock );
  1589. printf("ru_msgsnd %20ld %20ld \n", r0->ru_msgsnd ,r1->ru_msgsnd );
  1590. printf("ru_msgrcv %20ld %20ld \n", r0->ru_msgrcv ,r1->ru_msgrcv );
  1591. printf("ru_nsignals %20ld %20ld \n", r0->ru_nsignals ,r1->ru_nsignals);
  1592. printf("ru_nvcsw %20ld %20ld \n", r0->ru_nvcsw ,r1->ru_nvcsw );
  1593. printf("ru_nivcsw %20ld %20ld \n", r0->ru_nivcsw ,r1->ru_nivcsw );
  1594. */
  1595. /* cpu time in mseconds */
  1596. t = (int)(cput / 1000.0);
  1597. /* ms == value of the internal clock at the end of the xfer */
  1598. /* also called real time. */
  1599. /* real time in mseconds */
  1600. ms = (int)(realt / 1000.0);
  1601. /* The display is based on variables provided by the function getrusage
  1602. Info located in : /usr/include/sys/resource.h
  1603. */
  1604. #if defined(SYSV)
  1605. #if defined(_AIX)
  1606. /* with AIX cernsp most counters are wrong
  1607. * cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
  1608. */
  1609. cp = "%Uuser %Ssys %Ereal %P\0";
  1610. #else
  1611. #if defined(__osf__)
  1612. cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
  1613. #else
  1614. #if defined(sgi) /* IRIX 3.3 will show 0 for %M,%F,%R,%C */
  1615. cp = "%Uuser %Ssys %Ereal %P %Mmaxrss %F+%Rpf %Ccsw\0";
  1616. #else
  1617. #if defined(__Lynx__)
  1618. cp = "%Uuser %Ssys %Ereal %P\0";
  1619. #else
  1620. cp = "%Uuser %Ssys %Ereal %P\0"; /* all SYSV except those mentionned */
  1621. #endif /*__lynx__ */
  1622. #endif /* sgi */
  1623. #endif /*__osf__ */
  1624. #endif /* _AIX */
  1625. #else /* BSD system */
  1626. cp = "%Uuser %Ssys %Ereal %P %Xi+%Dd %Mmaxrss %F+%Rpf %Ccsw\0";
  1627. #endif /* SYSV */
  1628. for (; *cp; cp++) {
  1629. if (*cp != '%') {
  1630. putc(*cp, stderr);
  1631. /* *outp++ = *cp; */
  1632. } else if (cp[1])
  1633. switch(*++cp) {
  1634. case 'U':
  1635. tvsub(&tdiff, &r1->ru_utime, &r0->ru_utime);
  1636. fprintf(stderr,"%ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
  1637. break;
  1638. case 'S':
  1639. tvsub(&tdiff, &r1->ru_stime, &r0->ru_stime);
  1640. fprintf(stderr,"%ld.%06ld", (long)tdiff.tv_sec, (long)tdiff.tv_usec);
  1641. break;
  1642. case 'E':
  1643. psecs( ms / 1000);
  1644. break;
  1645. case 'P':
  1646. fprintf(stderr,"%.1f%%", (cput*100.0 / (realt ? realt : 1.0)) );
  1647. break;
  1648. #if !defined(SYSV) || defined(__osf__) || defined(_AIX)
  1649. case 'W':
  1650. {
  1651. int i = r1->ru_nswap - r0->ru_nswap;
  1652. fprintf(stderr,"%d", i);
  1653. break;
  1654. }
  1655. case 'X':
  1656. fprintf(stderr,"%ld", t == 0 ? 0 : (r1->ru_ixrss-r0->ru_ixrss)/t);
  1657. break;
  1658. case 'D':
  1659. fprintf(stderr,"%ld", t == 0 ? 0 :
  1660. (r1->ru_idrss+r1->ru_isrss - (r0->ru_idrss+r0->ru_isrss))/t);
  1661. break;
  1662. case 'K':
  1663. fprintf(stderr,"%ld", t == 0 ? 0 :
  1664. ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
  1665. (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
  1666. break;
  1667. case 'M':
  1668. fprintf(stderr,"%ld", r1->ru_maxrss/2);
  1669. break;
  1670. case 'F':
  1671. fprintf(stderr,"%ld", r1->ru_majflt-r0->ru_majflt);
  1672. break;
  1673. case 'R':
  1674. fprintf(stderr,"%ld", r1->ru_minflt-r0->ru_minflt);
  1675. break;
  1676. case 'I':
  1677. fprintf(stderr,"%ld", r1->ru_inblock-r0->ru_inblock);
  1678. break;
  1679. case 'O':
  1680. fprintf(stderr,"%ld", r1->ru_oublock-r0->ru_oublock);
  1681. break;
  1682. case 'C':
  1683. fprintf(stderr,"%ld+%ld",
  1684. r1->ru_nvcsw-r0->ru_nvcsw, r1->ru_nivcsw-r0->ru_nivcsw);
  1685. break;
  1686. #endif /* !SYSV || __osf__ */
  1687. default:
  1688. putc(*cp, stderr);
  1689. break;
  1690. } /* switch */
  1691. } /* for */
  1692. }
  1693. /*--------------------------------------------------------------------------*/
  1694. /* add 2 times structure and move usec bigger than 1000000 to sec */
  1695. void
  1696. tvadd(tsum, t0, t1)
  1697. struct timeval *tsum, *t0, *t1;
  1698. {
  1699. tsum->tv_sec = t0->tv_sec + t1->tv_sec;
  1700. tsum->tv_usec = t0->tv_usec + t1->tv_usec;
  1701. if (tsum->tv_usec > 1000000) tsum->tv_sec++, tsum->tv_usec -= 1000000;
  1702. }
  1703. /*--------------------------------------------------------------------------*/
  1704. /* substract 2 time structure (t1 > t0) */
  1705. void
  1706. tvsub(tdiff, t1, t0)
  1707. struct timeval *tdiff, *t1, *t0;
  1708. {
  1709. tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
  1710. tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
  1711. if (tdiff->tv_usec < 0) tdiff->tv_sec--, tdiff->tv_usec += 1000000;
  1712. }
  1713. /*--------------------------------------------------------------------------*/
  1714. void
  1715. psecs(int l)
  1716. {
  1717. int i = (int)l/3600;
  1718. if (i) {
  1719. /* hours:min:sec */
  1720. fprintf(stderr,"%d:", i);
  1721. i = (int)l % 3600;
  1722. fprintf(stderr,"%.2d:%.2d", i/60, i%60);
  1723. } else {
  1724. /* min:sec */
  1725. i = (int)l;
  1726. fprintf(stderr,"%d:%.2d", i/60, i%60);
  1727. }
  1728. }
  1729. /*--------------------------------------------------------------------------*/
  1730. /* N R E A D */
  1731. int
  1732. Nread(SOCKET s, void* bufp, int count )
  1733. {
  1734. #if defined(__lynx__) || defined(__svr4__) || defined(_AIX) || defined(__NTVIS__) || defined(__FreeBSD__)
  1735. struct sockaddr from;
  1736. #else
  1737. struct sockaddr_in from;
  1738. #endif
  1739. int len = sizeof(from);
  1740. register int cnt;
  1741. if (timeout > 0) {
  1742. fd_set readfds, exceptfds;
  1743. struct timeval tv_timeout;
  1744. int n;
  1745. tv_timeout.tv_sec = timeout/1000;
  1746. tv_timeout.tv_usec = (timeout%1000)*1000;
  1747. FD_ZERO(&readfds);
  1748. FD_ZERO(&exceptfds);
  1749. FD_SET(s, &readfds);
  1750. FD_SET(s, &exceptfds);
  1751. n = select( s+1, &readfds, NULL, &exceptfds, &tv_timeout );
  1752. if (n == SOCKET_ERROR) {
  1753. sockets_err("select read");
  1754. } else if (n == 0) {
  1755. return (0);
  1756. }
  1757. }
  1758. if( udp )
  1759. {cnt = recvfrom( s, bufp, count, 0, SOCKADDR_CAST &from, &len );
  1760. numCalls++;
  1761. }
  1762. else
  1763. {if( b_flag ) cnt = mread( s, bufp, count ); /* fill bufp */
  1764. else
  1765. {
  1766. #if defined(__NTVIS__)
  1767. cnt = recv( s, bufp, count, 0 );
  1768. #else
  1769. cnt = read( s, bufp, count );
  1770. #endif /* __NTVIS__ */
  1771. numCalls++;
  1772. }
  1773. if (touchdata && cnt > 0)
  1774. {register int c = cnt, sum = 0;
  1775. register char *b = bufp;
  1776. while (c--) sum += *b++;
  1777. }
  1778. }
  1779. /* rchrch printf (" numcall %d read buffer %d bytes \n",numCalls,cnt); */
  1780. return(cnt);
  1781. }
  1782. /*--------------------------------------------------------------------------*/
  1783. /* N W R I T E */
  1784. int
  1785. Nwrite( int s, void* bufp, int count )
  1786. {
  1787. register int cnt;
  1788. if( udp )
  1789. {
  1790. again:
  1791. cnt = sendto( s, bufp, count, 0, SOCKADDR_CAST &sinhim,
  1792. sizeof(sinhim) );
  1793. numCalls++;
  1794. #if defined(__NTVIS__)
  1795. if( cnt<0 && WSAENOBUFS == WSAGetLastError())
  1796. #else
  1797. if( cnt<0 && errno == ENOBUFS )
  1798. #endif /* __NTVIS__ */
  1799. { delay(18000); errno = 0; goto again; }
  1800. } else /* if (udp) */
  1801. {
  1802. #if defined(__NTVIS__)
  1803. cnt = send( s, bufp, count, 0 );
  1804. numCalls++;
  1805. #else
  1806. cnt = write( s, bufp, count );
  1807. numCalls++;
  1808. #endif /* __NTVIS__ */
  1809. }
  1810. return(cnt);
  1811. }
  1812. /*--------------------------------------------------------------------------*/
  1813. void
  1814. delay(us)
  1815. int us;
  1816. {
  1817. struct timeval tv;
  1818. tv.tv_sec = 0;
  1819. tv.tv_usec = (time_t)us;
  1820. #if defined(__hpux__)
  1821. select(1, 0, 0, 0, &tv);
  1822. #else
  1823. select(1, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
  1824. #endif
  1825. }
  1826. /*--------------------------------------------------------------------------*/
  1827. /* M R E A D
  1828. *
  1829. * This function performs the function of a read(II) but will
  1830. * call read(II) multiple times in order to get the requested
  1831. * number of characters. This can be necessary because
  1832. * network connections don't deliver data with the same
  1833. * grouping as it is written with. Written by Robert S. Miles, BRL.
  1834. */
  1835. int
  1836. mread(int s, char* bufp, unsigned n)
  1837. {
  1838. register unsigned count = 0;
  1839. register int nread;
  1840. do
  1841. {
  1842. #if defined(__NTVIS__)
  1843. nread = recv(s, bufp, n-count, 0);
  1844. #else
  1845. nread = read(s, bufp, n-count);
  1846. #endif /* __NTVIS__ */
  1847. numCalls++;
  1848. if(nread < 0) {perror("ttcp_mread"); return(-1); }
  1849. if(nread == 0) return((int)count);
  1850. count += (unsigned)nread;
  1851. bufp += nread;
  1852. }while(count < n);
  1853. return((int)count);
  1854. }
  1855. /*--------------------------------------------------------------------------*/
  1856. void
  1857. open_log()
  1858. {static long sysTicks;
  1859. #if defined(UNDER_CE)
  1860. SYSTEMTIME SystemTime;
  1861. #endif
  1862. sprintf(logfile,"%s_%s",logfile_head,trans?"t":"r");
  1863. fprintf(stderr,"open the log file >%s<\n",logfile);
  1864. if ((fplog = fopen(logfile,"r")) == NULL)
  1865. {if ((fplog = fopen(logfile,"a+")) == NULL)
  1866. {fprintf(stderr,"Failure : creation of the file >%s< \n",logfile );
  1867. exit(1);
  1868. }
  1869. else
  1870. {fprintf(fplog," creation date : ");
  1871. /* get date */
  1872. #if defined(UNDER_CE)
  1873. GetLocalTime(&SystemTime);
  1874. sprintf(fplog,"%02d-%02d-%02d %02d:%02d\n", SystemTime.wDay, month[SystemTime.wMonth - 1],
  1875. SystemTime.wYear, SystemTime.wHour, SystemTime.wMinute);
  1876. #else
  1877. time(&sysTicks);
  1878. tms = localtime(&sysTicks);
  1879. fprintf(fplog,"%02d-%02d-%02d %02d:%02d\n",
  1880. tms->tm_mday, tms->tm_mon, tms->tm_year,tms->tm_hour, tms->tm_min);
  1881. #endif
  1882. /* An other version will produce : Mon Aug 4 16:32:16 1997
  1883. * long lDxcomsTicks; char *pDateTime;
  1884. * time(&lDxcomsTicks);
  1885. * pDateTime = ctime(&lDxcomsTicks); *(pDateTime+24) = '\0';
  1886. * fprintf(fplog," ttcp called : %s", pDateTime);
  1887. */
  1888. fprintf(fplog,"format\n");
  1889. fprintf(fplog,",buflen, nbuf(byte), bufalign(byte), bufoffset(byte)");
  1890. fprintf(fplog,", port, sockbufsize(byte), UserTime(sec), SysTime(sec)\n");
  1891. fprintf(fplog,", CPUusage(%%), Validity, nbytes(byte), realt(sec)");
  1892. fprintf(fplog,", rate(MB/sec), I/O call, hours*3600+min*60+sec\n\n");
  1893. /* day-month-year, hour:minute\n\n"); */
  1894. }
  1895. } /* file already exist */
  1896. else
  1897. {fclose (fplog);
  1898. if ((fplog = fopen(logfile,"a+")) == NULL)
  1899. {fprintf(stderr,"Failure : access of the file >%s< \n",logfile );
  1900. exit(1);
  1901. }
  1902. }
  1903. }
  1904. /*--------------------------------------------------------------------------*/
  1905. void
  1906. close_log()
  1907. {
  1908. #if defined(UNDER_CE)
  1909. SYSTEMTIME SystemTime;
  1910. GetLocalTime(&SystemTime);
  1911. fprintf(fplog," %d\n", SystemTime.wHour * 3600 + SystemTime.wMinute * 60 + SystemTime.wSecond);
  1912. #else
  1913. static long sysTicks;
  1914. time(&sysTicks);
  1915. tms = localtime(&sysTicks);
  1916. fprintf(fplog," %d\n",((tms->tm_hour)*3600 + (tms->tm_min)*60 + tms->tm_sec));
  1917. #endif
  1918. fclose(fplog);
  1919. fflush(fplog);
  1920. }
  1921. /*--------------------------------------------------------------------------*/
  1922. /* routine emulating UNIX function under NT */
  1923. #if defined(__NTVIS__)
  1924. /*---------------------------------------------------------------------------*/
  1925. static void
  1926. error(char *pch)
  1927. {
  1928. if (!opterr) {
  1929. return; // without printing
  1930. }
  1931. fprintf(stderr, "%s: %s: %c\n",
  1932. (NULL != progname) ? progname : "getopt", pch, optopt);
  1933. }
  1934. /*---------------------------------------------------------------------------*/
  1935. int
  1936. getopt(int argc, char **argv, char *ostr)
  1937. {
  1938. static char *place = EMSG; /* option letter processing */
  1939. register char *oli; /* option letter list index */
  1940. if (!*place) {
  1941. // update scanning pointer
  1942. if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
  1943. place = EMSG;
  1944. return -1;
  1945. }
  1946. // place now points to the first char past the initial '-'
  1947. if (place[0] == '-') {
  1948. // found "--"
  1949. // Was the word just a '--'?
  1950. if (place[1] == '\0')
  1951. ++optind; // yes, so consume the word
  1952. // otherwise, the '--' was the start of a longer word,
  1953. // so do not consume it.
  1954. place = EMSG;
  1955. return -1;
  1956. }
  1957. }
  1958. /* option letter okay? */
  1959. if ((optopt = (int)*place++) == (int)':'
  1960. || !(oli = strchr(ostr, optopt))) {
  1961. if (!*place) {
  1962. ++optind;
  1963. }
  1964. error("illegal option");
  1965. return BADCH;
  1966. }
  1967. if (*++oli != ':') {
  1968. /* don't need argument */
  1969. optarg = NULL;
  1970. if (!*place)
  1971. ++optind;
  1972. } else {
  1973. /* need an argument */
  1974. if (*place) {
  1975. optarg = place; /* no white space */
  1976. } else if (argc <= ++optind) {
  1977. /* no arg */
  1978. place = EMSG;
  1979. error("option requires an argument");
  1980. return BADCH;
  1981. } else {
  1982. optarg = argv[optind]; /* white space */
  1983. }
  1984. place = EMSG;
  1985. ++optind;
  1986. }
  1987. return optopt; // return option letter
  1988. }
  1989. void
  1990. usleep(unsigned int microseconds)
  1991. {
  1992. Sleep(microseconds/1000);
  1993. }
  1994. #endif /* __NTVIS__ */
  1995. /*--------------------------------------------------------------------------*/
  1996. /*--------------------------------------------------------------------------*/
  1997. void
  1998. do_help()
  1999. {
  2000. char More_help[] =
  2001. " Details about the reply: \n"
  2002. " Example: \n"
  2003. " ttcp-t: buflen=8192, nbuf=100, align=16384/0, port=5010\n"
  2004. " ttcp-t: File-Descriptor 0x4 Opened\n"
  2005. " # tcp sender -> <host> #\n"
  2006. " ttcp-t: 819200 bytes in 1.152557 real seconds = 694.109 KB/sec +++\n"
  2007. " ttcp-t: 100 I/O calls, 11.526 msec(real)/call, 0.213 msec(cpu)/call\n"
  2008. " ttcp-t: 0.001914user 0.019388sys 0:01real 1% 9i+58d 190maxrss 1+2pf 177+180csw\n"
  2009. " ttcp-t: buffer address 0x28000\n"
  2010. " ttcp-t: File-Descriptor fd 0x4 Closed\n"
  2011. " ttcp done.\n\n"
  2012. "cpu seconds == (sec) elapse ru_utime + elapse ru_stime.\n"
  2013. " ru_utime == The total amount of time running in user mode.\n"
  2014. " ru_stime == The total amount of time spent in the system.\n"
  2015. " executing on behalf of the process.\n"
  2016. "real seconds == elapse time calculated by the system timer (date).\n"
  2017. "I/O calls == I/O call to the driver.\n"
  2018. "msec/call == average elapse time (Real seconds) between each I/O.\n"
  2019. "calls/sec == invert of msec/call.\n"
  2020. "user == (sec.msec) elaspe ru_utime.\n"
  2021. "sys == (sec.msec) elapse ru_stime.\n"
  2022. "real == (min:sec) CPU seconds.\n"
  2023. "%% == Real seconds / CPU seconds.\n"
  2024. "(ru_ixrss)i+(ru_idrss)d\n"
  2025. " ru_ixrss == An integral value indicating the amount of memory \n"
  2026. " used by the text segment that was also shared among\n"
  2027. " other processes. This value is expressed in units of\n"
  2028. " kilobytes * seconds-of-execution and is calculated \n"
  2029. " by adding the number of shared memory pages in use \n"
  2030. " each time the internal system clock ticks, and then\n"
  2031. " averaging over one-second intervals.\n"
  2032. " ru_idrss == An integral value of the amount of unshared memory \n"
  2033. " in the data segment of a process (expressed in \n"
  2034. " units of kilobytes * seconds-of-execution).\n";
  2035. char More_help1[] =
  2036. " (ru_maxrss/2)maxrss.\n"
  2037. " ru_maxrss == The maximum size, in kilobytes, of the used\n"
  2038. " resident set size. \n"
  2039. " (ru_majflt)+(ru_minflt)pf : Page fault\n"
  2040. " ru_majflt == The number of page faults serviced that required\n"
  2041. " I/O activity.\n"
  2042. " ru_minflt == The number of page faults serviced without any\n"
  2043. " I/O activity. In this case, I/O activity is \n"
  2044. " avoided by reclaiming a page frame from the list \n"
  2045. " of pages awaiting reallocation. \n"
  2046. "(ru_nvcsw)+(ru_nivcsw)csw : context switch\n"
  2047. " ru_nvcsw == The number of times a context switch resulted \n"
  2048. " because a process voluntarily gave up the \n"
  2049. " processor before its time slice was completed. \n"
  2050. " This usually occurs while the process waits \n"
  2051. " for availability of a resource.\n"
  2052. " ru_nivcsw == The number of times a context switch resulted \n"
  2053. " because a higher priority process ran or because\n"
  2054. " the current process exceeded its time slice.\n\n";
  2055. char More_help2[] =
  2056. "log file format :\n"
  2057. " buflen, nbuf(byte), bufalign(byte), bufoffset(byte)\n"
  2058. " port, sockbufsize(byte), UserTime(sec), SysTime(sec), CPUusage(%)\n"
  2059. " nbytes(byte), realt(sec), rate(MB/sec), I/O call,\n"
  2060. " hours*3600+min*60+sec\n\n";
  2061. fprintf(stderr,More_help);
  2062. fprintf(stderr,More_help1);
  2063. fprintf(stderr,More_help2);
  2064. }
  2065. /*---------------------------------------------------------------------------*/
  2066. void
  2067. do_Usage()
  2068. {
  2069. char Usage[] =
  2070. " Usage: ttcp -t [-options] host [ < in ] ttcp -r [-options > out]\n"
  2071. "Example: ttcp -t -s -v -n100 host ttcp -r -s -v -n100\n"
  2072. "Common options:\n"
  2073. " -V prints version number and date of last modification\n"
  2074. " -L create and append all results to a file named ttcp_log\n"
  2075. " -h more help\n"
  2076. " -l ## length of bufs read from or written to network (default 8192,\n"
  2077. " max 65535)\n"
  2078. " -u use UDP instead of TCP\n"
  2079. " -p ## port number to send to or listen at (default 5010)\n"
  2080. #if defined(__linux__)
  2081. " -P ## link-layer priority (default 0)\n"
  2082. #endif
  2083. " -s (ttcp -t) : source a pattern to network\n"
  2084. " (ttcp -r) : sink (discard) all data from network\n"
  2085. " -A ## align the start of buffers to this modulus (default 16384)\n"
  2086. " -O ## start buffers at this offset from the modulus (default 0)\n"
  2087. " -v verbose: print more statistics\n"
  2088. " -d set SO_DEBUG socket option\n"
  2089. " -b ## set socket buffer size (if supported)\n"
  2090. " -f X format for rate: b,B = bits, bytes k,K = kilo{bits,bytes};\n"
  2091. " m,M = mega{bits,bytes}; g,G = giga{bits,bytes}\n"
  2092. " -w ## set timeout value (in milliseconds) to exit if no receive data or tcp connect\n"
  2093. "Options specific to (ttcp -t) :\n"
  2094. " -n ## number of source bufs written to network (default 2048)\n"
  2095. " -x use random data in tcp/udp frames (-I provides seed)\n"
  2096. " -D don't buffer TCP writes (sets TCP_NODELAY socket option)\n"
  2097. " -H print hash marks to indicate progress, one per buffer\n"
  2098. " -I init/seed value for RNG when sending random size bufs (default 1)\n"
  2099. " -N ## number of source bufs per burst, i.e between sleeps (default 1)\n"
  2100. " -R ## send random size buffers with minimum size specified, max size\n"
  2101. " is value of -l option\n"
  2102. " -S ## millisecs between bursts (only used for udp, 10ms resolution)\n"
  2103. "Options specific to (ttcp -r) :\n"
  2104. " -B for -s, only output full blocks as specified by -l (for TAR)\n"
  2105. " -T \"touch\": access each byte as it's read\n"
  2106. " -i report information on out of order sequence numbers\n"
  2107. #if defined(DEBUG)
  2108. "Options for debug:\n"
  2109. " --nostart do not send UDP start packets\n"
  2110. " --noend do not send UDP end packets\n"
  2111. " --nodata do not send UDP data packets\n"
  2112. " --debug print extra debug outputs\n"
  2113. #endif /* DEBUG */
  2114. ;
  2115. fprintf(stderr,Usage);
  2116. }
  2117. /* Define automatic Emacs variables for consistent code formatting */
  2118. /* Local Variables: */
  2119. /* c-basic-offset:2 */
  2120. /* indent-tabs-mode:nil */
  2121. /* End: */