1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Copyright (c) 2003-2008 Fabrice Bellard
4 * Copyright (c) 2010-2019 Red Hat, Inc.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #ifndef UTIL_H_
25 #define UTIL_H_
26
27 #include <glib.h>
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <inttypes.h>
38
39 #ifdef _WIN32
40 #include <winsock2.h>
41 #include <windows.h>
42 #include <ws2tcpip.h>
43 #else
44 #include <sys/socket.h>
45 #include <netinet/tcp.h>
46 #include <netinet/in.h>
47 #endif
48
49 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
50 #define SLIRP_PACKED __attribute__((gcc_struct, packed))
51 #else
52 #define SLIRP_PACKED __attribute__((packed))
53 #endif
54
55 #ifndef DIV_ROUND_UP
56 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
57 #endif
58
59 #ifndef container_of
60 #define container_of(ptr, type, member) \
61 __extension__({ \
62 void *__mptr = (void *)(ptr); \
63 ((type *)(__mptr - offsetof(type, member))); \
64 })
65 #endif
66
67 #ifndef G_SIZEOF_MEMBER
68 #define G_SIZEOF_MEMBER(type, member) sizeof(((type *)0)->member)
69 #endif
70
71 #if defined(_WIN32) /* CONFIG_IOVEC */
72 #if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
73 struct iovec {
74 void *iov_base;
75 size_t iov_len;
76 };
77 #endif
78 #else
79 #include <sys/uio.h>
80 #endif
81
82 #define stringify(s) tostring(s)
83 #define tostring(s) #s
84
85 #define SCALE_MS 1000000
86
87 #define ETH_ALEN 6
88 #define ETH_ADDRSTRLEN 18 /* "xx:xx:xx:xx:xx:xx", with trailing NUL */
89 #define ETH_HLEN 14
90 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
91 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
92 #define ETH_P_IPV6 (0x86dd)
93 #define ETH_P_VLAN (0x8100)
94 #define ETH_P_DVLAN (0x88a8)
95 #define ETH_P_NCSI (0x88f8)
96 #define ETH_P_UNKNOWN (0xffff)
97
98 /* FIXME: remove me when made standalone */
99 #ifdef _WIN32
100 #undef accept
101 #undef bind
102 #undef closesocket
103 #undef connect
104 #undef getpeername
105 #undef getsockname
106 #undef getsockopt
107 #undef ioctlsocket
108 #undef listen
109 #undef recv
110 #undef recvfrom
111 #undef send
112 #undef sendto
113 #undef setsockopt
114 #undef shutdown
115 #undef socket
116 #endif
117
118 #ifdef _WIN32
119 #define connect slirp_connect_wrap
120 int slirp_connect_wrap(int fd, const struct sockaddr *addr, int addrlen);
121 #define listen slirp_listen_wrap
122 int slirp_listen_wrap(int fd, int backlog);
123 #define bind slirp_bind_wrap
124 int slirp_bind_wrap(int fd, const struct sockaddr *addr, int addrlen);
125 #define socket slirp_socket_wrap
126 int slirp_socket_wrap(int domain, int type, int protocol);
127 #define accept slirp_accept_wrap
128 int slirp_accept_wrap(int fd, struct sockaddr *addr, int *addrlen);
129 #define shutdown slirp_shutdown_wrap
130 int slirp_shutdown_wrap(int fd, int how);
131 #define getpeername slirp_getpeername_wrap
132 int slirp_getpeername_wrap(int fd, struct sockaddr *addr, int *addrlen);
133 #define getsockname slirp_getsockname_wrap
134 int slirp_getsockname_wrap(int fd, struct sockaddr *addr, int *addrlen);
135 #define send slirp_send_wrap
136 ssize_t slirp_send_wrap(int fd, const void *buf, size_t len, int flags);
137 #define sendto slirp_sendto_wrap
138 ssize_t slirp_sendto_wrap(int fd, const void *buf, size_t len, int flags,
139 const struct sockaddr *dest_addr, int addrlen);
140 #define recv slirp_recv_wrap
141 ssize_t slirp_recv_wrap(int fd, void *buf, size_t len, int flags);
142 #define recvfrom slirp_recvfrom_wrap
143 ssize_t slirp_recvfrom_wrap(int fd, void *buf, size_t len, int flags,
144 struct sockaddr *src_addr, int *addrlen);
145 #define closesocket slirp_closesocket_wrap
146 int slirp_closesocket_wrap(int fd);
147 #define ioctlsocket slirp_ioctlsocket_wrap
148 int slirp_ioctlsocket_wrap(int fd, int req, void *val);
149 #define getsockopt slirp_getsockopt_wrap
150 int slirp_getsockopt_wrap(int sockfd, int level, int optname, void *optval,
151 int *optlen);
152 #define setsockopt slirp_setsockopt_wrap
153 int slirp_setsockopt_wrap(int sockfd, int level, int optname,
154 const void *optval, int optlen);
155 #define inet_aton slirp_inet_aton
156 int slirp_inet_aton(const char *cp, struct in_addr *ia);
157 #else
158 #define closesocket(s) close(s)
159 #define ioctlsocket(s, r, v) ioctl(s, r, v)
160 #endif
161
162 int slirp_socket(int domain, int type, int protocol);
163 void slirp_set_nonblock(int fd);
164
slirp_socket_set_v6only(int fd,int v)165 static inline int slirp_socket_set_v6only(int fd, int v)
166 {
167 return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v, sizeof(v));
168 }
169
slirp_socket_set_nodelay(int fd)170 static inline int slirp_socket_set_nodelay(int fd)
171 {
172 int v = 1;
173 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
174 }
175
slirp_socket_set_fast_reuse(int fd)176 static inline int slirp_socket_set_fast_reuse(int fd)
177 {
178 #ifndef _WIN32
179 int v = 1;
180 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
181 #else
182 /* Enabling the reuse of an endpoint that was used by a socket still in
183 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
184 * fast reuse is the default and SO_REUSEADDR does strange things. So we
185 * don't have to do anything here. More info can be found at:
186 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
187 return 0;
188 #endif
189 }
190
191 void slirp_pstrcpy(char *buf, int buf_size, const char *str);
192
193 int slirp_fmt(char *str, size_t size, const char *format, ...) G_GNUC_PRINTF(3, 4);
194 int slirp_fmt0(char *str, size_t size, const char *format, ...) G_GNUC_PRINTF(3, 4);
195
196 /*
197 * Pretty print a MAC address into out_str.
198 * As a convenience returns out_str.
199 */
200 const char *slirp_ether_ntoa(const uint8_t *addr, char *out_str,
201 size_t out_str_len);
202
203 #endif
204