/* Socket functions to support our mailclient
 */
#include "tcpSocket.h"

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

/* Create Socket
 * returns an Handle to our socket
 */
tSocket *createSocket(tSocket *aInSocket, char *sInHostName, int iInPort) {

	if (aInSocket == NULL)
		return (NULL);
	aInSocket->sHostName = sInHostName;
	aInSocket->iPort = iInPort;
	aInSocket->iSocketHandle = -1;
	aInSocket->iIsConnected = 0;
	aInSocket->aHostAddress = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in));
	aInSocket->aHostEntry = (struct hostent *) malloc(sizeof(struct hostent));
	
	aInSocket->iSocketHandle = socket(PF_INET, SOCK_STREAM, 0);
	aInSocket->aHostEntry = gethostbyname(aInSocket->sHostName);
	
   if (aInSocket->iSocketHandle < 0) return false;
	    if (!(aInSocket->aHostEntry)) {
		     // closeConnection();
		     return NULL;
	}
	
	aInSocket->aHostAddress->sin_family = AF_INET;
	aInSocket->aHostAddress->sin_port = htons(aInSocket->iPort);
	
	printf(" blub!"); fflush(stdout);

	memcpy(&(aInSocket->aHostAddress->sin_addr), (aInSocket->aHostEntry->h_addr), 
			 aInSocket->aHostEntry->h_length);
	printf(" blub!"); fflush(stdout);
	aInSocket->iIsConnected = 1;
	printf(" blub!"); fflush(stdout);
	return aInSocket;
}

/* Connect the socket
 * returns the socket handle
 */
tSocket *connectSocket(tSocket *aInSocket){
	int ires = connect(aInSocket->iSocketHandle, (struct sockaddr*)aInSocket->aHostAddress, sizeof(struct sockaddr_in));
	if (ires < 0){
   perror("Error connecting to the HTTP-socket!");
   fflush(stdout);
		switch (errno) {
			case HOST_NOT_FOUND: return NULL; 						//(errHostNotFound);
			case NO_ADDRESS: return NULL;								//(errAdrNotFound);
			case NO_RECOVERY: return NULL; 							//(errServer);
			case TRY_AGAIN: return NULL; 								//(errTryAgain);
			break;
		}
	   return NULL;				// (errConnect);
	}
	return aInSocket;   // later: true
}

char readCharFromSocket(tSocket *aInSocket){
	char c = 0;


   if (aInSocket->iSocketHandle < 0) return '\0';

	// speed
	if (read(aInSocket->iSocketHandle, &c, 1) == 0){
		aInSocket->iIsConnected = 0;
		return 0;
	} else {
		while ((c == 13) || (c == 0)){   // why do I do this?
			if (read(aInSocket->iSocketHandle, &c, 1) == 0){
				aInSocket->iIsConnected = 0;
				return 0;
			}
		}
	}
	return c;
}

/* puts a NULL-terminated String out to the socket 
 * identified by aInSocket handle
 */
tSocket *writeLineToSocket(tSocket *aInSocket, const char *sMessage){
	char *sSendString = NULL;

   if (aInSocket->iSocketHandle < 0) return NULL;
	if (sMessage == NULL) return NULL;
   
	sSendString = (char *) malloc(strlen(sMessage)+2+1);
	sprintf (sSendString, "%s%c%c", sMessage,  '\r', '\n');
	sSendString[strlen(sMessage)+2] = '\0';
	write (aInSocket->iSocketHandle, sSendString, strlen(sSendString)); 
	fsync (aInSocket->iSocketHandle);
   
	if (sSendString != NULL){
		free(sSendString);
		sSendString = NULL;
	}
   return aInSocket;
}

