1 /*
2  * winsock.h - Windows socket compatibility layer
3  *
4  * Copyright (C) 2013 - 2019, Max Lv <max.c.lv@gmail.com>
5  *
6  * This file is part of the shadowsocks-libev.
7  *
8  * shadowsocks-libev is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * shadowsocks-libev is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with shadowsocks-libev; see the file COPYING. If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef _WINSOCK_H
24 #define _WINSOCK_H
25 
26 #ifdef __MINGW32__
27 
28 // Target NT6
29 #ifndef WIN32_LEAN_AND_MEAN
30 #define WIN32_LEAN_AND_MEAN
31 #endif
32 
33 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
34 #undef _WIN32_WINNT
35 #endif
36 
37 #ifndef _WIN32_WINNT
38 #define _WIN32_WINNT 0x0600
39 #endif
40 
41 // Winsock headers
42 #include <windows.h>
43 #include <winsock2.h>
44 #include <ws2tcpip.h>
45 #include <mswsock.h>
46 
47 // Override POSIX error number
48 #ifdef errno
49 #undef errno
50 #endif
51 #define errno WSAGetLastError()
52 
53 #ifdef EWOULDBLOCK
54 #undef EWOULDBLOCK
55 #endif
56 #define EWOULDBLOCK WSAEWOULDBLOCK
57 
58 #ifdef CONNECT_IN_PROGRESS
59 #undef CONNECT_IN_PROGRESS
60 #endif
61 #define CONNECT_IN_PROGRESS WSAEWOULDBLOCK
62 
63 #ifdef EOPNOTSUPP
64 #undef EOPNOTSUPP
65 #endif
66 #define EOPNOTSUPP WSAEOPNOTSUPP
67 
68 #ifdef EPROTONOSUPPORT
69 #undef EPROTONOSUPPORT
70 #endif
71 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
72 
73 #ifdef ENOPROTOOPT
74 #undef ENOPROTOOPT
75 #endif
76 #define ENOPROTOOPT WSAENOPROTOOPT
77 
78 // Check if ConnectEx supported in header
79 #ifdef WSAID_CONNECTEX
80 // Hardcode TCP fast open option
81 #ifndef TCP_FASTOPEN
82 #define TCP_FASTOPEN 15
83 #endif
84 // Enable TFO support
85 #define TCP_FASTOPEN_WINSOCK 1
86 #endif
87 
88 // Override close function
89 #define close(fd) closesocket(fd)
90 
91 // Override MinGW functions
92 #define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char *)(d), e)
93 #define inet_ntop(a, b, c, d) inet_ntop(a, (void *)(b), c, d)
94 
95 // Override Windows built-in functions
96 #ifdef ERROR
97 #undef ERROR
98 #endif
99 #define ERROR(s) ss_error(s)
100 
101 #ifdef gai_strerror
102 #undef gai_strerror
103 #endif
104 #define gai_strerror(e) ss_gai_strerror(e)
105 char *ss_gai_strerror(int ecode);
106 
107 // Missing Unix functions
108 #define sleep(x) Sleep((x) * 1000)
109 #define bzero(s, n) memset(s, 0, n)
110 #define strndup(s, n) ss_strndup(s, n)
111 
112 // Winsock compatibility functions
113 int setnonblocking(SOCKET socket);
114 void winsock_init(void);
115 void winsock_cleanup(void);
116 #ifdef TCP_FASTOPEN_WINSOCK
117 LPFN_CONNECTEX winsock_getconnectex(void);
118 int winsock_dummybind(SOCKET fd, struct sockaddr *sa);
119 #endif
120 
121 #endif // __MINGW32__
122 
123 #endif // _WINSOCK_H
124