udp_flood.c 4.9 KB

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