/* Copyright (c) 2003, Rene Luria aka herel <rene@luria.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice,
 *     * this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *     * notice, this list of conditions and the following disclaimer in the
 *     * documentation and/or other materials provided with the distribution.
 *     * Neither the name of the author nor the names of its
 *     * contributors may be used to endorse or promote products derived from
 *     * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#define POUET EXIT_SUCCESS
#define DTC EXIT_FAILURE

#ifdef WWW
#define MESSAGE "Content-type: text/html\n\n<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n\t<!DOCTYPE html\nPUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\"><head><title>S.O.S</title></head><body><p>S.O.S</p></body></html>\n"
#define PORT_DEFAULT 80
#else
#define MESSAGE "S.O.S\n"
#define PORT_DEFAULT 8888
#endif

/***
 * setup_server
 * ˇ retourne une socket serveur toute nikel
 * args: numéro de port
 * ret: numéro de la socket ou -1$
 ***/
int setup_server (short int port)
{
    int serverfd;
    if ((serverfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror ("socket");
        return -1;
    }
    {
        int yes = 1;
        if (setsockopt (serverfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0)
        {
            perror ("setsockopt");
            return -1;
        }
    }
    {
        struct sockaddr_in serveur;
        memset (&serveur, 0, sizeof serveur);
        serveur.sin_family = PF_INET;
        serveur.sin_port = htons (port);
        serveur.sin_addr.s_addr = INADDR_ANY;
        if (bind (serverfd, (struct sockaddr *) &serveur, sizeof serveur) < 0)
        {
            perror ("bind");
            return -1;
        }
    }
    if (listen (serverfd, 0) < 0)
    {
        perror ("listen");
        return -1;
    }
    return serverfd;
}

int main (int argc, char **argv)
{
    int serverfd;
    short port;
    switch (argc)
    {
        case 1:
            port = PORT_DEFAULT;
            break;
        case 2:
            port = atoi (argv[1]);
            break;
        default:
            fprintf (stderr, "usage: ./sos [port]\n");
            return DTC;
            break;
    }
    if ((serverfd = setup_server (port)) < 0)
    {
        return DTC;
    }
    while (1)
    {
        int client;
        struct sockaddr_in addresse;
        size_t taille = sizeof addresse;
        memset (&addresse, 0, taille);
        if ((client = accept (serverfd, (struct sockaddr *) &addresse, &taille)) >= 0)
        {
            printf ("CLIENT en perdition!\n");
            send (client, MESSAGE, strlen (MESSAGE) + 1, 0);
            close (client);
        }
        else
        {
            perror ("accept");
        }
    }
    return POUET;
}