VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_subr.c@ 14329

Last change on this file since 14329 was 14329, checked in by vboxsync, 16 years ago

slirp: removed unused control interface; added slirp_get_timeout_ms()

  • Property svn:eol-style set to native
File size: 36.5 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
34 * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
35 */
36
37/*
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
40 *
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
43 */
44
45#define WANT_SYS_IOCTL_H
46#include <slirp.h>
47
48
49/*
50 * Tcp initialization
51 */
52void
53tcp_init(PNATState pData)
54{
55 tcp_iss = 1; /* wrong */
56 tcb.so_next = tcb.so_prev = &tcb;
57 tcp_last_so = &tcb;
58#ifdef VBOX_WITH_BSD_TCP_REASS
59 tcp_reass_maxqlen = 48;
60 tcp_reass_maxseg = 256;
61#endif /* VBOX_WITH_BSD_TCP_REASS */
62}
63
64/*
65 * Create template to be used to send tcp packets on a connection.
66 * Call after host entry created, fills
67 * in a skeletal tcp/ip header, minimizing the amount of work
68 * necessary when the connection is used.
69 */
70/* struct tcpiphdr * */
71void
72tcp_template(tp)
73 struct tcpcb *tp;
74{
75 struct socket *so = tp->t_socket;
76 register struct tcpiphdr *n = &tp->t_template;
77
78 n->ti_next = n->ti_prev = 0;
79 n->ti_x1 = 0;
80 n->ti_pr = IPPROTO_TCP;
81 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
82 n->ti_src = so->so_faddr;
83 n->ti_dst = so->so_laddr;
84 n->ti_sport = so->so_fport;
85 n->ti_dport = so->so_lport;
86
87 n->ti_seq = 0;
88 n->ti_ack = 0;
89 n->ti_x2 = 0;
90 n->ti_off = 5;
91 n->ti_flags = 0;
92 n->ti_win = 0;
93 n->ti_sum = 0;
94 n->ti_urp = 0;
95}
96
97/*
98 * Send a single message to the TCP at address specified by
99 * the given TCP/IP header. If m == 0, then we make a copy
100 * of the tcpiphdr at ti and send directly to the addressed host.
101 * This is used to force keep alive messages out using the TCP
102 * template for a connection tp->t_template. If flags are given
103 * then we send a message back to the TCP which originated the
104 * segment ti, and discard the mbuf containing it and any other
105 * attached mbufs.
106 *
107 * In any case the ack and sequence number of the transmitted
108 * segment are as specified by the parameters.
109 */
110void
111tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
112{
113 register int tlen;
114 int win = 0;
115
116 DEBUG_CALL("tcp_respond");
117 DEBUG_ARG("tp = %lx", (long)tp);
118 DEBUG_ARG("ti = %lx", (long)ti);
119 DEBUG_ARG("m = %lx", (long)m);
120 DEBUG_ARG("ack = %u", ack);
121 DEBUG_ARG("seq = %u", seq);
122 DEBUG_ARG("flags = %x", flags);
123
124 if (tp)
125 win = sbspace(&tp->t_socket->so_rcv);
126 if (m == 0) {
127 if ((m = m_get(pData)) == NULL)
128 return;
129#ifdef TCP_COMPAT_42
130 tlen = 1;
131#else
132 tlen = 0;
133#endif
134 m->m_data += if_maxlinkhdr;
135 *mtod(m, struct tcpiphdr *) = *ti;
136 ti = mtod(m, struct tcpiphdr *);
137 flags = TH_ACK;
138 } else {
139 /*
140 * ti points into m so the next line is just making
141 * the mbuf point to ti
142 */
143 m->m_data = (caddr_t)ti;
144
145 m->m_len = sizeof (struct tcpiphdr);
146 tlen = 0;
147#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
148 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
149 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
150#undef xchg
151 }
152 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
153 tlen += sizeof (struct tcpiphdr);
154 m->m_len = tlen;
155
156 ti->ti_next = ti->ti_prev = 0;
157 ti->ti_x1 = 0;
158 ti->ti_seq = htonl(seq);
159 ti->ti_ack = htonl(ack);
160 ti->ti_x2 = 0;
161 ti->ti_off = sizeof (struct tcphdr) >> 2;
162 ti->ti_flags = flags;
163 if (tp)
164 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
165 else
166 ti->ti_win = htons((u_int16_t)win);
167 ti->ti_urp = 0;
168 ti->ti_sum = 0;
169 ti->ti_sum = cksum(m, tlen);
170 ((struct ip *)ti)->ip_len = tlen;
171
172 if(flags & TH_RST)
173 ((struct ip *)ti)->ip_ttl = MAXTTL;
174 else
175 ((struct ip *)ti)->ip_ttl = ip_defttl;
176
177 (void) ip_output(pData, (struct socket *)0, m);
178}
179
180/*
181 * Create a new TCP control block, making an
182 * empty reassembly queue and hooking it to the argument
183 * protocol control block.
184 */
185struct tcpcb *
186tcp_newtcpcb(PNATState pData, struct socket *so)
187{
188 register struct tcpcb *tp;
189
190 tp = (struct tcpcb *)malloc(sizeof(*tp));
191 if (tp == NULL)
192 return ((struct tcpcb *)0);
193
194 memset((char *) tp, 0, sizeof(struct tcpcb));
195#ifndef VBOX_WITH_BSD_TCP_REASS
196 tp->seg_next = tp->seg_prev = ptr_to_u32(pData, (struct tcpiphdr *)tp);
197#else /* VBOX_WITH_BSD_TCP_REASS */
198 LIST_INSERT_HEAD(&pData->tcpcbhead, tp, t_list);
199#endif /* VBOX_WITH_BSD_TCP_REASS */
200 tp->t_maxseg = tcp_mssdflt;
201
202 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
203 tp->t_socket = so;
204
205 /*
206 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
207 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
208 * reasonable initial retransmit time.
209 */
210 tp->t_srtt = TCPTV_SRTTBASE;
211 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
212 tp->t_rttmin = TCPTV_MIN;
213
214 TCPT_RANGESET(tp->t_rxtcur,
215 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
216 TCPTV_MIN, TCPTV_REXMTMAX);
217
218 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
219 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
220 tp->t_state = TCPS_CLOSED;
221
222 so->so_tcpcb = tp;
223
224 return (tp);
225}
226
227/*
228 * Drop a TCP connection, reporting
229 * the specified error. If connection is synchronized,
230 * then send a RST to peer.
231 */
232struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
233{
234/* tcp_drop(tp, errno)
235 register struct tcpcb *tp;
236 int errno;
237{
238*/
239
240 DEBUG_CALL("tcp_drop");
241 DEBUG_ARG("tp = %lx", (long)tp);
242 DEBUG_ARG("errno = %d", errno);
243
244 if (TCPS_HAVERCVDSYN(tp->t_state)) {
245 tp->t_state = TCPS_CLOSED;
246 (void) tcp_output(pData, tp);
247 tcpstat.tcps_drops++;
248 } else
249 tcpstat.tcps_conndrops++;
250/* if (errno == ETIMEDOUT && tp->t_softerror)
251 * errno = tp->t_softerror;
252 */
253/* so->so_error = errno; */
254 return (tcp_close(pData, tp));
255}
256
257/*
258 * Close a TCP control block:
259 * discard all space held by the tcp
260 * discard internet protocol block
261 * wake up any sleepers
262 */
263struct tcpcb *
264tcp_close(PNATState pData, register struct tcpcb *tp)
265{
266 register struct tcpiphdr *t;
267 struct socket *so = tp->t_socket;
268 register struct mbuf *m;
269
270#ifndef VBOX_WITH_BSD_TCP_REASS
271 DEBUG_CALL("tcp_close");
272 DEBUG_ARG("tp = %lx", (long )tp);
273
274 /* free the reassembly queue, if any */
275 t = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *);
276 while (t != (struct tcpiphdr *)tp) {
277 t = u32_to_ptr(pData, t->ti_next, struct tcpiphdr *);
278 m = REASS_MBUF_GET(u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
279 remque_32(pData, u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
280 m_freem(pData, m);
281 }
282 /* It's static */
283/* if (tp->t_template)
284 * (void) m_free(dtom(tp->t_template));
285 */
286/* free(tp, M_PCB); */
287 u32ptr_done(pData, ptr_to_u32(pData, tp), tp);
288#else /* VBOX_WITH_BSD_TCP_REASS */
289 struct tseg_qent *te;
290 DEBUG_CALL("tcp_close");
291 DEBUG_ARG("tp = %lx", (long )tp);
292 /*XXX: freeing the reassembly queue */
293 LIST_FOREACH(te, &tp->t_segq, tqe_q) {
294 LIST_REMOVE(te, tqe_q);
295 m_freem(pData, te->tqe_m);
296 free(te);
297 tcp_reass_qsize--;
298 }
299#endif /* VBOX_WITH_BSD_TCP_REASS */
300 free(tp);
301 so->so_tcpcb = 0;
302 soisfdisconnected(so);
303 /* clobber input socket cache if we're closing the cached connection */
304 if (so == tcp_last_so)
305 tcp_last_so = &tcb;
306 closesocket(so->s);
307 sbfree(&so->so_rcv);
308 sbfree(&so->so_snd);
309 sofree(pData, so);
310 tcpstat.tcps_closed++;
311 return ((struct tcpcb *)0);
312}
313
314void
315tcp_drain()
316{
317 /* XXX */
318}
319
320/*
321 * When a source quench is received, close congestion window
322 * to one segment. We will gradually open it again as we proceed.
323 */
324
325#ifdef notdef
326
327void
328tcp_quench(i, errno)
329
330 int errno;
331{
332 struct tcpcb *tp = intotcpcb(inp);
333
334 if (tp)
335 tp->snd_cwnd = tp->t_maxseg;
336}
337
338#endif /* notdef */
339
340/*
341 * TCP protocol interface to socket abstraction.
342 */
343
344/*
345 * User issued close, and wish to trail through shutdown states:
346 * if never received SYN, just forget it. If got a SYN from peer,
347 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
348 * If already got a FIN from peer, then almost done; go to LAST_ACK
349 * state. In all other cases, have already sent FIN to peer (e.g.
350 * after PRU_SHUTDOWN), and just have to play tedious game waiting
351 * for peer to send FIN or not respond to keep-alives, etc.
352 * We can let the user exit from the close as soon as the FIN is acked.
353 */
354void
355tcp_sockclosed(PNATState pData, struct tcpcb *tp)
356{
357
358 DEBUG_CALL("tcp_sockclosed");
359 DEBUG_ARG("tp = %lx", (long)tp);
360
361 switch (tp->t_state) {
362
363 case TCPS_CLOSED:
364 case TCPS_LISTEN:
365 case TCPS_SYN_SENT:
366 tp->t_state = TCPS_CLOSED;
367 tp = tcp_close(pData, tp);
368 break;
369
370 case TCPS_SYN_RECEIVED:
371 case TCPS_ESTABLISHED:
372 tp->t_state = TCPS_FIN_WAIT_1;
373 break;
374
375 case TCPS_CLOSE_WAIT:
376 tp->t_state = TCPS_LAST_ACK;
377 break;
378 }
379/* soisfdisconnecting(tp->t_socket); */
380 if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
381 soisfdisconnected(tp->t_socket);
382 if (tp)
383 tcp_output(pData, tp);
384}
385
386/*
387 * Connect to a host on the Internet
388 * Called by tcp_input
389 * Only do a connect, the tcp fields will be set in tcp_input
390 * return 0 if there's a result of the connect,
391 * else return -1 means we're still connecting
392 * The return value is almost always -1 since the socket is
393 * nonblocking. Connect returns after the SYN is sent, and does
394 * not wait for ACK+SYN.
395 */
396int tcp_fconnect(PNATState pData, struct socket *so)
397{
398 int ret=0;
399
400 DEBUG_CALL("tcp_fconnect");
401 DEBUG_ARG("so = %lx", (long )so);
402
403 if( (ret=so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0) {
404 int opt, s=so->s;
405 struct sockaddr_in addr;
406
407 fd_nonblock(s);
408 opt = 1;
409 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
410 opt = 1;
411 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
412
413 addr.sin_family = AF_INET;
414 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr) {
415 /* It's an alias */
416 switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask) {
417 case CTL_DNS:
418 if (!get_dns_addr(pData, &dns_addr))
419 addr.sin_addr = dns_addr;
420 else
421 addr.sin_addr = loopback_addr;
422 break;
423 case CTL_ALIAS:
424 default:
425 addr.sin_addr = loopback_addr;
426 break;
427 }
428 } else
429 addr.sin_addr = so->so_faddr;
430 addr.sin_port = so->so_fport;
431
432 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
433 "addr.sin_addr.s_addr=%.16s\n",
434 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
435 /* We don't care what port we get */
436 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
437
438 /*
439 * If it's not in progress, it failed, so we just return 0,
440 * without clearing SS_NOFDREF
441 */
442 soisfconnecting(so);
443 }
444
445 return(ret);
446}
447
448/*
449 * Accept the socket and connect to the local-host
450 *
451 * We have a problem. The correct thing to do would be
452 * to first connect to the local-host, and only if the
453 * connection is accepted, then do an accept() here.
454 * But, a) we need to know who's trying to connect
455 * to the socket to be able to SYN the local-host, and
456 * b) we are already connected to the foreign host by
457 * the time it gets to accept(), so... We simply accept
458 * here and SYN the local-host.
459 */
460void
461tcp_connect(PNATState pData, struct socket *inso)
462{
463 struct socket *so;
464 struct sockaddr_in addr;
465 socklen_t addrlen = sizeof(struct sockaddr_in);
466 struct tcpcb *tp;
467 int s, opt;
468
469 DEBUG_CALL("tcp_connect");
470 DEBUG_ARG("inso = %lx", (long)inso);
471
472 /*
473 * If it's an SS_ACCEPTONCE socket, no need to socreate()
474 * another socket, just use the accept() socket.
475 */
476 if (inso->so_state & SS_FACCEPTONCE) {
477 /* FACCEPTONCE already have a tcpcb */
478 so = inso;
479 } else {
480 if ((so = socreate()) == NULL) {
481 /* If it failed, get rid of the pending connection */
482 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
483 return;
484 }
485 if (tcp_attach(pData, so) < 0) {
486 free(so); /* NOT sofree */
487 return;
488 }
489 so->so_laddr = inso->so_laddr;
490 so->so_lport = inso->so_lport;
491 }
492
493 (void) tcp_mss(pData, sototcpcb(so), 0);
494
495 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
496 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
497 return;
498 }
499 fd_nonblock(s);
500 opt = 1;
501 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
502 opt = 1;
503 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
504 opt = 1;
505 setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
506
507 so->so_fport = addr.sin_port;
508 so->so_faddr = addr.sin_addr;
509 /* Translate connections from localhost to the real hostname */
510 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
511 so->so_faddr = alias_addr;
512
513 /* Close the accept() socket, set right state */
514 if (inso->so_state & SS_FACCEPTONCE) {
515 closesocket(so->s); /* If we only accept once, close the accept() socket */
516 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
517 /* if it's not FACCEPTONCE, it's already NOFDREF */
518 }
519 so->s = s;
520
521 so->so_iptos = tcp_tos(so);
522 tp = sototcpcb(so);
523
524 tcp_template(tp);
525
526 /* Compute window scaling to request. */
527/* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
528 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
529 * tp->request_r_scale++;
530 */
531
532/* soisconnecting(so); */ /* NOFDREF used instead */
533 tcpstat.tcps_connattempt++;
534
535 tp->t_state = TCPS_SYN_SENT;
536 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
537 tp->iss = tcp_iss;
538 tcp_iss += TCP_ISSINCR/2;
539 tcp_sendseqinit(tp);
540 tcp_output(pData, tp);
541}
542
543/*
544 * Attach a TCPCB to a socket.
545 */
546int
547tcp_attach(PNATState pData, struct socket *so)
548{
549 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
550 return -1;
551
552 insque(pData, so, &tcb);
553
554 return 0;
555}
556
557/*
558 * Set the socket's type of service field
559 */
560static const struct tos_t tcptos[] = {
561 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
562 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
563 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
564 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
565 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
566 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
567 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
568 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
569 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
570 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
571 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
572 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
573 {0, 0, 0, 0}
574};
575
576/*
577 * Return TOS according to the above table
578 */
579u_int8_t
580tcp_tos(so)
581 struct socket *so;
582{
583 int i = 0;
584
585 while(tcptos[i].tos) {
586 if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
587 (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
588 so->so_emu = tcptos[i].emu;
589 return tcptos[i].tos;
590 }
591 i++;
592 }
593
594 return 0;
595}
596
597/*
598 * Emulate programs that try and connect to us
599 * This includes ftp (the data connection is
600 * initiated by the server) and IRC (DCC CHAT and
601 * DCC SEND) for now
602 *
603 * NOTE: It's possible to crash SLiRP by sending it
604 * unstandard strings to emulate... if this is a problem,
605 * more checks are needed here
606 *
607 * XXX Assumes the whole command came in one packet
608 *
609 * XXX Some ftp clients will have their TOS set to
610 * LOWDELAY and so Nagel will kick in. Because of this,
611 * we'll get the first letter, followed by the rest, so
612 * we simply scan for ORT instead of PORT...
613 * DCC doesn't have this problem because there's other stuff
614 * in the packet before the DCC command.
615 *
616 * Return 1 if the mbuf m is still valid and should be
617 * sbappend()ed
618 *
619 * NOTE: if you return 0 you MUST m_free() the mbuf!
620 */
621int
622tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
623{
624 u_int n1, n2, n3, n4, n5, n6;
625 char buff[256];
626 u_int32_t laddr;
627 u_int lport;
628 char *bptr;
629
630 DEBUG_CALL("tcp_emu");
631 DEBUG_ARG("so = %lx", (long)so);
632 DEBUG_ARG("m = %lx", (long)m);
633
634 switch(so->so_emu) {
635 int x, i;
636
637 case EMU_IDENT:
638 /*
639 * Identification protocol as per rfc-1413
640 */
641
642 {
643 struct socket *tmpso;
644 struct sockaddr_in addr;
645 socklen_t addrlen = sizeof(struct sockaddr_in);
646 struct sbuf *so_rcv = &so->so_rcv;
647
648 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
649 so_rcv->sb_wptr += m->m_len;
650 so_rcv->sb_rptr += m->m_len;
651 m->m_data[m->m_len] = 0; /* NULL terminate */
652 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
653 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
654 HTONS(n1);
655 HTONS(n2);
656 /* n2 is the one on our host */
657 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
658 if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
659 tmpso->so_lport == n2 &&
660 tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
661 tmpso->so_fport == n1) {
662 if (getsockname(tmpso->s,
663 (struct sockaddr *)&addr, &addrlen) == 0)
664 n2 = ntohs(addr.sin_port);
665 break;
666 }
667 }
668 }
669 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
670 so_rcv->sb_rptr = so_rcv->sb_data;
671 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
672 }
673 m_free(pData, m);
674 return 0;
675 }
676
677#if 0
678 case EMU_RLOGIN:
679 /*
680 * Rlogin emulation
681 * First we accumulate all the initial option negotiation,
682 * then fork_exec() rlogin according to the options
683 */
684 {
685 int i, i2, n;
686 char *ptr;
687 char args[100];
688 char term[100];
689 struct sbuf *so_snd = &so->so_snd;
690 struct sbuf *so_rcv = &so->so_rcv;
691
692 /* First check if they have a priveladged port, or too much data has arrived */
693 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
694 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
695 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
696 so_snd->sb_wptr += 18;
697 so_snd->sb_cc += 18;
698 tcp_sockclosed(sototcpcb(so));
699 m_free(m);
700 return 0;
701 }
702
703 /* Append the current data */
704 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
705 so_rcv->sb_wptr += m->m_len;
706 so_rcv->sb_rptr += m->m_len;
707 m_free(m);
708
709 /*
710 * Check if we have all the initial options,
711 * and build argument list to rlogin while we're here
712 */
713 n = 0;
714 ptr = so_rcv->sb_data;
715 args[0] = 0;
716 term[0] = 0;
717 while (ptr < so_rcv->sb_wptr) {
718 if (*ptr++ == 0) {
719 n++;
720 if (n == 2) {
721 sprintf(args, "rlogin -l %s %s",
722 ptr, inet_ntoa(so->so_faddr));
723 } else if (n == 3) {
724 i2 = so_rcv->sb_wptr - ptr;
725 for (i = 0; i < i2; i++) {
726 if (ptr[i] == '/') {
727 ptr[i] = 0;
728#ifdef HAVE_SETENV
729 sprintf(term, "%s", ptr);
730#else
731 sprintf(term, "TERM=%s", ptr);
732#endif
733 ptr[i] = '/';
734 break;
735 }
736 }
737 }
738 }
739 }
740
741 if (n != 4)
742 return 0;
743
744 /* We have it, set our term variable and fork_exec() */
745#ifdef HAVE_SETENV
746 setenv("TERM", term, 1);
747#else
748 putenv(term);
749#endif
750 fork_exec(so, args, 2);
751 term[0] = 0;
752 so->so_emu = 0;
753
754 /* And finally, send the client a 0 character */
755 so_snd->sb_wptr[0] = 0;
756 so_snd->sb_wptr++;
757 so_snd->sb_cc++;
758
759 return 0;
760 }
761
762 case EMU_RSH:
763 /*
764 * rsh emulation
765 * First we accumulate all the initial option negotiation,
766 * then rsh_exec() rsh according to the options
767 */
768 {
769 int n;
770 char *ptr;
771 char *user;
772 char *args;
773 struct sbuf *so_snd = &so->so_snd;
774 struct sbuf *so_rcv = &so->so_rcv;
775
776 /* First check if they have a priveladged port, or too much data has arrived */
777 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
778 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
779 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
780 so_snd->sb_wptr += 18;
781 so_snd->sb_cc += 18;
782 tcp_sockclosed(sototcpcb(so));
783 m_free(m);
784 return 0;
785 }
786
787 /* Append the current data */
788 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
789 so_rcv->sb_wptr += m->m_len;
790 so_rcv->sb_rptr += m->m_len;
791 m_free(m);
792
793 /*
794 * Check if we have all the initial options,
795 * and build argument list to rlogin while we're here
796 */
797 n = 0;
798 ptr = so_rcv->sb_data;
799 user="";
800 args="";
801 if (so->extra==NULL) {
802 struct socket *ns;
803 struct tcpcb* tp;
804 int port=atoi(ptr);
805 if (port <= 0) return 0;
806 if (port > 1023 || port < 512) {
807 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
808 so_snd->sb_wptr += 18;
809 so_snd->sb_cc += 18;
810 tcp_sockclosed(sototcpcb(so));
811 return 0;
812 }
813 if ((ns=socreate()) == NULL)
814 return 0;
815 if (tcp_attach(ns)<0) {
816 free(ns);
817 return 0;
818 }
819
820 ns->so_laddr=so->so_laddr;
821 ns->so_lport=htons(port);
822
823 (void) tcp_mss(sototcpcb(ns), 0);
824
825 ns->so_faddr=so->so_faddr;
826 ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
827
828 if (ns->so_faddr.s_addr == 0 ||
829 ns->so_faddr.s_addr == loopback_addr.s_addr)
830 ns->so_faddr = alias_addr;
831
832 ns->so_iptos = tcp_tos(ns);
833 tp = sototcpcb(ns);
834
835 tcp_template(tp);
836
837 /* Compute window scaling to request. */
838 /* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
839 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
840 * tp->request_r_scale++;
841 */
842
843 /*soisfconnecting(ns);*/
844
845 tcpstat.tcps_connattempt++;
846
847 tp->t_state = TCPS_SYN_SENT;
848 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
849 tp->iss = tcp_iss;
850 tcp_iss += TCP_ISSINCR/2;
851 tcp_sendseqinit(tp);
852 tcp_output(tp);
853 so->extra=ns;
854 }
855 while (ptr < so_rcv->sb_wptr) {
856 if (*ptr++ == 0) {
857 n++;
858 if (n == 2) {
859 user=ptr;
860 } else if (n == 3) {
861 args=ptr;
862 }
863 }
864 }
865
866 if (n != 4)
867 return 0;
868
869 rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
870 so->so_emu = 0;
871 so->extra=NULL;
872
873 /* And finally, send the client a 0 character */
874 so_snd->sb_wptr[0] = 0;
875 so_snd->sb_wptr++;
876 so_snd->sb_cc++;
877
878 return 0;
879 }
880
881 case EMU_CTL:
882 {
883 int num;
884 struct sbuf *so_snd = &so->so_snd;
885 struct sbuf *so_rcv = &so->so_rcv;
886
887 /*
888 * If there is binary data here, we save it in so->so_m
889 */
890 if (!so->so_m) {
891 int rxlen;
892 char *rxdata;
893 rxdata=mtod(m, char *);
894 for (rxlen=m->m_len; rxlen; rxlen--) {
895 if (*rxdata++ & 0x80) {
896 so->so_m = m;
897 return 0;
898 }
899 }
900 } /* if(so->so_m==NULL) */
901
902 /*
903 * Append the line
904 */
905 sbappendsb(so_rcv, m);
906
907 /* To avoid going over the edge of the buffer, we reset it */
908 if (so_snd->sb_cc == 0)
909 so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
910
911 /*
912 * A bit of a hack:
913 * If the first packet we get here is 1 byte long, then it
914 * was done in telnet character mode, therefore we must echo
915 * the characters as they come. Otherwise, we echo nothing,
916 * because in linemode, the line is already echoed
917 * XXX two or more control connections won't work
918 */
919 if (do_echo == -1) {
920 if (m->m_len == 1) do_echo = 1;
921 else do_echo = 0;
922 }
923 if (do_echo) {
924 sbappendsb(so_snd, m);
925 m_free(m);
926 tcp_output(sototcpcb(so)); /* XXX */
927 } else
928 m_free(m);
929
930 num = 0;
931 while (num < so->so_rcv.sb_cc) {
932 if (*(so->so_rcv.sb_rptr + num) == '\n' ||
933 *(so->so_rcv.sb_rptr + num) == '\r') {
934 int n;
935
936 *(so_rcv->sb_rptr + num) = 0;
937 if (ctl_password && !ctl_password_ok) {
938 /* Need a password */
939 if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
940 if (strcmp(buff, ctl_password) == 0) {
941 ctl_password_ok = 1;
942 n = sprintf(so_snd->sb_wptr,
943 "Password OK.\r\n");
944 goto do_prompt;
945 }
946 }
947 n = sprintf(so_snd->sb_wptr,
948 "Error: Password required, log on with \"pass PASSWORD\"\r\n");
949 goto do_prompt;
950 }
951 cfg_quitting = 0;
952 n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
953 if (!cfg_quitting) {
954 /* Register the printed data */
955do_prompt:
956 so_snd->sb_cc += n;
957 so_snd->sb_wptr += n;
958 /* Add prompt */
959 n = sprintf(so_snd->sb_wptr, "Slirp> ");
960 so_snd->sb_cc += n;
961 so_snd->sb_wptr += n;
962 }
963 /* Drop so_rcv data */
964 so_rcv->sb_cc = 0;
965 so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
966 tcp_output(sototcpcb(so)); /* Send the reply */
967 }
968 num++;
969 }
970 return 0;
971 }
972#endif
973 case EMU_FTP: /* ftp */
974 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
975 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
976 /*
977 * Need to emulate the PORT command
978 */
979 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
980 &n1, &n2, &n3, &n4, &n5, &n6, buff);
981 if (x < 6)
982 return 1;
983
984 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
985 lport = htons((n5 << 8) | (n6));
986
987 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
988 return 1;
989
990 n6 = ntohs(so->so_fport);
991
992 n5 = (n6 >> 8) & 0xff;
993 n6 &= 0xff;
994
995 laddr = ntohl(so->so_faddr.s_addr);
996
997 n1 = ((laddr >> 24) & 0xff);
998 n2 = ((laddr >> 16) & 0xff);
999 n3 = ((laddr >> 8) & 0xff);
1000 n4 = (laddr & 0xff);
1001
1002 m->m_len = bptr - m->m_data; /* Adjust length */
1003 m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
1004 n1, n2, n3, n4, n5, n6, x==7?buff:"");
1005 return 1;
1006 } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
1007 /*
1008 * Need to emulate the PASV response
1009 */
1010 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
1011 &n1, &n2, &n3, &n4, &n5, &n6, buff);
1012 if (x < 6)
1013 return 1;
1014
1015 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1016 lport = htons((n5 << 8) | (n6));
1017
1018 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1019 return 1;
1020
1021 n6 = ntohs(so->so_fport);
1022
1023 n5 = (n6 >> 8) & 0xff;
1024 n6 &= 0xff;
1025
1026 laddr = ntohl(so->so_faddr.s_addr);
1027
1028 n1 = ((laddr >> 24) & 0xff);
1029 n2 = ((laddr >> 16) & 0xff);
1030 n3 = ((laddr >> 8) & 0xff);
1031 n4 = (laddr & 0xff);
1032
1033 m->m_len = bptr - m->m_data; /* Adjust length */
1034 m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
1035 n1, n2, n3, n4, n5, n6, x==7?buff:"");
1036
1037 return 1;
1038 }
1039
1040 return 1;
1041
1042 case EMU_KSH:
1043 /*
1044 * The kshell (Kerberos rsh) and shell services both pass
1045 * a local port port number to carry signals to the server
1046 * and stderr to the client. It is passed at the beginning
1047 * of the connection as a NUL-terminated decimal ASCII string.
1048 */
1049 so->so_emu = 0;
1050 for (lport = 0, i = 0; i < m->m_len-1; ++i) {
1051 if (m->m_data[i] < '0' || m->m_data[i] > '9')
1052 return 1; /* invalid number */
1053 lport *= 10;
1054 lport += m->m_data[i] - '0';
1055 }
1056 if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
1057 (so = solisten(pData, 0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1058 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
1059 return 1;
1060
1061 case EMU_IRC:
1062 /*
1063 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
1064 */
1065 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
1066 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
1067 return 1;
1068
1069 /* The %256s is for the broken mIRC */
1070 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1071 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1072 return 1;
1073
1074 m->m_len = bptr - m->m_data; /* Adjust length */
1075 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1076 (unsigned long)ntohl(so->so_faddr.s_addr),
1077 ntohs(so->so_fport), 1);
1078 } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1079 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1080 return 1;
1081
1082 m->m_len = bptr - m->m_data; /* Adjust length */
1083 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
1084 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1085 ntohs(so->so_fport), n1, 1);
1086 } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1087 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1088 return 1;
1089
1090 m->m_len = bptr - m->m_data; /* Adjust length */
1091 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1092 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1093 ntohs(so->so_fport), n1, 1);
1094 }
1095 return 1;
1096
1097#ifdef VBOX
1098 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
1099 * This is not legal when more than one slirp instance is active. */
1100#else /* !VBOX */
1101 case EMU_REALAUDIO:
1102 /*
1103 * RealAudio emulation - JP. We must try to parse the incoming
1104 * data and try to find the two characters that contain the
1105 * port number. Then we redirect an udp port and replace the
1106 * number with the real port we got.
1107 *
1108 * The 1.0 beta versions of the player are not supported
1109 * any more.
1110 *
1111 * A typical packet for player version 1.0 (release version):
1112 *
1113 * 0000:50 4E 41 00 05
1114 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
1115 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
1116 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
1117 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
1118 *
1119 * Now the port number 0x1BD7 is found at offset 0x04 of the
1120 * Now the port number 0x1BD7 is found at offset 0x04 of the
1121 * second packet. This time we received five bytes first and
1122 * then the rest. You never know how many bytes you get.
1123 *
1124 * A typical packet for player version 2.0 (beta):
1125 *
1126 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
1127 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
1128 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
1129 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
1130 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
1131 *
1132 * Port number 0x1BC1 is found at offset 0x0d.
1133 *
1134 * This is just a horrible switch statement. Variable ra tells
1135 * us where we're going.
1136 */
1137
1138 bptr = m->m_data;
1139 while (bptr < m->m_data + m->m_len) {
1140 u_short p;
1141 static int ra = 0;
1142 char ra_tbl[4];
1143
1144 ra_tbl[0] = 0x50;
1145 ra_tbl[1] = 0x4e;
1146 ra_tbl[2] = 0x41;
1147 ra_tbl[3] = 0;
1148
1149 switch (ra) {
1150 case 0:
1151 case 2:
1152 case 3:
1153 if (*bptr++ != ra_tbl[ra]) {
1154 ra = 0;
1155 continue;
1156 }
1157 break;
1158
1159 case 1:
1160 /*
1161 * We may get 0x50 several times, ignore them
1162 */
1163 if (*bptr == 0x50) {
1164 ra = 1;
1165 bptr++;
1166 continue;
1167 } else if (*bptr++ != ra_tbl[ra]) {
1168 ra = 0;
1169 continue;
1170 }
1171 break;
1172
1173 case 4:
1174 /*
1175 * skip version number
1176 */
1177 bptr++;
1178 break;
1179
1180 case 5:
1181 /*
1182 * The difference between versions 1.0 and
1183 * 2.0 is here. For future versions of
1184 * the player this may need to be modified.
1185 */
1186 if (*(bptr + 1) == 0x02)
1187 bptr += 8;
1188 else
1189 bptr += 4;
1190 break;
1191
1192 case 6:
1193 /* This is the field containing the port
1194 * number that RA-player is listening to.
1195 */
1196 lport = (((u_char*)bptr)[0] << 8)
1197 + ((u_char *)bptr)[1];
1198 if (lport < 6970)
1199 lport += 256; /* don't know why */
1200 if (lport < 6970 || lport > 7170)
1201 return 1; /* failed */
1202
1203 /* try to get udp port between 6970 - 7170 */
1204 for (p = 6970; p < 7071; p++) {
1205 if (udp_listen( htons(p),
1206 so->so_laddr.s_addr,
1207 htons(lport),
1208 SS_FACCEPTONCE)) {
1209 break;
1210 }
1211 }
1212 if (p == 7071)
1213 p = 0;
1214 *(u_char *)bptr++ = (p >> 8) & 0xff;
1215 *(u_char *)bptr++ = p & 0xff;
1216 ra = 0;
1217 return 1; /* port redirected, we're done */
1218 break;
1219
1220 default:
1221 ra = 0;
1222 }
1223 ra++;
1224 }
1225 return 1;
1226#endif /* !VBOX */
1227
1228 default:
1229 /* Ooops, not emulated, won't call tcp_emu again */
1230 so->so_emu = 0;
1231 return 1;
1232 }
1233}
1234
1235#if SIZEOF_CHAR_P != 4
1236/**
1237 * Slow pointer hashing that deals with automatic inserting and collisions.
1238 */
1239uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
1240{
1241 uint32_t i;
1242 if (pv == NULL)
1243 i = 0;
1244 else
1245 {
1246 const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1247 if (pData->apvHash[i1] == pv)
1248 i = i1;
1249 else
1250 {
1251 /*
1252 * Try up to 10 times then assume it's an insertion.
1253 * If we didn't find a free entry by then, try another 100 times.
1254 * If that fails, give up.
1255 */
1256 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1257 uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
1258 int cTries = 10;
1259 int cTries2 = 100;
1260
1261 i = i1;
1262 for (;;)
1263 {
1264 /* check if we should give in.*/
1265 if (--cTries > 0)
1266 {
1267 if (i1stFree != 0)
1268 {
1269 i = i1stFree;
1270 pData->apvHash[i] = pv;
1271 pData->cpvHashUsed++;
1272 if (i != i1)
1273 pData->cpvHashCollisions++;
1274 pData->cpvHashInserts++;
1275 break;
1276 }
1277 if (!cTries2)
1278 {
1279 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
1280 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1281 i = 0;
1282 break;
1283 }
1284 cTries = cTries2;
1285 cTries2 = 0;
1286 }
1287
1288 /* advance to the next hash entry and test it. */
1289 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1290 while (RT_UNLIKELY(!i))
1291 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1292 if (pData->apvHash[i] == pv)
1293 break;
1294 if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
1295 i1stFree = i;
1296 }
1297 }
1298 }
1299 return i;
1300}
1301
1302
1303/**
1304 * Removes the pointer from the hash table.
1305 */
1306void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
1307{
1308 /* We don't count NULL pointers. */
1309 if (pv == NULL)
1310 return;
1311 pData->cpvHashDone++;
1312
1313 /* try the hint */
1314 if ( iHint
1315 && iHint < RT_ELEMENTS(pData->apvHash)
1316 && pData->apvHash[iHint] == pv)
1317 {
1318 pData->apvHash[iHint] = NULL;
1319 pData->cpvHashUsed--;
1320 return;
1321 }
1322
1323 iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1324 if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
1325 {
1326 /*
1327 * Try up to 120 times then assert.
1328 */
1329 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1330 int cTries = 120;
1331 for (;;)
1332 {
1333 /* advance to the next hash entry and test it. */
1334 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1335 while (RT_UNLIKELY(!iHint))
1336 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1337 if (pData->apvHash[iHint] == pv)
1338 break;
1339
1340 /* check if we should give in.*/
1341 if (--cTries > 0)
1342 {
1343 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
1344 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1345 return;
1346 }
1347 }
1348 }
1349
1350 /* found it */
1351 pData->apvHash[iHint] = NULL;
1352 pData->cpvHashUsed--;
1353}
1354
1355#endif
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