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