/* 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 <netdb.h>
#include <netinet/in.h>
#include <unistd.h>

#define PORT_DEFAULT 8888
#define HOST_DEFAULT "localhost"
#define BUF_SIZE 128

static char *copyhost (char *p_source)
{
	char *p_dest;
	if ((p_dest = malloc (strlen (p_source) + 1)))
	{
		strcpy (p_dest, p_source);
	}
	else
	{
		perror ("malloc");
	}
	return p_dest;
}

/* on récup le host et le port en ligne de commande */
int main (int argc, char **argv)
{
	short int port;
	char *p_hostname;
	switch (argc)
	{
		case 3:
			port = atoi (argv[2]);
			if ((p_hostname = copyhost (argv[1])) == NULL)
			{
				return EXIT_FAILURE;
			}
			break;
		case 2:
			port = PORT_DEFAULT;
			if ((p_hostname = copyhost (argv[1])) == NULL)
			{
				return EXIT_FAILURE;
			}
			break;
		case 1:
			port = PORT_DEFAULT;
			if ((p_hostname = copyhost (HOST_DEFAULT)) == NULL)
			{
				return EXIT_FAILURE;
			}
			break;
		default:
			fprintf (stderr, "usage: ./sos.client [host [port]]\n");
			return EXIT_FAILURE;
	}
	{
		int sockfd;
		if ((sockfd = socket (PF_INET, SOCK_STREAM, 0)) < 0)
		{
			perror ("socket");
			return EXIT_FAILURE;
		}
		{
			struct sockaddr_in serveur;
			struct hostent *p_host;
			p_host = gethostbyname (p_hostname);
			memset (&serveur, 0, sizeof serveur);
			serveur.sin_family = AF_INET;
			serveur.sin_port = htons (port);
			serveur.sin_addr = * (struct in_addr *) p_host->h_addr;
			if (connect (sockfd, (struct sockaddr *) &serveur, sizeof serveur) < 0)
			{
				perror ("connect");
				return EXIT_FAILURE;
			}
			{
				char buffer[BUF_SIZE];
				memset (buffer, 0, BUF_SIZE);
				while (recv (sockfd, buffer, BUF_SIZE - 1, 0))
				{
					printf ("le PHARE a dit: %s", buffer);
				}
			}
		}
		close (sockfd);
	}
	return EXIT_SUCCESS;
}
