GPLization banner introduced to *.[hc] files
[sip-router] / test / udp_flood.c
1 /* $Id$ */
2 /*
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <netdb.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43
44 static char *id="$Id$";
45 static char *version="udp_flood 0.1";
46 static char* help_msg="\
47 Usage: udp_flood -f file -d address -p port -c count [-v]\n\
48 Options:\n\
49     -f file       file with the content of the udp packet (max 65k)\n\
50     -d address    destination address\n\
51     -p port       destination port\n\
52     -c count      number of packets to be sent\n\
53     -v            increase verbosity level\n\
54     -V            version number\n\
55     -h            this help message\n\
56 ";
57
58 #define BUF_SIZE 65535
59
60
61 int main (int argc, char** argv)
62 {
63         int fd;
64         int sock;
65         char c;
66         int n,r;
67         char* tmp;
68         char* buf[BUF_SIZE];
69         struct hostent* he;
70         struct sockaddr_in addr;
71         
72         int count;
73         int verbose;
74         char *fname;
75         char *dst;
76         int port;
77         
78         /* init */
79         count=0;
80         verbose=0;
81         fname=0;
82         dst=0;
83         port=0;
84
85         opterr=0;
86         while ((c=getopt(argc,argv, "f:c:d:p:vhV"))!=-1){
87                 switch(c){
88                         case 'f':
89                                 fname=optarg;
90                                 break;
91                         case 'v':
92                                 verbose++;
93                                 break;
94                         case 'd':
95                                 dst=optarg;
96                                 break;
97                         case 'p':
98                                 port=strtol(optarg, &tmp, 10);
99                                 if ((tmp==0)||(*tmp)){
100                                         fprintf(stderr, "bad port number: -p %s\n", optarg);
101                                         goto error;
102                                 }
103                                 break;
104                         case 'c':
105                                 count=strtol(optarg, &tmp, 10);
106                                 if ((tmp==0)||(*tmp)){
107                                         fprintf(stderr, "bad count: -c %s\n", optarg);
108                                         goto error;
109                                 }
110                                 break;
111                         case 'V':
112                                 printf("version: %s\n", version);
113                                 printf("%s\n",id);
114                                 exit(0);
115                                 break;
116                         case 'h':
117                                 printf("version: %s\n", version);
118                                 printf("%s", help_msg);
119                                 exit(0);
120                                 break;
121                         case '?':
122                                 if (isprint(optopt))
123                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
124                                 else
125                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
126                                 goto error;
127                         case ':':
128                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
129                                                 optopt);
130                                 goto error;
131                                 break;
132                         default:
133                                         abort();
134                 }
135         }
136         
137         /* check if all the required params are present */
138         if (fname==0){
139                 fprintf(stderr, "Missing -f file\n");
140                 exit(-1);
141         }
142         if (dst==0){
143                 fprintf(stderr, "Missing destination (-d ...)\n");
144                 exit(-1);
145         }
146         if(port==0){
147                 fprintf(stderr, "Missing port number (-p port)\n");
148                 exit(-1);
149         }else if(port<0){
150                 fprintf(stderr, "Invalid port number (-p %d)\n", port);
151                 exit(-1);
152         }
153         if(count==0){
154                 fprintf(stderr, "Missing packet count (-c number)\n");
155                 exit(-1);
156         }else if(count<0){
157                 fprintf(stderr, "Invalid packet count (-c %d)\n", count);
158                 exit(-1);
159         }
160         
161         /* open packet file */
162         fd=open(fname, O_RDONLY);
163         if (fd<0){
164                 fprintf(stderr, "ERROR: loading packet-file(%s): %s\n", fname,
165                                 strerror(errno));
166                 goto error;
167         }
168         n=read(fd, buf, BUF_SIZE);
169         if (n<0){
170                 fprintf(stderr, "ERROR: reading file(%s): %s\n", fname,
171                                 strerror(errno));
172                 goto error;
173         }
174         if (verbose) printf("read %d bytes from file %s\n", n, fname);
175         close(fd);
176
177         /* resolve destination */
178         he=gethostbyname(dst);
179         if (he==0){
180                 fprintf(stderr, "ERROR: could not resolve %s\n", dst);
181                 goto error;
182         }
183         /* open socket*/
184         addr.sin_family=he->h_addrtype;
185         addr.sin_port=htons(port);
186         memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
187         
188         sock = socket(he->h_addrtype, SOCK_DGRAM, 0);
189         if (sock==-1){
190                 fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
191                 goto error;
192         }
193         if (connect(sock, (struct sockaddr*) &addr, sizeof(struct sockaddr))!=0){
194                 fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
195                 goto error;
196         }
197
198
199         /* flood loop */
200         for (r=0; r<count; r++){
201                 if ((verbose>1)&&(r%1000))  putchar('.');
202                 if (send(sock, buf, n, 0)==-1) {
203                         fprintf(stderr, "Error: send: %s\n",  strerror(errno));
204                         exit(1);
205                 }
206         }
207         printf("\n%d packets sent, %d bytes each => total %d bytes\n",
208                         count, n, n*count);
209
210         close(sock);
211         exit(0);
212
213 error:
214         exit(-1);
215 }