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