VirtualBox

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

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

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