chardev: ensure qemu_chr_parse_compat reports missing driver error
[qemu/ar7.git] / slirp / util.c
blob84f5afdbc3afe3168b85cf461aea849a1608534e
1 /*
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
8 * the QEMU tools.
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
26 * THE SOFTWARE.
28 #include "util.h"
30 #include <glib.h>
31 #include <fcntl.h>
32 #include <stdint.h>
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) {
39 return 0;
41 ia->s_addr = addr;
42 return 1;
44 #endif
46 void slirp_set_nonblock(int fd)
48 #ifndef _WIN32
49 int f;
50 f = fcntl(fd, F_GETFL);
51 assert(f != -1);
52 f = fcntl(fd, F_SETFL, f | O_NONBLOCK);
53 assert(f != -1);
54 #else
55 unsigned long opt = 1;
56 ioctlsocket(fd, FIONBIO, &opt);
57 #endif
60 static void slirp_set_cloexec(int fd)
62 #ifndef _WIN32
63 int f;
64 f = fcntl(fd, F_GETFD);
65 assert(f != -1);
66 f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
67 assert(f != -1);
68 #endif
72 * Opens a socket with FD_CLOEXEC set
74 int slirp_socket(int domain, int type, int protocol)
76 int ret;
78 #ifdef SOCK_CLOEXEC
79 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
80 if (ret != -1 || errno != EINVAL) {
81 return ret;
83 #endif
84 ret = socket(domain, type, protocol);
85 if (ret >= 0) {
86 slirp_set_cloexec(ret);
89 return ret;
92 #ifdef _WIN32
93 static int socket_error(void)
95 switch (WSAGetLastError()) {
96 case 0:
97 return 0;
98 case WSAEINTR:
99 return EINTR;
100 case WSAEINVAL:
101 return EINVAL;
102 case WSA_INVALID_HANDLE:
103 return EBADF;
104 case WSA_NOT_ENOUGH_MEMORY:
105 return ENOMEM;
106 case WSA_INVALID_PARAMETER:
107 return EINVAL;
108 case WSAENAMETOOLONG:
109 return ENAMETOOLONG;
110 case WSAENOTEMPTY:
111 return ENOTEMPTY;
112 case WSAEWOULDBLOCK:
113 /* not using EWOULDBLOCK as we don't want code to have
114 * to check both EWOULDBLOCK and EAGAIN */
115 return EAGAIN;
116 case WSAEINPROGRESS:
117 return EINPROGRESS;
118 case WSAEALREADY:
119 return EALREADY;
120 case WSAENOTSOCK:
121 return ENOTSOCK;
122 case WSAEDESTADDRREQ:
123 return EDESTADDRREQ;
124 case WSAEMSGSIZE:
125 return EMSGSIZE;
126 case WSAEPROTOTYPE:
127 return EPROTOTYPE;
128 case WSAENOPROTOOPT:
129 return ENOPROTOOPT;
130 case WSAEPROTONOSUPPORT:
131 return EPROTONOSUPPORT;
132 case WSAEOPNOTSUPP:
133 return EOPNOTSUPP;
134 case WSAEAFNOSUPPORT:
135 return EAFNOSUPPORT;
136 case WSAEADDRINUSE:
137 return EADDRINUSE;
138 case WSAEADDRNOTAVAIL:
139 return EADDRNOTAVAIL;
140 case WSAENETDOWN:
141 return ENETDOWN;
142 case WSAENETUNREACH:
143 return ENETUNREACH;
144 case WSAENETRESET:
145 return ENETRESET;
146 case WSAECONNABORTED:
147 return ECONNABORTED;
148 case WSAECONNRESET:
149 return ECONNRESET;
150 case WSAENOBUFS:
151 return ENOBUFS;
152 case WSAEISCONN:
153 return EISCONN;
154 case WSAENOTCONN:
155 return ENOTCONN;
156 case WSAETIMEDOUT:
157 return ETIMEDOUT;
158 case WSAECONNREFUSED:
159 return ECONNREFUSED;
160 case WSAELOOP:
161 return ELOOP;
162 case WSAEHOSTUNREACH:
163 return EHOSTUNREACH;
164 default:
165 return EIO;
169 #undef ioctlsocket
170 int slirp_ioctlsocket(int fd, int req, void *val)
172 int ret;
173 ret = ioctlsocket(fd, req, val);
174 if (ret < 0) {
175 errno = socket_error();
177 return ret;
180 #undef closesocket
181 int slirp_closesocket(int fd)
183 int ret;
184 ret = closesocket(fd);
185 if (ret < 0) {
186 errno = socket_error();
188 return ret;
190 #endif /* WIN32 */
192 void slirp_pstrcpy(char *buf, int buf_size, const char *str)
194 int c;
195 char *q = buf;
197 if (buf_size <= 0)
198 return;
200 for(;;) {
201 c = *str++;
202 if (c == 0 || q >= buf + buf_size - 1)
203 break;
204 *q++ = c;
206 *q = '\0';