/* sendsocket.c
 *
 * Copyright (C) 1999-2003 Paul Boersma
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * pb 1999/12/21
 * pb 2002/03/07 GPL
 * pb 2003/12/19 return NULL if no error
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if defined (UNIX) || defined (__MACH__)
	#include <sys/types.h>
	#include <unistd.h>
	#include <ctype.h>
	#include <netdb.h>
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
#elif defined (_WIN32)
	/* CodeWarrior 5 BUG FIX:
	   The following three files prevent windows.h from being included,
	   which would give redeclaration errors. */
	#include <windef.h>
	#include <winbase.h>
	#define _INC_WINDOWS
	#include <winsock.h>
#endif
#include "sendsocket.h"

char * sendsocket (const char *hostNameAndPort, const char *command) {
	static char result [200];
	char hostName [61], *colon;
	int port;
	if (strlen (hostNameAndPort) > 60)
		return "(sendsocket:) Host-name-and-port string too long.";
	strcpy (hostName, hostNameAndPort);
	colon = strchr (hostName, ':');
	if (colon == NULL)
		return "(sendsocket:) Missing colon. Host name and port should be in the format \"hostName:port\".";
	*colon = '\0';
	port = atoi (colon + 1);
#ifdef macintosh
	(void) command;
	sprintf (result, "Sendsocket not implemented on Macintosh.\n"
		"No message sent to port %d on host %s.", port, hostName);
#else
{
	struct hostent *host;
	struct sockaddr_in his_addr;   /* Address of remote host. */
	int stat, sokket = -1;
	long bytesSent;
	result [0] = '\0';   /* No error yet. */

	#ifdef _WIN32
	{
		static int initialized;
		if (! initialized) {
			WSADATA wsaData;
			WORD requestedWinsockVersion = MAKEWORD (1,1);   /* Require version 1.1. */
			if (WSAStartup (requestedWinsockVersion, & wsaData)) {
				return "(sendsocket:) Cannot find a usable WINSOCK.DLL.";
			}
			/*if (LOBYTE (wsaData. wVersion) != 1 || HIBYTE (wsaData. wVersion) != 1) { 
				WSACleanup (); 
				return "(sendsocket:) WINSOCK.DLL does not support version 1.1.";
			}*/
			initialized = 1;
		}
	}
	#endif

	if (isdigit (hostName [0])) {
		if ((his_addr. sin_addr.s_addr = inet_addr ((char *) hostName)) == 0xFFFFFFFF) {
			sprintf (result, "(sendsocket:) Invalid hostname \"%s\".", hostName);
			return result;
		}
		his_addr. sin_family = AF_INET;
	} else if ((host = gethostbyname (hostName)) != NULL) {
		his_addr. sin_family = host -> h_addrtype;
		memcpy (& his_addr. sin_addr, host -> h_addr, host -> h_length);
	} else {
		sprintf (result, "(sendsocket:) Unknown host \"%s\".", hostName);
		return result;
	}
	his_addr. sin_port = htons (port);

	sokket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sokket < 0) {
		strcpy (result, "(sendsocket:) Cannot create socket.");
		goto end;
	}

	stat = connect (sokket, (struct sockaddr *) & his_addr, sizeof (struct sockaddr_in));
	if (stat != 0) {
		strcpy (result, "(sendsocket:) Cannot connect to socket.");
		goto end;
	}

	/*
		Melder_casual ("Connected to port %d on host %s.", ntohs (his_addr. sin_port), inet_ntoa (his_addr. sin_addr));
	*/
   
	bytesSent = send (sokket, command, strlen (command) + 1, 0);   /* Include null byte. */
	if (bytesSent < 0) {
		strcpy (result, "(sendsocket:) Data not sent.");
		goto end;
	}
	/*
		Melder_casual ("sendsocket: %d bytes written.", bytesSent);
	*/
end:
	if (sokket >= 0) {
		#if defined (UNIX) || defined (__MACH__)
			close (sokket);
		#elif defined (_WIN32)
			closesocket (sokket);
		#endif
	}
}
#endif
	return result [0] == '\0' ? NULL: result;
}

/* End of file sendsocket.c */
