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 <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>
36 
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
45 
46 #if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
47 #define SLIRP_PACKED __attribute__((gcc_struct, packed))
48 #else
49 #define SLIRP_PACKED __attribute__((packed))
50 #endif
51 
52 #ifndef DIV_ROUND_UP
53 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
54 #endif
55 
56 #ifndef container_of
57 #define container_of(ptr, type, member)              \
58     __extension__({                                  \
59         void *__mptr = (void *)(ptr);                \
60         ((type *)(__mptr - offsetof(type, member))); \
61     })
62 #endif
63 
64 #if defined(_WIN32) /* CONFIG_IOVEC */
65 #if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
66 struct iovec {
67     void *iov_base;
68     size_t iov_len;
69 };
70 #endif
71 #else
72 #include <sys/uio.h>
73 #endif
74 
75 #define stringify(s) tostring(s)
76 #define tostring(s) #s
77 
78 #define SCALE_MS 1000000
79 
80 #define ETH_ALEN 6
81 #define ETH_HLEN 14
82 #define ETH_P_IP (0x0800) /* Internet Protocol packet  */
83 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
84 #define ETH_P_IPV6 (0x86dd)
85 #define ETH_P_VLAN (0x8100)
86 #define ETH_P_DVLAN (0x88a8)
87 #define ETH_P_NCSI (0x88f8)
88 #define ETH_P_UNKNOWN (0xffff)
89 
90 /* FIXME: remove me when made standalone */
91 #ifdef _WIN32
92 #undef accept
93 #undef bind
94 #undef closesocket
95 #undef connect
96 #undef getpeername
97 #undef getsockname
98 #undef getsockopt
99 #undef ioctlsocket
100 #undef listen
101 #undef recv
102 #undef recvfrom
103 #undef send
104 #undef sendto
105 #undef setsockopt
106 #undef shutdown
107 #undef socket
108 #endif
109 
110 #ifdef _WIN32
111 #define connect slirp_connect_wrap
112 int slirp_connect_wrap(int fd, const struct sockaddr *addr, int addrlen);
113 #define listen slirp_listen_wrap
114 int slirp_listen_wrap(int fd, int backlog);
115 #define bind slirp_bind_wrap
116 int slirp_bind_wrap(int fd, const struct sockaddr *addr, int addrlen);
117 #define socket slirp_socket_wrap
118 int slirp_socket_wrap(int domain, int type, int protocol);
119 #define accept slirp_accept_wrap
120 int slirp_accept_wrap(int fd, struct sockaddr *addr, int *addrlen);
121 #define shutdown slirp_shutdown_wrap
122 int slirp_shutdown_wrap(int fd, int how);
123 #define getpeername slirp_getpeername_wrap
124 int slirp_getpeername_wrap(int fd, struct sockaddr *addr, int *addrlen);
125 #define getsockname slirp_getsockname_wrap
126 int slirp_getsockname_wrap(int fd, struct sockaddr *addr, int *addrlen);
127 #define send slirp_send_wrap
128 ssize_t slirp_send_wrap(int fd, const void *buf, size_t len, int flags);
129 #define sendto slirp_sendto_wrap
130 ssize_t slirp_sendto_wrap(int fd, const void *buf, size_t len, int flags,
131                           const struct sockaddr *dest_addr, int addrlen);
132 #define recv slirp_recv_wrap
133 ssize_t slirp_recv_wrap(int fd, void *buf, size_t len, int flags);
134 #define recvfrom slirp_recvfrom_wrap
135 ssize_t slirp_recvfrom_wrap(int fd, void *buf, size_t len, int flags,
136                             struct sockaddr *src_addr, int *addrlen);
137 #define closesocket slirp_closesocket_wrap
138 int slirp_closesocket_wrap(int fd);
139 #define ioctlsocket slirp_ioctlsocket_wrap
140 int slirp_ioctlsocket_wrap(int fd, int req, void *val);
141 #define getsockopt slirp_getsockopt_wrap
142 int slirp_getsockopt_wrap(int sockfd, int level, int optname, void *optval,
143                           int *optlen);
144 #define setsockopt slirp_setsockopt_wrap
145 int slirp_setsockopt_wrap(int sockfd, int level, int optname,
146                           const void *optval, int optlen);
147 #define inet_aton slirp_inet_aton
148 int slirp_inet_aton(const char *cp, struct in_addr *ia);
149 #else
150 #define closesocket(s) close(s)
151 #define ioctlsocket(s, r, v) ioctl(s, r, v)
152 #endif
153 
154 int slirp_socket(int domain, int type, int protocol);
155 void slirp_set_nonblock(int fd);
156 
slirp_socket_set_nodelay(int fd)157 static inline int slirp_socket_set_nodelay(int fd)
158 {
159     int v = 1;
160     return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
161 }
162 
slirp_socket_set_fast_reuse(int fd)163 static inline int slirp_socket_set_fast_reuse(int fd)
164 {
165 #ifndef _WIN32
166     int v = 1;
167     return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
168 #else
169     /* Enabling the reuse of an endpoint that was used by a socket still in
170      * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
171      * fast reuse is the default and SO_REUSEADDR does strange things. So we
172      * don't have to do anything here. More info can be found at:
173      * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
174     return 0;
175 #endif
176 }
177 
178 void slirp_pstrcpy(char *buf, int buf_size, const char *str);
179 
180 #endif
181