aboutsummaryrefslogtreecommitdiff
path: root/externals/libressl/crypto/bio/b_win.c
blob: e261cd2c426f2f1d1f0ab24dcffd80242ae35009 (plain) (blame)
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
/*
 * Public domain
 *
 * Dongsheng Song <dongsheng.song@gmail.com>
 * Brent Cook <bcook@openbsd.org>
 */

#include <ws2tcpip.h>

#include <openssl/bio.h>
#include <openssl/err.h>

int
BIO_sock_init(void)
{
	/*
	 * WSAStartup loads the winsock .dll and initializes the networking
	 * stack on Windows, or simply increases the reference count.
	 */
	static struct WSAData wsa_state = {0};
	WORD version_requested = MAKEWORD(2, 2);
	static int wsa_init_done = 0;
	if (!wsa_init_done) {
		if (WSAStartup(version_requested, &wsa_state) != 0) {
			int err = WSAGetLastError();
			SYSerror(err);
			BIOerror(BIO_R_WSASTARTUP);
			return (-1);
		}
		wsa_init_done = 1;
	}
 	return (1);
}

void
BIO_sock_cleanup(void)
{
	/*
	 * We could call WSACleanup here, but it is easy to get it wrong. Since
	 * this API provides no way to even tell if it failed, there is no safe
	 * way to expose that functionality here.
	 *
	 * The cost of leaving the networking DLLs loaded may have been large
	 * during the Windows 3.1/win32s era, but it is small in modern
	 * contexts, so don't bother.
	 */
}

int
BIO_socket_nbio(int s, int mode)
{
	u_long value = mode;
	return ioctlsocket(s, FIONBIO, &value) != SOCKET_ERROR;
}