VirtualBox

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

Last change on this file was 109105, checked in by vboxsync, 12 days ago

NetworkServices/NAT/VBoxNetSlirpNAT: Windows build fixes, bugref:10268

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

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