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_wrap(int fd
, int req
, void *val
)
173 ret
= ioctlsocket(fd
, req
, val
);
175 errno
= socket_error();
181 int slirp_closesocket_wrap(int fd
)
184 ret
= closesocket(fd
);
186 errno
= socket_error();
192 int slirp_connect_wrap(int sockfd
, const struct sockaddr
*addr
, int addrlen
)
195 ret
= connect(sockfd
, addr
, addrlen
);
197 errno
= socket_error();
203 int slirp_listen_wrap(int sockfd
, int backlog
)
206 ret
= listen(sockfd
, backlog
);
208 errno
= socket_error();
214 int slirp_bind_wrap(int sockfd
, const struct sockaddr
*addr
, int addrlen
)
217 ret
= bind(sockfd
, addr
, addrlen
);
219 errno
= socket_error();
225 int slirp_socket_wrap(int domain
, int type
, int protocol
)
228 ret
= socket(domain
, type
, protocol
);
230 errno
= socket_error();
236 int slirp_accept_wrap(int sockfd
, struct sockaddr
*addr
, int *addrlen
)
239 ret
= accept(sockfd
, addr
, addrlen
);
241 errno
= socket_error();
247 int slirp_shutdown_wrap(int sockfd
, int how
)
250 ret
= shutdown(sockfd
, how
);
252 errno
= socket_error();
258 int slirp_getsockopt_wrap(int sockfd
, int level
, int optname
,
259 void *optval
, int *optlen
)
262 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
264 errno
= socket_error();
270 int slirp_setsockopt_wrap(int sockfd
, int level
, int optname
,
271 const void *optval
, int optlen
)
274 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
276 errno
= socket_error();
282 int slirp_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
286 ret
= getpeername(sockfd
, addr
, addrlen
);
288 errno
= socket_error();
294 int slirp_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
298 ret
= getsockname(sockfd
, addr
, addrlen
);
300 errno
= socket_error();
306 ssize_t
slirp_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
309 ret
= send(sockfd
, buf
, len
, flags
);
311 errno
= socket_error();
317 ssize_t
slirp_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
318 const struct sockaddr
*addr
, int addrlen
)
321 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
323 errno
= socket_error();
329 ssize_t
slirp_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
332 ret
= recv(sockfd
, buf
, len
, flags
);
334 errno
= socket_error();
340 ssize_t
slirp_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
341 struct sockaddr
*addr
, int *addrlen
)
344 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
346 errno
= socket_error();
352 void slirp_pstrcpy(char *buf
, int buf_size
, const char *str
)
362 if (c
== 0 || q
>= buf
+ buf_size
- 1)