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 | #include <iprt/req.h>
|
---|
24 | #include "ip_icmp.h"
|
---|
25 |
|
---|
26 | /** Number of DHCP clients supported by NAT. */
|
---|
27 | #define NB_ADDR 16
|
---|
28 |
|
---|
29 | /** Where to start DHCP IP number allocation. */
|
---|
30 | #define START_ADDR 15
|
---|
31 |
|
---|
32 | /** DHCP Lease time. */
|
---|
33 | #define LEASE_TIME (24 * 3600)
|
---|
34 |
|
---|
35 | /** Entry in the table of known DHCP clients. */
|
---|
36 | typedef struct
|
---|
37 | {
|
---|
38 | bool allocated;
|
---|
39 | uint8_t macaddr[6];
|
---|
40 | } BOOTPClient;
|
---|
41 |
|
---|
42 |
|
---|
43 | /** TFTP session entry. */
|
---|
44 | struct tftp_session
|
---|
45 | {
|
---|
46 | int in_use;
|
---|
47 | unsigned char filename[TFTP_FILENAME_MAX];
|
---|
48 |
|
---|
49 | struct in_addr client_ip;
|
---|
50 | u_int16_t client_port;
|
---|
51 |
|
---|
52 | int timestamp;
|
---|
53 | };
|
---|
54 |
|
---|
55 | #ifdef VBOX_WITH_MULTI_DNS
|
---|
56 | struct dns_entry
|
---|
57 | {
|
---|
58 | struct in_addr de_addr;
|
---|
59 | LIST_ENTRY(dns_entry) de_list;
|
---|
60 | };
|
---|
61 | LIST_HEAD(dns_list_head, dns_entry);
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | /** Main state/configuration structure for slirp NAT. */
|
---|
65 | typedef struct NATState
|
---|
66 | {
|
---|
67 | /* Stuff from boot.c */
|
---|
68 | BOOTPClient bootp_clients[NB_ADDR];
|
---|
69 | const char *bootp_filename;
|
---|
70 | /* Stuff from if.c */
|
---|
71 | int if_mtu, if_mru;
|
---|
72 | int if_comp;
|
---|
73 | int if_maxlinkhdr;
|
---|
74 | int if_queued;
|
---|
75 | int if_thresh;
|
---|
76 | struct mbuf if_fastq;
|
---|
77 | struct mbuf if_batchq;
|
---|
78 | struct mbuf *next_m;
|
---|
79 | /* Stuff from icmp.c */
|
---|
80 | struct icmpstat_t icmpstat;
|
---|
81 | /* Stuff from ip_input.c */
|
---|
82 | struct ipstat_t ipstat;
|
---|
83 | struct ipqhead ipq[IPREASS_NHASH];
|
---|
84 | int maxnipq; /* Administrative limit on # of reass queues*/
|
---|
85 | int maxfragsperpacket; /* Maximum number of IPv4 fragments allowed per packet */
|
---|
86 | int nipq; /* total number of reass queues */
|
---|
87 | uint16_t ip_currid;
|
---|
88 | /* Stuff from mbuf.c */
|
---|
89 | int mbuf_alloced, mbuf_max;
|
---|
90 | int msize;
|
---|
91 | struct mbuf m_freelist, m_usedlist;
|
---|
92 | /* Stuff from slirp.c */
|
---|
93 | void *pvUser;
|
---|
94 | uint32_t curtime;
|
---|
95 | uint32_t time_fasttimo;
|
---|
96 | uint32_t last_slowtimo;
|
---|
97 | bool do_slowtimo;
|
---|
98 | bool link_up;
|
---|
99 | struct timeval tt;
|
---|
100 | struct in_addr our_addr;
|
---|
101 | struct in_addr alias_addr;
|
---|
102 | struct in_addr special_addr;
|
---|
103 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
104 | PRTREQQUEUE pReqQueue;
|
---|
105 | #endif
|
---|
106 | #ifndef VBOX_WITH_MULTI_DNS
|
---|
107 | struct in_addr dns_addr;
|
---|
108 | #else
|
---|
109 | struct dns_list_head dns_list_head;
|
---|
110 | #endif
|
---|
111 | struct in_addr tftp_server;
|
---|
112 | struct in_addr loopback_addr;
|
---|
113 | uint32_t netmask;
|
---|
114 | uint8_t client_ethaddr[6];
|
---|
115 | struct ex_list *exec_list;
|
---|
116 | char slirp_hostname[33];
|
---|
117 | bool fPassDomain;
|
---|
118 | const char *pszDomain;
|
---|
119 | /* Stuff from tcp_input.c */
|
---|
120 | struct socket tcb;
|
---|
121 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
122 | RTCRITSECT tcb_mutex;
|
---|
123 | #endif
|
---|
124 | struct socket *tcp_last_so;
|
---|
125 | tcp_seq tcp_iss;
|
---|
126 | /* Stuff from tcp_timer.c */
|
---|
127 | struct tcpstat_t tcpstat;
|
---|
128 | uint32_t tcp_now;
|
---|
129 | int tcp_reass_qsize;
|
---|
130 | int tcp_reass_maxqlen;
|
---|
131 | int tcp_reass_maxseg;
|
---|
132 | int tcp_reass_overflows;
|
---|
133 | /* Stuff from tftp.c */
|
---|
134 | struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
|
---|
135 | const char *tftp_prefix;
|
---|
136 | /* Stuff from udp.c */
|
---|
137 | struct udpstat_t udpstat;
|
---|
138 | struct socket udb;
|
---|
139 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
140 | RTCRITSECT udb_mutex;
|
---|
141 | #endif
|
---|
142 | struct socket *udp_last_so;
|
---|
143 | struct socket icmp_socket;
|
---|
144 | struct icmp_storage icmp_msg_head;
|
---|
145 | # ifndef RT_OS_WINDOWS
|
---|
146 | /* counter of sockets needed for allocation enough room to
|
---|
147 | * process sockets with poll/epoll
|
---|
148 | *
|
---|
149 | * NSOCK_INC/DEC should be injected before every
|
---|
150 | * operation on socket queue (tcb, udb)
|
---|
151 | */
|
---|
152 | int nsock;
|
---|
153 | # define NSOCK_INC() do {pData->nsock++;} while (0)
|
---|
154 | # define NSOCK_DEC() do {pData->nsock--;} while (0)
|
---|
155 | # else
|
---|
156 | # define NSOCK_INC() do {} while (0)
|
---|
157 | # define NSOCK_DEC() do {} while (0)
|
---|
158 | # endif
|
---|
159 | # ifdef RT_OS_WINDOWS
|
---|
160 | void *pvIcmpBuffer;
|
---|
161 | size_t szIcmpBuffer;
|
---|
162 | /* Accordin MSDN specification IcmpParseReplies
|
---|
163 | * function should be detected in runtime
|
---|
164 | */
|
---|
165 | long (WINAPI * pfIcmpParseReplies)(void *, long);
|
---|
166 | BOOL (WINAPI * pfIcmpCloseHandle)(HANDLE);
|
---|
167 | HMODULE hmIcmpLibrary;
|
---|
168 | # endif
|
---|
169 | #if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
170 | # define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
|
---|
171 | HANDLE phEvents[VBOX_EVENT_COUNT];
|
---|
172 | #endif
|
---|
173 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
174 | int fIcmp;
|
---|
175 | #endif
|
---|
176 | STAMPROFILE StatFill;
|
---|
177 | STAMPROFILE StatPoll;
|
---|
178 | STAMPROFILE StatFastTimer;
|
---|
179 | STAMPROFILE StatSlowTimer;
|
---|
180 | STAMCOUNTER StatTCP;
|
---|
181 | STAMCOUNTER StatUDP;
|
---|
182 | STAMCOUNTER StatTCPHot;
|
---|
183 | STAMCOUNTER StatUDPHot;
|
---|
184 | } NATState;
|
---|
185 |
|
---|
186 |
|
---|
187 | /** Default IP time to live. */
|
---|
188 | #define ip_defttl IPDEFTTL
|
---|
189 |
|
---|
190 | /** Number of permanent buffers in mbuf. */
|
---|
191 | #define mbuf_thresh 30
|
---|
192 |
|
---|
193 | /** Use a fixed time before sending keepalive. */
|
---|
194 | #define tcp_keepidle TCPTV_KEEP_IDLE
|
---|
195 |
|
---|
196 | /** Use a fixed interval between keepalive. */
|
---|
197 | #define tcp_keepintvl TCPTV_KEEPINTVL
|
---|
198 |
|
---|
199 | /** Maximum idle time before timing out a connection. */
|
---|
200 | #define tcp_maxidle (TCPTV_KEEPCNT * tcp_keepintvl)
|
---|
201 |
|
---|
202 | /** Default TCP socket options. */
|
---|
203 | #define so_options DO_KEEPALIVE
|
---|
204 |
|
---|
205 | /** Default TCP MSS value. */
|
---|
206 | #define tcp_mssdflt TCP_MSS
|
---|
207 |
|
---|
208 | /** Default TCP round trip time. */
|
---|
209 | #define tcp_rttdflt (TCPTV_SRTTDFLT / PR_SLOWHZ)
|
---|
210 |
|
---|
211 | /** Enable RFC1323 performance enhancements.
|
---|
212 | * @todo check if it really works, it was turned off before. */
|
---|
213 | #define tcp_do_rfc1323 1
|
---|
214 |
|
---|
215 | /** TCP receive buffer size. */
|
---|
216 | #define tcp_rcvspace TCP_RCVSPACE
|
---|
217 |
|
---|
218 | /** TCP receive buffer size. */
|
---|
219 | #define tcp_sndspace TCP_SNDSPACE
|
---|
220 |
|
---|
221 | /* TCP duplicate ACK retransmit threshold. */
|
---|
222 | #define tcprexmtthresh 3
|
---|
223 |
|
---|
224 |
|
---|
225 | #define bootp_filename pData->bootp_filename
|
---|
226 | #define bootp_clients pData->bootp_clients
|
---|
227 |
|
---|
228 | #define if_mtu pData->if_mtu
|
---|
229 | #define if_mru pData->if_mru
|
---|
230 | #define if_comp pData->if_comp
|
---|
231 | #define if_maxlinkhdr pData->if_maxlinkhdr
|
---|
232 | #define if_queued pData->if_queued
|
---|
233 | #define if_thresh pData->if_thresh
|
---|
234 | #define if_fastq pData->if_fastq
|
---|
235 | #define if_batchq pData->if_batchq
|
---|
236 | #define next_m pData->next_m
|
---|
237 |
|
---|
238 | #define icmpstat pData->icmpstat
|
---|
239 |
|
---|
240 | #define ipstat pData->ipstat
|
---|
241 | #define ipq pData->ipq
|
---|
242 | #define ip_currid pData->ip_currid
|
---|
243 |
|
---|
244 | #define mbuf_alloced pData->mbuf_alloced
|
---|
245 | #define mbuf_max pData->mbuf_max
|
---|
246 | #define msize pData->msize
|
---|
247 | #define m_freelist pData->m_freelist
|
---|
248 | #define m_usedlist pData->m_usedlist
|
---|
249 |
|
---|
250 | #define curtime pData->curtime
|
---|
251 | #define time_fasttimo pData->time_fasttimo
|
---|
252 | #define last_slowtimo pData->last_slowtimo
|
---|
253 | #define do_slowtimo pData->do_slowtimo
|
---|
254 | #define link_up pData->link_up
|
---|
255 | #define cUsers pData->cUsers
|
---|
256 | #define tt pData->tt
|
---|
257 | #define our_addr pData->our_addr
|
---|
258 | #define alias_addr pData->alias_addr
|
---|
259 | #define special_addr pData->special_addr
|
---|
260 | #define dns_addr pData->dns_addr
|
---|
261 | #define loopback_addr pData->loopback_addr
|
---|
262 | #define client_ethaddr pData->client_ethaddr
|
---|
263 | #define exec_list pData->exec_list
|
---|
264 | #define slirp_hostname pData->slirp_hostname
|
---|
265 |
|
---|
266 | #define tcb pData->tcb
|
---|
267 | #define tcp_last_so pData->tcp_last_so
|
---|
268 | #define tcp_iss pData->tcp_iss
|
---|
269 |
|
---|
270 | #define tcpstat pData->tcpstat
|
---|
271 | #define tcp_now pData->tcp_now
|
---|
272 |
|
---|
273 | #define tftp_sessions pData->tftp_sessions
|
---|
274 | #define tftp_prefix pData->tftp_prefix
|
---|
275 |
|
---|
276 | #define udpstat pData->udpstat
|
---|
277 | #define udb pData->udb
|
---|
278 | #define udp_last_so pData->udp_last_so
|
---|
279 |
|
---|
280 | #define maxfragsperpacket pData->maxfragsperpacket
|
---|
281 | #define maxnipq pData->maxnipq
|
---|
282 | #define nipq pData->nipq
|
---|
283 |
|
---|
284 | #define tcp_reass_qsize pData->tcp_reass_qsize
|
---|
285 | #define tcp_reass_maxqlen pData->tcp_reass_maxqlen
|
---|
286 | #define tcp_reass_maxseg pData->tcp_reass_maxseg
|
---|
287 | #define tcp_reass_overflows pData->tcp_reass_overflows
|
---|
288 |
|
---|
289 | #if !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
290 | # define fIcmp pData->fIcmp
|
---|
291 | #endif
|
---|
292 |
|
---|
293 | #define queue_tcp_label tcb
|
---|
294 | #define queue_udp_label udb
|
---|
295 | #define __X(x) x
|
---|
296 | #define _X(x) __X(x)
|
---|
297 | #define _str(x) #x
|
---|
298 | #define str(x) _str(x)
|
---|
299 |
|
---|
300 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
301 |
|
---|
302 | # define QSOCKET_LOCK(queue) \
|
---|
303 | do { \
|
---|
304 | int rc; \
|
---|
305 | /* Assert(strcmp(RTThreadSelfName(), "EMT") != 0); */ \
|
---|
306 | rc = RTCritSectEnter(&_X(queue) ## _mutex); \
|
---|
307 | AssertReleaseRC(rc); \
|
---|
308 | } while (0)
|
---|
309 | # define QSOCKET_UNLOCK(queue) \
|
---|
310 | do { \
|
---|
311 | int rc; \
|
---|
312 | rc = RTCritSectLeave(&_X(queue) ## _mutex); \
|
---|
313 | AssertReleaseRC(rc); \
|
---|
314 | } while (0)
|
---|
315 | # define QSOCKET_LOCK_CREATE(queue) \
|
---|
316 | do { \
|
---|
317 | int rc; \
|
---|
318 | rc = RTCritSectInit(&pData->queue ## _mutex); \
|
---|
319 | AssertReleaseRC(rc); \
|
---|
320 | } while (0)
|
---|
321 | # define QSOCKET_LOCK_DESTROY(queue) \
|
---|
322 | do { \
|
---|
323 | int rc = RTCritSectDelete(&pData->queue ## _mutex); \
|
---|
324 | AssertReleaseRC(rc); \
|
---|
325 | } while (0)
|
---|
326 |
|
---|
327 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
328 | QSOCKET_LOCK(__X(queue_## label ## _label)); \
|
---|
329 | (so) = (_X(queue_ ## label ## _label)).so_next; \
|
---|
330 | QSOCKET_UNLOCK(__X(queue_## label ##_label)); \
|
---|
331 | if ((so) != &(_X(queue_## label ## _label))) SOCKET_LOCK((so)); \
|
---|
332 | for(;;) \
|
---|
333 | { \
|
---|
334 | if ((so) == &(_X(queue_## label ## _label))) \
|
---|
335 | { \
|
---|
336 | break; \
|
---|
337 | } \
|
---|
338 | Log2(("%s:%d Processing so:%R[natsock]\n", __FUNCTION__, __LINE__, (so)));
|
---|
339 |
|
---|
340 | # define CONTINUE_NO_UNLOCK(label) goto loop_end_ ## label ## _mt_nounlock
|
---|
341 | # define CONTINUE(label) goto loop_end_ ## label ## _mt
|
---|
342 | /* @todo replace queue parameter with macrodinition */
|
---|
343 | /* _mt_nounlock - user should lock so_next before calling CONTINUE_NO_UNLOCK */
|
---|
344 | # define LOOP_LABEL(label, so, sonext) loop_end_ ## label ## _mt: \
|
---|
345 | (sonext) = (so)->so_next; \
|
---|
346 | SOCKET_UNLOCK(so); \
|
---|
347 | QSOCKET_LOCK(_X(queue_ ## label ## _label)); \
|
---|
348 | if ((sonext) != &(_X(queue_## label ## _label))) \
|
---|
349 | { \
|
---|
350 | SOCKET_LOCK((sonext)); \
|
---|
351 | QSOCKET_UNLOCK(_X(queue_ ## label ## _label)); \
|
---|
352 | } \
|
---|
353 | else \
|
---|
354 | { \
|
---|
355 | so = &_X(queue_ ## label ## _label); \
|
---|
356 | QSOCKET_UNLOCK(_X(queue_ ## label ## _label)); \
|
---|
357 | break; \
|
---|
358 | } \
|
---|
359 | (so) = (sonext); \
|
---|
360 | continue; \
|
---|
361 | loop_end_ ## label ## _mt_nounlock: \
|
---|
362 | (so) = (sonext)
|
---|
363 |
|
---|
364 | # define DO_TCP_OUTPUT(data, sotcb) \
|
---|
365 | do { \
|
---|
366 | PRTREQ pReq = NULL; \
|
---|
367 | int rc; \
|
---|
368 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
369 | AssertReleaseRC(rc); \
|
---|
370 | pReq->u.Internal.pfn = (PFNRT)tcp_output; \
|
---|
371 | pReq->u.Internal.cArgs = 2; \
|
---|
372 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
373 | pReq->u.Internal.aArgs[1] = (uintptr_t)(sotcb); \
|
---|
374 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
375 | rc = RTReqQueue(pReq, 0); \
|
---|
376 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
377 | { \
|
---|
378 | SOCKET_UNLOCK(so); \
|
---|
379 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
380 | AssertReleaseRC(rc); \
|
---|
381 | SOCKET_LOCK(so); \
|
---|
382 | RTReqFree(pReq); \
|
---|
383 | } \
|
---|
384 | else \
|
---|
385 | AssertReleaseRC(rc); \
|
---|
386 | } while(0)
|
---|
387 |
|
---|
388 | # define DO_TCP_INPUT(data, mbuf, size, so) \
|
---|
389 | do { \
|
---|
390 | PRTREQ pReq = NULL; \
|
---|
391 | int rc; \
|
---|
392 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
393 | AssertReleaseRC(rc); \
|
---|
394 | pReq->u.Internal.pfn = (PFNRT)tcp_input; \
|
---|
395 | pReq->u.Internal.cArgs = 4; \
|
---|
396 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
397 | pReq->u.Internal.aArgs[1] = (uintptr_t)(mbuf); \
|
---|
398 | pReq->u.Internal.aArgs[2] = (uintptr_t)(size); \
|
---|
399 | pReq->u.Internal.aArgs[3] = (uintptr_t)(so); \
|
---|
400 | pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT; \
|
---|
401 | rc = RTReqQueue(pReq, 0); \
|
---|
402 | AssertReleaseRC(rc); \
|
---|
403 | } while(0)
|
---|
404 |
|
---|
405 | # define DO_TCP_CONNECT(data, so) \
|
---|
406 | do { \
|
---|
407 | PRTREQ pReq = NULL; \
|
---|
408 | int rc; \
|
---|
409 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
410 | AssertReleaseRC(rc); \
|
---|
411 | pReq->u.Internal.pfn = (PFNRT)tcp_connect; \
|
---|
412 | pReq->u.Internal.cArgs = 2; \
|
---|
413 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
414 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
415 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
416 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
417 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
418 | { \
|
---|
419 | SOCKET_UNLOCK(so); \
|
---|
420 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
421 | AssertReleaseRC(rc); \
|
---|
422 | SOCKET_LOCK(so); \
|
---|
423 | RTReqFree(pReq); \
|
---|
424 | } \
|
---|
425 | else \
|
---|
426 | AssertReleaseRC(rc); \
|
---|
427 | } while(0)
|
---|
428 |
|
---|
429 | # define DO_SOREAD(ret, data, so, ifclose) \
|
---|
430 | do { \
|
---|
431 | PRTREQ pReq = NULL; \
|
---|
432 | int rc; \
|
---|
433 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
434 | AssertReleaseRC(rc); \
|
---|
435 | pReq->u.Internal.pfn = (PFNRT)soread_queue; \
|
---|
436 | pReq->u.Internal.cArgs = 4; \
|
---|
437 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
438 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
439 | pReq->u.Internal.aArgs[2] = (uintptr_t)(ifclose); \
|
---|
440 | pReq->u.Internal.aArgs[3] = (uintptr_t)&(ret); \
|
---|
441 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
442 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
443 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
444 | { \
|
---|
445 | SOCKET_UNLOCK(so); \
|
---|
446 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
447 | AssertReleaseRC(rc); \
|
---|
448 | SOCKET_LOCK(so); \
|
---|
449 | RTReqFree(pReq); \
|
---|
450 | } \
|
---|
451 | else \
|
---|
452 | AssertReleaseRC(rc); \
|
---|
453 | } while(0)
|
---|
454 |
|
---|
455 | # define DO_SOWRITE(ret, data, so) \
|
---|
456 | do { \
|
---|
457 | PRTREQ pReq = NULL; \
|
---|
458 | int rc; \
|
---|
459 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
460 | AssertReleaseRC(rc); \
|
---|
461 | pReq->u.Internal.pfn = (PFNRT)sowrite; \
|
---|
462 | pReq->u.Internal.cArgs = 2; \
|
---|
463 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
464 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
465 | pReq->fFlags = RTREQFLAGS_RETURN_MASK; \
|
---|
466 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
467 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
468 | { \
|
---|
469 | SOCKET_UNLOCK(so); \
|
---|
470 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
471 | SOCKET_LOCK(so); \
|
---|
472 | ret = pReq->iStatus; \
|
---|
473 | RTReqFree(pReq); \
|
---|
474 | } \
|
---|
475 | else \
|
---|
476 | AssertReleaseRC(rc); \
|
---|
477 | } while(0)
|
---|
478 |
|
---|
479 | # define DO_SORECFROM(data, so) \
|
---|
480 | do { \
|
---|
481 | PRTREQ pReq = NULL; \
|
---|
482 | int rc; \
|
---|
483 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
484 | AssertReleaseRC(rc); \
|
---|
485 | pReq->u.Internal.pfn = (PFNRT)sorecvfrom; \
|
---|
486 | pReq->u.Internal.cArgs = 2; \
|
---|
487 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
488 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
489 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
490 | rc = RTReqQueue(pReq, 0); \
|
---|
491 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
492 | { \
|
---|
493 | SOCKET_UNLOCK(so); \
|
---|
494 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
495 | AssertReleaseRC(rc); \
|
---|
496 | SOCKET_LOCK(so); \
|
---|
497 | RTReqFree(pReq); \
|
---|
498 | } \
|
---|
499 | else \
|
---|
500 | AssertReleaseRC(rc); \
|
---|
501 | } while(0)
|
---|
502 |
|
---|
503 | # define DO_UDP_DETACH(data, so, so_next) \
|
---|
504 | do { \
|
---|
505 | PRTREQ pReq = NULL; \
|
---|
506 | int rc; \
|
---|
507 | rc = RTReqAlloc((data)->pReqQueue, &pReq, RTREQTYPE_INTERNAL); \
|
---|
508 | AssertReleaseRC(rc); \
|
---|
509 | pReq->u.Internal.pfn = (PFNRT)udp_detach; \
|
---|
510 | pReq->u.Internal.cArgs = 2; \
|
---|
511 | pReq->u.Internal.aArgs[0] = (uintptr_t)(data); \
|
---|
512 | pReq->u.Internal.aArgs[1] = (uintptr_t)(so); \
|
---|
513 | pReq->fFlags = RTREQFLAGS_VOID; \
|
---|
514 | rc = RTReqQueue(pReq, 0); /* don't wait, we have to release lock before*/ \
|
---|
515 | if (RT_LIKELY(rc) == VERR_TIMEOUT) \
|
---|
516 | { \
|
---|
517 | SOCKET_UNLOCK(so); \
|
---|
518 | rc = RTReqWait(pReq, RT_INDEFINITE_WAIT); \
|
---|
519 | AssertReleaseRC(rc); \
|
---|
520 | if ((so_next) != &udb) SOCKET_LOCK((so_next)); \
|
---|
521 | RTReqFree(pReq); \
|
---|
522 | } \
|
---|
523 | else \
|
---|
524 | AssertReleaseRC(rc); \
|
---|
525 | } while(0)
|
---|
526 |
|
---|
527 | # define SOLOOKUP(so, label, src, sport, dst, dport) \
|
---|
528 | do { \
|
---|
529 | struct socket *sonxt; \
|
---|
530 | (so) = NULL; \
|
---|
531 | QSOCKET_FOREACH(so, sonxt, label) \
|
---|
532 | /* { */ \
|
---|
533 | if ( so->so_lport == (sport) \
|
---|
534 | && so->so_laddr.s_addr == (src).s_addr \
|
---|
535 | && so->so_faddr.s_addr == (dst).s_addr \
|
---|
536 | && so->so_fport == (dport)) \
|
---|
537 | { \
|
---|
538 | if (sonxt != &__X(queue_ ## label ## _label)) \
|
---|
539 | SOCKET_UNLOCK(sonxt); \
|
---|
540 | break; /*so is locked*/ \
|
---|
541 | } \
|
---|
542 | LOOP_LABEL(so, sonxt, label); \
|
---|
543 | } \
|
---|
544 | } \
|
---|
545 | } while (0)
|
---|
546 |
|
---|
547 | #else /* !VBOX_WITH_SLIRP_MT */
|
---|
548 |
|
---|
549 | # define QSOCKET_LOCK(queue) do {} while (0)
|
---|
550 | # define QSOCKET_UNLOCK(queue) do {} while (0)
|
---|
551 | # define QSOCKET_LOCK_CREATE(queue) do {} while (0)
|
---|
552 | # define QSOCKET_LOCK_DESTROY(queue) do {} while (0)
|
---|
553 | # define QSOCKET_FOREACH(so, sonext, label) \
|
---|
554 | for ((so) = __X(queue_ ## label ## _label).so_next; \
|
---|
555 | (so) != &(__X(queue_ ## label ## _label)); \
|
---|
556 | (so) = (sonext)) \
|
---|
557 | { \
|
---|
558 | (sonext) = (so)->so_next;
|
---|
559 | # define CONTINUE(label) continue
|
---|
560 | # define CONTINUE_NO_UNLOCK(label) continue
|
---|
561 | # define LOOP_LABEL(label, so, sonext) /* empty*/
|
---|
562 | # define DO_TCP_OUTPUT(data, sotcb) tcp_output((data), (sotcb))
|
---|
563 | # define DO_TCP_INPUT(data, mbuf, size, so) tcp_input((data), (mbuf), (size), (so))
|
---|
564 | # define DO_TCP_CONNECT(data, so) tcp_connect((data), (so))
|
---|
565 | # define DO_SOREAD(ret, data, so, ifclose) \
|
---|
566 | do { \
|
---|
567 | (ret) = soread((data), (so), (ifclose)); \
|
---|
568 | } while(0)
|
---|
569 | # define DO_SOWRITE(ret, data, so) \
|
---|
570 | do { \
|
---|
571 | (ret) = sowrite((data), (so)); \
|
---|
572 | } while(0)
|
---|
573 | # define DO_SORECFROM(data, so) sorecvfrom((data), (so))
|
---|
574 | # define SOLOOKUP(so, label, src, sport, dst, dport) \
|
---|
575 | do { \
|
---|
576 | (so) = solookup(&__X(queue_ ## label ## _label), (src), (sport), (dst), (dport)); \
|
---|
577 | } while (0)
|
---|
578 | # define DO_UDP_DETACH(data, so, ignored) udp_detach((data), (so))
|
---|
579 |
|
---|
580 | #endif /* !VBOX_WITH_SLIRP_MT */
|
---|
581 |
|
---|
582 | #define TCP_OUTPUT(data, sotcb) DO_TCP_OUTPUT((data), (sotcb))
|
---|
583 | #define TCP_INPUT(data, mbuf, size, so) DO_TCP_INPUT((data), (mbuf), (size), (so))
|
---|
584 | #define TCP_CONNECT(data, so) DO_TCP_CONNECT((data), (so))
|
---|
585 | #define SOREAD(ret, data, so, ifclose) DO_SOREAD((ret), (data), (so), (ifclose))
|
---|
586 | #define SOWRITE(ret, data, so) DO_SOWRITE((ret), (data), (so))
|
---|
587 | #define SORECVFROM(data, so) DO_SORECFROM((data), (so))
|
---|
588 | #define UDP_DETACH(data, so, so_next) DO_UDP_DETACH((data), (so), (so_next))
|
---|
589 |
|
---|
590 | #endif /* !_slirp_state_h_ */
|
---|