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