2 * Copyright (c) 2004 Robert N. M. Watson
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
46 fprintf(stderr, "netblast [ip] [port] [payloadsize] [duration]\n");
50 static int global_stop_flag;
53 signal_handler(int signum __unused)
60 * Loop that blasts packets: begin by recording time information, resetting
61 * stats. Set the interval timer for when we want to wake up. Then go.
62 * SIGALRM will set a flag indicating it's time to stop. Note that there's
63 * some overhead to the signal and timer setup, so the smaller the duration,
64 * the higher the relative overhead.
67 blast_loop(int s, long duration, u_char *packet, u_int packet_len)
69 struct timespec starttime, tmptime;
72 int send_errors, send_calls;
74 if (signal(SIGALRM, signal_handler) == SIG_ERR) {
79 if (clock_getres(CLOCK_REALTIME, &tmptime) == -1) {
80 perror("clock_getres");
84 if (clock_gettime(CLOCK_REALTIME, &starttime) == -1) {
85 perror("clock_gettime");
89 it.it_interval.tv_sec = 0;
90 it.it_interval.tv_usec = 0;
91 it.it_value.tv_sec = duration;
92 it.it_value.tv_usec = 0;
94 if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
99 send_errors = send_calls = 0;
101 while (global_stop_flag == 0) {
103 * We maintain and, if there's room, send a counter. Note
104 * that even if the error is purely local, we still increment
105 * the counter, so missing sequence numbers on the receive
106 * side should not be assumed to be packets lost in transit.
107 * For example, if the UDP socket gets back an ICMP from a
108 * previous send, the error will turn up the current send
109 * operation, causing the current sequence number also to be
112 * XXXRW: Note alignment assumption.
114 if (packet_len >= 4) {
115 *((u_int32_t *)packet) = htonl(counter);
118 if (send(s, packet, packet_len, 0) < 0)
123 if (clock_gettime(CLOCK_REALTIME, &tmptime) == -1) {
124 perror("clock_gettime");
129 printf("start: %zd.%09lu\n", starttime.tv_sec,
131 printf("finish: %zd.%09lu\n", tmptime.tv_sec,
133 printf("send calls: %d\n", send_calls);
134 printf("send errors: %d\n", send_errors);
135 printf("approx send rate: %ld\n", (send_calls - send_errors) /
137 printf("approx error rate: %d\n", (send_errors / send_calls));
143 main(int argc, char *argv[])
145 long payloadsize, port, duration;
146 struct sockaddr_in sin;
147 char *dummy, *packet;
153 bzero(&sin, sizeof(sin));
154 sin.sin_len = sizeof(sin);
155 sin.sin_family = AF_INET;
156 if (inet_aton(argv[1], &sin.sin_addr) == 0) {
161 port = strtoul(argv[2], &dummy, 10);
162 if (port < 1 || port > 65535 || *dummy != '\0')
164 sin.sin_port = htons(port);
166 payloadsize = strtoul(argv[3], &dummy, 10);
167 if (payloadsize < 0 || *dummy != '\0')
169 if (payloadsize > 32768) {
170 fprintf(stderr, "payloadsize > 32768\n");
174 duration = strtoul(argv[4], &dummy, 10);
175 if (duration < 0 || *dummy != '\0')
178 packet = malloc(payloadsize);
179 if (packet == NULL) {
183 bzero(packet, payloadsize);
185 s = socket(PF_INET, SOCK_DGRAM, 0);
191 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
196 return (blast_loop(s, duration, packet, payloadsize));