nbd: Permit simple error to NBD_CMD_BLOCK_STATUS
[qemu/ericb.git] / slirp / src / util.h
blob01f1e0e0680ef00ea6336296ab0894ab87e64ede
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Copyright (c) 2003-2008 Fabrice Bellard
4 * Copyright (c) 2010-2019 Red Hat, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #ifndef UTIL_H_
25 #define UTIL_H_
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <inttypes.h>
37 #ifdef _WIN32
38 #include <winsock2.h>
39 #include <windows.h>
40 #else
41 #include <sys/socket.h>
42 #include <netinet/tcp.h>
43 #include <netinet/in.h>
44 #endif
46 #if defined(_WIN32)
47 # define SLIRP_PACKED __attribute__((gcc_struct, packed))
48 #else
49 # define SLIRP_PACKED __attribute__((packed))
50 #endif
52 #ifndef DIV_ROUND_UP
53 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
54 #endif
56 #ifndef container_of
57 #define container_of(ptr, type, member) __extension__ ({ \
58 void *__mptr = (void *)(ptr); \
59 ((type *)(__mptr - offsetof(type, member))); })
60 #endif
62 #if defined(_WIN32) /* CONFIG_IOVEC */
63 # if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
64 struct iovec {
65 void *iov_base;
66 size_t iov_len;
68 # endif
69 #else
70 #include <sys/uio.h>
71 #endif
73 #define SCALE_MS 1000000
75 #define ETH_ALEN 6
76 #define ETH_HLEN 14
77 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
78 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
79 #define ETH_P_IPV6 (0x86dd)
80 #define ETH_P_VLAN (0x8100)
81 #define ETH_P_DVLAN (0x88a8)
82 #define ETH_P_NCSI (0x88f8)
83 #define ETH_P_UNKNOWN (0xffff)
85 /* FIXME: remove me when made standalone */
86 #ifdef _WIN32
87 #undef accept
88 #undef bind
89 #undef closesocket
90 #undef connect
91 #undef getpeername
92 #undef getsockname
93 #undef getsockopt
94 #undef ioctlsocket
95 #undef listen
96 #undef recv
97 #undef recvfrom
98 #undef send
99 #undef sendto
100 #undef setsockopt
101 #undef shutdown
102 #undef socket
103 #endif
105 #ifdef _WIN32
106 #define connect slirp_connect_wrap
107 int slirp_connect_wrap(int fd, const struct sockaddr *addr, int addrlen);
108 #define listen slirp_listen_wrap
109 int slirp_listen_wrap(int fd, int backlog);
110 #define bind slirp_bind_wrap
111 int slirp_bind_wrap(int fd, const struct sockaddr *addr, int addrlen);
112 #define socket slirp_socket_wrap
113 int slirp_socket_wrap(int domain, int type, int protocol);
114 #define accept slirp_accept_wrap
115 int slirp_accept_wrap(int fd, struct sockaddr *addr, int *addrlen);
116 #define shutdown slirp_shutdown_wrap
117 int slirp_shutdown_wrap(int fd, int how);
118 #define getpeername slirp_getpeername_wrap
119 int slirp_getpeername_wrap(int fd, struct sockaddr *addr, int *addrlen);
120 #define getsockname slirp_getsockname_wrap
121 int slirp_getsockname_wrap(int fd, struct sockaddr *addr, int *addrlen);
122 #define send slirp_send_wrap
123 ssize_t slirp_send_wrap(int fd, const void *buf, size_t len, int flags);
124 #define sendto slirp_sendto_wrap
125 ssize_t slirp_sendto_wrap(int fd, const void *buf, size_t len, int flags,
126 const struct sockaddr *dest_addr, int addrlen);
127 #define recv slirp_recv_wrap
128 ssize_t slirp_recv_wrap(int fd, void *buf, size_t len, int flags);
129 #define recvfrom slirp_recvfrom_wrap
130 ssize_t slirp_recvfrom_wrap(int fd, void *buf, size_t len, int flags,
131 struct sockaddr *src_addr, int *addrlen);
132 #define closesocket slirp_closesocket_wrap
133 int slirp_closesocket_wrap(int fd);
134 #define ioctlsocket slirp_ioctlsocket_wrap
135 int slirp_ioctlsocket_wrap(int fd, int req, void *val);
136 #define getsockopt slirp_getsockopt_wrap
137 int slirp_getsockopt_wrap(int sockfd, int level, int optname,
138 void *optval, int *optlen);
139 #define setsockopt slirp_setsockopt_wrap
140 int slirp_setsockopt_wrap(int sockfd, int level, int optname,
141 const void *optval, int optlen);
142 #define inet_aton slirp_inet_aton
143 int slirp_inet_aton(const char *cp, struct in_addr *ia);
144 #else
145 #define closesocket(s) close(s)
146 #define ioctlsocket(s, r, v) ioctl(s, r, v)
147 #endif
149 int slirp_socket(int domain, int type, int protocol);
150 void slirp_set_nonblock(int fd);
152 static inline int slirp_socket_set_nodelay(int fd)
154 int v = 1;
155 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
158 static inline int slirp_socket_set_fast_reuse(int fd)
160 #ifndef _WIN32
161 int v = 1;
162 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
163 #else
164 /* Enabling the reuse of an endpoint that was used by a socket still in
165 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
166 * fast reuse is the default and SO_REUSEADDR does strange things. So we
167 * don't have to do anything here. More info can be found at:
168 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
169 return 0;
170 #endif
173 void slirp_pstrcpy(char *buf, int buf_size, const char *str);
175 #endif