VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/winutils.h@ 52934

Last change on this file since 52934 was 52286, checked in by vboxsync, 10 years ago

NAT/Net: Add SET_SOCKERRNO() macro. Use it avoid clobbering errno.

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
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# define SET_SOCKERRNO(error) do { WSASetLastError(error); } while (0)
40
41/**
42 * "Windows Sockets Error Codes" obtained with WSAGetLastError().
43 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
44 *
45 * This block of error codes from <winsock2.h> conflicts with "POSIX
46 * supplement" error codes from <errno.h>, but we don't expect to ever
47 * encounter the latter in the proxy code, so redefine them to their
48 * unixy names.
49 */
50# undef EWOULDBLOCK
51# define EWOULDBLOCK WSAEWOULDBLOCK
52# undef EINPROGRESS
53# define EINPROGRESS WSAEINPROGRESS
54# undef EALREADY
55# define EALREADY WSAEALREADY
56# undef ENOTSOCK
57# define ENOTSOCK WSAENOTSOCK
58# undef EDESTADDRREQ
59# define EDESTADDRREQ WSAEDESTADDRREQ
60# undef EMSGSIZE
61# define EMSGSIZE WSAEMSGSIZE
62# undef EPROTOTYPE
63# define EPROTOTYPE WSAEPROTOTYPE
64# undef ENOPROTOOPT
65# define ENOPROTOOPT WSAENOPROTOOPT
66# undef EPROTONOSUPPORT
67# define EPROTONOSUPPORT WSAEPROTONOSUPPORT
68# undef ESOCKTNOSUPPORT
69# define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
70# undef EOPNOTSUPP
71# define EOPNOTSUPP WSAEOPNOTSUPP
72# undef EPFNOSUPPORT
73# define EPFNOSUPPORT WSAEPFNOSUPPORT
74# undef EAFNOSUPPORT
75# define EAFNOSUPPORT WSAEAFNOSUPPORT
76# undef EADDRINUSE
77# define EADDRINUSE WSAEADDRINUSE
78# undef EADDRNOTAVAIL
79# define EADDRNOTAVAIL WSAEADDRNOTAVAIL
80# undef ENETDOWN
81# define ENETDOWN WSAENETDOWN
82# undef ENETUNREACH
83# define ENETUNREACH WSAENETUNREACH
84# undef ENETRESET
85# define ENETRESET WSAENETRESET
86# undef ECONNABORTED
87# define ECONNABORTED WSAECONNABORTED
88# undef ECONNRESET
89# define ECONNRESET WSAECONNRESET
90# undef ENOBUFS
91# define ENOBUFS WSAENOBUFS
92# undef EISCONN
93# define EISCONN WSAEISCONN
94# undef ENOTCONN
95# define ENOTCONN WSAENOTCONN
96# undef ESHUTDOWN
97# define ESHUTDOWN WSAESHUTDOWN
98# undef ETOOMANYREFS
99# define ETOOMANYREFS WSAETOOMANYREFS
100# undef ETIMEDOUT
101# define ETIMEDOUT WSAETIMEDOUT
102# undef ECONNREFUSED
103# define ECONNREFUSED WSAECONNREFUSED
104# undef ELOOP
105# define ELOOP WSAELOOP
106# undef ENAMETOOLONG
107# define ENAMETOOLONG WSAENAMETOOLONG
108# undef EHOSTDOWN
109# define EHOSTDOWN WSAEHOSTDOWN
110# undef EHOSTUNREACH
111# define EHOSTUNREACH WSAEHOSTUNREACH
112
113/**
114 * parameters to shutdown (2) with Winsock2
115 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
116 */
117# define SHUT_RD SD_RECEIVE
118# define SHUT_WR SD_SEND
119# define SHUT_RDWR SD_BOTH
120
121typedef ULONG nfds_t;
122
123typedef WSABUF IOVEC;
124
125# define IOVEC_GET_BASE(iov) ((iov).buf)
126# define IOVEC_SET_BASE(iov, b) ((iov).buf = (b))
127
128# define IOVEC_GET_LEN(iov) ((iov).len)
129# define IOVEC_SET_LEN(iov, l) ((iov).len = (ULONG)(l))
130
131#if _WIN32_WINNT < 0x0600
132/* otherwise defined the other way around in ws2def.h */
133#define cmsghdr _WSACMSGHDR
134
135#undef CMSG_DATA /* wincrypt.h can byte my shiny metal #undef */
136#define CMSG_DATA WSA_CMSG_DATA
137#define CMSG_LEN WSA_CMSG_LEN
138#define CMSG_SPACE WSA_CMSG_SPACE
139
140#define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
141#define CMSG_NXTHDR WSA_CMSG_NXTHDR
142#endif /* _WIN32_WINNT < 0x0600 - provide unglified CMSG names */
143
144RT_C_DECLS_BEGIN
145int RTWinSocketPair(int domain, int type, int protocol, SOCKET socket_vector[2]);
146RT_C_DECLS_END
147
148# else /* !RT_OS_WINDOWS */
149
150# include <errno.h>
151
152# define SOCKET int
153# define INVALID_SOCKET (-1)
154# define SOCKET_ERROR (-1)
155
156# define SOCKERRNO() (errno)
157# define SET_SOCKERRNO(error) do { errno = (error); } while (0)
158
159# define closesocket(s) close(s)
160# define ioctlsocket(s, req, arg) ioctl((s), (req), (arg))
161
162typedef struct iovec IOVEC;
163
164# define IOVEC_GET_BASE(iov) ((iov).iov_base)
165# define IOVEC_SET_BASE(iov, b) ((iov).iov_base = (b))
166
167# define IOVEC_GET_LEN(iov) ((iov).iov_len)
168# define IOVEC_SET_LEN(iov, l) ((iov).iov_len = (l))
169# endif
170
171DECLINLINE(int)
172proxy_error_is_transient(int error)
173{
174# if !defined(RT_OS_WINDOWS)
175 return error == EWOULDBLOCK
176# if EAGAIN != EWOULDBLOCK
177 || error == EAGAIN
178# endif
179 || error == EINTR
180 || error == ENOBUFS
181 || error == ENOMEM;
182# else
183 return error == WSAEWOULDBLOCK
184 || error == WSAEINTR /* NB: we don't redefine EINTR above */
185 || error == WSAENOBUFS;
186# endif
187}
188
189#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette