Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / crypto / bio / b_win.c
blobb8d01ae20f5f0f536ee61fdfc2dc408c3a1c4ccc
1 /*
2 * Public domain
4 * Dongsheng Song <dongsheng.song@gmail.com>
5 * Brent Cook <bcook@openbsd.org>
6 */
8 #include <ws2tcpip.h>
10 #include <openssl/bio.h>
11 #include <openssl/err.h>
13 int
14 BIO_sock_init(void)
17 * WSAStartup loads the winsock .dll and initializes the networking
18 * stack on Windows, or simply increases the reference count.
20 static struct WSAData wsa_state = {0};
21 WORD version_requested = MAKEWORD(2, 2);
22 static int wsa_init_done = 0;
23 if (!wsa_init_done) {
24 if (WSAStartup(version_requested, &wsa_state) != 0) {
25 int err = WSAGetLastError();
26 SYSerr(SYS_F_WSASTARTUP, err);
27 BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
28 return (-1);
30 wsa_init_done = 1;
32 return (1);
35 void
36 BIO_sock_cleanup(void)
39 * We could call WSACleanup here, but it is easy to get it wrong. Since
40 * this API provides no way to even tell if it failed, there is no safe
41 * way to expose that functionality here.
43 * The cost of leaving the networking DLLs loaded may have been large
44 * during the Windows 3.1/win32s era, but it is small in modern
45 * contexts, so don't bother.
49 int
50 BIO_socket_nbio(int s, int mode)
52 u_long value = mode;
53 return ioctlsocket(s, FIONBIO, &value) != SOCKET_ERROR;