VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/slirp.h@ 61198

Last change on this file since 61198 was 60142, checked in by vboxsync, 9 years ago

NAT: Make host resolver asynchronous so that a blocked synchronous
lookup doesn't stop the whole tcp/ip stack.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* $Id: slirp.h 60142 2016-03-22 21:44:59Z vboxsync $ */
2/** @file
3 * NAT - slirp (declarations/defines).
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef __COMMON_H__
19#define __COMMON_H__
20
21#include <VBox/vmm/stam.h>
22
23#ifdef RT_OS_WINDOWS
24# include <winsock2.h>
25# include <ws2tcpip.h>
26typedef int socklen_t;
27#endif
28#ifdef RT_OS_OS2 /* temporary workaround, see ticket #127 */
29# define mbstat mbstat_os2
30# include <sys/socket.h>
31# undef mbstat
32typedef int socklen_t;
33#endif
34
35#define CONFIG_QEMU
36
37#ifdef DEBUG
38# undef DEBUG
39# define DEBUG 1
40#endif
41
42#ifndef CONFIG_QEMU
43# include "version.h"
44#endif
45#define LOG_GROUP LOG_GROUP_DRV_NAT
46#include <VBox/log.h>
47#include <iprt/mem.h>
48#ifdef RT_OS_WINDOWS
49# include <windows.h>
50# include <io.h>
51#endif
52#include <iprt/asm.h>
53#include <iprt/assert.h>
54#include <iprt/string.h>
55#include <iprt/dir.h>
56#include <iprt/rand.h>
57#include <iprt/net.h>
58#include <VBox/types.h>
59
60#undef malloc
61#define malloc dont_use_malloc
62#undef free
63#define free dont_use_free
64#undef realloc
65#define realloc dont_use_realloc
66#undef strdup
67#define strdup dont_use_strdup
68
69#include "slirp_config.h"
70
71#ifdef RT_OS_WINDOWS
72
73# ifndef _MSC_VER
74# include <inttypes.h>
75# endif
76
77
78# include <sys/timeb.h>
79# include <iphlpapi.h>
80
81/* We don't want the errno.h versions of these error defines. */
82# if defined(_MSC_VER) && _MSC_VER >= 1600
83# include <errno.h>
84# undef EWOULDBLOCK
85# undef EINPROGRESS
86# undef ENOTCONN
87# undef EHOSTUNREACH
88# undef ENETUNREACH
89# undef ECONNREFUSED
90# undef ECONNRESET
91# undef EHOSTDOWN
92# undef ENETDOWN
93# endif
94# define EWOULDBLOCK WSAEWOULDBLOCK
95# define EINPROGRESS WSAEINPROGRESS
96# define ENOTCONN WSAENOTCONN
97# define EHOSTUNREACH WSAEHOSTUNREACH
98# define ENETUNREACH WSAENETUNREACH
99# define ECONNREFUSED WSAECONNREFUSED
100# define ECONNRESET WSAECONNRESET
101# define EHOSTDOWN WSAEHOSTDOWN
102# define ENETDOWN WSAENETDOWN
103
104typedef uint8_t u_int8_t;
105typedef uint16_t u_int16_t;
106typedef uint32_t u_int32_t;
107
108#else /* !RT_OS_WINDOWS */
109
110# define ioctlsocket ioctl
111# define closesocket(s) close(s)
112# define O_BINARY 0
113
114#endif /* !RT_OS_WINDOWS */
115
116#if defined(RT_OS_WINDOWS) || defined (RT_OS_SOLARIS)
117typedef uint64_t u_int64_t;
118typedef char *caddr_t;
119#endif
120
121#include <sys/types.h>
122#ifdef HAVE_SYS_BITYPES_H
123# include <sys/bitypes.h>
124#endif
125
126#ifdef _MSC_VER
127# include <time.h>
128#else /* !_MSC_VER */
129# include <sys/time.h>
130#endif /* !_MSC_VER */
131
132#ifdef NEED_TYPEDEFS
133typedef char int8_t;
134typedef unsigned char u_int8_t;
135
136# if SIZEOF_SHORT == 2
137 typedef short int16_t;
138 typedef unsigned short u_int16_t;
139# else
140# if SIZEOF_INT == 2
141 typedef int int16_t;
142 typedef unsigned int u_int16_t;
143# else
144 #error Cannot find a type with sizeof() == 2
145# endif
146# endif
147
148# if SIZEOF_SHORT == 4
149 typedef short int32_t;
150 typedef unsigned short u_int32_t;
151# else
152# if SIZEOF_INT == 4
153 typedef int int32_t;
154 typedef unsigned int u_int32_t;
155# else
156 #error Cannot find a type with sizeof() == 4
157# endif
158# endif
159#endif /* NEED_TYPEDEFS */
160
161#ifdef HAVE_UNISTD_H
162# include <unistd.h>
163#endif
164
165#ifdef HAVE_STDLIB_H
166# include <stdlib.h>
167#endif
168
169#include <errno.h>
170
171
172#ifndef HAVE_MEMMOVE
173# define memmove(x, y, z) bcopy(y, x, z)
174#endif
175
176#if TIME_WITH_SYS_TIME
177# include <sys/time.h>
178# include <time.h>
179#else
180# if HAVE_SYS_TIME_H
181# include <sys/time.h>
182# else
183# include <time.h>
184# endif
185#endif
186
187#ifdef HAVE_STRING_H
188# include <string.h>
189#else
190# include <strings.h>
191#endif
192
193#ifndef RT_OS_WINDOWS
194# include <sys/uio.h>
195#endif
196
197#ifndef RT_OS_WINDOWS
198# include <netinet/in.h>
199# include <arpa/inet.h>
200#endif
201
202#ifdef GETTIMEOFDAY_ONE_ARG
203# define gettimeofday(x, y) gettimeofday(x)
204#endif
205
206#ifndef HAVE_INET_ATON
207int inet_aton (const char *cp, struct in_addr *ia);
208#endif
209
210#include <fcntl.h>
211#ifndef NO_UNIX_SOCKETS
212# include <sys/un.h>
213#endif
214#include <signal.h>
215#ifdef HAVE_SYS_SIGNAL_H
216# include <sys/signal.h>
217#endif
218#ifndef RT_OS_WINDOWS
219# include <sys/socket.h>
220#endif
221
222#if defined(HAVE_SYS_IOCTL_H)
223# include <sys/ioctl.h>
224#endif
225
226#ifdef HAVE_SYS_SELECT_H
227# include <sys/select.h>
228#endif
229
230#ifdef HAVE_SYS_WAIT_H
231# include <sys/wait.h>
232#endif
233
234#ifdef HAVE_SYS_FILIO_H
235# include <sys/filio.h>
236#endif
237
238#if defined(__STDC__) || defined(_MSC_VER)
239# include <stdarg.h>
240#else
241# include <varargs.h>
242#endif
243
244#include <sys/stat.h>
245
246/* Avoid conflicting with the libc insque() and remque(), which
247 * have different prototypes. */
248#define insque slirp_insque
249#define remque slirp_remque
250
251#ifdef HAVE_SYS_STROPTS_H
252# include <sys/stropts.h>
253#endif
254
255#include "libslirp.h"
256
257#include "debug.h"
258
259#include "ip.h"
260#include "tcp.h"
261#include "tcp_timer.h"
262#include "tcp_var.h"
263#include "tcpip.h"
264#include "udp.h"
265#include "icmp_var.h"
266#include "mbuf.h"
267#include "if.h"
268#include "sbuf.h"
269#include "socket.h"
270#include "main.h"
271#include "misc.h"
272#include "ctl.h"
273#include "bootp.h"
274#include "tftp.h"
275
276#include "slirp_state.h"
277#include "slirp_dns.h"
278
279#undef PVM /* XXX Mac OS X hack */
280
281#ifndef NULL
282# define NULL (void *)0
283#endif
284
285void if_start (PNATState);
286
287#ifndef HAVE_INDEX
288 char *index (const char *, int);
289#endif
290
291#ifndef HAVE_GETHOSTID
292 long gethostid (void);
293#endif
294
295#ifndef RT_OS_WINDOWS
296#include <netdb.h>
297#endif
298
299#include "dnsproxy/dnsproxy.h"
300
301#define DEFAULT_BAUD 115200
302
303int get_dns_addr(PNATState pData);
304
305/* cksum.c */
306typedef uint16_t u_short;
307typedef unsigned int u_int;
308#include "in_cksum.h"
309
310/* if.c */
311void if_init (PNATState);
312void if_output (PNATState, struct socket *, struct mbuf *);
313
314/* ip_input.c */
315void ip_init (PNATState);
316void ip_input (PNATState, struct mbuf *);
317struct mbuf * ip_reass (PNATState, register struct mbuf *);
318void ip_freef (PNATState, struct ipqhead *, struct ipq_t *);
319void ip_slowtimo (PNATState);
320void ip_stripoptions (register struct mbuf *, struct mbuf *);
321
322/* ip_output.c */
323int ip_output (PNATState, struct socket *, struct mbuf *);
324int ip_output0 (PNATState, struct socket *, struct mbuf *, int urg);
325
326/* tcp_input.c */
327int tcp_reass (PNATState, struct tcpcb *, struct tcphdr *, int *, struct mbuf *);
328void tcp_input (PNATState, register struct mbuf *, int, struct socket *);
329void tcp_fconnect_failed(PNATState, struct socket *, int);
330void tcp_dooptions (PNATState, struct tcpcb *, u_char *, int, struct tcpiphdr *);
331void tcp_xmit_timer (PNATState, register struct tcpcb *, int);
332int tcp_mss (PNATState, register struct tcpcb *, u_int);
333
334/* tcp_output.c */
335int tcp_output (PNATState, register struct tcpcb *);
336void tcp_setpersist (register struct tcpcb *);
337
338/* tcp_subr.c */
339void tcp_init (PNATState);
340void tcp_template (struct tcpcb *);
341void tcp_respond (PNATState, struct tcpcb *, register struct tcpiphdr *, register struct mbuf *, tcp_seq, tcp_seq, int);
342struct tcpcb * tcp_newtcpcb (PNATState, struct socket *);
343struct tcpcb * tcp_close (PNATState, register struct tcpcb *);
344void tcp_drain (void);
345void tcp_sockclosed (PNATState, struct tcpcb *);
346int tcp_fconnect (PNATState, struct socket *);
347void tcp_connect (PNATState, struct socket *);
348int tcp_attach (PNATState, struct socket *);
349u_int8_t tcp_tos (struct socket *);
350int tcp_ctl (PNATState, struct socket *);
351struct tcpcb *tcp_drop(PNATState, struct tcpcb *tp, int err);
352
353/* hostres.c */
354struct mbuf *hostresolver(PNATState, struct mbuf *, uint32_t src, uint16_t sport);
355
356/*slirp.c*/
357void slirp_arp_who_has(PNATState pData, uint32_t dst);
358int slirp_arp_cache_update_or_add(PNATState pData, uint32_t dst, const uint8_t *mac);
359int slirp_init_dns_list(PNATState pData);
360void slirp_release_dns_list(PNATState pData);
361#define MIN_MRU 128
362#define MAX_MRU 16384
363
364#ifndef RT_OS_WINDOWS
365# define min(x, y) ((x) < (y) ? (x) : (y))
366# define max(x, y) ((x) > (y) ? (x) : (y))
367#endif
368
369#ifdef RT_OS_WINDOWS
370# undef errno
371# if 0 /* debugging */
372int errno_func(const char *file, int line);
373# define errno (errno_func(__FILE__, __LINE__))
374# else
375# define errno (WSAGetLastError())
376# endif
377#endif
378
379# define ETH_ALEN 6
380# define ETH_HLEN 14
381
382# define ARPOP_REQUEST 1 /* ARP request */
383# define ARPOP_REPLY 2 /* ARP reply */
384
385struct ethhdr
386{
387 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
388 unsigned char h_source[ETH_ALEN]; /* source ether addr */
389 unsigned short h_proto; /* packet type ID field */
390};
391AssertCompileSize(struct ethhdr, 14);
392
393/*
394 * (vvl) externing of sscanf.
395 */
396int sscanf(const char *s, const char *format, ...);
397
398#if defined(VBOX_SLIRP_ALIAS) || defined(VBOX_SLIRP_BSD)
399
400# define ip_next(ip) (void *)((uint8_t *)(ip) + ((ip)->ip_hl << 2))
401# define udp_next(udp) (void *)((uint8_t *)&((struct udphdr *)(udp))[1])
402# define bcopy(src, dst, len) memcpy((dst), (src), (len))
403# define bcmp(a1, a2, len) memcmp((a1), (a2), (len))
404# define NO_FW_PUNCH
405/* Two wrongs don't make a right, but this at least averts harm. */
406# define NO_USE_SOCKETS
407
408# ifdef alias_addr
409# ifndef VBOX_SLIRP_BSD
410# error alias_addr has already defined!!!
411# else
412# undef alias_addr
413# endif
414# endif
415
416# define arc4random() RTRandU32()
417# undef malloc
418# undef calloc
419# undef free
420# define malloc(x) RTMemAlloc((x))
421# define calloc(x, n) RTMemAllocZ((x)*(n))
422# define free(x) RTMemFree((x))
423# ifndef __unused
424# define __unused
425# endif
426
427# define strncasecmp RTStrNICmp
428# define stderr NULL
429# define stdout NULL
430
431# ifdef VBOX_WITH_DEBUG_LIBALIAS
432# define LIBALIAS_DEBUG
433# endif
434
435# define fflush(x) do{} while(0)
436# include "ext.h"
437#endif /*VBOX_SLIRP_ALIAS*/
438
439/**
440 * @todo might be useful to make it configurable, especially in terms of Intnet behind NAT
441 */
442# define maxusers 32
443# define max_protohdr 0
444/**
445 * @todo (vvl) for now ignore these values, later perhaps initialize tuning parameters
446 */
447# define TUNABLE_INT_FETCH(name, pval) do { } while (0)
448# define SYSCTL_PROC(a0, a1, a2, a3, a4, a5, a6, a7, a8) const int dummy_ ## a6 = 0
449# define SYSCTL_STRUCT(a0, a1, a2, a3, a4, a5, a6) const int dummy_ ## a5 = 0
450# define SYSINIT(a0, a1, a2, a3, a4) const int dummy_ ## a3 = 0
451# define sysctl_handle_int(a0, a1, a2, a3) 0
452# define EVENTHANDLER_INVOKE(a) do{}while(0)
453# define EVENTHANDLER_REGISTER(a0, a1, a2, a3) do{}while(0)
454# define KASSERT AssertMsg
455
456struct dummy_req
457{
458 void *newptr;
459};
460
461#define SYSCTL_HANDLER_ARGS PNATState pData, void *oidp, struct dummy_req *req
462
463void mbuf_init(void *);
464# define cksum(m, len) in_cksum_skip((m), (len), 0)
465
466int ftp_alias_load(PNATState);
467int ftp_alias_unload(PNATState);
468int nbt_alias_load(PNATState);
469int nbt_alias_unload(PNATState);
470int slirp_arp_lookup_ip_by_ether(PNATState, const uint8_t *, uint32_t *);
471int slirp_arp_lookup_ether_by_ip(PNATState, uint32_t, uint8_t *);
472
473static inline size_t slirp_size(PNATState pData)
474{
475 if (if_mtu < MSIZE)
476 return MCLBYTES;
477 else if (if_mtu < MCLBYTES)
478 return MCLBYTES;
479 else if (if_mtu < MJUM9BYTES)
480 return MJUM9BYTES;
481 else if (if_mtu < MJUM16BYTES)
482 return MJUM16BYTES;
483 else
484 AssertMsgFailed(("Unsupported size"));
485 return 0;
486}
487
488static inline bool slirpMbufTagService(PNATState pData, struct mbuf *m, uint8_t u8ServiceId)
489{
490 struct m_tag * t = NULL;
491 NOREF(pData);
492 /* if_encap assumes that all packets goes through aliased address(gw) */
493 if (u8ServiceId == CTL_ALIAS)
494 return true;
495 t = m_tag_get(PACKET_SERVICE, sizeof(uint8_t), 0);
496 if (!t)
497 return false;
498 *(uint8_t *)&t[1] = u8ServiceId;
499 m_tag_prepend(m, t);
500 return true;
501}
502
503/**
504 * This function tags mbuf allocated for special services.
505 * @todo: add service id verification.
506 */
507static inline struct mbuf *slirpServiceMbufAlloc(PNATState pData, uint8_t u8ServiceId)
508{
509 struct mbuf *m = NULL;
510 m = m_getcl(pData, M_DONTWAIT, MT_HEADER, M_PKTHDR);
511 if (!m)
512 return m;
513 if(!slirpMbufTagService(pData, m, u8ServiceId))
514 {
515 m_freem(pData, m);
516 return NULL;
517 }
518 return m;
519}
520
521static inline struct mbuf *slirpTftpMbufAlloc(PNATState pData)
522{
523 return slirpServiceMbufAlloc(pData, CTL_TFTP);
524}
525static inline struct mbuf *slirpDnsMbufAlloc(PNATState pData)
526{
527 return slirpServiceMbufAlloc(pData, CTL_DNS);
528}
529
530DECLINLINE(bool) slirpIsWideCasting(PNATState pData, uint32_t u32Addr)
531{
532 bool fWideCasting = false;
533 LogFlowFunc(("Enter: u32Addr:%RTnaipv4\n", u32Addr));
534 fWideCasting = ( u32Addr == INADDR_BROADCAST
535 || (u32Addr & RT_H2N_U32_C(~pData->netmask)) == RT_H2N_U32_C(~pData->netmask));
536 LogFlowFunc(("Leave: %RTbool\n", fWideCasting));
537 return fWideCasting;
538}
539#endif
540
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette