1 |
+ |
/* $MidnightBSD$ */ |
2 |
|
/*- |
3 |
|
* Copyright (c) 2004 Robert N. M. Watson |
4 |
|
* All rights reserved. |
24 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
* SUCH DAMAGE. |
26 |
|
* |
27 |
< |
* $FreeBSD: src/tools/tools/netrate/netblast/netblast.c,v 1.2 2005/02/12 20:03:30 rwatson Exp $ |
27 |
> |
* $FreeBSD: stable/10/tools/tools/netrate/netblast/netblast.c 227345 2011-11-08 17:23:43Z cognet $ |
28 |
|
*/ |
29 |
|
|
30 |
+ |
#include <sys/endian.h> |
31 |
|
#include <sys/types.h> |
32 |
|
#include <sys/socket.h> |
33 |
|
#include <sys/time.h> |
34 |
|
|
35 |
|
#include <netinet/in.h> |
36 |
+ |
#include <netdb.h> /* getaddrinfo */ |
37 |
|
|
35 |
– |
#include <arpa/inet.h> |
36 |
– |
|
38 |
|
#include <signal.h> |
39 |
|
#include <stdio.h> |
40 |
|
#include <stdlib.h> |
41 |
|
#include <string.h> |
42 |
+ |
#include <unistd.h> /* close */ |
43 |
|
|
44 |
|
static void |
45 |
|
usage(void) |
110 |
|
* previous send, the error will turn up the current send |
111 |
|
* operation, causing the current sequence number also to be |
112 |
|
* skipped. |
111 |
– |
* |
112 |
– |
* XXXRW: Note alignment assumption. |
113 |
|
*/ |
114 |
|
if (packet_len >= 4) { |
115 |
< |
*((u_int32_t *)packet) = htonl(counter); |
115 |
> |
be32enc(packet, counter); |
116 |
|
counter++; |
117 |
|
} |
118 |
|
if (send(s, packet, packet_len, 0) < 0) |
126 |
|
} |
127 |
|
|
128 |
|
printf("\n"); |
129 |
< |
printf("start: %d.%09lu\n", starttime.tv_sec, |
129 |
> |
printf("start: %zd.%09lu\n", starttime.tv_sec, |
130 |
|
starttime.tv_nsec); |
131 |
< |
printf("finish: %d.%09lu\n", tmptime.tv_sec, |
131 |
> |
printf("finish: %zd.%09lu\n", tmptime.tv_sec, |
132 |
|
tmptime.tv_nsec); |
133 |
|
printf("send calls: %d\n", send_calls); |
134 |
|
printf("send errors: %d\n", send_errors); |
142 |
|
int |
143 |
|
main(int argc, char *argv[]) |
144 |
|
{ |
145 |
< |
long payloadsize, port, duration; |
146 |
< |
struct sockaddr_in sin; |
145 |
> |
long payloadsize, duration; |
146 |
> |
struct addrinfo hints, *res, *res0; |
147 |
|
char *dummy, *packet; |
148 |
< |
int s; |
148 |
> |
int port, s, error; |
149 |
> |
const char *cause = NULL; |
150 |
|
|
151 |
|
if (argc != 5) |
152 |
|
usage(); |
153 |
|
|
154 |
< |
bzero(&sin, sizeof(sin)); |
155 |
< |
sin.sin_len = sizeof(sin); |
156 |
< |
sin.sin_family = AF_INET; |
156 |
< |
if (inet_aton(argv[1], &sin.sin_addr) == 0) { |
157 |
< |
perror(argv[1]); |
158 |
< |
return (-1); |
159 |
< |
} |
154 |
> |
memset(&hints, 0, sizeof(hints)); |
155 |
> |
hints.ai_family = PF_UNSPEC; |
156 |
> |
hints.ai_socktype = SOCK_DGRAM; |
157 |
|
|
158 |
|
port = strtoul(argv[2], &dummy, 10); |
159 |
< |
if (port < 1 || port > 65535 || *dummy != '\0') |
159 |
> |
if (port < 1 || port > 65535 || *dummy != '\0') { |
160 |
> |
fprintf(stderr, "Invalid port number: %s\n", argv[2]); |
161 |
|
usage(); |
162 |
< |
sin.sin_port = htons(port); |
162 |
> |
/*NOTREACHED*/ |
163 |
> |
} |
164 |
|
|
165 |
|
payloadsize = strtoul(argv[3], &dummy, 10); |
166 |
|
if (payloadsize < 0 || *dummy != '\0') |
168 |
|
if (payloadsize > 32768) { |
169 |
|
fprintf(stderr, "payloadsize > 32768\n"); |
170 |
|
return (-1); |
171 |
+ |
/*NOTREACHED*/ |
172 |
|
} |
173 |
|
|
174 |
|
duration = strtoul(argv[4], &dummy, 10); |
175 |
< |
if (duration < 0 || *dummy != '\0') |
175 |
> |
if (duration < 0 || *dummy != '\0') { |
176 |
> |
fprintf(stderr, "Invalid duration time: %s\n", argv[4]); |
177 |
|
usage(); |
178 |
+ |
/*NOTREACHED*/ |
179 |
+ |
} |
180 |
|
|
181 |
|
packet = malloc(payloadsize); |
182 |
|
if (packet == NULL) { |
183 |
|
perror("malloc"); |
184 |
|
return (-1); |
185 |
+ |
/*NOTREACHED*/ |
186 |
|
} |
183 |
– |
bzero(packet, payloadsize); |
187 |
|
|
188 |
< |
s = socket(PF_INET, SOCK_DGRAM, 0); |
189 |
< |
if (s == -1) { |
190 |
< |
perror("socket"); |
188 |
> |
bzero(packet, payloadsize); |
189 |
> |
error = getaddrinfo(argv[1],argv[2], &hints, &res0); |
190 |
> |
if (error) { |
191 |
> |
perror(gai_strerror(error)); |
192 |
|
return (-1); |
193 |
+ |
/*NOTREACHED*/ |
194 |
|
} |
195 |
+ |
s = -1; |
196 |
+ |
for (res = res0; res; res = res->ai_next) { |
197 |
+ |
s = socket(res->ai_family, res->ai_socktype, 0); |
198 |
+ |
if (s < 0) { |
199 |
+ |
cause = "socket"; |
200 |
+ |
continue; |
201 |
+ |
} |
202 |
|
|
203 |
< |
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { |
204 |
< |
perror("connect"); |
203 |
> |
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { |
204 |
> |
cause = "connect"; |
205 |
> |
close(s); |
206 |
> |
s = -1; |
207 |
> |
continue; |
208 |
> |
} |
209 |
> |
|
210 |
> |
break; /* okay we got one */ |
211 |
> |
} |
212 |
> |
if (s < 0) { |
213 |
> |
perror(cause); |
214 |
|
return (-1); |
215 |
+ |
/*NOTREACHED*/ |
216 |
|
} |
217 |
|
|
218 |
+ |
freeaddrinfo(res0); |
219 |
+ |
|
220 |
|
return (blast_loop(s, duration, packet, payloadsize)); |
221 |
+ |
|
222 |
|
} |