/
windows-compat.h
156 lines (135 loc) · 5.21 KB
/
windows-compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* nbdkit
* Copyright (C) 2020-2022 Red Hat Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Red Hat nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef NBDKIT_WINDOWS_COMPAT_H
#define NBDKIT_WINDOWS_COMPAT_H
#ifdef WIN32
#include <config.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <errno.h>
/* Note that this is part of Windows 10, but not yet available in
* mingw-w64.
*/
#ifdef HAVE_AFUNIX_H
#include <afunix.h>
#else
/* This is the same as the definition in common/include/unix-path-max.h
* so we should be fine here even if the other file was included.
*/
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
struct sockaddr_un
{
ADDRESS_FAMILY sun_family;
char sun_path[UNIX_PATH_MAX];
};
#endif /* !HAVE_AFUNIX_H */
/* Windows doesn't have O_CLOEXEC, but it also doesn't have file
* descriptors that can be inherited across exec. Similarly for
* O_NOCTTY.
*/
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
/* Windows <errno.h> lacks certain errnos, so replace them here as
* best we can.
*/
#ifndef EBADMSG
#define EBADMSG EPROTO
#endif
#ifndef ESHUTDOWN
#define ESHUTDOWN ECONNABORTED
#endif
/* Windows uses non-standard names for shutdown(how). */
#ifndef SHUT_WR
#define SHUT_WR SD_SEND
#define SHUT_RDWR SD_BOTH
#endif
/* This generated function translates Winsock errors into errno codes. */
extern int translate_winsock_error (const char *fn, int err);
/* Add wrappers around the Winsock syscalls that nbdkit uses. */
extern int win_accept (int fd, struct sockaddr *addr, socklen_t *len);
extern int win_bind (int fd, const struct sockaddr *addr, socklen_t len);
extern int win_closesocket (int fd);
extern int win_getpeername (int fd, struct sockaddr *addr, socklen_t *len);
extern int win_listen (int fd, int backlog);
extern int win_getsockopt (int fd, int level, int optname,
void *optval, socklen_t *optlen);
extern int win_recv (int fd, void *buf, size_t len, int flags);
extern int win_setsockopt (int fd, int level, int optname,
const void *optval, socklen_t optlen);
extern int win_socket (int domain, int type, int protocol);
extern int win_send (int fd, const void *buf, size_t len, int flags);
#define accept win_accept
#define bind win_bind
#define closesocket win_closesocket
#define getpeername win_getpeername
#define listen win_listen
#define getsockopt win_getsockopt
#define recv win_recv
#define setsockopt win_setsockopt
#define socket win_socket
#define send win_send
/* Windows has strange names for these functions. */
#define dup _dup
#define dup2 _dup2
/* setenv replacement. */
#define setenv(k, v, replace) _putenv_s ((k), (v));
/* Unfortunately quite commonly used at the moment. Make it a common
* macro so we can easily find places which need porting.
*
* Note: Don't use this for things which can never work on Windows
* (eg. Unix socket support). Those should just give regular errors.
*/
#define NOT_IMPLEMENTED_ON_WINDOWS(feature) \
do { \
fprintf (stderr, "nbdkit: %s is not implemented for Windows.\n", feature); \
fprintf (stderr, "You can help by contributing to the Windows port, see\n"); \
fprintf (stderr, "nbdkit README in the source for how to contribute.\n"); \
exit (EXIT_FAILURE); \
} while (0)
#else /* !WIN32 */
/* Windows doesn't have a generic function for closing anything,
* instead you have to call closesocket on a SOCKET object. We would
* like to #define close to point to the Windows alternative above,
* but that's not possible because it breaks things like
* backend->close. So instead the server code must call closesocket()
* on anything that might be a socket.
*/
#define closesocket close
#endif /* !WIN32 */
#endif /* NBDKIT_WINDOWS_COMPAT_H */