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 38
 #include <fcntl.h>
39 39
 #include <sys/socket.h>
40 40
 #include <netinet/in.h>
41
+#include <netinet/tcp.h>
41 42
 #include <arpa/inet.h>
42 43
 
43 44
 
44 45
 static char *id="$Id$";
45
-static char *version="udp_flood 0.1";
46
+static char *version="udp_flood 0.2";
46 47
 static char* help_msg="\
47 48
 Usage: udp_flood -f file -d address -p port -c count [-v]\n\
48 49
 Options:\n\
... ...
@@ -52,6 +57,10 @@ Options:\n\
52 52
     -c count      number of packets to be sent\n\
53 53
     -s usec       microseconds to sleep before sending \"throttle\" packets\n\
54 54
     -t throttle   number of packets to send before sleeping\n\
55
+    -r            sleep randomly up to -s usec packets (see -s) \n\
56
+    -T            use tcp instead of udp \n\
57
+    -n no         tcp connection number \n\
58
+    -R            close the tcp connections with RST (SO_LINGER) \n\
55 59
     -v            increase verbosity level\n\
56 60
     -V            version number\n\
57 61
     -h            this help message\n\
... ...
@@ -76,9 +85,15 @@ int main (int argc, char** argv)
76 76
 	char *fname;
77 77
 	char *dst;
78 78
 	int port;
79
-	long usec;
79
+	unsigned long usec;
80 80
 	int throttle;
81
+	int random_sleep;
82
+	int tcp;
83
+	int tcp_rst;
84
+	int con_no;
81 85
 	int t;
86
+	struct linger t_linger;
87
+	int k;
82 88
 	
83 89
 	/* init */
84 90
 	count=0;
... ...
@@ -88,9 +103,13 @@ int main (int argc, char** argv)
88 88
 	port=0;
89 89
 	usec=0;
90 90
 	throttle=0;
91
+	random_sleep=0;
92
+	tcp=0;
93
+	tcp_rst=0;
94
+	con_no=1;
91 95
 
92 96
 	opterr=0;
93
-	while ((c=getopt(argc,argv, "f:c:d:p:s:t:vhV"))!=-1){
97
+	while ((c=getopt(argc,argv, "f:c:d:p:s:t:n:rTRvhV"))!=-1){
94 98
 		switch(c){
95 99
 			case 'f':
96 100
 				fname=optarg;
... ...
@@ -129,6 +148,22 @@ int main (int argc, char** argv)
129 129
 					goto error;
130 130
 				}
131 131
 				break;
132
+			case 'n':
133
+				con_no=strtol(optarg, &tmp, 10);
134
+				if ((tmp==0)||(*tmp)||(con_no<1)){
135
+					fprintf(stderr, "bad count: -c %s\n", optarg);
136
+					goto error;
137
+				}
138
+				break;
139
+			case 'r':
140
+				random_sleep=1;
141
+				break;
142
+			case 'T':
143
+				tcp=1;
144
+				break;
145
+			case 'R':
146
+				tcp_rst=1;
147
+				break;
132 148
 			case 'V':
133 149
 				printf("version: %s\n", version);
134 150
 				printf("%s\n",id);
... ...
@@ -178,6 +213,7 @@ int main (int argc, char** argv)
178 178
 		fprintf(stderr, "Invalid packet count (-c %d)\n", count);
179 179
 		exit(-1);
180 180
 	}
181
+	if (!tcp) con_no=1;
181 182
 	
182 183
 	/* open packet file */
183 184
 	fd=open(fname, O_RDONLY);
... ...
@@ -206,37 +242,65 @@ int main (int argc, char** argv)
206 206
 	addr.sin_port=htons(port);
207 207
 	memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
208 208
 	
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
-
209
+	for (k=0; k<con_no; k++){
210
+		sock = socket(he->h_addrtype, (tcp)?SOCK_STREAM:SOCK_DGRAM, 0);
211
+		if (sock==-1){
212
+			fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
213
+			goto error;
214
+		}
215
+		if (tcp){
216
+			t=1;
217
+			if (setsockopt(sock, SOL_TCP , TCP_NODELAY, &t, sizeof(t))<0){
218
+				fprintf(stderr, "ERROR: could not disable Nagle: %s\n",
219
+								strerror(errno));
220
+			}
221
+			if (tcp_rst){
222
+				t_linger.l_onoff=1;
223
+				t_linger.l_linger=0;
224
+				if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &t_linger,
225
+									sizeof(t_linger))<0){
226
+					fprintf(stderr, "ERROR: could not set SO_LINGER: %s\n",
227
+									strerror(errno));
228
+				}
229
+			}
230
+		}
219 231
 
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);
232
+		if (connect(sock, (struct sockaddr*) &addr,
233
+					sizeof(struct sockaddr))!=0){
234
+			fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
235
+			goto error;
227 236
 		}
228
-		if (usec){
229
-			t--;
230
-			if (t==0){
231
-				usleep(usec);
232
-				t=throttle;
237
+		
238
+		
239
+		/* flood loop */
240
+		t=throttle;
241
+		for (r=0; r<count; r++){
242
+			if ((verbose>1)&&((r%1000)==999)){  putchar('.'); fflush(stdout); }
243
+			if (send(sock, buf, n, 0)==-1) {
244
+				fprintf(stderr, "Error: send: %s\n",  strerror(errno));
245
+				exit(1);
246
+			}
247
+			if (usec){
248
+				t--;
249
+				if (t==0){
250
+					usleep(random_sleep?
251
+								(unsigned long)((double)usec*rand()/RAND_MAX):usec);
252
+					t=throttle;
253
+				}
233 254
 			}
234 255
 		}
256
+		
257
+		close(sock);
258
+		if ((verbose) && (k%1000==999)) { putchar('#'); fflush(stdout); }
259
+	}
260
+	if (tcp){
261
+		printf("\n%d packets sent on %d tcp connections (%d on each of them),"
262
+				" %d bytes each => total %d bytes\n",
263
+				count*con_no, con_no, count, n, con_no*n*count);
264
+	}else{
265
+		printf("\n%d packets sent, %d bytes each => total %d bytes\n",
266
+				count, n, n*count);
235 267
 	}
236
-	printf("\n%d packets sent, %d bytes each => total %d bytes\n",
237
-			count, n, n*count);
238
-
239
-	close(sock);
240 268
 	exit(0);
241 269
 
242 270
 error: