1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/* sendsocket.c
*
* Copyright (C) 1999-2006 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
* pb 2004/10/11 re-included windows.h include file (undoes fix of CW5 include-file bug)
* pb 2004/10/11 user-readable error messages
* pb 2006/10/28 erased MacOS 9 stuff
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if defined (UNIX) || defined (macintosh)
#if defined (macintosh)
/* Prevent some warnings with MacOS X 10.4.2 / CodeWarrior 9.6 / Xcode 2.0 */
#define GATEWAY 0
#define SENDFILE 0
#endif
#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)
#include <windows.h>
#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 "Cannot send to socket because the host-name-and-port string is too long.";
strcpy (hostName, hostNameAndPort);
colon = strchr (hostName, ':');
if (colon == NULL)
return "Cannot send to socket because a colon is missing.\nHost name and port should be in the format \"hostName:port\".";
*colon = '\0';
port = atoi (colon + 1);
{
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 "Cannot send to socket because the socket library (WINSOCK.DLL) is not available, "
"too old, or otherwise unusable.";
}
initialized = 1;
}
}
#endif
if (isdigit (hostName [0])) {
if ((his_addr. sin_addr.s_addr = inet_addr ((char *) hostName)) == 0xFFFFFFFF) {
sprintf (result, "Cannot send to socket because the hostname \"%s\" is invalid.", 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, "Cannot send to socket because the host \"%s\" is unknown.", hostName);
return result;
}
his_addr. sin_port = htons (port);
sokket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sokket < 0) {
strcpy (result, "Cannot send to socket because the socket cannot be created.");
goto end;
}
stat = connect (sokket, (struct sockaddr *) & his_addr, sizeof (struct sockaddr_in));
if (stat != 0) {
strcpy (result, "Cannot send to socket because the connection cannot be made.");
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, "Data not sent to socket.");
goto end;
}
/*
Melder_casual ("sendsocket: %d bytes written.", bytesSent);
*/
end:
if (sokket >= 0) {
#if defined (UNIX) || defined (macintosh)
close (sokket);
#elif defined (_WIN32)
closesocket (sokket);
#endif
}
}
return result [0] == '\0' ? NULL: result;
}
/* End of file sendsocket.c */
|