1 | #ifndef __WINUTILS_H_
|
---|
2 | # define __WINUTILS_H_
|
---|
3 |
|
---|
4 | # include <iprt/cdefs.h>
|
---|
5 |
|
---|
6 | # ifdef RT_OS_WINDOWS
|
---|
7 | # include <WinSock2.h>
|
---|
8 | # include <ws2tcpip.h>
|
---|
9 | # include <mswsock.h>
|
---|
10 | # include <Windows.h>
|
---|
11 | # include <iprt/err.h>
|
---|
12 | # include <iprt/net.h>
|
---|
13 | # include <iprt/log.h>
|
---|
14 | /**
|
---|
15 | * Inclusion of lwip/def.h was added here to avoid conflict of definitions
|
---|
16 | * of hton-family functions in LWIP and windock's headers.
|
---|
17 | */
|
---|
18 | # include <lwip/def.h>
|
---|
19 |
|
---|
20 | # ifndef PF_LOCAL
|
---|
21 | # define PF_LOCAL AF_INET
|
---|
22 | # endif
|
---|
23 |
|
---|
24 | # ifdef DEBUG
|
---|
25 | # define err(code,...) do { \
|
---|
26 | AssertMsgFailed((__VA_ARGS__)); \
|
---|
27 | }while(0)
|
---|
28 | #else
|
---|
29 | # define err(code,...) do { \
|
---|
30 | DPRINTF0((__VA_ARGS__)); \
|
---|
31 | ExitProcess(code); \
|
---|
32 | }while(0)
|
---|
33 | #endif
|
---|
34 | # define errx err
|
---|
35 | # define __func__ __FUNCTION__
|
---|
36 | # define __attribute__(x) /* IGNORE */
|
---|
37 |
|
---|
38 | # define SOCKERRNO() (WSAGetLastError())
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * "Windows Sockets Error Codes" obtained with WSAGetLastError().
|
---|
42 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
|
---|
43 | *
|
---|
44 | * This block of error codes from <winsock2.h> conflicts with "POSIX
|
---|
45 | * supplement" error codes from <errno.h>, but we don't expect to ever
|
---|
46 | * encounter the latter in the proxy code, so redefine them to their
|
---|
47 | * unixy names.
|
---|
48 | */
|
---|
49 | # undef EWOULDBLOCK
|
---|
50 | # define EWOULDBLOCK WSAEWOULDBLOCK
|
---|
51 | # undef EINPROGRESS
|
---|
52 | # define EINPROGRESS WSAEINPROGRESS
|
---|
53 | # undef EALREADY
|
---|
54 | # define EALREADY WSAEALREADY
|
---|
55 | # undef ENOTSOCK
|
---|
56 | # define ENOTSOCK WSAENOTSOCK
|
---|
57 | # undef EDESTADDRREQ
|
---|
58 | # define EDESTADDRREQ WSAEDESTADDRREQ
|
---|
59 | # undef EMSGSIZE
|
---|
60 | # define EMSGSIZE WSAEMSGSIZE
|
---|
61 | # undef EPROTOTYPE
|
---|
62 | # define EPROTOTYPE WSAEPROTOTYPE
|
---|
63 | # undef ENOPROTOOPT
|
---|
64 | # define ENOPROTOOPT WSAENOPROTOOPT
|
---|
65 | # undef EPROTONOSUPPORT
|
---|
66 | # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
|
---|
67 | # undef ESOCKTNOSUPPORT
|
---|
68 | # define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
|
---|
69 | # undef EOPNOTSUPP
|
---|
70 | # define EOPNOTSUPP WSAEOPNOTSUPP
|
---|
71 | # undef EPFNOSUPPORT
|
---|
72 | # define EPFNOSUPPORT WSAEPFNOSUPPORT
|
---|
73 | # undef EAFNOSUPPORT
|
---|
74 | # define EAFNOSUPPORT WSAEAFNOSUPPORT
|
---|
75 | # undef EADDRINUSE
|
---|
76 | # define EADDRINUSE WSAEADDRINUSE
|
---|
77 | # undef EADDRNOTAVAIL
|
---|
78 | # define EADDRNOTAVAIL WSAEADDRNOTAVAIL
|
---|
79 | # undef ENETDOWN
|
---|
80 | # define ENETDOWN WSAENETDOWN
|
---|
81 | # undef ENETUNREACH
|
---|
82 | # define ENETUNREACH WSAENETUNREACH
|
---|
83 | # undef ENETRESET
|
---|
84 | # define ENETRESET WSAENETRESET
|
---|
85 | # undef ECONNABORTED
|
---|
86 | # define ECONNABORTED WSAECONNABORTED
|
---|
87 | # undef ECONNRESET
|
---|
88 | # define ECONNRESET WSAECONNRESET
|
---|
89 | # undef ENOBUFS
|
---|
90 | # define ENOBUFS WSAENOBUFS
|
---|
91 | # undef EISCONN
|
---|
92 | # define EISCONN WSAEISCONN
|
---|
93 | # undef ENOTCONN
|
---|
94 | # define ENOTCONN WSAENOTCONN
|
---|
95 | # undef ESHUTDOWN
|
---|
96 | # define ESHUTDOWN WSAESHUTDOWN
|
---|
97 | # undef ETOOMANYREFS
|
---|
98 | # define ETOOMANYREFS WSAETOOMANYREFS
|
---|
99 | # undef ETIMEDOUT
|
---|
100 | # define ETIMEDOUT WSAETIMEDOUT
|
---|
101 | # undef ECONNREFUSED
|
---|
102 | # define ECONNREFUSED WSAECONNREFUSED
|
---|
103 | # undef ELOOP
|
---|
104 | # define ELOOP WSAELOOP
|
---|
105 | # undef ENAMETOOLONG
|
---|
106 | # define ENAMETOOLONG WSAENAMETOOLONG
|
---|
107 | # undef EHOSTDOWN
|
---|
108 | # define EHOSTDOWN WSAEHOSTDOWN
|
---|
109 | # undef EHOSTUNREACH
|
---|
110 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * parameters to shutdown (2) with Winsock2
|
---|
114 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
|
---|
115 | */
|
---|
116 | # define SHUT_RD SD_RECEIVE
|
---|
117 | # define SHUT_WR SD_SEND
|
---|
118 | # define SHUT_RDWR SD_BOTH
|
---|
119 |
|
---|
120 | typedef ULONG nfds_t;
|
---|
121 |
|
---|
122 | typedef WSABUF IOVEC;
|
---|
123 |
|
---|
124 | # define IOVEC_GET_BASE(iov) ((iov).buf)
|
---|
125 | # define IOVEC_SET_BASE(iov, b) ((iov).buf = (b))
|
---|
126 |
|
---|
127 | # define IOVEC_GET_LEN(iov) ((iov).len)
|
---|
128 | # define IOVEC_SET_LEN(iov, l) ((iov).len = (ULONG)(l))
|
---|
129 |
|
---|
130 | #if _WIN32_WINNT < 0x0600
|
---|
131 | /* otherwise defined the other way around in ws2def.h */
|
---|
132 | #define cmsghdr _WSACMSGHDR
|
---|
133 |
|
---|
134 | #undef CMSG_DATA /* wincrypt.h can byte my shiny metal #undef */
|
---|
135 | #define CMSG_DATA WSA_CMSG_DATA
|
---|
136 | #define CMSG_LEN WSA_CMSG_LEN
|
---|
137 | #define CMSG_SPACE WSA_CMSG_SPACE
|
---|
138 |
|
---|
139 | #define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
|
---|
140 | #define CMSG_NXTHDR WSA_CMSG_NXTHDR
|
---|
141 | #endif /* _WIN32_WINNT < 0x0600 - provide unglified CMSG names */
|
---|
142 |
|
---|
143 | RT_C_DECLS_BEGIN
|
---|
144 | int RTWinSocketPair(int domain, int type, int protocol, SOCKET socket_vector[2]);
|
---|
145 | RT_C_DECLS_END
|
---|
146 |
|
---|
147 | # else /* !RT_OS_WINDOWS */
|
---|
148 |
|
---|
149 | # include <errno.h>
|
---|
150 |
|
---|
151 | # define SOCKET int
|
---|
152 | # define INVALID_SOCKET (-1)
|
---|
153 | # define SOCKET_ERROR (-1)
|
---|
154 |
|
---|
155 | # define SOCKERRNO() (errno)
|
---|
156 |
|
---|
157 | # define closesocket(s) close(s)
|
---|
158 | # define ioctlsocket(s, req, arg) ioctl((s), (req), (arg))
|
---|
159 |
|
---|
160 | typedef struct iovec IOVEC;
|
---|
161 |
|
---|
162 | # define IOVEC_GET_BASE(iov) ((iov).iov_base)
|
---|
163 | # define IOVEC_SET_BASE(iov, b) ((iov).iov_base = (b))
|
---|
164 |
|
---|
165 | # define IOVEC_GET_LEN(iov) ((iov).iov_len)
|
---|
166 | # define IOVEC_SET_LEN(iov, l) ((iov).iov_len = (l))
|
---|
167 | # endif
|
---|
168 |
|
---|
169 | DECLINLINE(int)
|
---|
170 | proxy_error_is_transient(int error)
|
---|
171 | {
|
---|
172 | # if !defined(RT_OS_WINDOWS)
|
---|
173 | return error == EWOULDBLOCK
|
---|
174 | # if EAGAIN != EWOULDBLOCK
|
---|
175 | || error == EAGAIN
|
---|
176 | # endif
|
---|
177 | || error == EINTR
|
---|
178 | || error == ENOBUFS
|
---|
179 | || error == ENOMEM;
|
---|
180 | # else
|
---|
181 | return error == WSAEWOULDBLOCK
|
---|
182 | || error == WSAEINTR /* NB: we don't redefine EINTR above */
|
---|
183 | || error == WSAENOBUFS;
|
---|
184 | # endif
|
---|
185 | }
|
---|
186 |
|
---|
187 | #endif
|
---|