netblast.c (225334) | netblast.c (227345) |
---|---|
1/*- 2 * Copyright (c) 2004 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 2004 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/tools/tools/netrate/netblast/netblast.c 225334 2011-09-02 16:40:18Z marius $ | 26 * $FreeBSD: head/tools/tools/netrate/netblast/netblast.c 227345 2011-11-08 17:23:43Z cognet $ |
27 */ 28 29#include <sys/endian.h> 30#include <sys/types.h> 31#include <sys/socket.h> 32#include <sys/time.h> 33 34#include <netinet/in.h> | 27 */ 28 29#include <sys/endian.h> 30#include <sys/types.h> 31#include <sys/socket.h> 32#include <sys/time.h> 33 34#include <netinet/in.h> |
35#include <netdb.h> /* getaddrinfo */ |
|
35 | 36 |
36#include <arpa/inet.h> 37 | |
38#include <signal.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> | 37#include <signal.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> |
41#include <unistd.h> /* close */ |
|
42 43static void 44usage(void) 45{ 46 47 fprintf(stderr, "netblast [ip] [port] [payloadsize] [duration]\n"); 48 exit(-1); 49} --- 86 unchanged lines hidden (view full) --- 136 printf("approx error rate: %d\n", (send_errors / send_calls)); 137 138 return (0); 139} 140 141int 142main(int argc, char *argv[]) 143{ | 42 43static void 44usage(void) 45{ 46 47 fprintf(stderr, "netblast [ip] [port] [payloadsize] [duration]\n"); 48 exit(-1); 49} --- 86 unchanged lines hidden (view full) --- 136 printf("approx error rate: %d\n", (send_errors / send_calls)); 137 138 return (0); 139} 140 141int 142main(int argc, char *argv[]) 143{ |
144 long payloadsize, port, duration; 145 struct sockaddr_in sin; | 144 long payloadsize, duration; 145 struct addrinfo hints, *res, *res0; |
146 char *dummy, *packet; | 146 char *dummy, *packet; |
147 int s; | 147 int port, s, error; 148 const char *cause = NULL; |
148 149 if (argc != 5) 150 usage(); 151 | 149 150 if (argc != 5) 151 usage(); 152 |
152 bzero(&sin, sizeof(sin)); 153 sin.sin_len = sizeof(sin); 154 sin.sin_family = AF_INET; 155 if (inet_aton(argv[1], &sin.sin_addr) == 0) { 156 perror(argv[1]); 157 return (-1); 158 } | 153 memset(&hints, 0, sizeof(hints)); 154 hints.ai_family = PF_UNSPEC; 155 hints.ai_socktype = SOCK_DGRAM; |
159 160 port = strtoul(argv[2], &dummy, 10); | 156 157 port = strtoul(argv[2], &dummy, 10); |
161 if (port < 1 || port > 65535 || *dummy != '\0') | 158 if (port < 1 || port > 65535 || *dummy != '\0') { 159 fprintf(stderr, "Invalid port number: %s\n", argv[2]); |
162 usage(); | 160 usage(); |
163 sin.sin_port = htons(port); | 161 /*NOTREACHED*/ 162 } |
164 165 payloadsize = strtoul(argv[3], &dummy, 10); 166 if (payloadsize < 0 || *dummy != '\0') 167 usage(); 168 if (payloadsize > 32768) { 169 fprintf(stderr, "payloadsize > 32768\n"); 170 return (-1); | 163 164 payloadsize = strtoul(argv[3], &dummy, 10); 165 if (payloadsize < 0 || *dummy != '\0') 166 usage(); 167 if (payloadsize > 32768) { 168 fprintf(stderr, "payloadsize > 32768\n"); 169 return (-1); |
170 /*NOTREACHED*/ |
|
171 } 172 173 duration = strtoul(argv[4], &dummy, 10); | 171 } 172 173 duration = strtoul(argv[4], &dummy, 10); |
174 if (duration < 0 || *dummy != '\0') | 174 if (duration < 0 || *dummy != '\0') { 175 fprintf(stderr, "Invalid duration time: %s\n", argv[4]); |
175 usage(); | 176 usage(); |
177 /*NOTREACHED*/ 178 } |
|
176 177 packet = malloc(payloadsize); 178 if (packet == NULL) { 179 perror("malloc"); 180 return (-1); | 179 180 packet = malloc(payloadsize); 181 if (packet == NULL) { 182 perror("malloc"); 183 return (-1); |
184 /*NOTREACHED*/ |
|
181 } | 185 } |
182 bzero(packet, payloadsize); | |
183 | 186 |
184 s = socket(PF_INET, SOCK_DGRAM, 0); 185 if (s == -1) { 186 perror("socket"); | 187 bzero(packet, payloadsize); 188 error = getaddrinfo(argv[1],argv[2], &hints, &res0); 189 if (error) { 190 perror(gai_strerror(error)); |
187 return (-1); | 191 return (-1); |
192 /*NOTREACHED*/ |
|
188 } | 193 } |
194 s = -1; 195 for (res = res0; res; res = res->ai_next) { 196 s = socket(res->ai_family, res->ai_socktype, 0); 197 if (s < 0) { 198 cause = "socket"; 199 continue; 200 } |
|
189 | 201 |
190 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 191 perror("connect"); | 202 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { 203 cause = "connect"; 204 close(s); 205 s = -1; 206 continue; 207 } 208 209 break; /* okay we got one */ 210 } 211 if (s < 0) { 212 perror(cause); |
192 return (-1); | 213 return (-1); |
214 /*NOTREACHED*/ |
|
193 } 194 | 215 } 216 |
217 freeaddrinfo(res0); 218 |
|
195 return (blast_loop(s, duration, packet, payloadsize)); | 219 return (blast_loop(s, duration, packet, payloadsize)); |
220 |
|
196} | 221} |