]>
Commit | Line | Data |
---|---|---|
6087fd53 | 1 | /* SPDX-License-Identifier: MIT */ |
90329416 MAL |
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 | ||
707bd47e MAL |
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 | ||
90329416 MAL |
46 | #if defined(_WIN32) |
47 | # define SLIRP_PACKED __attribute__((gcc_struct, packed)) | |
48 | #else | |
49 | # define SLIRP_PACKED __attribute__((packed)) | |
50 | #endif | |
51 | ||
a9d8b3ec MAL |
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) __extension__ ({ \ | |
58 | void *__mptr = (void *)(ptr); \ | |
59 | ((type *)(__mptr - offsetof(type, member))); }) | |
60 | #endif | |
61 | ||
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; | |
67 | }; | |
68 | # endif | |
69 | #else | |
70 | #include <sys/uio.h> | |
71 | #endif | |
72 | ||
759c5a6a MAL |
73 | #define SCALE_MS 1000000 |
74 | ||
c1c5c0ff MAL |
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) | |
84 | ||
fdbfba8c | 85 | /* FIXME: remove me when made standalone */ |
707bd47e | 86 | #ifdef _WIN32 |
adf1add2 MAL |
87 | #undef accept |
88 | #undef bind | |
fdbfba8c | 89 | #undef closesocket |
adf1add2 MAL |
90 | #undef connect |
91 | #undef getpeername | |
92 | #undef getsockname | |
fdbfba8c MAL |
93 | #undef getsockopt |
94 | #undef ioctlsocket | |
adf1add2 | 95 | #undef listen |
fdbfba8c | 96 | #undef recv |
adf1add2 MAL |
97 | #undef recvfrom |
98 | #undef send | |
99 | #undef sendto | |
fdbfba8c | 100 | #undef setsockopt |
adf1add2 MAL |
101 | #undef shutdown |
102 | #undef socket | |
fdbfba8c MAL |
103 | #endif |
104 | ||
105 | #ifdef _WIN32 | |
adf1add2 MAL |
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); | |
5a4af0d4 MAL |
142 | #define inet_aton slirp_inet_aton |
143 | int slirp_inet_aton(const char *cp, struct in_addr *ia); | |
707bd47e | 144 | #else |
fdbfba8c MAL |
145 | #define closesocket(s) close(s) |
146 | #define ioctlsocket(s, r, v) ioctl(s, r, v) | |
707bd47e MAL |
147 | #endif |
148 | ||
149 | int slirp_socket(int domain, int type, int protocol); | |
848c7092 | 150 | void slirp_set_nonblock(int fd); |
707bd47e MAL |
151 | |
152 | static inline int slirp_socket_set_nodelay(int fd) | |
153 | { | |
154 | int v = 1; | |
fdbfba8c | 155 | return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
707bd47e MAL |
156 | } |
157 | ||
158 | static inline int slirp_socket_set_fast_reuse(int fd) | |
159 | { | |
160 | #ifndef _WIN32 | |
161 | int v = 1; | |
fdbfba8c | 162 | return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)); |
707bd47e MAL |
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 | |
171 | } | |
172 | ||
d1c4b3e9 MAL |
173 | void slirp_pstrcpy(char *buf, int buf_size, const char *str); |
174 | ||
90329416 | 175 | #endif |