ddos/server.c
changeset 159 44823a881da1
parent 157 e8fbd0653fda
child 160 26414889dded
equal deleted inserted replaced
158:2cddd4d26139 159:44823a881da1
    28 {
    28 {
    29     int server_socket, status;
    29     int server_socket, status;
    30     struct addrinfo hints;
    30     struct addrinfo hints;
    31     struct addrinfo *result, *rp;
    31     struct addrinfo *result, *rp;
    32 
    32 
    33     int yes = 1;
    33     int yes = 1, no = 0;
    34 
    34 
    35     memset (&hints, 0, sizeof (hints));
    35     memset (&hints, 0, sizeof (hints));
    36     hints.ai_family = AF_UNSPEC;
    36     hints.ai_family = AF_UNSPEC;
    37     hints.ai_socktype = SOCK_STREAM;
    37     hints.ai_socktype = SOCK_STREAM;
    38     hints.ai_flags = AI_PASSIVE;
    38     hints.ai_flags = AI_PASSIVE;
    46     for (rp = result; rp != NULL; rp = rp->ai_next) {
    46     for (rp = result; rp != NULL; rp = rp->ai_next) {
    47         if ((server_socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0) {
    47         if ((server_socket = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol)) < 0) {
    48             perror ("SOCKET");
    48             perror ("SOCKET");
    49             continue;
    49             continue;
    50         }
    50         }
    51 
    51         // enable rebinding of the socket
    52         if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
    52         if (setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
    53             perror ("setsockopt");
    53             perror ("setsockopt");
    54             return -1;
    54             return -1;
    55         }
    55         }
       
    56         // disable IPV6ONLY, eg enable an hybrid socket
       
    57         if (rp->ai_family == AF_INET6)
       
    58             setsockopt (server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof (int));
    56 
    59 
    57         if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) {
    60         if ((bind (server_socket, rp->ai_addr, rp->ai_addrlen)) == -1) {
    58             perror ("BIND");
    61             perror ("BIND");
    59             close (server_socket);
    62             close (server_socket);
    60             server_socket = -1;
    63             server_socket = -1;