/*
 * client.c - a test program for sockets
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>

#define CR		'\015'
#define LF		'\012'
#define CRLF		"\015\012"
#define EOM             ".\015\012"
#define SOCKBUF		4096

char	outbuf[SOCKBUF];
char	inbuf[SOCKBUF];

int
establish_connection(hostname, portname)
char	*hostname, *portname;
{
	struct sockaddr_in	sname;
	struct hostent		*hp;
	struct servent		*sp;

	int	sock, portnum;

	/*
	 * resolve hostname
	 */
	hp = gethostbyname(hostname);
	if (hp == NULL) {
		fprintf(stderr, "unknown host %s\n", hostname);
		exit(1);
	}

	/*
	 * resolve portname
	 */
	if (isdigit(*port)) {
		portnum = htons(atoi(portname));
		portname = port;
	} else {
		sp = getservbyname(portname, "tcp");
		if (sp == NULL) {
			fprintf(stderr, "unknown port %s\n", portname);
			exit(1);
		}
		portnum = sp->s_port;
		portname = sp->s_name;
	}


	/*
	 * build server socket name
	 */
	bzero(sname, sizeof(sname));
	sname.sin_family = AF_INET;
	bcopy(hp->h_addr, &sname.sin_addr, hp->h_length);
	sname.sin_port = portnum;


	/*
	 * create a network socket
	 */
	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("creating socket");
		exit(1);
	}

	/*
	 * make a connection to the specified server on the socket
	 */
	if (connect(sock, &sname, sizeof(sname)) < 0) {
		perror("connect");
		exit(1);
	}

	return sock;
}

main(argc, argv)
int	argc;
char	*argv[];
{
	extern int		errno;
	struct sockaddr_in	sname;
	struct hostent		*hp;
	struct servent		*sp;

	int	sock, portnum;
	int	red, inlen, sockflags;

	host = argc < 2 ? "bloom-beacon" : argv[1];
	port = argc < 3 ? "nntp" : argv[2];

	/*
	 * resolve hostname
	 */
	hp = gethostbyname(host);
	if (hp == NULL) {
		fprintf(stderr, "unknown host %s\n", host);
		exit(1);
	}

	/*
	 * resolve portname
	 */

	if (isdigit(*port)) {
		portnum = htons(atoi(port));
		portname = port;
	} else {
		sp = getservbyname(port, "tcp");
		if (sp == NULL) {
			fprintf(stderr, "unknown port %s\n", port);
			exit(1);
		}
		portnum = sp->s_port;
		portname = sp->s_name;
	}


	/*
	 * build server socket name
	 */
	bzero(sname, sizeof(sname));
	sname.sin_family = AF_INET;
	bcopy(hp->h_addr, &sname.sin_addr, hp->h_length);
	sname.sin_port = portnum;

	/*
	 * print status
	 */
	printf("Connecting to %s, port %s (%d) ...",
	       hp->h_name, portname, ntohs(portnum)), fflush(stdout);

	/*
	 * create a network socket
	 */
	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("creating socket");
		exit(1);
	}
	printf("."), fflush(stdout);

	/*
	 * make a connection to the specified server on the socket
	 */
	if (connect(sock, &sname, sizeof(sname)) < 0) {
		perror("connect");
		exit(1);
	}
	printf(". "), fflush(stdout);

	inlen = 1;
	sockflags = fcntl(sock, F_GETFL);
	for (;;) {
		if (fcntl(sock, F_SETFL, sockflags & ~FNDELAY) == -1)
			perror("fcntl");
		while ((red = read(sock, inbuf, sizeof(inbuf))) > 0) {
			if (fcntl(sock, F_SETFL, sockflags | FNDELAY) == -1)
				perror("fcntl");

			write(1, inbuf, red);

			if (red<4 || !strncmp(&inbuf[red - 3],".\015\012", 3))
				break;
		}

		if (red < 0) {
			if (errno == EWOULDBLOCK)
				printf("[block] ");
			else
				perror("read");
		}

		if ((inlen = getline(outbuf)) < 0)
			break;

		if (write(sock, outbuf, inlen) < 0) {
			perror("socket write");
			exit(1);
		}
	}
	close(socket);

	exit(0);
}

getline(data)
char	*data;
{
	int	red;

	do {
		printf("%s %s: ", host, port), fflush(stdout);
		red = read(0, data, SOCKBUF);
	} while (red > 0 && isspace(*data));

	if (red <= 0) {
		perror("input read");
		return -1;
	}

	return red;
}
