2 * util.c (mostly based on QEMU os-win32.c)
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010-2016 Red Hat, Inc.
7 * QEMU library functions for win32 which are shared between QEMU and
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #if defined(_WIN32) && !defined(WITH_QEMU)
35 int inet_aton(const char *cp
, struct in_addr
*ia
)
37 uint32_t addr
= inet_addr(cp
);
38 if (addr
== 0xffffffff) {
46 void slirp_set_nonblock(int fd
)
50 f
= fcntl(fd
, F_GETFL
);
52 f
= fcntl(fd
, F_SETFL
, f
| O_NONBLOCK
);
55 unsigned long opt
= 1;
56 ioctlsocket(fd
, FIONBIO
, &opt
);
60 static void slirp_set_cloexec(int fd
)
64 f
= fcntl(fd
, F_GETFD
);
66 f
= fcntl(fd
, F_SETFD
, f
| FD_CLOEXEC
);
72 * Opens a socket with FD_CLOEXEC set
74 int slirp_socket(int domain
, int type
, int protocol
)
79 ret
= socket(domain
, type
| SOCK_CLOEXEC
, protocol
);
80 if (ret
!= -1 || errno
!= EINVAL
) {
84 ret
= socket(domain
, type
, protocol
);
86 slirp_set_cloexec(ret
);
93 static int socket_error(void)
95 switch (WSAGetLastError()) {
102 case WSA_INVALID_HANDLE
:
104 case WSA_NOT_ENOUGH_MEMORY
:
106 case WSA_INVALID_PARAMETER
:
108 case WSAENAMETOOLONG
:
113 /* not using EWOULDBLOCK as we don't want code to have
114 * to check both EWOULDBLOCK and EAGAIN */
122 case WSAEDESTADDRREQ
:
130 case WSAEPROTONOSUPPORT
:
131 return EPROTONOSUPPORT
;
134 case WSAEAFNOSUPPORT
:
138 case WSAEADDRNOTAVAIL
:
139 return EADDRNOTAVAIL
;
146 case WSAECONNABORTED
:
158 case WSAECONNREFUSED
:
162 case WSAEHOSTUNREACH
:
170 int slirp_ioctlsocket(int fd
, int req
, void *val
)
173 ret
= ioctlsocket(fd
, req
, val
);
175 errno
= socket_error();
181 int slirp_closesocket(int fd
)
184 ret
= closesocket(fd
);
186 errno
= socket_error();