udp_flood_disc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <netdb.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <fcntl.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #ifdef __linux__
  33. #include <linux/types.h>
  34. #include <linux/errqueue.h>
  35. #endif
  36. static char *id="$Id$";
  37. static char *version="udp_flood_disc 0.1";
  38. static char* help_msg="\
  39. Usage: udp_flood -f file -d address -p port -c count [-v]\n\
  40. Options:\n\
  41. -f file file with the content of the udp packet (max 65k)\n\
  42. -d address destination address\n\
  43. -p port destination port\n\
  44. -c count number of packets to be sent\n\
  45. -v increase verbosity level\n\
  46. -V version number\n\
  47. -h this help message\n\
  48. ";
  49. #define BUF_SIZE 65535
  50. int main (int argc, char** argv)
  51. {
  52. int fd;
  53. int sock;
  54. char c;
  55. int n,r;
  56. char* tmp;
  57. char* buf[BUF_SIZE];
  58. struct hostent* he;
  59. struct sockaddr_in addr;
  60. int count;
  61. int verbose;
  62. char *fname;
  63. char *dst;
  64. int port;
  65. #ifdef __linux__
  66. int optval;
  67. #endif
  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. #ifdef __linux__
  179. /* enable error receiving on unconnected sockets*/
  180. optval=1;
  181. if(setsockopt(sock,SOL_IP,IP_RECVERR,(void*)&optval,sizeof(optval))==-1){
  182. fprintf(stderr, "Error: setsockopt: %s\n", strerror(errno));
  183. exit(1);
  184. }
  185. #endif
  186. /* flood loop */
  187. for (r=0; r<count; r++){
  188. if ((verbose>1)&&(r%1000)) putchar('.');
  189. if (sendto(sock,buf, n, 0, (struct sockaddr*) &addr, sizeof(addr))==-1)
  190. {
  191. fprintf(stderr, "Error: send: %s\n", strerror(errno));
  192. exit(1);
  193. }
  194. }
  195. printf("\n%d packets sent, %d bytes each => total %d bytes\n",
  196. count, n, n*count);
  197. close(sock);
  198. exit(0);
  199. error:
  200. exit(-1);
  201. }