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.

ev_win32.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * libev win32 compatibility cruft (_not_ a backend)
  3. *
  4. * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modifica-
  8. * tion, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  19. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  21. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  25. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26. * OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * the GNU General Public License ("GPL") version 2 or any later version,
  30. * in which case the provisions of the GPL are applicable instead of
  31. * the above. If you wish to allow the use of your version of this file
  32. * only under the terms of the GPL and not to allow others to use your
  33. * version of this file under the BSD license, indicate your decision
  34. * by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL. If you do not delete the
  36. * provisions above, a recipient may use your version of this file under
  37. * either the BSD or the GPL.
  38. */
  39. #ifdef _WIN32
  40. /* note: the comment below could not be substantiated, but what would I care */
  41. /* MSDN says this is required to handle SIGFPE */
  42. /* my wild guess would be that using something floating-pointy is required */
  43. /* for the crt to do something about it */
  44. volatile double SIGFPE_REQ = 0.0f;
  45. static SOCKET
  46. ev_tcp_socket (void)
  47. {
  48. #if EV_USE_WSASOCKET
  49. return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0);
  50. #else
  51. return socket (AF_INET, SOCK_STREAM, 0);
  52. #endif
  53. }
  54. /* oh, the humanity! */
  55. static int
  56. ev_pipe (int filedes [2])
  57. {
  58. struct sockaddr_in addr = { 0 };
  59. int addr_size = sizeof (addr);
  60. struct sockaddr_in adr2;
  61. int adr2_size = sizeof (adr2);
  62. SOCKET listener;
  63. SOCKET sock [2] = { -1, -1 };
  64. if ((listener = ev_tcp_socket ()) == INVALID_SOCKET)
  65. return -1;
  66. addr.sin_family = AF_INET;
  67. addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  68. addr.sin_port = 0;
  69. if (bind (listener, (struct sockaddr *)&addr, addr_size))
  70. goto fail;
  71. if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))
  72. goto fail;
  73. if (listen (listener, 1))
  74. goto fail;
  75. if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET)
  76. goto fail;
  77. if (connect (sock [0], (struct sockaddr *)&addr, addr_size))
  78. goto fail;
  79. /* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */
  80. /* when convenient, probably by just removing error checking altogether? */
  81. if ((sock [1] = accept (listener, 0, 0)) < 0)
  82. goto fail;
  83. /* windows vista returns fantasy port numbers for sockets:
  84. * example for two interconnected tcp sockets:
  85. *
  86. * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364
  87. * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363
  88. * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363
  89. * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365
  90. *
  91. * wow! tridirectional sockets!
  92. *
  93. * this way of checking ports seems to work:
  94. */
  95. if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))
  96. goto fail;
  97. if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))
  98. goto fail;
  99. errno = WSAEINVAL;
  100. if (addr_size != adr2_size
  101. || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */
  102. || addr.sin_port != adr2.sin_port)
  103. goto fail;
  104. closesocket (listener);
  105. #if EV_SELECT_IS_WINSOCKET
  106. filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]);
  107. filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]);
  108. #else
  109. /* when select isn't winsocket, we also expect socket, connect, accept etc.
  110. * to work on fds */
  111. filedes [0] = sock [0];
  112. filedes [1] = sock [1];
  113. #endif
  114. return 0;
  115. fail:
  116. closesocket (listener);
  117. if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
  118. if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
  119. return -1;
  120. }
  121. #undef pipe
  122. #define pipe(filedes) ev_pipe (filedes)
  123. #define EV_HAVE_EV_TIME 1
  124. ev_tstamp
  125. ev_time (void)
  126. {
  127. FILETIME ft;
  128. ULARGE_INTEGER ui;
  129. GetSystemTimeAsFileTime (&ft);
  130. ui.u.LowPart = ft.dwLowDateTime;
  131. ui.u.HighPart = ft.dwHighDateTime;
  132. /* also, msvc cannot convert ulonglong to double... yes, it is that sucky */
  133. return EV_TS_FROM_USEC (((LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-1));
  134. }
  135. #endif