1 | /** @file
|
---|
2 | * NAT state/configuration.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
17 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
18 | * additional information or have any questions.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef ___slirp_state_h
|
---|
22 | #define ___slirp_state_h
|
---|
23 |
|
---|
24 | #include <iprt/req.h>
|
---|
25 |
|
---|
26 | #define COUNTERS_INIT
|
---|
27 | #include "counters.h"
|
---|
28 |
|
---|
29 | #include "ip_icmp.h"
|
---|
30 | #include "dnsproxy/dnsproxy.h"
|
---|
31 |
|
---|
32 | /** Number of DHCP clients supported by NAT. */
|
---|
33 | #define NB_ADDR 16
|
---|
34 |
|
---|
35 | /** Where to start DHCP IP number allocation. */
|
---|
36 | #define START_ADDR 15
|
---|
37 |
|
---|
38 | /** DHCP Lease time. */
|
---|
39 | #define LEASE_TIME (24 * 3600)
|
---|
40 |
|
---|
41 | /** Entry in the table of known DHCP clients. */
|
---|
42 | typedef struct
|
---|
43 | {
|
---|
44 | uint32_t xid;
|
---|
45 | bool allocated;
|
---|
46 | uint8_t macaddr[6];
|
---|
47 | struct in_addr addr;
|
---|
48 | int number;
|
---|
49 | } BOOTPClient;
|
---|
50 |
|
---|
51 |
|
---|
52 | /** TFTP session entry. */
|
---|
53 | struct tftp_session
|
---|
54 | {
|
---|
55 | int in_use;
|
---|
56 | unsigned char filename[TFTP_FILENAME_MAX];
|
---|
57 |
|
---|
58 | struct in_addr client_ip;
|
---|
59 | u_int16_t client_port;
|
---|
60 |
|
---|
61 | int timestamp;
|
---|
62 | };
|
---|
63 |
|
---|
64 | struct dns_domain_entry
|
---|
65 | {
|
---|
66 | char *dd_pszDomain;
|
---|
67 | LIST_ENTRY(dns_domain_entry) dd_list;
|
---|
68 | };
|
---|
69 | LIST_HEAD(dns_domain_list_head, dns_domain_entry);
|
---|
70 |
|
---|
71 | struct dns_entry
|
---|
72 | {
|
---|
73 | struct in_addr de_addr;
|
---|
74 | TAILQ_ENTRY(dns_entry) de_list;
|
---|
75 | };
|
---|
76 | TAILQ_HEAD(dns_list_head, dns_entry);
|
---|
77 |
|
---|
78 | struct port_forward_rule
|
---|
79 | {
|
---|
80 | uint16_t proto;
|
---|
81 | uint16_t host_port;
|
---|
82 | uint16_t guest_port;
|
---|
83 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
84 | struct in_addr guest_addr;
|
---|
85 | #endif
|
---|
86 | struct in_addr bind_ip;
|
---|
87 | uint8_t mac_address[6]; /*need ETH_ALEN here */
|
---|
88 | int activated;
|
---|
89 | LIST_ENTRY(port_forward_rule) list;
|
---|
90 | };
|
---|
91 | LIST_HEAD(port_forward_rule_list, port_forward_rule);
|
---|
92 |
|
---|
93 | /* forward declaration */
|
---|
94 | struct proto_handler;
|
---|
95 |
|
---|
96 | /** Main state/configuration structure for slirp NAT. */
|
---|
97 | typedef struct NATState
|
---|
98 | {
|
---|
99 | /* Stuff from boot.c */
|
---|
100 | BOOTPClient bootp_clients[NB_ADDR];
|
---|
101 | const char *bootp_filename;
|
---|
102 | /* Stuff from if.c */
|
---|
103 | int if_mtu, if_mru;
|
---|
104 | int if_comp;
|
---|
105 | int if_maxlinkhdr;
|
---|
106 | int if_queued;
|
---|
107 | int if_thresh;
|
---|
108 | struct mbuf if_fastq;
|
---|
109 | struct mbuf if_batchq;
|
---|
110 | struct mbuf *next_m;
|
---|
111 | /* Stuff from icmp.c */
|
---|
112 | struct icmpstat_t icmpstat;
|
---|
113 | /* Stuff from ip_input.c */
|
---|
114 | struct ipstat_t ipstat;
|
---|
115 | struct ipqhead ipq[IPREASS_NHASH];
|
---|
116 | int maxnipq; /* Administrative limit on # of reass queues*/
|
---|
117 | int maxfragsperpacket; /* Maximum number of IPv4 fragments allowed per packet */
|
---|
118 | int nipq; /* total number of reass queues */
|
---|
119 | uint16_t ip_currid;
|
---|
120 | /* Stuff from mbuf.c */
|
---|
121 | int mbuf_alloced, mbuf_max;
|
---|
122 | int msize;
|
---|
123 | struct mbuf m_freelist, m_usedlist;
|
---|
124 | /* Stuff from slirp.c */
|
---|
125 | void *pvUser;
|
---|
126 | uint32_t curtime;
|
---|
127 | uint32_t time_fasttimo;
|
---|
128 | uint32_t last_slowtimo;
|
---|
129 | bool do_slowtimo;
|
---|
130 | bool link_up;
|
---|
131 | struct timeval tt;
|
---|
132 | struct in_addr our_addr;
|
---|
133 | struct in_addr alias_addr;
|
---|
134 | struct in_addr special_addr;
|
---|
135 |
|
---|
136 | int tcp_rcvspace;
|
---|
137 | int tcp_sndspace;
|
---|
138 | int socket_rcv;
|
---|
139 | int socket_snd;
|
---|
140 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
141 | PRTREQQUEUE pReqQueue;
|
---|
142 | #endif
|
---|
143 | #ifdef RT_OS_WINDOWS
|
---|
144 | ULONG (WINAPI * pfGetAdaptersAddresses)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
|
---|
145 | #endif
|
---|
146 | struct dns_list_head dns_list_head;
|
---|
147 | struct dns_domain_list_head dns_domain_list_head;
|
---|
148 | struct in_addr tftp_server;
|
---|
149 | struct in_addr loopback_addr;
|
---|
150 | uint32_t netmask;
|
---|
151 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
152 | uint8_t client_ethaddr[6];
|
---|
153 | #endif
|
---|
154 | const uint8_t *slirp_ethaddr;
|
---|
155 | struct ex_list *exec_list;
|
---|
156 | char slirp_hostname[33];
|
---|
157 | bool fPassDomain;
|
---|
158 | struct in_addr bindIP;
|
---|
159 | /* Stuff from tcp_input.c */
|
---|
160 | struct socket tcb;
|
---|
161 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
162 | RTCRITSECT tcb_mutex;
|
---|
163 | #endif
|
---|
164 | struct socket *tcp_last_so;
|
---|
165 | tcp_seq tcp_iss;
|
---|
166 | /* Stuff from tcp_timer.c */
|
---|
167 | struct tcpstat_t tcpstat;
|
---|
168 | uint32_t tcp_now;
|
---|
169 | int tcp_reass_qsize;
|
---|
170 | int tcp_reass_maxqlen;
|
---|
171 | int tcp_reass_maxseg;
|
---|
172 | int tcp_reass_overflows;
|
---|
173 | /* Stuff from tftp.c */
|
---|
174 | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
---|
175 | const char *tftp_prefix;
|
---|
176 | /* Stuff from udp.c */
|
---|
177 | struct udpstat_t udpstat;
|
---|
178 | struct socket udb;
|
---|
179 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
180 | RTCRITSECT udb_mutex;
|
---|
181 | #endif
|
---|
182 | struct socket *udp_last_so;
|
---|
183 | struct socket icmp_socket;
|
---|
184 | struct icmp_storage icmp_msg_head;
|
---|
185 | # ifndef RT_OS_WINDOWS
|
---|
186 | /* counter of sockets needed for allocation enough room to
|
---|
187 | * process sockets with poll/epoll
|
---|
188 | *
|
---|
189 | * NSOCK_INC/DEC should be injected before every
|
---|
190 | * operation on socket queue (tcb, udb)
|
---|
191 | */
|
---|
192 | int nsock;
|
---|
193 | # define NSOCK_INC() do {pData->nsock++;} while (0)
|
---|
194 | # define NSOCK_DEC() do {pData->nsock--;} while (0)
|
---|
195 | # define NSOCK_INC_EX(ex) do {ex->pData->nsock++;} while (0)
|
---|
196 | # define NSOCK_DEC_EX(ex) do {ex->pData->nsock--;} while (0)
|
---|
197 | # else
|
---|
198 | # define NSOCK_INC() do {} while (0)
|
---|
199 | # define NSOCK_DEC() do {} while (0)
|
---|
200 | # define NSOCK_INC_EX(ex) do {} while (0)
|
---|
201 | # define NSOCK_DEC_EX(ex) do {} while (0)
|
---|
202 | # endif
|
---|
203 | # ifdef RT_OS_WINDOWS
|
---|
204 | void *pvIcmpBuffer;
|
---|
205 | size_t szIcmpBuffer;
|
---|
206 | /* Accordin MSDN specification IcmpParseReplies
|
---|
207 | * function should be detected in runtime
|
---|
208 | */
|
---|
209 | long (WINAPI * pfIcmpParseReplies)(void *, long);
|
---|
210 | BOOL (WINAPI * pfIcmpCloseHandle)(HANDLE);
|
---|
211 | HMODULE hmIcmpLibrary;
|
---|
212 | # endif
|
---|
213 | #if defined(RT_OS_WINDOWS)
|
---|
214 | # define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
|
---|
215 | HANDLE phEvents[VBOX_EVENT_COUNT];
|
---|
216 | #endif
|
---|
217 |
|
---|
218 | /* from dnsproxy/dnsproxy.h*/
|
---|
219 | unsigned int authoritative_port;
|
---|
220 | unsigned int authoritative_timeout;
|
---|
221 | unsigned int recursive_port;
|
---|
222 | unsigned int recursive_timeout;
|
---|
223 | unsigned int stats_timeout;
|
---|
224 | unsigned int port;
|
---|
225 |
|
---|
226 | unsigned long active_queries;
|
---|
227 | unsigned long all_queries;
|
---|
228 | unsigned long authoritative_queries;
|
---|
229 | unsigned long recursive_queries;
|
---|
230 | unsigned long removed_queries;
|
---|
231 | unsigned long dropped_queries;
|
---|
232 | unsigned long answered_queries;
|
---|
233 | unsigned long dropped_answers;
|
---|
234 | unsigned long late_answers;
|
---|
235 | unsigned long hash_collisions;
|
---|
236 | /*dnsproxy/dnsproxy.c*/
|
---|
237 | unsigned short queryid;
|
---|
238 | struct sockaddr_in authoritative_addr;
|
---|
239 | struct sockaddr_in recursive_addr;
|
---|
240 | int sock_query;
|
---|
241 | int sock_answer;
|
---|
242 | /* dnsproxy/hash.c */
|
---|
243 | #define HASHSIZE 10
|
---|
244 | #define HASH(id) (id & ((1 << HASHSIZE) - 1))
|
---|
245 | struct request *request_hash[1 << HASHSIZE];
|
---|
246 | /* this field control behaviour of DHCP server */
|
---|
247 | bool use_dns_proxy;
|
---|
248 |
|
---|
249 | LIST_HEAD(RT_NOTHING, libalias) instancehead;
|
---|
250 | struct libalias *proxy_alias;
|
---|
251 | LIST_HEAD(handler_chain, proto_handler) handler_chain;
|
---|
252 | struct port_forward_rule_list port_forward_rule_head;
|
---|
253 | int port_forwarding_activated;
|
---|
254 | /*libalis modules' handlers*/
|
---|
255 | struct proto_handler *ftp_module;
|
---|
256 | struct proto_handler *nbt_module;
|
---|
257 |
|
---|
258 | #define PROFILE_COUNTER(name, dsc) STAMPROFILE Stat ## name
|
---|
259 | #define COUNTING_COUNTER(name, dsc) STAMCOUNTER Stat ## name
|
---|
260 |
|
---|
261 | #include "counters.h"
|
---|
262 |
|
---|
263 | } NATState;
|
---|
264 |
|
---|
265 |
|
---|
266 | /** Default IP time to live. */
|
---|
267 | #define ip_defttl IPDEFTTL
|
---|
268 |
|
---|
269 | /** Number of permanent buffers in mbuf. */
|
---|
270 | #define mbuf_thresh 30
|
---|
271 |
|
---|
272 | /** Use a fixed time before sending keepalive. */
|
---|
273 | #define tcp_keepidle TCPTV_KEEP_IDLE
|
---|
274 |
|
---|
275 | /** Use a fixed interval between keepalive. */
|
---|
276 | #define tcp_keepintvl TCPTV_KEEPINTVL
|
---|
277 |
|
---|
278 | /** Maximum idle time before timing out a connection. */
|
---|
279 | #define tcp_maxidle (TCPTV_KEEPCNT * tcp_keepintvl)
|
---|
280 |
|
---|
281 | /** Default TCP socket options. */
|
---|
282 | #define so_options DO_KEEPALIVE
|
---|
283 |
|
---|
284 | /** Default TCP MSS value. */
|
---|
285 | #define tcp_mssdflt TCP_MSS
|
---|
286 |
|
---|
287 | /** Default TCP round trip time. */
|
---|
288 | #define tcp_rttdflt (TCPTV_SRTTDFLT / PR_SLOWHZ)
|
---|
289 |
|
---|
290 | /** Enable RFC1323 performance enhancements.
|
---|
291 | * @todo check if it really works, it was turned off before. */
|
---|
292 | #define tcp_do_rfc1323 1
|
---|
293 |
|
---|
294 | /** TCP receive buffer size. */
|
---|
295 | #define tcp_rcvspace pData->tcp_rcvspace
|
---|
296 |
|
---|
297 | /** TCP receive buffer size. */
|
---|
298 | #define tcp_sndspace pData->tcp_sndspace
|
---|
299 |
|
---|
300 | /* TCP duplicate ACK retransmit threshold. */
|
---|
301 | #define tcprexmtthresh 3
|
---|
302 |
|
---|
303 |
|
---|
304 | #define bootp_filename pData->bootp_filename
|
---|
305 | #define bootp_clients pData->bootp_clients
|
---|
306 |
|
---|
307 | #define if_mtu pData->if_mtu
|
---|
308 | #define if_mru pData->if_mru
|
---|
309 | #define if_comp pData->if_comp
|
---|
310 | #define if_maxlinkhdr pData->if_maxlinkhdr
|
---|
311 | #define if_queued pData->if_queued
|
---|
312 | #define if_thresh pData->if_thresh
|
---|
313 | #define if_fastq pData->if_fastq
|
---|
314 | #define if_batchq pData->if_batchq
|
---|
315 | #define next_m pData->next_m
|
---|
316 |
|
---|
317 | #define icmpstat pData->icmpstat
|
---|
318 |
|
---|
319 | #define ipstat pData->ipstat
|
---|
320 | #define ipq pData->ipq
|
---|
321 | #define ip_currid pData->ip_currid
|
---|
322 |
|
---|
323 | #define mbuf_alloced pData->mbuf_alloced
|
---|
324 | #define mbuf_max pData->mbuf_max
|
---|
325 | #define msize pData->msize
|
---|
326 | #define m_freelist pData->m_freelist
|
---|
327 | #define m_usedlist pData->m_usedlist
|
---|
328 |
|
---|
329 | #define curtime pData->curtime
|
---|
330 | #define time_fasttimo pData->time_fasttimo
|
---|
331 | #define last_slowtimo pData->last_slowtimo
|
---|
332 | #define do_slowtimo pData->do_slowtimo
|
---|
333 | #define link_up pData->link_up
|
---|
334 | #define cUsers pData->cUsers
|
---|
335 | #define tt pData->tt
|
---|
336 | #define our_addr pData->our_addr
|
---|
337 | #ifndef VBOX_SLIRP_ALIAS
|
---|
338 | # define alias_addr pData->alias_addr
|
---|
339 | #else
|
---|
340 | # define handler_chain pData->handler_chain
|
---|
341 | #endif
|
---|
342 | #define special_addr pData->special_addr
|
---|
343 | #define dns_addr pData->dns_addr
|
---|
344 | #define loopback_addr pData->loopback_addr
|
---|
345 | #define client_ethaddr pData->client_ethaddr
|
---|
346 | #define exec_list pData->exec_list
|
---|
347 | #define slirp_hostname pData->slirp_hostname
|
---|
348 |
|
---|
349 | #define tcb pData->tcb
|
---|
350 | #define tcp_last_so pData->tcp_last_so
|
---|
351 | #define tcp_iss pData->tcp_iss
|
---|
352 |
|
---|
353 | #define tcpstat pData->tcpstat
|
---|
354 | #define tcp_now pData->tcp_now
|
---|
355 |
|
---|
356 | #define tftp_sessions pData->tftp_sessions
|
---|
357 | #define tftp_prefix pData->tftp_prefix
|
---|
358 |
|
---|
359 | #define udpstat pData->udpstat
|
---|
360 | #define udb pData->udb
|
---|
361 | #define udp_last_so pData->udp_last_so
|
---|
362 |
|
---|
363 | #define maxfragsperpacket pData->maxfragsperpacket
|
---|
364 | #define maxnipq pData->maxnipq
|
---|
365 | #define nipq pData->nipq
|
---|
366 |
|
---|
367 | #define tcp_reass_qsize pData->tcp_reass_qsize
|
---|
368 | #define tcp_reass_maxqlen pData->tcp_reass_maxqlen
|
---|
369 | #define tcp_reass_maxseg pData->tcp_reass_maxseg
|
---|
370 | #define tcp_reass_overflows pData->tcp_reass_overflows
|
---|
371 |
|
---|
372 | #define queue_tcp_label tcb
|
---|
373 | #define queue_udp_label udb
|
---|
374 | #define VBOX_X2(x) x
|
---|
375 | #define VBOX_X(x) VBOX_X2(x)
|
---|
376 |
|
---|
377 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
378 |
|
---|
379 | # define QSOCKET_LOCK(queue) \
|
---|
380 | do { \
|
---|
381 | int rc; \
|
---|
382 | /* Assert(strcmp(RTThreadSelfName(), "EMT") != 0); */ \
|
---|
383 | rc = RTCritSectEnter(&VBOX_X(queue) ## _mutex); \
|
---|
384 | AssertReleaseRC(rc); \
|
---|
385 | } while (0)
|
---|
386 | # define QSOCKET_UNLOCK(queue) \
|
---|
387 | do { \
|
---|
388 | int rc; \
|
---|
389 | rc = RTCritSectLeave(&VBOX_X(queue) ## _mutex); \
|
---|
390 | AssertReleaseRC(rc); \
|
---|
391 | } while (0)
|
---|
392 | # define QSOCKET_LOCK_CREATE(queue) \
|
---|
393 | do { \
|
---|
394 | int rc; \
|
---|
395 | rc = RTCritSectInit(&pData->queue ## _mutex); \
|
---|
396 | AssertReleaseRC(rc); \
|
---|
397 | } while (0)
|
---|
398 | # define QSOCKET_LOCK_DESTROY(queue) \
|
---|
399 | do { \
|
---|
400 | int rc = RTCritSectDelete(&pData->queue ## _mutex); \
|
---|
401 | AssertReleaseRC(rc); \
|
---|
402 | } while (0)
|
---|
403 |
|
---|
404 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
405 | QSOCKET_LOCK(VBOX_X2(queue_## label ## _label)); \
|
---|
406 | (so) = (VBOX_X(queue_ ## label ## _label)).so_next; \
|
---|
407 | QSOCKET_UNLOCK(VBOX_X2(queue_## label ##_label)); \
|
---|
408 | if ((so) != &(VBOX_X(queue_## label ## _label))) SOCKET_LOCK((so));\
|
---|
409 | for (;;) \
|
---|
410 | { \
|
---|
411 | if ((so) == &(VBOX_X(queue_## label ## _label))) \
|
---|
412 | { \
|
---|
413 | break; \
|
---|
414 | } \
|
---|
415 | Log2(("%s:%d Processing so:%R[natsock]\n", __FUNCTION__, __LINE__, (so)));
|
---|
416 |
|
---|
417 | # define CONTINUE_NO_UNLOCK(label) goto loop_end_ ## label ## _mt_nounlock
|
---|
418 | # define CONTINUE(label) goto loop_end_ ## label ## _mt
|
---|
419 | /* @todo replace queue parameter with macrodinition */
|
---|
420 | /* _mt_nounlock - user should lock so_next before calling CONTINUE_NO_UNLOCK */
|
---|
421 | # define LOOP_LABEL(label, so, sonext) loop_end_ ## label ## _mt: \
|
---|
422 | (sonext) = (so)->so_next; \
|
---|
423 | SOCKET_UNLOCK(so); \
|
---|
424 | QSOCKET_LOCK(VBOX_X(queue_ ## label ## _label)); \
|
---|
425 | if ((sonext) != &(VBOX_X(queue_## label ## _label))) \
|
---|
426 | { \
|
---|
427 | SOCKET_LOCK((sonext)); \
|
---|
428 | QSOCKET_UNLOCK(VBOX_X(queue_ ## label ## _label)); \
|
---|
429 | } \
|
---|
430 | else \
|
---|
431 | { \
|
---|
432 | so = &VBOX_X(queue_ ## label ## _label); \
|
---|
433 | QSOCKET_UNLOCK(VBOX_X(queue_ ## label ## _label)); \
|
---|
434 | break; \
|
---|
435 | } \
|
---|
436 | (so) = (sonext); \
|
---|
437 | continue; \
|
---|
438 | loop_end_ ## label ## _mt_nounlock: \
|
---|
439 | (so) = (sonext)
|
---|
440 |
|
---|
441 | # define DO_TCP_OUTPUT(data, sotcb) \
|
---|
442 | do { \
|
---|
443 | PRTREQ pReq = NULL; \
|
---|
444 | int rc; \
|
---|
445 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
446 | AssertReleaseRC(rc); \
|
---|
447 | pReq->u.Internal.pfn = (PFNRT)tcp_output; \
|
---|
448 | pReq->u.Internal.cArgs = 2; \
|
---|
449 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
450 | pReq->u.Internal.aArgs[1] = (uintptr_t)(sotcb); \
|
---|
451 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
452 | rc = RTReqQueue(pReq, 0); \
|
---|
453 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
454 | { \
|
---|
455 | SOCKET_UNLOCK(so); \
|
---|
456 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
457 | AssertReleaseRC(rc); \
|
---|
458 | SOCKET_LOCK(so); \
|
---|
459 | RTReqFree(pReq); \
|
---|
460 | } \
|
---|
461 | else \
|
---|
462 | AssertReleaseRC(rc); \
|
---|
463 | } while(0)
|
---|
464 |
|
---|
465 | # define DO_TCP_INPUT(data, mbuf, size, so) \
|
---|
466 | do { \
|
---|
467 | PRTREQ pReq = NULL; \
|
---|
468 | int rc; \
|
---|
469 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
470 | AssertReleaseRC(rc); \
|
---|
471 | pReq->u.Internal.pfn = (PFNRT)tcp_input; \
|
---|
472 | pReq->u.Internal.cArgs = 4; \
|
---|
473 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
474 | pReq->u.Internal.aArgs[1] = (uintptr_t)(mbuf); \
|
---|
475 | pReq->u.Internal.aArgs[2] = (uintptr_t)(size); \
|
---|
476 | pReq->u.Internal.aArgs[3] = (uintptr_t)(so); \
|
---|
477 | pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT; \
|
---|
478 | rc = RTReqQueue(pReq, 0); \
|
---|
479 | AssertReleaseRC(rc); \
|
---|
480 | } while(0)
|
---|
481 |
|
---|
482 | # define DO_TCP_CONNECT(data, so) \
|
---|
483 | do { \
|
---|
484 | PRTREQ pReq = NULL; \
|
---|
485 | int rc; \
|
---|
486 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
487 | AssertReleaseRC(rc); \
|
---|
488 | pReq->u.Internal.pfn = (PFNRT)tcp_connect; \
|
---|
489 | pReq->u.Internal.cArgs = 2; \
|
---|
490 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
491 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
492 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
493 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
494 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
495 | { \
|
---|
496 | SOCKET_UNLOCK(so); \
|
---|
497 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
498 | AssertReleaseRC(rc); \
|
---|
499 | SOCKET_LOCK(so); \
|
---|
500 | RTReqFree(pReq); \
|
---|
501 | } \
|
---|
502 | else \
|
---|
503 | AssertReleaseRC(rc); \
|
---|
504 | } while(0)
|
---|
505 |
|
---|
506 | # define DO_SOREAD(ret, data, so, ifclose) \
|
---|
507 | do { \
|
---|
508 | PRTREQ pReq = NULL; \
|
---|
509 | int rc; \
|
---|
510 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
511 | AssertReleaseRC(rc); \
|
---|
512 | pReq->u.Internal.pfn = (PFNRT)soread_queue; \
|
---|
513 | pReq->u.Internal.cArgs = 4; \
|
---|
514 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
515 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
516 | pReq->u.Internal.aArgs[2] = (uintptr_t)(ifclose); \
|
---|
517 | pReq->u.Internal.aArgs[3] = (uintptr_t)&(ret); \
|
---|
518 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
519 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
520 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
521 | { \
|
---|
522 | SOCKET_UNLOCK(so); \
|
---|
523 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
524 | AssertReleaseRC(rc); \
|
---|
525 | SOCKET_LOCK(so); \
|
---|
526 | RTReqFree(pReq); \
|
---|
527 | } \
|
---|
528 | else \
|
---|
529 | AssertReleaseRC(rc); \
|
---|
530 | } while(0)
|
---|
531 |
|
---|
532 | # define DO_SOWRITE(ret, data, so) \
|
---|
533 | do { \
|
---|
534 | PRTREQ pReq = NULL; \
|
---|
535 | int rc; \
|
---|
536 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
537 | AssertReleaseRC(rc); \
|
---|
538 | pReq->u.Internal.pfn = (PFNRT)sowrite; \
|
---|
539 | pReq->u.Internal.cArgs = 2; \
|
---|
540 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
541 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
542 | pReq->fFlags = RTREQFLAGS_RETURN_MASK; \
|
---|
543 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
544 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
545 | { \
|
---|
546 | SOCKET_UNLOCK(so); \
|
---|
547 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
548 | SOCKET_LOCK(so); \
|
---|
549 | ret = pReq->iStatus; \
|
---|
550 | RTReqFree(pReq); \
|
---|
551 | } \
|
---|
552 | else \
|
---|
553 | AssertReleaseRC(rc); \
|
---|
554 | } while(0)
|
---|
555 |
|
---|
556 | # define DO_SORECFROM(data, so) \
|
---|
557 | do { \
|
---|
558 | PRTREQ pReq = NULL; \
|
---|
559 | int rc; \
|
---|
560 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
561 | AssertReleaseRC(rc); \
|
---|
562 | pReq->u.Internal.pfn = (PFNRT)sorecvfrom; \
|
---|
563 | pReq->u.Internal.cArgs = 2; \
|
---|
564 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
565 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
566 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
567 | rc = RTReqQueue(pReq, 0); \
|
---|
568 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
569 | { \
|
---|
570 | SOCKET_UNLOCK(so); \
|
---|
571 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
572 | AssertReleaseRC(rc); \
|
---|
573 | SOCKET_LOCK(so); \
|
---|
574 | RTReqFree(pReq); \
|
---|
575 | } \
|
---|
576 | else \
|
---|
577 | AssertReleaseRC(rc); \
|
---|
578 | } while(0)
|
---|
579 |
|
---|
580 | # define DO_UDP_DETACH(data, so, so_next) \
|
---|
581 | do { \
|
---|
582 | PRTREQ pReq = NULL; \
|
---|
583 | int rc; \
|
---|
584 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
585 | AssertReleaseRC(rc); \
|
---|
586 | pReq->u.Internal.pfn = (PFNRT)udp_detach; \
|
---|
587 | pReq->u.Internal.cArgs = 2; \
|
---|
588 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
589 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
590 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
591 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
592 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
593 | { \
|
---|
594 | SOCKET_UNLOCK(so); \
|
---|
595 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
596 | AssertReleaseRC(rc); \
|
---|
597 | if ((so_next) != &udb) SOCKET_LOCK((so_next)); \
|
---|
598 | RTReqFree(pReq); \
|
---|
599 | } \
|
---|
600 | else \
|
---|
601 | AssertReleaseRC(rc); \
|
---|
602 | } while(0)
|
---|
603 |
|
---|
604 | # define SOLOOKUP(so, label, src, sport, dst, dport) \
|
---|
605 | do { \
|
---|
606 | struct socket *sonxt; \
|
---|
607 | (so) = NULL; \
|
---|
608 | QSOCKET_FOREACH(so, sonxt, label) \
|
---|
609 | /* { */ \
|
---|
610 | if ( so->so_lport == (sport) \
|
---|
611 | && so->so_laddr.s_addr == (src).s_addr \
|
---|
612 | && so->so_faddr.s_addr == (dst).s_addr \
|
---|
613 | && so->so_fport == (dport)) \
|
---|
614 | { \
|
---|
615 | if (sonxt != &VBOX_X2(queue_ ## label ## _label)) \
|
---|
616 | SOCKET_UNLOCK(sonxt); \
|
---|
617 | break; /*so is locked*/ \
|
---|
618 | } \
|
---|
619 | LOOP_LABEL(so, sonxt, label); \
|
---|
620 | } \
|
---|
621 | } \
|
---|
622 | } while (0)
|
---|
623 |
|
---|
624 | #else /* !VBOX_WITH_SLIRP_MT */
|
---|
625 |
|
---|
626 | # define QSOCKET_LOCK(queue) do {} while (0)
|
---|
627 | # define QSOCKET_UNLOCK(queue) do {} while (0)
|
---|
628 | # define QSOCKET_LOCK_CREATE(queue) do {} while (0)
|
---|
629 | # define QSOCKET_LOCK_DESTROY(queue) do {} while (0)
|
---|
630 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
631 | for ((so) = VBOX_X2(queue_ ## label ## _label).so_next; \
|
---|
632 | (so) != &(VBOX_X2(queue_ ## label ## _label)); \
|
---|
633 | (so) = (sonext)) \
|
---|
634 | { \
|
---|
635 | (sonext) = (so)->so_next;
|
---|
636 | # define CONTINUE(label) continue
|
---|
637 | # define CONTINUE_NO_UNLOCK(label) continue
|
---|
638 | # define LOOP_LABEL(label, so, sonext) /* empty*/
|
---|
639 | # define DO_TCP_OUTPUT(data, sotcb) tcp_output((data), (sotcb))
|
---|
640 | # define DO_TCP_INPUT(data, mbuf, size, so) tcp_input((data), (mbuf), (size), (so))
|
---|
641 | # define DO_TCP_CONNECT(data, so) tcp_connect((data), (so))
|
---|
642 | # define DO_SOREAD(ret, data, so, ifclose) \
|
---|
643 | do { \
|
---|
644 | (ret) = soread((data), (so), (ifclose)); \
|
---|
645 | } while(0)
|
---|
646 | # define DO_SOWRITE(ret, data, so) \
|
---|
647 | do { \
|
---|
648 | (ret) = sowrite((data), (so)); \
|
---|
649 | } while(0)
|
---|
650 | # define DO_SORECFROM(data, so) sorecvfrom((data), (so))
|
---|
651 | # define SOLOOKUP(so, label, src, sport, dst, dport) \
|
---|
652 | do { \
|
---|
653 | (so) = solookup(&VBOX_X2(queue_ ## label ## _label), (src), (sport), (dst), (dport)); \
|
---|
654 | } while (0)
|
---|
655 | # define DO_UDP_DETACH(data, so, ignored) udp_detach((data), (so))
|
---|
656 |
|
---|
657 | #endif /* !VBOX_WITH_SLIRP_MT */
|
---|
658 |
|
---|
659 | #define TCP_OUTPUT(data, sotcb) DO_TCP_OUTPUT((data), (sotcb))
|
---|
660 | #define TCP_INPUT(data, mbuf, size, so) DO_TCP_INPUT((data), (mbuf), (size), (so))
|
---|
661 | #define TCP_CONNECT(data, so) DO_TCP_CONNECT((data), (so))
|
---|
662 | #define SOREAD(ret, data, so, ifclose) DO_SOREAD((ret), (data), (so), (ifclose))
|
---|
663 | #define SOWRITE(ret, data, so) DO_SOWRITE((ret), (data), (so))
|
---|
664 | #define SORECVFROM(data, so) DO_SORECFROM((data), (so))
|
---|
665 | #define UDP_DETACH(data, so, so_next) DO_UDP_DETACH((data), (so), (so_next))
|
---|
666 |
|
---|
667 | /* dnsproxy/dnsproxy.c */
|
---|
668 | #define authoritative_port pData->authoritative_port
|
---|
669 | #define authoritative_timeout pData->authoritative_timeout
|
---|
670 | #define recursive_port pData->recursive_port
|
---|
671 | #define recursive_timeout pData->recursive_timeout
|
---|
672 | #define stats_timeout pData->stats_timeout
|
---|
673 | /* dnsproxy/hash.c */
|
---|
674 | #define dns_port pData->port
|
---|
675 | #define request_hash pData->request_hash
|
---|
676 | #define hash_collisions pData->hash_collisions
|
---|
677 | #define active_queries pData->active_queries
|
---|
678 | #define all_queries pData->all_queries
|
---|
679 | #define authoritative_queries pData->authoritative_queries
|
---|
680 | #define recursive_queries pData->recursive_queries
|
---|
681 | #define removed_queries pData->removed_queries
|
---|
682 | #define dropped_queries pData->dropped_queries
|
---|
683 | #define answered_queries pData->answered_queries
|
---|
684 | #define dropped_answers pData->dropped_answers
|
---|
685 | #define late_answers pData->late_answers
|
---|
686 |
|
---|
687 | /* dnsproxy/dnsproxy.c */
|
---|
688 | #define queryid pData->queryid
|
---|
689 | #define authoritative_addr pData->authoritative_addr
|
---|
690 | #define recursive_addr pData->recursive_addr
|
---|
691 | #define sock_query pData->sock_query
|
---|
692 | #define sock_answer pData->sock_answer
|
---|
693 |
|
---|
694 | #define instancehead pData->instancehead
|
---|
695 |
|
---|
696 | #endif /* !___slirp_state_h */
|
---|