73be14e6a3b41ec2924c2e810d704637404186ec
[sip-router] / test / udp_flood.c
1 /* $Id$ */
2
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <netdb.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17
18 static char *id="$Id$";
19 static char *version="udp_flood 0.1";
20 static char* help_msg="\
21 Usage: udp_flood -f file -d address -p port -c count [-v]\n\
22 Options:\n\
23     -f file       file with the content of the udp packet (max 65k)\n\
24     -d address    destination address\n\
25     -p port       destination port\n\
26     -c count      number of packets to be sent\n\
27     -v            increase verbosity level\n\
28     -V            version number\n\
29     -h            this help message\n\
30 ";
31
32 #define BUF_SIZE 65535
33
34
35 int main (int argc, char** argv)
36 {
37         int fd;
38         int sock;
39         char c;
40         int n,r;
41         char* tmp;
42         char* buf[BUF_SIZE];
43         struct hostent* he;
44         struct sockaddr_in addr;
45         
46         int count;
47         int verbose;
48         char *fname;
49         char *dst;
50         int port;
51         
52         /* init */
53         count=0;
54         verbose=0;
55         fname=0;
56         dst=0;
57         port=0;
58
59         opterr=0;
60         while ((c=getopt(argc,argv, "f:c:d:p:vhV"))!=-1){
61                 switch(c){
62                         case 'f':
63                                 fname=optarg;
64                                 break;
65                         case 'v':
66                                 verbose++;
67                                 break;
68                         case 'd':
69                                 dst=optarg;
70                                 break;
71                         case 'p':
72                                 port=strtol(optarg, &tmp, 10);
73                                 if ((tmp==0)||(*tmp)){
74                                         fprintf(stderr, "bad port number: -p %s\n", optarg);
75                                         goto error;
76                                 }
77                                 break;
78                         case 'c':
79                                 count=strtol(optarg, &tmp, 10);
80                                 if ((tmp==0)||(*tmp)){
81                                         fprintf(stderr, "bad count: -c %s\n", optarg);
82                                         goto error;
83                                 }
84                                 break;
85                         case 'V':
86                                 printf("version: %s\n", version);
87                                 printf("%s\n",id);
88                                 exit(0);
89                                 break;
90                         case 'h':
91                                 printf("version: %s\n", version);
92                                 printf("%s", help_msg);
93                                 exit(0);
94                                 break;
95                         case '?':
96                                 if (isprint(optopt))
97                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
98                                 else
99                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
100                                 goto error;
101                         case ':':
102                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
103                                                 optopt);
104                                 goto error;
105                                 break;
106                         default:
107                                         abort();
108                 }
109         }
110         
111         /* check if all the required params are present */
112         if (fname==0){
113                 fprintf(stderr, "Missing -f file\n");
114                 exit(-1);
115         }
116         if (dst==0){
117                 fprintf(stderr, "Missing destination (-d ...)\n");
118                 exit(-1);
119         }
120         if(port==0){
121                 fprintf(stderr, "Missing port number (-p port)\n");
122                 exit(-1);
123         }else if(port<0){
124                 fprintf(stderr, "Invalid port number (-p %d)\n", port);
125                 exit(-1);
126         }
127         if(count==0){
128                 fprintf(stderr, "Missing packet count (-c number)\n");
129                 exit(-1);
130         }else if(count<0){
131                 fprintf(stderr, "Invalid packet count (-c %d)\n", count);
132                 exit(-1);
133         }
134         
135         /* open packet file */
136         fd=open(fname, O_RDONLY);
137         if (fd<0){
138                 fprintf(stderr, "ERROR: loading packet-file(%s): %s\n", fname,
139                                 strerror(errno));
140                 goto error;
141         }
142         n=read(fd, buf, BUF_SIZE);
143         if (n<0){
144                 fprintf(stderr, "ERROR: reading file(%s): %s\n", fname,
145                                 strerror(errno));
146                 goto error;
147         }
148         if (verbose) printf("read %d bytes from file %s\n", n, fname);
149         close(fd);
150
151         /* resolve destination */
152         he=gethostbyname(dst);
153         if (he==0){
154                 fprintf(stderr, "ERROR: could not resolve %s\n", dst);
155                 goto error;
156         }
157         /* open socket*/
158         addr.sin_family=he->h_addrtype;
159         addr.sin_port=htons(port);
160         memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
161         
162         sock = socket(he->h_addrtype, SOCK_DGRAM, 0);
163         if (sock==-1){
164                 fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
165                 goto error;
166         }
167         if (connect(sock, (struct sockaddr*) &addr, sizeof(struct sockaddr))!=0){
168                 fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
169                 goto error;
170         }
171
172
173         /* flood loop */
174         for (r=0; r<count; r++){
175                 if ((verbose>1)&&(r%1000))  putchar('.');
176                 if (send(sock, buf, n, 0)==-1) {
177                         fprintf(stderr, "Error: send: %s\n",  strerror(errno));
178                         exit(1);
179                 }
180         }
181         printf("\n%d packets sent, %d bytes each => total %d bytes\n",
182                         count, n, n*count);
183
184         close(sock);
185         exit(0);
186
187 error:
188         exit(-1);
189 }