ddos/server.c
changeset 0 af501b0c1716
child 9 c3fecc82ade6
equal deleted inserted replaced
-1:000000000000 0:af501b0c1716
       
     1 /**
       
     2  * $Id: server.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
       
     3  * $Source: /development/c/demos/ddos/server.c,v $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 #include <unistd.h>
       
    11 #include <sys/socket.h>
       
    12 #include <netinet/in.h>
       
    13 #include <arpa/inet.h>
       
    14 #include <sys/types.h>
       
    15 #include <signal.h>
       
    16 
       
    17 int set_limit (int);
       
    18 
       
    19 int main (int argc, char **argv)
       
    20 {
       
    21     char message[81];
       
    22     int server_socket;
       
    23     int client_socket;
       
    24     struct sockaddr_in sa;
       
    25     struct sockaddr_in ca;
       
    26     socklen_t size;
       
    27     int len;
       
    28     int status;
       
    29     pid_t pid;
       
    30     pid_t parent_pid;
       
    31 
       
    32     server_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
       
    33     if (server_socket == -1) {
       
    34         perror ("socket");
       
    35         return EXIT_FAILURE;
       
    36     }
       
    37 
       
    38     sa.sin_family = AF_INET;
       
    39     sa.sin_port = htons (4000);
       
    40     sa.sin_addr.s_addr = INADDR_ANY;
       
    41 
       
    42     size = sizeof (sa);
       
    43 
       
    44     status = bind (server_socket, (struct sockaddr *)&sa, size);
       
    45     if (status != 0) {
       
    46         perror ("BIND");
       
    47         return EXIT_FAILURE;
       
    48     }
       
    49 
       
    50     status = listen (server_socket, 10);
       
    51     if (status != 0) {
       
    52         perror ("LISTEN");
       
    53         return EXIT_FAILURE;
       
    54     }
       
    55 
       
    56     int links = 0;
       
    57 
       
    58     parent_pid = getpid ();
       
    59 
       
    60     /*
       
    61      * Child quits immediately, father mustn't wait
       
    62      */
       
    63     signal (SIGCHLD, SIG_IGN);
       
    64 
       
    65     if (set_limit (500) != 0) {
       
    66         printf ("Cannot limit the process limit\n");
       
    67         return EXIT_FAILURE;
       
    68     }
       
    69 
       
    70     for (;;) {
       
    71         size = sizeof (ca);
       
    72         client_socket = accept (server_socket, (struct sockaddr *)&sa, (socklen_t *) & size);
       
    73 
       
    74         printf ("PARENT PID = %d\n", parent_pid);
       
    75 
       
    76         pid = fork ();
       
    77         switch (pid) {
       
    78         case 0:                /* Child */
       
    79             close (server_socket);
       
    80             if ((len = read (client_socket, message, 80)) == -1)
       
    81                 break;
       
    82             message[len] = 0;
       
    83 
       
    84             len = write (client_socket, "Please wait...\r\n", 17);
       
    85             sleep (10);
       
    86 
       
    87             printf ("DELETING LINK [%04d] %04d\n", getpid (), links--);
       
    88             shutdown (client_socket, SHUT_RDWR);
       
    89             close (client_socket);
       
    90             return 1;
       
    91         case -1:
       
    92             perror ("Fork Error");
       
    93             close (client_socket);
       
    94             /*
       
    95              * server is still alive and will continue
       
    96              * * when resources are available
       
    97              */
       
    98             break;
       
    99         default:               /* PID > 0 */
       
   100             close (client_socket);
       
   101             printf ("Starting LINK [%04d] %04d\n", pid, links++);
       
   102         }
       
   103     }
       
   104     close (server_socket);
       
   105     return 0;
       
   106 }