1 | #ifndef __COMMON_H__
|
---|
2 | #define __COMMON_H__
|
---|
3 |
|
---|
4 | #include <VBox/stam.h>
|
---|
5 |
|
---|
6 | #ifdef RT_OS_WINDOWS
|
---|
7 | # include <winsock2.h>
|
---|
8 | typedef int socklen_t;
|
---|
9 | #endif
|
---|
10 | #ifdef RT_OS_OS2 /* temporary workaround, see ticket #127 */
|
---|
11 | # define mbstat mbstat_os2
|
---|
12 | # include <sys/socket.h>
|
---|
13 | # undef mbstat
|
---|
14 | typedef int socklen_t;
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #define CONFIG_QEMU
|
---|
18 |
|
---|
19 | #ifdef DEBUG
|
---|
20 | # undef DEBUG
|
---|
21 | # define DEBUG 1
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #ifndef CONFIG_QEMU
|
---|
25 | # include "version.h"
|
---|
26 | #endif
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
28 | #include <VBox/log.h>
|
---|
29 | #ifdef VBOX_WITH_SLIRP_MEMORY_CHECK
|
---|
30 | # define RTMEM_WRAP_TO_EF_APIS
|
---|
31 | #endif /* VBOX_WITH_SLIRP_MEMORY_CHECK */
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #ifdef RT_OS_WINDOWS
|
---|
34 | # include <windows.h>
|
---|
35 | # include <io.h>
|
---|
36 | #endif
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/dir.h>
|
---|
40 | #include <VBox/types.h>
|
---|
41 |
|
---|
42 | #define malloc(a) RTMemAlloc(a)
|
---|
43 | #define free(a) RTMemFree(a)
|
---|
44 | #define realloc(a,b) RTMemRealloc(a, b)
|
---|
45 |
|
---|
46 | #include "slirp_config.h"
|
---|
47 |
|
---|
48 | #ifdef RT_OS_WINDOWS
|
---|
49 |
|
---|
50 | # ifndef _MSC_VER
|
---|
51 | # include <inttypes.h>
|
---|
52 | # endif
|
---|
53 |
|
---|
54 | typedef uint8_t u_int8_t;
|
---|
55 | typedef uint16_t u_int16_t;
|
---|
56 | typedef uint32_t u_int32_t;
|
---|
57 | typedef uint64_t u_int64_t;
|
---|
58 | typedef char *caddr_t;
|
---|
59 |
|
---|
60 | # include <sys/timeb.h>
|
---|
61 | # include <iphlpapi.h>
|
---|
62 |
|
---|
63 | # define EWOULDBLOCK WSAEWOULDBLOCK
|
---|
64 | # define EINPROGRESS WSAEINPROGRESS
|
---|
65 | # define ENOTCONN WSAENOTCONN
|
---|
66 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
67 | # define ENETUNREACH WSAENETUNREACH
|
---|
68 | # define ECONNREFUSED WSAECONNREFUSED
|
---|
69 |
|
---|
70 | #else /* !RT_OS_WINDOWS */
|
---|
71 |
|
---|
72 | # define ioctlsocket ioctl
|
---|
73 | # define closesocket(s) close(s)
|
---|
74 | # define O_BINARY 0
|
---|
75 |
|
---|
76 | #endif /* !RT_OS_WINDOWS */
|
---|
77 |
|
---|
78 | #include <sys/types.h>
|
---|
79 | #ifdef HAVE_SYS_BITYPES_H
|
---|
80 | # include <sys/bitypes.h>
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #ifdef _MSC_VER
|
---|
84 | # include <time.h>
|
---|
85 | #else /* !_MSC_VER */
|
---|
86 | # include <sys/time.h>
|
---|
87 | #endif /* !_MSC_VER */
|
---|
88 |
|
---|
89 | #ifdef NEED_TYPEDEFS
|
---|
90 | typedef char int8_t;
|
---|
91 | typedef unsigned char u_int8_t;
|
---|
92 |
|
---|
93 | # if SIZEOF_SHORT == 2
|
---|
94 | typedef short int16_t;
|
---|
95 | typedef unsigned short u_int16_t;
|
---|
96 | # else
|
---|
97 | # if SIZEOF_INT == 2
|
---|
98 | typedef int int16_t;
|
---|
99 | typedef unsigned int u_int16_t;
|
---|
100 | # else
|
---|
101 | #error Cannot find a type with sizeof() == 2
|
---|
102 | # endif
|
---|
103 | # endif
|
---|
104 |
|
---|
105 | # if SIZEOF_SHORT == 4
|
---|
106 | typedef short int32_t;
|
---|
107 | typedef unsigned short u_int32_t;
|
---|
108 | # else
|
---|
109 | # if SIZEOF_INT == 4
|
---|
110 | typedef int int32_t;
|
---|
111 | typedef unsigned int u_int32_t;
|
---|
112 | # else
|
---|
113 | #error Cannot find a type with sizeof() == 4
|
---|
114 | # endif
|
---|
115 | # endif
|
---|
116 | #endif /* NEED_TYPEDEFS */
|
---|
117 |
|
---|
118 | #ifdef HAVE_UNISTD_H
|
---|
119 | # include <unistd.h>
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | #ifdef HAVE_STDLIB_H
|
---|
123 | # include <stdlib.h>
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | #include <stdio.h>
|
---|
127 | #include <errno.h>
|
---|
128 |
|
---|
129 | #ifndef HAVE_MEMMOVE
|
---|
130 | # define memmove(x, y, z) bcopy(y, x, z)
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | #if TIME_WITH_SYS_TIME
|
---|
134 | # include <sys/time.h>
|
---|
135 | # include <time.h>
|
---|
136 | #else
|
---|
137 | # if HAVE_SYS_TIME_H
|
---|
138 | # include <sys/time.h>
|
---|
139 | # else
|
---|
140 | # include <time.h>
|
---|
141 | # endif
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | #ifdef HAVE_STRING_H
|
---|
145 | # include <string.h>
|
---|
146 | #else
|
---|
147 | # include <strings.h>
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | #ifndef RT_OS_WINDOWS
|
---|
151 | # include <sys/uio.h>
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | #ifndef _P
|
---|
155 | #ifndef NO_PROTOTYPES
|
---|
156 | # define _P(x) x
|
---|
157 | #else
|
---|
158 | # define _P(x) ()
|
---|
159 | #endif
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | #ifndef RT_OS_WINDOWS
|
---|
163 | # include <netinet/in.h>
|
---|
164 | # include <arpa/inet.h>
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | #ifdef GETTIMEOFDAY_ONE_ARG
|
---|
168 | # define gettimeofday(x, y) gettimeofday(x)
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | /* Systems lacking strdup() definition in <string.h>. */
|
---|
172 | #if defined(ultrix)
|
---|
173 | char *strdup _P((const char *));
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | /* Systems lacking malloc() definition in <stdlib.h>. */
|
---|
177 | #if defined(ultrix) || defined(hcx)
|
---|
178 | void *malloc _P((size_t arg));
|
---|
179 | void free _P((void *ptr));
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | #ifndef HAVE_INET_ATON
|
---|
183 | int inet_aton _P((const char *cp, struct in_addr *ia));
|
---|
184 | #endif
|
---|
185 |
|
---|
186 | #include <fcntl.h>
|
---|
187 | #ifndef NO_UNIX_SOCKETS
|
---|
188 | # include <sys/un.h>
|
---|
189 | #endif
|
---|
190 | #include <signal.h>
|
---|
191 | #ifdef HAVE_SYS_SIGNAL_H
|
---|
192 | # include <sys/signal.h>
|
---|
193 | #endif
|
---|
194 | #ifndef RT_OS_WINDOWS
|
---|
195 | # include <sys/socket.h>
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | #if defined(HAVE_SYS_IOCTL_H)
|
---|
199 | # include <sys/ioctl.h>
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | #ifdef HAVE_SYS_SELECT_H
|
---|
203 | # include <sys/select.h>
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | #ifdef HAVE_SYS_WAIT_H
|
---|
207 | # include <sys/wait.h>
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | #ifdef HAVE_SYS_FILIO_H
|
---|
211 | # include <sys/filio.h>
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | #if defined(__STDC__) || defined(_MSC_VER)
|
---|
215 | # include <stdarg.h>
|
---|
216 | #else
|
---|
217 | # include <varargs.h>
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | #include <sys/stat.h>
|
---|
221 |
|
---|
222 | /* Avoid conflicting with the libc insque() and remque(), which
|
---|
223 | * have different prototypes. */
|
---|
224 | #define insque slirp_insque
|
---|
225 | #define remque slirp_remque
|
---|
226 |
|
---|
227 | #ifdef HAVE_SYS_STROPTS_H
|
---|
228 | # include <sys/stropts.h>
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | #include "libslirp.h"
|
---|
232 |
|
---|
233 | #include "debug.h"
|
---|
234 |
|
---|
235 | #include "ip.h"
|
---|
236 | #include "tcp.h"
|
---|
237 | #include "tcp_timer.h"
|
---|
238 | #include "tcp_var.h"
|
---|
239 | #include "tcpip.h"
|
---|
240 | #include "udp.h"
|
---|
241 | #include "icmp_var.h"
|
---|
242 | #include "mbuf.h"
|
---|
243 | #include "sbuf.h"
|
---|
244 | #include "socket.h"
|
---|
245 | #include "if.h"
|
---|
246 | #include "main.h"
|
---|
247 | #include "misc.h"
|
---|
248 | #include "ctl.h"
|
---|
249 | #include "bootp.h"
|
---|
250 | #include "tftp.h"
|
---|
251 |
|
---|
252 | #include "slirp_state.h"
|
---|
253 |
|
---|
254 | #undef PVM /* XXX Mac OS X hack */
|
---|
255 |
|
---|
256 | #ifndef NULL
|
---|
257 | # define NULL (void *)0
|
---|
258 | #endif
|
---|
259 |
|
---|
260 | void if_start _P((PNATState));
|
---|
261 |
|
---|
262 | #ifdef BAD_SPRINTF
|
---|
263 | # define vsprintf vsprintf_len
|
---|
264 | # define sprintf sprintf_len
|
---|
265 | extern int vsprintf_len _P((char *, const char *, va_list));
|
---|
266 | extern int sprintf_len _P((char *, const char *, ...));
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | #ifdef DECLARE_SPRINTF
|
---|
270 | # ifndef BAD_SPRINTF
|
---|
271 | extern int vsprintf _P((char *, const char *, va_list));
|
---|
272 | # endif
|
---|
273 | extern int vfprintf _P((FILE *, const char *, va_list));
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | #ifndef HAVE_STRERROR
|
---|
277 | extern char *strerror _P((int error));
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | #ifndef HAVE_INDEX
|
---|
281 | char *index _P((const char *, int));
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | #ifndef HAVE_GETHOSTID
|
---|
285 | long gethostid _P((void));
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | DECLINLINE(void) lprint (const char *pszFormat, ...)
|
---|
289 | {
|
---|
290 | #ifdef LOG_ENABLED
|
---|
291 | va_list args;
|
---|
292 | va_start(args, pszFormat);
|
---|
293 | #if 1 /* nearly only for stats which we always want. */
|
---|
294 | RTLogPrintfV(pszFormat, args);
|
---|
295 | #else
|
---|
296 | Log(("%N", pszFormat, &args));
|
---|
297 | #endif
|
---|
298 | va_end(args);
|
---|
299 | #endif
|
---|
300 | }
|
---|
301 |
|
---|
302 | extern int do_echo;
|
---|
303 |
|
---|
304 | #if SIZEOF_CHAR_P == 4
|
---|
305 | # define insque_32 insque
|
---|
306 | # define remque_32 remque
|
---|
307 | #else
|
---|
308 | extern void insque_32 _P((PNATState, void *, void *));
|
---|
309 | extern void remque_32 _P((PNATState, void *));
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | #ifndef RT_OS_WINDOWS
|
---|
313 | #include <netdb.h>
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | #define DEFAULT_BAUD 115200
|
---|
317 |
|
---|
318 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr);
|
---|
319 |
|
---|
320 | /* cksum.c */
|
---|
321 | int cksum(struct mbuf *m, int len);
|
---|
322 |
|
---|
323 | /* if.c */
|
---|
324 | void if_init _P((PNATState));
|
---|
325 | void if_output _P((PNATState, struct socket *, struct mbuf *));
|
---|
326 |
|
---|
327 | /* ip_input.c */
|
---|
328 | void ip_init _P((PNATState));
|
---|
329 | void ip_input _P((PNATState, struct mbuf *));
|
---|
330 | #ifndef VBOX_WITH_BSD_REASS
|
---|
331 | struct ip * ip_reass _P((PNATState, register struct ipasfrag *, register struct ipq_t *));
|
---|
332 | void ip_freef _P((PNATState, struct ipq_t *));
|
---|
333 | #else /* !VBOX_WITH_BSD_REASS */
|
---|
334 | struct mbuf * ip_reass _P((PNATState, register struct mbuf *));
|
---|
335 | void ip_freef _P((PNATState, struct ipqhead *, struct ipq_t *));
|
---|
336 | #endif /* VBOX_WITH_BSD_REASS */
|
---|
337 | void ip_enq _P((PNATState, register struct ipasfrag *, register struct ipasfrag *));
|
---|
338 | void ip_deq _P((PNATState, register struct ipasfrag *));
|
---|
339 | void ip_slowtimo _P((PNATState));
|
---|
340 | void ip_stripoptions _P((register struct mbuf *, struct mbuf *));
|
---|
341 |
|
---|
342 | /* ip_output.c */
|
---|
343 | int ip_output _P((PNATState, struct socket *, struct mbuf *));
|
---|
344 |
|
---|
345 | /* tcp_input.c */
|
---|
346 | #ifndef VBOX_WITH_BSD_REASS
|
---|
347 | int tcp_reass _P((PNATState, register struct tcpcb *, register struct tcpiphdr *, struct mbuf *));
|
---|
348 | #else /* !VBOX_WITH_BSD_REASS */
|
---|
349 | int tcp_reass _P((PNATState, struct tcpcb *, struct tcphdr *, int *, struct mbuf *));
|
---|
350 | #endif /* VBOX_WITH_BSD_REASS */
|
---|
351 | void tcp_input _P((PNATState, register struct mbuf *, int, struct socket *));
|
---|
352 | void tcp_dooptions _P((PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *));
|
---|
353 | void tcp_xmit_timer _P((PNATState, register struct tcpcb *, int));
|
---|
354 | int tcp_mss _P((PNATState, register struct tcpcb *, u_int));
|
---|
355 |
|
---|
356 | /* tcp_output.c */
|
---|
357 | int tcp_output _P((PNATState, register struct tcpcb *));
|
---|
358 | void tcp_setpersist _P((register struct tcpcb *));
|
---|
359 |
|
---|
360 | /* tcp_subr.c */
|
---|
361 | void tcp_init _P((PNATState));
|
---|
362 | void tcp_template _P((struct tcpcb *));
|
---|
363 | void tcp_respond _P((PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int));
|
---|
364 | struct tcpcb * tcp_newtcpcb _P((PNATState, struct socket *));
|
---|
365 | struct tcpcb * tcp_close _P((PNATState, register struct tcpcb *));
|
---|
366 | void tcp_drain _P((void));
|
---|
367 | void tcp_sockclosed _P((PNATState, struct tcpcb *));
|
---|
368 | int tcp_fconnect _P((PNATState, struct socket *));
|
---|
369 | void tcp_connect _P((PNATState, struct socket *));
|
---|
370 | int tcp_attach _P((PNATState, struct socket *));
|
---|
371 | u_int8_t tcp_tos _P((struct socket *));
|
---|
372 | int tcp_emu _P((PNATState, struct socket *, struct mbuf *));
|
---|
373 | int tcp_ctl _P((PNATState, struct socket *));
|
---|
374 | struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
|
---|
375 |
|
---|
376 | #define MIN_MRU 128
|
---|
377 | #define MAX_MRU 16384
|
---|
378 |
|
---|
379 | #ifndef RT_OS_WINDOWS
|
---|
380 | # define min(x,y) ((x) < (y) ? (x) : (y))
|
---|
381 | # define max(x,y) ((x) > (y) ? (x) : (y))
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | #ifdef RT_OS_WINDOWS
|
---|
385 | # undef errno
|
---|
386 | # define errno (WSAGetLastError())
|
---|
387 | #endif
|
---|
388 |
|
---|
389 | #endif
|
---|