2
0

udp_flood_disc.c 5.1 KB

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