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 | # include <ws2tcpip.h>
|
---|
9 | typedef int socklen_t;
|
---|
10 | #endif
|
---|
11 | #ifdef RT_OS_OS2 /* temporary workaround, see ticket #127 */
|
---|
12 | # define mbstat mbstat_os2
|
---|
13 | # include <sys/socket.h>
|
---|
14 | # undef mbstat
|
---|
15 | typedef int socklen_t;
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #define CONFIG_QEMU
|
---|
19 |
|
---|
20 | #ifdef DEBUG
|
---|
21 | # undef DEBUG
|
---|
22 | # define DEBUG 1
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #ifndef CONFIG_QEMU
|
---|
26 | # include "version.h"
|
---|
27 | #endif
|
---|
28 | #define LOG_GROUP LOG_GROUP_DRV_NAT
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #ifdef RT_OS_WINDOWS
|
---|
32 | # include <windows.h>
|
---|
33 | # include <io.h>
|
---|
34 | #endif
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/dir.h>
|
---|
38 | #include <iprt/rand.h>
|
---|
39 | #include <VBox/types.h>
|
---|
40 |
|
---|
41 | #undef malloc
|
---|
42 | #define malloc dont_use_malloc
|
---|
43 | #undef free
|
---|
44 | #define free dont_use_free
|
---|
45 | #undef realloc
|
---|
46 | #define realloc dont_use_realloc
|
---|
47 | #undef strdup
|
---|
48 | #define strdup dont_use_strdup
|
---|
49 |
|
---|
50 | #include "slirp_config.h"
|
---|
51 |
|
---|
52 | #ifdef RT_OS_WINDOWS
|
---|
53 |
|
---|
54 | # ifndef _MSC_VER
|
---|
55 | # include <inttypes.h>
|
---|
56 | # endif
|
---|
57 |
|
---|
58 | typedef uint8_t u_int8_t;
|
---|
59 | typedef uint16_t u_int16_t;
|
---|
60 | typedef uint32_t u_int32_t;
|
---|
61 | typedef uint64_t u_int64_t;
|
---|
62 | typedef char *caddr_t;
|
---|
63 |
|
---|
64 | # include <sys/timeb.h>
|
---|
65 | # include <iphlpapi.h>
|
---|
66 |
|
---|
67 | # define EWOULDBLOCK WSAEWOULDBLOCK
|
---|
68 | # define EINPROGRESS WSAEINPROGRESS
|
---|
69 | # define ENOTCONN WSAENOTCONN
|
---|
70 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
71 | # define ENETUNREACH WSAENETUNREACH
|
---|
72 | # define ECONNREFUSED WSAECONNREFUSED
|
---|
73 |
|
---|
74 | #else /* !RT_OS_WINDOWS */
|
---|
75 |
|
---|
76 | # define ioctlsocket ioctl
|
---|
77 | # define closesocket(s) close(s)
|
---|
78 | # define O_BINARY 0
|
---|
79 |
|
---|
80 | #endif /* !RT_OS_WINDOWS */
|
---|
81 |
|
---|
82 | #include <sys/types.h>
|
---|
83 | #ifdef HAVE_SYS_BITYPES_H
|
---|
84 | # include <sys/bitypes.h>
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #ifdef _MSC_VER
|
---|
88 | # include <time.h>
|
---|
89 | #else /* !_MSC_VER */
|
---|
90 | # include <sys/time.h>
|
---|
91 | #endif /* !_MSC_VER */
|
---|
92 |
|
---|
93 | #ifdef NEED_TYPEDEFS
|
---|
94 | typedef char int8_t;
|
---|
95 | typedef unsigned char u_int8_t;
|
---|
96 |
|
---|
97 | # if SIZEOF_SHORT == 2
|
---|
98 | typedef short int16_t;
|
---|
99 | typedef unsigned short u_int16_t;
|
---|
100 | # else
|
---|
101 | # if SIZEOF_INT == 2
|
---|
102 | typedef int int16_t;
|
---|
103 | typedef unsigned int u_int16_t;
|
---|
104 | # else
|
---|
105 | #error Cannot find a type with sizeof() == 2
|
---|
106 | # endif
|
---|
107 | # endif
|
---|
108 |
|
---|
109 | # if SIZEOF_SHORT == 4
|
---|
110 | typedef short int32_t;
|
---|
111 | typedef unsigned short u_int32_t;
|
---|
112 | # else
|
---|
113 | # if SIZEOF_INT == 4
|
---|
114 | typedef int int32_t;
|
---|
115 | typedef unsigned int u_int32_t;
|
---|
116 | # else
|
---|
117 | #error Cannot find a type with sizeof() == 4
|
---|
118 | # endif
|
---|
119 | # endif
|
---|
120 | #endif /* NEED_TYPEDEFS */
|
---|
121 |
|
---|
122 | #ifdef HAVE_UNISTD_H
|
---|
123 | # include <unistd.h>
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | #ifdef HAVE_STDLIB_H
|
---|
127 | # include <stdlib.h>
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | #include <stdio.h>
|
---|
131 | #include <errno.h>
|
---|
132 |
|
---|
133 | #ifndef HAVE_MEMMOVE
|
---|
134 | # define memmove(x, y, z) bcopy(y, x, z)
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | #if TIME_WITH_SYS_TIME
|
---|
138 | # include <sys/time.h>
|
---|
139 | # include <time.h>
|
---|
140 | #else
|
---|
141 | # if HAVE_SYS_TIME_H
|
---|
142 | # include <sys/time.h>
|
---|
143 | # else
|
---|
144 | # include <time.h>
|
---|
145 | # endif
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | #ifdef HAVE_STRING_H
|
---|
149 | # include <string.h>
|
---|
150 | #else
|
---|
151 | # include <strings.h>
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | #ifndef RT_OS_WINDOWS
|
---|
155 | # include <sys/uio.h>
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | #ifndef RT_OS_WINDOWS
|
---|
159 | # include <netinet/in.h>
|
---|
160 | # include <arpa/inet.h>
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #ifdef GETTIMEOFDAY_ONE_ARG
|
---|
164 | # define gettimeofday(x, y) gettimeofday(x)
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | #ifndef HAVE_INET_ATON
|
---|
168 | int inet_aton (const char *cp, struct in_addr *ia);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | #include <fcntl.h>
|
---|
172 | #ifndef NO_UNIX_SOCKETS
|
---|
173 | # include <sys/un.h>
|
---|
174 | #endif
|
---|
175 | #include <signal.h>
|
---|
176 | #ifdef HAVE_SYS_SIGNAL_H
|
---|
177 | # include <sys/signal.h>
|
---|
178 | #endif
|
---|
179 | #ifndef RT_OS_WINDOWS
|
---|
180 | # include <sys/socket.h>
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | #if defined(HAVE_SYS_IOCTL_H)
|
---|
184 | # include <sys/ioctl.h>
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #ifdef HAVE_SYS_SELECT_H
|
---|
188 | # include <sys/select.h>
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | #ifdef HAVE_SYS_WAIT_H
|
---|
192 | # include <sys/wait.h>
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | #ifdef HAVE_SYS_FILIO_H
|
---|
196 | # include <sys/filio.h>
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | #if defined(__STDC__) || defined(_MSC_VER)
|
---|
200 | # include <stdarg.h>
|
---|
201 | #else
|
---|
202 | # include <varargs.h>
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | #include <sys/stat.h>
|
---|
206 |
|
---|
207 | /* Avoid conflicting with the libc insque() and remque(), which
|
---|
208 | * have different prototypes. */
|
---|
209 | #define insque slirp_insque
|
---|
210 | #define remque slirp_remque
|
---|
211 |
|
---|
212 | #ifdef HAVE_SYS_STROPTS_H
|
---|
213 | # include <sys/stropts.h>
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | #include "libslirp.h"
|
---|
217 |
|
---|
218 | #include "debug.h"
|
---|
219 |
|
---|
220 | #include "ip.h"
|
---|
221 | #include "tcp.h"
|
---|
222 | #include "tcp_timer.h"
|
---|
223 | #include "tcp_var.h"
|
---|
224 | #include "tcpip.h"
|
---|
225 | #include "udp.h"
|
---|
226 | #include "icmp_var.h"
|
---|
227 | #include "mbuf.h"
|
---|
228 | #include "sbuf.h"
|
---|
229 | #include "socket.h"
|
---|
230 | #include "if.h"
|
---|
231 | #include "main.h"
|
---|
232 | #include "misc.h"
|
---|
233 | #include "ctl.h"
|
---|
234 | #include "bootp.h"
|
---|
235 | #include "tftp.h"
|
---|
236 |
|
---|
237 | #include "slirp_state.h"
|
---|
238 |
|
---|
239 | #undef PVM /* XXX Mac OS X hack */
|
---|
240 |
|
---|
241 | #ifndef NULL
|
---|
242 | # define NULL (void *)0
|
---|
243 | #endif
|
---|
244 |
|
---|
245 | void if_start (PNATState);
|
---|
246 |
|
---|
247 | #ifndef HAVE_INDEX
|
---|
248 | char *index (const char *, int);
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | #ifndef HAVE_GETHOSTID
|
---|
252 | long gethostid (void);
|
---|
253 | #endif
|
---|
254 |
|
---|
255 | #if SIZEOF_CHAR_P == 4
|
---|
256 | # define insque_32 insque
|
---|
257 | # define remque_32 remque
|
---|
258 | #else
|
---|
259 | extern void insque_32 (PNATState, void *, void *);
|
---|
260 | extern void remque_32 (PNATState, void *);
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | #ifndef RT_OS_WINDOWS
|
---|
264 | #include <netdb.h>
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | #ifdef VBOX_WITH_SLIRP_DNS_PROXY
|
---|
268 | # include "dnsproxy/dnsproxy.h"
|
---|
269 | #endif
|
---|
270 |
|
---|
271 | #define DEFAULT_BAUD 115200
|
---|
272 |
|
---|
273 | int get_dns_addr(PNATState pData, struct in_addr *pdns_addr);
|
---|
274 |
|
---|
275 | /* cksum.c */
|
---|
276 | int cksum(struct mbuf *m, int len);
|
---|
277 |
|
---|
278 | /* if.c */
|
---|
279 | void if_init (PNATState);
|
---|
280 | void if_output (PNATState, struct socket *, struct mbuf *);
|
---|
281 |
|
---|
282 | /* ip_input.c */
|
---|
283 | void ip_init (PNATState);
|
---|
284 | void ip_input (PNATState, struct mbuf *);
|
---|
285 | struct mbuf * ip_reass (PNATState, register struct mbuf *);
|
---|
286 | void ip_freef (PNATState, struct ipqhead *, struct ipq_t *);
|
---|
287 | void ip_slowtimo (PNATState);
|
---|
288 | void ip_stripoptions (register struct mbuf *, struct mbuf *);
|
---|
289 |
|
---|
290 | /* ip_output.c */
|
---|
291 | int ip_output (PNATState, struct socket *, struct mbuf *);
|
---|
292 |
|
---|
293 | /* tcp_input.c */
|
---|
294 | int tcp_reass (PNATState, struct tcpcb *, struct tcphdr *, int *, struct mbuf *);
|
---|
295 | void tcp_input (PNATState, register struct mbuf *, int, struct socket *);
|
---|
296 | void tcp_dooptions (PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *);
|
---|
297 | void tcp_xmit_timer (PNATState, register struct tcpcb *, int);
|
---|
298 | int tcp_mss (PNATState, register struct tcpcb *, u_int);
|
---|
299 |
|
---|
300 | /* tcp_output.c */
|
---|
301 | int tcp_output (PNATState, register struct tcpcb *);
|
---|
302 | void tcp_setpersist (register struct tcpcb *);
|
---|
303 |
|
---|
304 | /* tcp_subr.c */
|
---|
305 | void tcp_init (PNATState);
|
---|
306 | void tcp_template (struct tcpcb *);
|
---|
307 | void tcp_respond (PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
|
---|
308 | struct tcpcb * tcp_newtcpcb (PNATState, struct socket *);
|
---|
309 | struct tcpcb * tcp_close (PNATState, register struct tcpcb *);
|
---|
310 | void tcp_drain (void);
|
---|
311 | void tcp_sockclosed (PNATState, struct tcpcb *);
|
---|
312 | int tcp_fconnect (PNATState, struct socket *);
|
---|
313 | void tcp_connect (PNATState, struct socket *);
|
---|
314 | int tcp_attach (PNATState, struct socket *);
|
---|
315 | u_int8_t tcp_tos (struct socket *);
|
---|
316 | int tcp_emu (PNATState, struct socket *, struct mbuf *);
|
---|
317 | int tcp_ctl (PNATState, struct socket *);
|
---|
318 | struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
|
---|
319 |
|
---|
320 | uint16_t slirp_get_service(int proto, uint16_t dport, uint16_t sport);
|
---|
321 |
|
---|
322 | #define MIN_MRU 128
|
---|
323 | #define MAX_MRU 16384
|
---|
324 |
|
---|
325 | #ifndef RT_OS_WINDOWS
|
---|
326 | # define min(x, y) ((x) < (y) ? (x) : (y))
|
---|
327 | # define max(x, y) ((x) > (y) ? (x) : (y))
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | #ifdef RT_OS_WINDOWS
|
---|
331 | # undef errno
|
---|
332 | # if 0 /* debugging */
|
---|
333 | int errno_func(const char *file, int line);
|
---|
334 | # define errno (errno_func(__FILE__, __LINE__))
|
---|
335 | # else
|
---|
336 | # define errno (WSAGetLastError())
|
---|
337 | # endif
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
341 | #define DO_ALIAS(paddr) \
|
---|
342 | do { \
|
---|
343 | if ((paddr)->s_addr == dns_addr.s_addr) \
|
---|
344 | { \
|
---|
345 | (paddr)->s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS); \
|
---|
346 | } \
|
---|
347 | } while(0)
|
---|
348 | #else
|
---|
349 | #define DO_ALIAS(paddr) do {} while (0)
|
---|
350 | #endif
|
---|
351 |
|
---|
352 |
|
---|
353 | # ifdef VBOX_WITHOUT_SLIRP_CLIENT_ETHER
|
---|
354 | # define ETH_ALEN 6
|
---|
355 | # define ETH_HLEN 14
|
---|
356 |
|
---|
357 | # define ARPOP_REQUEST 1 /* ARP request */
|
---|
358 | # define ARPOP_REPLY 2 /* ARP reply */
|
---|
359 |
|
---|
360 | struct ethhdr
|
---|
361 | {
|
---|
362 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
|
---|
363 | unsigned char h_source[ETH_ALEN]; /* source ether addr */
|
---|
364 | unsigned short h_proto; /* packet type ID field */
|
---|
365 | };
|
---|
366 | AssertCompileSize(struct ethhdr, 14);
|
---|
367 | # endif
|
---|
368 | #if defined(VBOX_WITH_SLIRP_ALIAS) && defined(VBOX_SLIRP_ALIAS)
|
---|
369 |
|
---|
370 | # define ip_next(ip) (void *)((uint8_t *)(ip) + ((ip)->ip_hl << 2))
|
---|
371 | # define udp_next(udp) (void *)((uint8_t *)&((struct udphdr *)(udp))[1] )
|
---|
372 | # define bcopy(src, dst, len) memcpy((dst), (src), (len))
|
---|
373 | # define bcmp(a1, a2, len) memcmp((a1), (a2), (len))
|
---|
374 | # define NO_FW_PUNCH
|
---|
375 |
|
---|
376 | # ifdef alias_addr
|
---|
377 | # error alias_addr has already defined!!!
|
---|
378 | # endif
|
---|
379 |
|
---|
380 | # define arc4random() RTRandU32()
|
---|
381 | # undef malloc
|
---|
382 | # undef calloc
|
---|
383 | # undef free
|
---|
384 | # define malloc(x) RTMemAlloc((x))
|
---|
385 | # define calloc(x, n) RTMemAllocZ((x)*(n))
|
---|
386 | # define free(x) RTMemFree((x))
|
---|
387 | # ifndef __unused
|
---|
388 | # define __unused
|
---|
389 | # endif
|
---|
390 |
|
---|
391 | # define strncasecmp RTStrNICmp
|
---|
392 |
|
---|
393 | # define LIBALIAS_DEBUG
|
---|
394 |
|
---|
395 | # ifdef fprintf
|
---|
396 | # undef fprintf
|
---|
397 | # endif /*fprintf*/
|
---|
398 | # ifdef fflush
|
---|
399 | # undef fflush
|
---|
400 | # endif /*fflush*/
|
---|
401 | # ifdef printf
|
---|
402 | # undef printf
|
---|
403 | # endif /*printf*/
|
---|
404 | #define fflush(x) do{}while(0)
|
---|
405 | # define fprintf vbox_slirp_fprintf
|
---|
406 | # define printf vbox_slirp_printf
|
---|
407 | static void vbox_slirp_printV(char *format, va_list args)
|
---|
408 | {
|
---|
409 | char buffer[1024];
|
---|
410 | memset(buffer, 0, 1024);
|
---|
411 | RTStrPrintfV(buffer, 1024, format, args);
|
---|
412 |
|
---|
413 | Log2(("NAT:ALIAS: %s\n", buffer));
|
---|
414 | }
|
---|
415 | static void vbox_slirp_printf(char *format, ...)
|
---|
416 | {
|
---|
417 | va_list args;
|
---|
418 | va_start(args, format);
|
---|
419 | vbox_slirp_printV(format, args);
|
---|
420 | va_end(args);
|
---|
421 | }
|
---|
422 | static void vbox_slirp_fprintf(void *ignored, char *format, ...)
|
---|
423 | {
|
---|
424 | va_list args;
|
---|
425 | va_start(args, format);
|
---|
426 | vbox_slirp_printV(format, args);
|
---|
427 | va_end(args);
|
---|
428 | }
|
---|
429 | #endif /*VBOX_WITH_SLIRP_ALIAS && VBOX_SLIRP_ALIAS*/
|
---|
430 |
|
---|
431 | #ifdef VBOX_WITH_SLIRP_ALIAS
|
---|
432 | int ftp_alias_load(void);
|
---|
433 | int ftp_alias_unload(void);
|
---|
434 | int nbt_alias_load(void);
|
---|
435 | int nbt_alias_unload(void);
|
---|
436 | #endif /*VBOX_WITH_SLIRP_ALIAS*/
|
---|
437 |
|
---|
438 | #ifdef VBOX_WITH_STATISTICS
|
---|
439 | #define VBOX_SLIRP_COUNTER_OPER_ARG0(name, type, op) \
|
---|
440 | STAM_##type##_##op(&pData->Stat##name)
|
---|
441 | #define VBOX_SLIRP_COUNTER_OPER_ARG1(name, type, op, arg) \
|
---|
442 | STAM_##type##_##op(&pData->Stat##name, arg)
|
---|
443 | #else
|
---|
444 | #define VBOX_SLIRP_COUNTER_OPER_ARG0(name, type, op) do{}while(0)
|
---|
445 | #define VBOX_SLIRP_COUNTER_OPER_ARG1(name, type, op, arg) do{}while(0)
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | #define SLIRP_PROFILE_START(name, arg) \
|
---|
449 | VBOX_SLIRP_COUNTER_OPER_ARG1(name, PROFILE, START, arg)
|
---|
450 | #define SLIRP_PROFILE_STOP(name, arg) \
|
---|
451 | VBOX_SLIRP_COUNTER_OPER_ARG1(name, PROFILE, STOP, arg)
|
---|
452 |
|
---|
453 | #define SLIRP_COUNTER_RESET(name) \
|
---|
454 | VBOX_SLIRP_COUNTER_OPER_ARG0(name, COUNTER, RESET)
|
---|
455 |
|
---|
456 | #define SLIRP_COUNTER_INC(name) \
|
---|
457 | VBOX_SLIRP_COUNTER_OPER_ARG0(name, COUNTER, INC)
|
---|
458 |
|
---|
459 | #define SLIRP_COUNTER_ADD(name, val) \
|
---|
460 | VBOX_SLIRP_COUNTER_OPER_ARG1(name, COUNTER, ADD, (val))
|
---|
461 |
|
---|
462 | #endif
|
---|