VirtualBox

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

Last change on this file since 41800 was 39287, checked in by vboxsync, 13 years ago

NAT: "sending to home" on wide cast and cloning udp sockets. Both disabled.

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