1 /* SPDX-License-Identifier: MIT */
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
32 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/tcp.h>
43 #include <netinet/in.h>
47 # define SLIRP_PACKED __attribute__((gcc_struct, packed))
49 # define SLIRP_PACKED __attribute__((packed))
53 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
57 #define container_of(ptr, type, member) __extension__ ({ \
58 void *__mptr = (void *)(ptr); \
59 ((type *)(__mptr - offsetof(type, member))); })
62 #if defined(_WIN32) /* CONFIG_IOVEC */
63 # if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
73 #define SCALE_MS 1000000
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 */
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
);
145 #define closesocket(s) close(s)
146 #define ioctlsocket(s, r, v) ioctl(s, r, v)
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
)
155 return setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &v
, sizeof(v
));
158 static inline int slirp_socket_set_fast_reuse(int fd
)
162 return setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &v
, sizeof(v
));
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 */
173 void slirp_pstrcpy(char *buf
, int buf_size
, const char *str
);