ddos/server.c
changeset 160 26414889dded
parent 159 44823a881da1
child 161 36f763c8ab16
equal deleted inserted replaced
159:44823a881da1 160:26414889dded
     5 
     5 
     6 #include <stdio.h>
     6 #include <stdio.h>
     7 #include <stdlib.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     8 #include <string.h>
     9 #include <unistd.h>
     9 #include <unistd.h>
       
    10 #include <sys/socket.h>
    10 #include <sys/types.h>
    11 #include <sys/types.h>
    11 #include <sys/socket.h>
    12 #include <netdb.h>
    12 #include <netinet/in.h>
    13 #include <netinet/in.h>
    13 #include <netdb.h>
       
    14 #include <arpa/inet.h>
       
    15 #include <sys/types.h>
       
    16 #include <signal.h>
    14 #include <signal.h>
    17 
       
    18 #define MAXLEN 80
       
    19 
    15 
    20 int set_proc_limit (int);
    16 int set_proc_limit (int);
    21 
    17 
       
    18 #define SVPORT "4000"
       
    19 #define MAXLEN 1024
       
    20 #define MAXMEM 200
       
    21 
    22 int server_write (int client_socket, char *s)
    22 int server_write (int client_socket, char *s)
    23 {
    23 {
       
    24     if (s == NULL) {
       
    25         fprintf (stderr, "Invalid data: s == NULL");
       
    26         return -1;
       
    27     }
       
    28 
    24     return write (client_socket, s, strlen (s));
    29     return write (client_socket, s, strlen (s));
    25 }
    30 }
    26 
    31 
    27 int server_init (char *port)
    32 int server_init (char *port)
    28 {
    33 {
    53             perror ("setsockopt");
    58             perror ("setsockopt");
    54             return -1;
    59             return -1;
    55         }
    60         }
    56         // disable IPV6ONLY, eg enable an hybrid socket
    61         // disable IPV6ONLY, eg enable an hybrid socket
    57         if (rp->ai_family == AF_INET6)
    62         if (rp->ai_family == AF_INET6)
    58             setsockopt (server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof (int));
    63             if (setsockopt (server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof (int)) == -1)
       
    64                 perror ("setsockopt");
    59 
    65 
    60         if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) {
    66         if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) {
    61             perror ("BIND");
    67             perror ("BIND");
    62             close (server_socket);
    68             close (server_socket);
    63             server_socket = -1;
    69             server_socket = -1;
    64         } else {
    70         } else {
    65             printf ("bound to an %s socket\n", (rp->ai_family == AF_INET6) ? "IPv6" : "IPv4");
    71             printf ("bound to an %s socket on port %s\n", (rp->ai_family == AF_INET6) ? "IPv6" : "IPv4", port);
    66         }
    72         }
    67     }
    73     }
    68 
    74 
    69     status = listen (server_socket, 10);
    75     status = listen (server_socket, 10);
    70 
    76 
    88     int len;
    94     int len;
    89 
    95 
    90     pid_t pid;
    96     pid_t pid;
    91     pid_t parent_pid;
    97     pid_t parent_pid;
    92 
    98 
    93     if ((server_socket = server_init ("4000")) < 0)
    99     if ((server_socket = server_init (SVPORT)) < 0)
    94         return EXIT_FAILURE;
   100         return EXIT_FAILURE;
    95 
   101 
    96     int links = 0;
   102     int links = 0;
    97 
   103 
    98     parent_pid = getpid ();
   104     parent_pid = getpid ();