1 #include "HsNet.h"
2 #include "HsFFI.h"
3 
4 #if defined(_WIN32)
5 
6 static int winsock_inited = 0;
7 
8 static void
shutdownHandler(void)9 shutdownHandler(void)
10 {
11   WSACleanup();
12 }
13 
14 /* Initialising WinSock... */
15 int
initWinSock()16 initWinSock ()
17 {
18   WORD wVersionRequested;
19   WSADATA wsaData;
20   int err;
21 
22   if (!winsock_inited) {
23     wVersionRequested = MAKEWORD( 2, 2 );
24 
25     err = WSAStartup ( wVersionRequested, &wsaData );
26 
27     if ( err != 0 ) {
28        return err;
29     }
30 
31     if ( LOBYTE( wsaData.wVersion ) != 2 ||
32        HIBYTE( wsaData.wVersion ) != 2 ) {
33       WSACleanup();
34       return (-1);
35     }
36 
37     atexit(shutdownHandler);
38     winsock_inited = 1;
39   }
40   return 0;
41 }
42 
43 SOCKET
wsaDuplicate(SOCKET s)44 wsaDuplicate (SOCKET s)
45 {
46   WSAPROTOCOL_INFOW protocolInfo;
47   if (WSADuplicateSocketW (s, GetCurrentProcessId (), &protocolInfo) != 0)
48     return -1;
49 
50   SOCKET res = WSASocketW(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
51                           FROM_PROTOCOL_INFO, &protocolInfo, 0, 0);
52   if (res == SOCKET_ERROR)
53     return -1;
54 
55   return res;
56 }
57 
58 #endif
59