Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipc.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (c) 2003 Tungsten Graphics, Inc.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files ("the
  5. * Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject to
  9. * the following conditions: The above copyright notice, the Tungsten
  10. * Graphics splash screen, and this permission notice shall be included
  11. * in all copies or substantial portions of the Software. THE SOFTWARE
  12. * IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  15. * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  18. * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. /*
  21. * Simple IPC API
  22. * Brian Paul
  23. */
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <netinet/in.h>
  29. #include <netinet/tcp.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <unistd.h>
  33. #include <sys/socket.h>
  34. #include "ipc.h"
  35. #if defined(IRIX) || defined(irix)
  36. typedef int socklen_t;
  37. #endif
  38. #define NO_DELAY 1
  39. #define DEFAULT_MASTER_PORT 7011
  40. /*
  41. * Return my hostname in <nameOut>.
  42. * Return 1 for success, 0 for error.
  43. */
  44. int
  45. MyHostName(char *nameOut, int maxNameLength)
  46. {
  47. int k = gethostname(nameOut, maxNameLength);
  48. return k==0;
  49. }
  50. /*
  51. * Create a socket attached to a port. Later, we can call AcceptConnection
  52. * on the socket returned from this function.
  53. * Return the new socket number or -1 if error.
  54. */
  55. int
  56. CreatePort(int *port)
  57. {
  58. char hostname[1000];
  59. struct sockaddr_in servaddr;
  60. struct hostent *hp;
  61. int so_reuseaddr = 1;
  62. int tcp_nodelay = 1;
  63. int sock, k;
  64. /* create socket */
  65. sock = socket(AF_INET, SOCK_STREAM, 0);
  66. assert(sock > 2);
  67. /* get my host name */
  68. k = gethostname(hostname, 1000);
  69. assert(k == 0);
  70. /* get hostent info */
  71. hp = gethostbyname(hostname);
  72. assert(hp);
  73. /* initialize the servaddr struct */
  74. memset(&servaddr, 0, sizeof(servaddr) );
  75. servaddr.sin_family = AF_INET;
  76. servaddr.sin_port = htons((unsigned short) (*port));
  77. memcpy((char *) &servaddr.sin_addr, hp->h_addr,
  78. sizeof(servaddr.sin_addr));
  79. /* deallocate when we exit */
  80. k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  81. (char *) &so_reuseaddr, sizeof(so_reuseaddr));
  82. assert(k==0);
  83. /* send packets immediately */
  84. #if NO_DELAY
  85. k = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  86. (char *) &tcp_nodelay, sizeof(tcp_nodelay));
  87. assert(k==0);
  88. #endif
  89. if (*port == 0)
  90. *port = DEFAULT_MASTER_PORT;
  91. k = 1;
  92. while (k && (*port < 65534)) {
  93. /* bind our address to the socket */
  94. servaddr.sin_port = htons((unsigned short) (*port));
  95. k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
  96. if (k)
  97. *port = *port + 1;
  98. }
  99. #if 0
  100. printf("###### Real Port: %d\n", *port);
  101. #endif
  102. /* listen for connections */
  103. k = listen(sock, 100);
  104. assert(k == 0);
  105. return sock;
  106. }
  107. /*
  108. * Accept a connection on the named socket.
  109. * Return a new socket for the new connection, or -1 if error.
  110. */
  111. int
  112. AcceptConnection(int socket)
  113. {
  114. struct sockaddr addr;
  115. socklen_t addrLen;
  116. int newSock;
  117. addrLen = sizeof(addr);
  118. newSock = accept(socket, &addr, &addrLen);
  119. if (newSock == 1)
  120. return -1;
  121. else
  122. return newSock;
  123. }
  124. /*
  125. * Contact the server running on the given host on the named port.
  126. * Return socket number or -1 if error.
  127. */
  128. int
  129. Connect(const char *hostname, int port)
  130. {
  131. struct sockaddr_in servaddr;
  132. struct hostent *hp;
  133. int sock, k;
  134. int tcp_nodelay = 1;
  135. assert(port);
  136. sock = socket(AF_INET, SOCK_STREAM, 0);
  137. assert(sock >= 0);
  138. hp = gethostbyname(hostname);
  139. assert(hp);
  140. memset(&servaddr, 0, sizeof(servaddr));
  141. servaddr.sin_family = AF_INET;
  142. servaddr.sin_port = htons((unsigned short) port);
  143. memcpy((char *) &servaddr.sin_addr, hp->h_addr, sizeof(servaddr.sin_addr));
  144. k = connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
  145. if (k != 0) {
  146. perror("Connect:");
  147. return -1;
  148. }
  149. #if NO_DELAY
  150. /* send packets immediately */
  151. k = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  152. (char *) &tcp_nodelay, sizeof(tcp_nodelay));
  153. assert(k==0);
  154. #endif
  155. return sock;
  156. }
  157. void
  158. CloseSocket(int socket)
  159. {
  160. close(socket);
  161. }
  162. int
  163. SendData(int socket, const void *data, int bytes)
  164. {
  165. int sent = 0;
  166. int b;
  167. while (sent < bytes) {
  168. b = write(socket, (char *) data + sent, bytes - sent);
  169. if (b <= 0)
  170. return -1; /* something broke */
  171. sent += b;
  172. }
  173. return sent;
  174. }
  175. int
  176. ReceiveData(int socket, void *data, int bytes)
  177. {
  178. int received = 0, b;
  179. while (received < bytes) {
  180. b = read(socket, (char *) data + received, bytes - received);
  181. if (b <= 0)
  182. return -1;
  183. received += b;
  184. }
  185. return received;
  186. }
  187. int
  188. SendString(int socket, const char *str)
  189. {
  190. const int len = strlen(str);
  191. int sent, b;
  192. /* first, send a 4-byte length indicator */
  193. b = write(socket, &len, sizeof(len));
  194. if (b <= 0)
  195. return -1;
  196. sent = SendData(socket, str, len);
  197. assert(sent == len);
  198. return sent;
  199. }
  200. int
  201. ReceiveString(int socket, char *str, int maxLen)
  202. {
  203. int len, received, b;
  204. /* first, read 4 bytes to see how long of string to receive */
  205. b = read(socket, &len, sizeof(len));
  206. if (b <= 0)
  207. return -1;
  208. assert(len <= maxLen); /* XXX fix someday */
  209. assert(len >= 0);
  210. received = ReceiveData(socket, str, len);
  211. assert(received != -1);
  212. assert(received == len);
  213. str[len] = 0;
  214. return received;
  215. }