2 * Copyright (c) 2003-2008 Fabrice Bellard
3 * Copyright (c) 2010-2019 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/tcp.h>
42 #include <netinet/in.h>
46 # define SLIRP_PACKED __attribute__((gcc_struct, packed))
48 # define SLIRP_PACKED __attribute__((packed))
52 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
56 #define container_of(ptr, type, member) __extension__ ({ \
57 void *__mptr = (void *)(ptr); \
58 ((type *)(__mptr - offsetof(type, member))); })
61 #if defined(_WIN32) /* CONFIG_IOVEC */
62 # if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
72 #define SCALE_MS 1000000
76 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
77 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
78 #define ETH_P_IPV6 (0x86dd)
79 #define ETH_P_VLAN (0x8100)
80 #define ETH_P_DVLAN (0x88a8)
81 #define ETH_P_NCSI (0x88f8)
82 #define ETH_P_UNKNOWN (0xffff)
84 /* FIXME: remove me when made standalone */
105 #define connect slirp_connect_wrap
106 int slirp_connect_wrap(int fd
, const struct sockaddr
*addr
, int addrlen
);
107 #define listen slirp_listen_wrap
108 int slirp_listen_wrap(int fd
, int backlog
);
109 #define bind slirp_bind_wrap
110 int slirp_bind_wrap(int fd
, const struct sockaddr
*addr
, int addrlen
);
111 #define socket slirp_socket_wrap
112 int slirp_socket_wrap(int domain
, int type
, int protocol
);
113 #define accept slirp_accept_wrap
114 int slirp_accept_wrap(int fd
, struct sockaddr
*addr
, int *addrlen
);
115 #define shutdown slirp_shutdown_wrap
116 int slirp_shutdown_wrap(int fd
, int how
);
117 #define getpeername slirp_getpeername_wrap
118 int slirp_getpeername_wrap(int fd
, struct sockaddr
*addr
, int *addrlen
);
119 #define getsockname slirp_getsockname_wrap
120 int slirp_getsockname_wrap(int fd
, struct sockaddr
*addr
, int *addrlen
);
121 #define send slirp_send_wrap
122 ssize_t
slirp_send_wrap(int fd
, const void *buf
, size_t len
, int flags
);
123 #define sendto slirp_sendto_wrap
124 ssize_t
slirp_sendto_wrap(int fd
, const void *buf
, size_t len
, int flags
,
125 const struct sockaddr
*dest_addr
, int addrlen
);
126 #define recv slirp_recv_wrap
127 ssize_t
slirp_recv_wrap(int fd
, void *buf
, size_t len
, int flags
);
128 #define recvfrom slirp_recvfrom_wrap
129 ssize_t
slirp_recvfrom_wrap(int fd
, void *buf
, size_t len
, int flags
,
130 struct sockaddr
*src_addr
, int *addrlen
);
131 #define closesocket slirp_closesocket_wrap
132 int slirp_closesocket_wrap(int fd
);
133 #define ioctlsocket slirp_ioctlsocket_wrap
134 int slirp_ioctlsocket_wrap(int fd
, int req
, void *val
);
135 #define getsockopt slirp_getsockopt_wrap
136 int slirp_getsockopt_wrap(int sockfd
, int level
, int optname
,
137 void *optval
, int *optlen
);
138 #define setsockopt slirp_setsockopt_wrap
139 int slirp_setsockopt_wrap(int sockfd
, int level
, int optname
,
140 const void *optval
, int optlen
);
142 int inet_aton(const char *cp
, struct in_addr
*ia
);
144 #define closesocket(s) close(s)
145 #define ioctlsocket(s, r, v) ioctl(s, r, v)
148 int slirp_socket(int domain
, int type
, int protocol
);
149 void slirp_set_nonblock(int fd
);
151 static inline int slirp_socket_set_nodelay(int fd
)
154 return setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &v
, sizeof(v
));
157 static inline int slirp_socket_set_fast_reuse(int fd
)
161 return setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &v
, sizeof(v
));
163 /* Enabling the reuse of an endpoint that was used by a socket still in
164 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
165 * fast reuse is the default and SO_REUSEADDR does strange things. So we
166 * don't have to do anything here. More info can be found at:
167 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
172 void slirp_pstrcpy(char *buf
, int buf_size
, const char *str
);