/chromium-webcl/src/third_party/mesa/MesaLib/src/gallium/auxiliary/util/u_network.c

https://bitbucket.org/peixuan/chromium_r197479_base · C · 188 lines · 160 code · 26 blank · 2 comment · 19 complexity · 311c0f3424ede0cc15c61c170f85a734 MD5 · raw file

  1. #include "pipe/p_compiler.h"
  2. #include "util/u_network.h"
  3. #include "util/u_debug.h"
  4. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  5. # include <winsock2.h>
  6. # include <windows.h>
  7. #elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_CYGWIN)
  8. # include <sys/socket.h>
  9. # include <netinet/in.h>
  10. # include <unistd.h>
  11. # include <fcntl.h>
  12. # include <netdb.h>
  13. #else
  14. # warning "No socket implementation"
  15. #endif
  16. boolean
  17. u_socket_init()
  18. {
  19. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  20. WORD wVersionRequested;
  21. WSADATA wsaData;
  22. int err;
  23. /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
  24. wVersionRequested = MAKEWORD(1, 1);
  25. err = WSAStartup(wVersionRequested, &wsaData);
  26. if (err != 0) {
  27. debug_printf("WSAStartup failed with error: %d\n", err);
  28. return FALSE;
  29. }
  30. return TRUE;
  31. #elif defined(PIPE_HAVE_SOCKETS)
  32. return TRUE;
  33. #else
  34. return FALSE;
  35. #endif
  36. }
  37. void
  38. u_socket_stop()
  39. {
  40. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  41. WSACleanup();
  42. #endif
  43. }
  44. void
  45. u_socket_close(int s)
  46. {
  47. if (s < 0)
  48. return;
  49. #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE)
  50. shutdown(s, SHUT_RDWR);
  51. close(s);
  52. #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  53. shutdown(s, SD_BOTH);
  54. closesocket(s);
  55. #else
  56. assert(0);
  57. #endif
  58. }
  59. int u_socket_accept(int s)
  60. {
  61. #if defined(PIPE_HAVE_SOCKETS)
  62. return accept(s, NULL, NULL);
  63. #else
  64. return -1;
  65. #endif
  66. }
  67. int
  68. u_socket_send(int s, void *data, size_t size)
  69. {
  70. #if defined(PIPE_HAVE_SOCKETS)
  71. return send(s, data, size, 0);
  72. #else
  73. return -1;
  74. #endif
  75. }
  76. int
  77. u_socket_peek(int s, void *data, size_t size)
  78. {
  79. #if defined(PIPE_HAVE_SOCKETS)
  80. return recv(s, data, size, MSG_PEEK);
  81. #else
  82. return -1;
  83. #endif
  84. }
  85. int
  86. u_socket_recv(int s, void *data, size_t size)
  87. {
  88. #if defined(PIPE_HAVE_SOCKETS)
  89. return recv(s, data, size, 0);
  90. #else
  91. return -1;
  92. #endif
  93. }
  94. int
  95. u_socket_connect(const char *hostname, uint16_t port)
  96. {
  97. #if defined(PIPE_HAVE_SOCKETS)
  98. int s;
  99. struct sockaddr_in sa;
  100. struct hostent *host = NULL;
  101. memset(&sa, 0, sizeof(struct sockaddr_in));
  102. host = gethostbyname(hostname);
  103. if (!host)
  104. return -1;
  105. memcpy((char *)&sa.sin_addr,host->h_addr_list[0],host->h_length);
  106. sa.sin_family= host->h_addrtype;
  107. sa.sin_port = htons(port);
  108. s = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP);
  109. if (s < 0)
  110. return -1;
  111. if (connect(s, (struct sockaddr *)&sa, sizeof(sa))) {
  112. u_socket_close(s);
  113. return -1;
  114. }
  115. return s;
  116. #else
  117. assert(0);
  118. return -1;
  119. #endif
  120. }
  121. int
  122. u_socket_listen_on_port(uint16_t portnum)
  123. {
  124. #if defined(PIPE_HAVE_SOCKETS)
  125. int s;
  126. struct sockaddr_in sa;
  127. memset(&sa, 0, sizeof(struct sockaddr_in));
  128. sa.sin_family = AF_INET;
  129. sa.sin_port = htons(portnum);
  130. s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  131. if (s < 0)
  132. return -1;
  133. if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == -1) {
  134. u_socket_close(s);
  135. return -1;
  136. }
  137. listen(s, 0);
  138. return s;
  139. #else
  140. assert(0);
  141. return -1;
  142. #endif
  143. }
  144. void
  145. u_socket_block(int s, boolean block)
  146. {
  147. #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_APPLE)
  148. int old = fcntl(s, F_GETFL, 0);
  149. if (old == -1)
  150. return;
  151. /* TODO obey block */
  152. if (block)
  153. fcntl(s, F_SETFL, old & ~O_NONBLOCK);
  154. else
  155. fcntl(s, F_SETFL, old | O_NONBLOCK);
  156. #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  157. u_long iMode = block ? 0 : 1;
  158. ioctlsocket(s, FIONBIO, &iMode);
  159. #else
  160. assert(0);
  161. #endif
  162. }