Browse code

- gcc 2.95 fixes - udp_flood updated to work also for tcp (for stress testing the ser tcp part)

Andrei Pelinescu-Onciul authored on 09/09/2005 14:29:48
Showing 4 changed files
... ...
@@ -334,9 +334,9 @@ static unsigned int get_sys_version(int* major, int* minor, int* minor2)
334 334
 char* check_poll_method(enum poll_types poll_method)
335 335
 {
336 336
 	char* ret;
337
-	ret=0;
338 337
 	unsigned int os_ver;
339 338
 
339
+	ret=0;
340 340
 	os_ver=get_sys_version(0,0,0);	
341 341
 	switch(poll_method){
342 342
 		case POLL_NONE:
... ...
@@ -297,7 +297,7 @@ inline static int io_watch_add(	io_wait_h* h,
297 297
 	int check_io;
298 298
 	struct pollfd pf;
299 299
 	
300
-	check_io=0; /* set to 1 if we need to check for pre-existiing queued
300
+	check_io=0; /* set to 1 if we need to check for pre-existing queued
301 301
 				   io/data on the fd */
302 302
 	idx=-1;
303 303
 #endif
... ...
@@ -9,7 +9,7 @@ Contact: <sip:213.20.128.35:9315>
9 9
 User-Agent: Windows RTC/1.0
10 10
 Proxy-Authorization: Digest username="jiri", realm="iptel.org", algorithm="MD5", uri="sip:jiri@bat.iptel.org", nonce="3cef753900000001771328f5ae1b8b7f0d742da1feb5753c", response="53fe98db10e1074b03b3e06438bda70f"
11 11
 Content-Type: application/sdp
12
-Content-Length: 451
12
+Content-Length: 452
13 13
 
14 14
 v=0
15 15
 o=jku2 0 0 IN IP4 213.20.128.35
... ...
@@ -24,6 +24,10 @@
24 24
  * along with this program; if not, write to the Free Software 
25 25
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 26
  */
27
+/*
28
+ * History:
29
+ *  2005-09-09  basic tcp support added (andrei)
30
+ */
27 31
 
28 32
 
29 33
 
... ...
@@ -38,11 +42,12 @@
38 42
 #include <fcntl.h>
39 43
 #include <sys/socket.h>
40 44
 #include <netinet/in.h>
45
+#include <netinet/tcp.h>
41 46
 #include <arpa/inet.h>
42 47
 
43 48
 
44 49
 static char *id="$Id$";
45
-static char *version="udp_flood 0.1";
50
+static char *version="udp_flood 0.2";
46 51
 static char* help_msg="\
47 52
 Usage: udp_flood -f file -d address -p port -c count [-v]\n\
48 53
 Options:\n\
... ...
@@ -52,6 +57,10 @@ Options:\n\
52 57
     -c count      number of packets to be sent\n\
53 58
     -s usec       microseconds to sleep before sending \"throttle\" packets\n\
54 59
     -t throttle   number of packets to send before sleeping\n\
60
+    -r            sleep randomly up to -s usec packets (see -s) \n\
61
+    -T            use tcp instead of udp \n\
62
+    -n no         tcp connection number \n\
63
+    -R            close the tcp connections with RST (SO_LINGER) \n\
55 64
     -v            increase verbosity level\n\
56 65
     -V            version number\n\
57 66
     -h            this help message\n\
... ...
@@ -76,9 +85,15 @@ int main (int argc, char** argv)
76 85
 	char *fname;
77 86
 	char *dst;
78 87
 	int port;
79
-	long usec;
88
+	unsigned long usec;
80 89
 	int throttle;
90
+	int random_sleep;
91
+	int tcp;
92
+	int tcp_rst;
93
+	int con_no;
81 94
 	int t;
95
+	struct linger t_linger;
96
+	int k;
82 97
 	
83 98
 	/* init */
84 99
 	count=0;
... ...
@@ -88,9 +103,13 @@ int main (int argc, char** argv)
88 103
 	port=0;
89 104
 	usec=0;
90 105
 	throttle=0;
106
+	random_sleep=0;
107
+	tcp=0;
108
+	tcp_rst=0;
109
+	con_no=1;
91 110
 
92 111
 	opterr=0;
93
-	while ((c=getopt(argc,argv, "f:c:d:p:s:t:vhV"))!=-1){
112
+	while ((c=getopt(argc,argv, "f:c:d:p:s:t:n:rTRvhV"))!=-1){
94 113
 		switch(c){
95 114
 			case 'f':
96 115
 				fname=optarg;
... ...
@@ -129,6 +148,22 @@ int main (int argc, char** argv)
129 148
 					goto error;
130 149
 				}
131 150
 				break;
151
+			case 'n':
152
+				con_no=strtol(optarg, &tmp, 10);
153
+				if ((tmp==0)||(*tmp)||(con_no<1)){
154
+					fprintf(stderr, "bad count: -c %s\n", optarg);
155
+					goto error;
156
+				}
157
+				break;
158
+			case 'r':
159
+				random_sleep=1;
160
+				break;
161
+			case 'T':
162
+				tcp=1;
163
+				break;
164
+			case 'R':
165
+				tcp_rst=1;
166
+				break;
132 167
 			case 'V':
133 168
 				printf("version: %s\n", version);
134 169
 				printf("%s\n",id);
... ...
@@ -178,6 +213,7 @@ int main (int argc, char** argv)
178 213
 		fprintf(stderr, "Invalid packet count (-c %d)\n", count);
179 214
 		exit(-1);
180 215
 	}
216
+	if (!tcp) con_no=1;
181 217
 	
182 218
 	/* open packet file */
183 219
 	fd=open(fname, O_RDONLY);
... ...
@@ -206,37 +242,65 @@ int main (int argc, char** argv)
206 242
 	addr.sin_port=htons(port);
207 243
 	memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
208 244
 	
209
-	sock = socket(he->h_addrtype, SOCK_DGRAM, 0);
210
-	if (sock==-1){
211
-		fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
212
-		goto error;
213
-	}
214
-	if (connect(sock, (struct sockaddr*) &addr, sizeof(struct sockaddr))!=0){
215
-		fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
216
-		goto error;
217
-	}
218
-
245
+	for (k=0; k<con_no; k++){
246
+		sock = socket(he->h_addrtype, (tcp)?SOCK_STREAM:SOCK_DGRAM, 0);
247
+		if (sock==-1){
248
+			fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
249
+			goto error;
250
+		}
251
+		if (tcp){
252
+			t=1;
253
+			if (setsockopt(sock, SOL_TCP , TCP_NODELAY, &t, sizeof(t))<0){
254
+				fprintf(stderr, "ERROR: could not disable Nagle: %s\n",
255
+								strerror(errno));
256
+			}
257
+			if (tcp_rst){
258
+				t_linger.l_onoff=1;
259
+				t_linger.l_linger=0;
260
+				if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &t_linger,
261
+									sizeof(t_linger))<0){
262
+					fprintf(stderr, "ERROR: could not set SO_LINGER: %s\n",
263
+									strerror(errno));
264
+				}
265
+			}
266
+		}
219 267
 
220
-	/* flood loop */
221
-	t=throttle;
222
-	for (r=0; r<count; r++){
223
-		if ((verbose>1)&&(r%1000))  putchar('.');
224
-		if (send(sock, buf, n, 0)==-1) {
225
-			fprintf(stderr, "Error: send: %s\n",  strerror(errno));
226
-			exit(1);
268
+		if (connect(sock, (struct sockaddr*) &addr,
269
+					sizeof(struct sockaddr))!=0){
270
+			fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
271
+			goto error;
227 272
 		}
228
-		if (usec){
229
-			t--;
230
-			if (t==0){
231
-				usleep(usec);
232
-				t=throttle;
273
+		
274
+		
275
+		/* flood loop */
276
+		t=throttle;
277
+		for (r=0; r<count; r++){
278
+			if ((verbose>1)&&((r%1000)==999)){  putchar('.'); fflush(stdout); }
279
+			if (send(sock, buf, n, 0)==-1) {
280
+				fprintf(stderr, "Error: send: %s\n",  strerror(errno));
281
+				exit(1);
282
+			}
283
+			if (usec){
284
+				t--;
285
+				if (t==0){
286
+					usleep(random_sleep?
287
+								(unsigned long)((double)usec*rand()/RAND_MAX):usec);
288
+					t=throttle;
289
+				}
233 290
 			}
234 291
 		}
292
+		
293
+		close(sock);
294
+		if ((verbose) && (k%1000==999)) { putchar('#'); fflush(stdout); }
295
+	}
296
+	if (tcp){
297
+		printf("\n%d packets sent on %d tcp connections (%d on each of them),"
298
+				" %d bytes each => total %d bytes\n",
299
+				count*con_no, con_no, count, n, con_no*n*count);
300
+	}else{
301
+		printf("\n%d packets sent, %d bytes each => total %d bytes\n",
302
+				count, n, n*count);
235 303
 	}
236
-	printf("\n%d packets sent, %d bytes each => total %d bytes\n",
237
-			count, n, n*count);
238
-
239
-	close(sock);
240 304
 	exit(0);
241 305
 
242 306
 error: