sendsocket.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* sendsocket.c
  2. *
  3. * Copyright (C) 1999-2006,2016 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdlib.h>
  19. #ifdef NO_NETWORK
  20. /*
  21. If Praat is to be linked statically for reasons of having old C and C++ libraries
  22. on the target system, then it cannot use networking functions, because these tend
  23. to require the library version that was used for linking (on Linux).
  24. */
  25. #else
  26. #include <string.h>
  27. #include <stdio.h>
  28. #if defined (UNIX) || defined (macintosh)
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #include <ctype.h>
  32. #include <netdb.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <arpa/inet.h>
  36. #elif defined (_WIN32)
  37. #include <windows.h>
  38. #include <winsock.h>
  39. #endif
  40. #endif
  41. #include "sendsocket.h"
  42. char * sendsocket (const char *hostNameAndPort, const char *command) {
  43. #ifdef NO_NETWORK
  44. return NULL;
  45. #else
  46. static char result [200];
  47. char hostName [61], *colon;
  48. int port;
  49. if (strlen (hostNameAndPort) > 60)
  50. return "Cannot send to socket because the host-name-and-port string is too long.";
  51. strcpy (hostName, hostNameAndPort);
  52. colon = strchr (hostName, ':');
  53. if (colon == NULL)
  54. return "Cannot send to socket because a colon is missing.\nHost name and port should be in the format \"hostName:port\".";
  55. *colon = '\0';
  56. port = atoi (colon + 1);
  57. {
  58. struct hostent *host;
  59. struct sockaddr_in his_addr; // address of remote host
  60. int stat, sokket = -1;
  61. long bytesSent;
  62. result [0] = '\0'; // no error yet
  63. #ifdef _WIN32
  64. {
  65. static int initialized;
  66. if (! initialized) {
  67. WSADATA wsaData;
  68. WORD requestedWinsockVersion = MAKEWORD (1,1); // require version 1.1
  69. if (WSAStartup (requestedWinsockVersion, & wsaData)) {
  70. return "Cannot send to socket because the socket library (WINSOCK.DLL) is not available, "
  71. "too old, or otherwise unusable.";
  72. }
  73. initialized = 1;
  74. }
  75. }
  76. #endif
  77. if (isdigit (hostName [0])) {
  78. if ((his_addr. sin_addr.s_addr = inet_addr ((char *) hostName)) == 0xFFFFFFFF) {
  79. sprintf (result, "Cannot send to socket because the hostname \"%s\" is invalid.", hostName);
  80. return result;
  81. }
  82. his_addr. sin_family = AF_INET;
  83. } else if ((host = gethostbyname (hostName)) != NULL) {
  84. his_addr. sin_family = host -> h_addrtype;
  85. memcpy (& his_addr. sin_addr, host -> h_addr, host -> h_length);
  86. } else {
  87. sprintf (result, "Cannot send to socket because the host \"%s\" is unknown.", hostName);
  88. return result;
  89. }
  90. his_addr. sin_port = htons (port);
  91. sokket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  92. if (sokket < 0) {
  93. strcpy (result, "Cannot send to socket because the socket cannot be created.");
  94. goto end;
  95. }
  96. stat = connect (sokket, (struct sockaddr *) & his_addr, sizeof (struct sockaddr_in));
  97. if (stat != 0) {
  98. strcpy (result, "Cannot send to socket because the connection cannot be made.");
  99. goto end;
  100. }
  101. /*
  102. Melder_casual (U"Connected to port ", ntohs (his_addr. sin_port), U" on host ", Melder_peek8to32 (inet_ntoa (his_addr. sin_addr)), U".");
  103. */
  104. bytesSent = send (sokket, command, strlen (command) + 1, 0); /* Include null byte. */
  105. if (bytesSent < 0) {
  106. strcpy (result, "Data not sent to socket.");
  107. goto end;
  108. }
  109. /*
  110. Melder_casual (U"sendsocket: ", bytesSent, U" bytes written.");
  111. */
  112. end:
  113. if (sokket >= 0) {
  114. #if defined (UNIX) || defined (macintosh)
  115. close (sokket);
  116. #elif defined (_WIN32)
  117. closesocket (sokket);
  118. #endif
  119. }
  120. }
  121. return result [0] == '\0' ? NULL: result;
  122. #endif
  123. }
  124. /* End of file sendsocket.c */