VirtualBox

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

Last change on this file since 85805 was 85195, checked in by vboxsync, 5 years ago

slirp: Clang 11 / OSX SDK 10.13 adjustments. bugref:9790

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

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