File: initWinSock.c

package info (click to toggle)
haskell-network 3.1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: sh: 3,264; haskell: 2,002; ansic: 536; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 990 bytes parent folder | download | duplicates (3)
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