2 * Copyright (c) 1993 Winning Strategies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Winning Strategies, Inc.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/usr.sbin/spray/spray.c,v 1.5.2.2 2001/07/30 10:23:00 dd Exp $
31 * $DragonFly: src/usr.sbin/spray/spray.c,v 1.4 2005/03/19 17:43:18 liamfoy Exp $
35 #include <rpcsvc/spray.h>
44 #define SPRAYOVERHEAD 86
47 static void usage(void);
48 static void print_xferstats(unsigned int, int, double);
49 static int getnum(const char *);
52 char spray_buffer
[SPRAYMAX
];
55 struct timeval NO_DEFAULT
= { -1, -1 };
56 struct timeval ONE_WAY
= { 0, 0 };
57 struct timeval TIMEOUT
= { 25, 0 };
60 main(int argc
, char *argv
[])
62 spraycumul host_stats
;
70 double xmit_time
; /* time to receive data */
72 while ((c
= getopt(argc
, argv
, "c:d:l:")) != -1) {
75 count
= getnum(optarg
);
78 delay
= getnum(optarg
);
81 length
= getnum(optarg
);
97 /* Correct packet length. */
98 if (length
> SPRAYMAX
) {
100 } else if (length
< SPRAYOVERHEAD
) {
101 length
= SPRAYOVERHEAD
;
103 /* The RPC portion of the packet is a multiple of 32 bits. */
104 length
-= SPRAYOVERHEAD
- 3;
106 length
+= SPRAYOVERHEAD
;
111 * The default value of count is the number of packets required
112 * to make the total stream size 100000 bytes.
115 count
= 100000 / length
;
118 /* Initialize spray argument */
119 host_array
.sprayarr_len
= length
- SPRAYOVERHEAD
;
120 host_array
.sprayarr_val
= spray_buffer
;
123 /* create connection with server */
124 if ((cl
= clnt_create(*argv
, SPRAYPROG
, SPRAYVERS
, "udp")) == NULL
) {
125 clnt_pcreateerror(getprogname());
130 * For some strange reason, RPC 4.0 sets the default timeout,
131 * thus timeouts specified in clnt_call() are always ignored.
133 * The following (undocumented) hack resets the internal state
134 * of the client handle.
136 clnt_control(cl
, CLSET_TIMEOUT
, (caddr_t
)&NO_DEFAULT
);
139 /* Clear server statistics */
140 if (clnt_call(cl
, SPRAYPROC_CLEAR
, (xdrproc_t
)xdr_void
, NULL
,
141 (xdrproc_t
)xdr_void
, NULL
, TIMEOUT
) != RPC_SUCCESS
) {
142 clnt_perror(cl
, getprogname());
146 /* Spray server with packets */
147 printf ("sending %u packets of lnth %d to %s ...", count
, length
,
151 for (i
= 0; i
< count
; i
++) {
152 clnt_call(cl
, SPRAYPROC_SPRAY
, (xdrproc_t
)xdr_sprayarr
,
153 &host_array
, (xdrproc_t
)xdr_void
, NULL
, ONE_WAY
);
161 /* Collect statistics from server */
162 if (clnt_call(cl
, SPRAYPROC_GET
, (xdrproc_t
)xdr_void
, NULL
,
163 (xdrproc_t
)xdr_spraycumul
, &host_stats
, TIMEOUT
) != RPC_SUCCESS
) {
164 clnt_perror(cl
, getprogname());
168 xmit_time
= host_stats
.clock
.sec
+
169 (host_stats
.clock
.usec
/ 1000000.0);
171 printf ("\n\tin %.2f seconds elapsed time\n", xmit_time
);
174 /* report dropped packets */
175 if (host_stats
.counter
!= count
) {
176 int packets_dropped
= count
- host_stats
.counter
;
178 printf("\t%d packets (%.2f%%) dropped\n",
180 100.0 * packets_dropped
/ count
);
182 printf("\tno packets dropped\n");
186 print_xferstats(count
, length
, xmit_time
);
189 print_xferstats(host_stats
.counter
, length
, xmit_time
);
197 print_xferstats(u_int packets
, int packetlen
, double xfertime
)
200 double pps
; /* packets per second */
201 double bps
; /* bytes per second */
203 datalen
= packets
* packetlen
;
204 pps
= packets
/ xfertime
;
205 bps
= datalen
/ xfertime
;
207 printf("\t%.0f packets/sec, ", pps
);
210 printf ("%.1fK ", bps
/ 1024);
212 printf ("%.0f ", bps
);
214 printf("bytes/sec\n");
218 getnum(const char *arg
)
223 tmp
= strtol(arg
, &ep
, 10);
224 if (*ep
!= NULL
|| tmp
< INT_MIN
|| tmp
> INT_MAX
)
225 errx(1, "invalid value: %s", arg
);
234 "usage: spray [-c count] [-l length] [-d delay] host\n");