VirtualBox

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

Last change on this file since 3237 was 1076, checked in by vboxsync, 18 years ago

Removed tons of ifdef VBOX conditionals to make slirp readable again

  • Property svn:eol-style set to native
File size: 37.7 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}
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(0xffffff00)) == special_addr.s_addr) {
394 /* It's an alias */
395 switch(ntohl(so->so_faddr.s_addr) & 0xff) {
396 case CTL_DNS:
397 addr.sin_addr = dns_addr;
398 break;
399 case CTL_ALIAS:
400 default:
401 addr.sin_addr = loopback_addr;
402 break;
403 }
404 } else
405 addr.sin_addr = so->so_faddr;
406 addr.sin_port = so->so_fport;
407
408 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
409 "addr.sin_addr.s_addr=%.16s\n",
410 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
411 /* We don't care what port we get */
412 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
413
414 /*
415 * If it's not in progress, it failed, so we just return 0,
416 * without clearing SS_NOFDREF
417 */
418 soisfconnecting(so);
419 }
420
421 return(ret);
422}
423
424/*
425 * Accept the socket and connect to the local-host
426 *
427 * We have a problem. The correct thing to do would be
428 * to first connect to the local-host, and only if the
429 * connection is accepted, then do an accept() here.
430 * But, a) we need to know who's trying to connect
431 * to the socket to be able to SYN the local-host, and
432 * b) we are already connected to the foreign host by
433 * the time it gets to accept(), so... We simply accept
434 * here and SYN the local-host.
435 */
436void
437tcp_connect(PNATState pData, struct socket *inso)
438{
439 struct socket *so;
440 struct sockaddr_in addr;
441 socklen_t addrlen = sizeof(struct sockaddr_in);
442 struct tcpcb *tp;
443 int s, opt;
444
445 DEBUG_CALL("tcp_connect");
446 DEBUG_ARG("inso = %lx", (long)inso);
447
448 /*
449 * If it's an SS_ACCEPTONCE socket, no need to socreate()
450 * another socket, just use the accept() socket.
451 */
452 if (inso->so_state & SS_FACCEPTONCE) {
453 /* FACCEPTONCE already have a tcpcb */
454 so = inso;
455 } else {
456 if ((so = socreate()) == NULL) {
457 /* If it failed, get rid of the pending connection */
458 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
459 return;
460 }
461 if (tcp_attach(pData, so) < 0) {
462 free(so); /* NOT sofree */
463 return;
464 }
465 so->so_laddr = inso->so_laddr;
466 so->so_lport = inso->so_lport;
467 }
468
469 (void) tcp_mss(pData, sototcpcb(so), 0);
470
471 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
472 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
473 return;
474 }
475 fd_nonblock(s);
476 opt = 1;
477 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
478 opt = 1;
479 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
480 opt = 1;
481 setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
482
483 so->so_fport = addr.sin_port;
484 so->so_faddr = addr.sin_addr;
485 /* Translate connections from localhost to the real hostname */
486 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
487 so->so_faddr = alias_addr;
488
489 /* Close the accept() socket, set right state */
490 if (inso->so_state & SS_FACCEPTONCE) {
491 closesocket(so->s); /* If we only accept once, close the accept() socket */
492 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
493 /* if it's not FACCEPTONCE, it's already NOFDREF */
494 }
495 so->s = s;
496
497 so->so_iptos = tcp_tos(so);
498 tp = sototcpcb(so);
499
500 tcp_template(tp);
501
502 /* Compute window scaling to request. */
503/* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
504 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
505 * tp->request_r_scale++;
506 */
507
508/* soisconnecting(so); */ /* NOFDREF used instead */
509 tcpstat.tcps_connattempt++;
510
511 tp->t_state = TCPS_SYN_SENT;
512 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
513 tp->iss = tcp_iss;
514 tcp_iss += TCP_ISSINCR/2;
515 tcp_sendseqinit(tp);
516 tcp_output(pData, tp);
517}
518
519/*
520 * Attach a TCPCB to a socket.
521 */
522int
523tcp_attach(PNATState pData, struct socket *so)
524{
525 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
526 return -1;
527
528 insque(pData, so, &tcb);
529
530 return 0;
531}
532
533/*
534 * Set the socket's type of service field
535 */
536static const struct tos_t tcptos[] = {
537 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
538 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
539 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
540 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
541 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
542 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
543 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
544 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
545 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
546 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
547 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
548 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
549 {0, 0, 0, 0}
550};
551
552/*
553 * Return TOS according to the above table
554 */
555u_int8_t
556tcp_tos(so)
557 struct socket *so;
558{
559 int i = 0;
560
561 while(tcptos[i].tos) {
562 if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
563 (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
564 so->so_emu = tcptos[i].emu;
565 return tcptos[i].tos;
566 }
567 i++;
568 }
569
570 return 0;
571}
572
573/*
574 * Emulate programs that try and connect to us
575 * This includes ftp (the data connection is
576 * initiated by the server) and IRC (DCC CHAT and
577 * DCC SEND) for now
578 *
579 * NOTE: It's possible to crash SLiRP by sending it
580 * unstandard strings to emulate... if this is a problem,
581 * more checks are needed here
582 *
583 * XXX Assumes the whole command came in one packet
584 *
585 * XXX Some ftp clients will have their TOS set to
586 * LOWDELAY and so Nagel will kick in. Because of this,
587 * we'll get the first letter, followed by the rest, so
588 * we simply scan for ORT instead of PORT...
589 * DCC doesn't have this problem because there's other stuff
590 * in the packet before the DCC command.
591 *
592 * Return 1 if the mbuf m is still valid and should be
593 * sbappend()ed
594 *
595 * NOTE: if you return 0 you MUST m_free() the mbuf!
596 */
597int
598tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
599{
600 u_int n1, n2, n3, n4, n5, n6;
601 char buff[256];
602 u_int32_t laddr;
603 u_int lport;
604 char *bptr;
605
606 DEBUG_CALL("tcp_emu");
607 DEBUG_ARG("so = %lx", (long)so);
608 DEBUG_ARG("m = %lx", (long)m);
609
610 switch(so->so_emu) {
611 int x, i;
612
613 case EMU_IDENT:
614 /*
615 * Identification protocol as per rfc-1413
616 */
617
618 {
619 struct socket *tmpso;
620 struct sockaddr_in addr;
621 socklen_t addrlen = sizeof(struct sockaddr_in);
622 struct sbuf *so_rcv = &so->so_rcv;
623
624 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
625 so_rcv->sb_wptr += m->m_len;
626 so_rcv->sb_rptr += m->m_len;
627 m->m_data[m->m_len] = 0; /* NULL terminate */
628 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
629 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
630 HTONS(n1);
631 HTONS(n2);
632 /* n2 is the one on our host */
633 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
634 if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
635 tmpso->so_lport == n2 &&
636 tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
637 tmpso->so_fport == n1) {
638 if (getsockname(tmpso->s,
639 (struct sockaddr *)&addr, &addrlen) == 0)
640 n2 = ntohs(addr.sin_port);
641 break;
642 }
643 }
644 }
645 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
646 so_rcv->sb_rptr = so_rcv->sb_data;
647 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
648 }
649 m_free(pData, m);
650 return 0;
651 }
652
653#if 0
654 case EMU_RLOGIN:
655 /*
656 * Rlogin emulation
657 * First we accumulate all the initial option negotiation,
658 * then fork_exec() rlogin according to the options
659 */
660 {
661 int i, i2, n;
662 char *ptr;
663 char args[100];
664 char term[100];
665 struct sbuf *so_snd = &so->so_snd;
666 struct sbuf *so_rcv = &so->so_rcv;
667
668 /* First check if they have a priveladged port, or too much data has arrived */
669 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
670 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
671 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
672 so_snd->sb_wptr += 18;
673 so_snd->sb_cc += 18;
674 tcp_sockclosed(sototcpcb(so));
675 m_free(m);
676 return 0;
677 }
678
679 /* Append the current data */
680 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
681 so_rcv->sb_wptr += m->m_len;
682 so_rcv->sb_rptr += m->m_len;
683 m_free(m);
684
685 /*
686 * Check if we have all the initial options,
687 * and build argument list to rlogin while we're here
688 */
689 n = 0;
690 ptr = so_rcv->sb_data;
691 args[0] = 0;
692 term[0] = 0;
693 while (ptr < so_rcv->sb_wptr) {
694 if (*ptr++ == 0) {
695 n++;
696 if (n == 2) {
697 sprintf(args, "rlogin -l %s %s",
698 ptr, inet_ntoa(so->so_faddr));
699 } else if (n == 3) {
700 i2 = so_rcv->sb_wptr - ptr;
701 for (i = 0; i < i2; i++) {
702 if (ptr[i] == '/') {
703 ptr[i] = 0;
704#ifdef HAVE_SETENV
705 sprintf(term, "%s", ptr);
706#else
707 sprintf(term, "TERM=%s", ptr);
708#endif
709 ptr[i] = '/';
710 break;
711 }
712 }
713 }
714 }
715 }
716
717 if (n != 4)
718 return 0;
719
720 /* We have it, set our term variable and fork_exec() */
721#ifdef HAVE_SETENV
722 setenv("TERM", term, 1);
723#else
724 putenv(term);
725#endif
726 fork_exec(so, args, 2);
727 term[0] = 0;
728 so->so_emu = 0;
729
730 /* And finally, send the client a 0 character */
731 so_snd->sb_wptr[0] = 0;
732 so_snd->sb_wptr++;
733 so_snd->sb_cc++;
734
735 return 0;
736 }
737
738 case EMU_RSH:
739 /*
740 * rsh emulation
741 * First we accumulate all the initial option negotiation,
742 * then rsh_exec() rsh according to the options
743 */
744 {
745 int n;
746 char *ptr;
747 char *user;
748 char *args;
749 struct sbuf *so_snd = &so->so_snd;
750 struct sbuf *so_rcv = &so->so_rcv;
751
752 /* First check if they have a priveladged port, or too much data has arrived */
753 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
754 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
755 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
756 so_snd->sb_wptr += 18;
757 so_snd->sb_cc += 18;
758 tcp_sockclosed(sototcpcb(so));
759 m_free(m);
760 return 0;
761 }
762
763 /* Append the current data */
764 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
765 so_rcv->sb_wptr += m->m_len;
766 so_rcv->sb_rptr += m->m_len;
767 m_free(m);
768
769 /*
770 * Check if we have all the initial options,
771 * and build argument list to rlogin while we're here
772 */
773 n = 0;
774 ptr = so_rcv->sb_data;
775 user="";
776 args="";
777 if (so->extra==NULL) {
778 struct socket *ns;
779 struct tcpcb* tp;
780 int port=atoi(ptr);
781 if (port <= 0) return 0;
782 if (port > 1023 || port < 512) {
783 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
784 so_snd->sb_wptr += 18;
785 so_snd->sb_cc += 18;
786 tcp_sockclosed(sototcpcb(so));
787 return 0;
788 }
789 if ((ns=socreate()) == NULL)
790 return 0;
791 if (tcp_attach(ns)<0) {
792 free(ns);
793 return 0;
794 }
795
796 ns->so_laddr=so->so_laddr;
797 ns->so_lport=htons(port);
798
799 (void) tcp_mss(sototcpcb(ns), 0);
800
801 ns->so_faddr=so->so_faddr;
802 ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
803
804 if (ns->so_faddr.s_addr == 0 ||
805 ns->so_faddr.s_addr == loopback_addr.s_addr)
806 ns->so_faddr = alias_addr;
807
808 ns->so_iptos = tcp_tos(ns);
809 tp = sototcpcb(ns);
810
811 tcp_template(tp);
812
813 /* Compute window scaling to request. */
814 /* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
815 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
816 * tp->request_r_scale++;
817 */
818
819 /*soisfconnecting(ns);*/
820
821 tcpstat.tcps_connattempt++;
822
823 tp->t_state = TCPS_SYN_SENT;
824 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
825 tp->iss = tcp_iss;
826 tcp_iss += TCP_ISSINCR/2;
827 tcp_sendseqinit(tp);
828 tcp_output(tp);
829 so->extra=ns;
830 }
831 while (ptr < so_rcv->sb_wptr) {
832 if (*ptr++ == 0) {
833 n++;
834 if (n == 2) {
835 user=ptr;
836 } else if (n == 3) {
837 args=ptr;
838 }
839 }
840 }
841
842 if (n != 4)
843 return 0;
844
845 rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
846 so->so_emu = 0;
847 so->extra=NULL;
848
849 /* And finally, send the client a 0 character */
850 so_snd->sb_wptr[0] = 0;
851 so_snd->sb_wptr++;
852 so_snd->sb_cc++;
853
854 return 0;
855 }
856
857 case EMU_CTL:
858 {
859 int num;
860 struct sbuf *so_snd = &so->so_snd;
861 struct sbuf *so_rcv = &so->so_rcv;
862
863 /*
864 * If there is binary data here, we save it in so->so_m
865 */
866 if (!so->so_m) {
867 int rxlen;
868 char *rxdata;
869 rxdata=mtod(m, char *);
870 for (rxlen=m->m_len; rxlen; rxlen--) {
871 if (*rxdata++ & 0x80) {
872 so->so_m = m;
873 return 0;
874 }
875 }
876 } /* if(so->so_m==NULL) */
877
878 /*
879 * Append the line
880 */
881 sbappendsb(so_rcv, m);
882
883 /* To avoid going over the edge of the buffer, we reset it */
884 if (so_snd->sb_cc == 0)
885 so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
886
887 /*
888 * A bit of a hack:
889 * If the first packet we get here is 1 byte long, then it
890 * was done in telnet character mode, therefore we must echo
891 * the characters as they come. Otherwise, we echo nothing,
892 * because in linemode, the line is already echoed
893 * XXX two or more control connections won't work
894 */
895 if (do_echo == -1) {
896 if (m->m_len == 1) do_echo = 1;
897 else do_echo = 0;
898 }
899 if (do_echo) {
900 sbappendsb(so_snd, m);
901 m_free(m);
902 tcp_output(sototcpcb(so)); /* XXX */
903 } else
904 m_free(m);
905
906 num = 0;
907 while (num < so->so_rcv.sb_cc) {
908 if (*(so->so_rcv.sb_rptr + num) == '\n' ||
909 *(so->so_rcv.sb_rptr + num) == '\r') {
910 int n;
911
912 *(so_rcv->sb_rptr + num) = 0;
913 if (ctl_password && !ctl_password_ok) {
914 /* Need a password */
915 if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
916 if (strcmp(buff, ctl_password) == 0) {
917 ctl_password_ok = 1;
918 n = sprintf(so_snd->sb_wptr,
919 "Password OK.\r\n");
920 goto do_prompt;
921 }
922 }
923 n = sprintf(so_snd->sb_wptr,
924 "Error: Password required, log on with \"pass PASSWORD\"\r\n");
925 goto do_prompt;
926 }
927 cfg_quitting = 0;
928 n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
929 if (!cfg_quitting) {
930 /* Register the printed data */
931do_prompt:
932 so_snd->sb_cc += n;
933 so_snd->sb_wptr += n;
934 /* Add prompt */
935 n = sprintf(so_snd->sb_wptr, "Slirp> ");
936 so_snd->sb_cc += n;
937 so_snd->sb_wptr += n;
938 }
939 /* Drop so_rcv data */
940 so_rcv->sb_cc = 0;
941 so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
942 tcp_output(sototcpcb(so)); /* Send the reply */
943 }
944 num++;
945 }
946 return 0;
947 }
948#endif
949 case EMU_FTP: /* ftp */
950 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
951 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
952 /*
953 * Need to emulate the PORT command
954 */
955 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
956 &n1, &n2, &n3, &n4, &n5, &n6, buff);
957 if (x < 6)
958 return 1;
959
960 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
961 lport = htons((n5 << 8) | (n6));
962
963 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
964 return 1;
965
966 n6 = ntohs(so->so_fport);
967
968 n5 = (n6 >> 8) & 0xff;
969 n6 &= 0xff;
970
971 laddr = ntohl(so->so_faddr.s_addr);
972
973 n1 = ((laddr >> 24) & 0xff);
974 n2 = ((laddr >> 16) & 0xff);
975 n3 = ((laddr >> 8) & 0xff);
976 n4 = (laddr & 0xff);
977
978 m->m_len = bptr - m->m_data; /* Adjust length */
979 m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
980 n1, n2, n3, n4, n5, n6, x==7?buff:"");
981 return 1;
982 } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
983 /*
984 * Need to emulate the PASV response
985 */
986 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
987 &n1, &n2, &n3, &n4, &n5, &n6, buff);
988 if (x < 6)
989 return 1;
990
991 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
992 lport = htons((n5 << 8) | (n6));
993
994 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
995 return 1;
996
997 n6 = ntohs(so->so_fport);
998
999 n5 = (n6 >> 8) & 0xff;
1000 n6 &= 0xff;
1001
1002 laddr = ntohl(so->so_faddr.s_addr);
1003
1004 n1 = ((laddr >> 24) & 0xff);
1005 n2 = ((laddr >> 16) & 0xff);
1006 n3 = ((laddr >> 8) & 0xff);
1007 n4 = (laddr & 0xff);
1008
1009 m->m_len = bptr - m->m_data; /* Adjust length */
1010 m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
1011 n1, n2, n3, n4, n5, n6, x==7?buff:"");
1012
1013 return 1;
1014 }
1015
1016 return 1;
1017
1018 case EMU_KSH:
1019 /*
1020 * The kshell (Kerberos rsh) and shell services both pass
1021 * a local port port number to carry signals to the server
1022 * and stderr to the client. It is passed at the beginning
1023 * of the connection as a NUL-terminated decimal ASCII string.
1024 */
1025 so->so_emu = 0;
1026 for (lport = 0, i = 0; i < m->m_len-1; ++i) {
1027 if (m->m_data[i] < '0' || m->m_data[i] > '9')
1028 return 1; /* invalid number */
1029 lport *= 10;
1030 lport += m->m_data[i] - '0';
1031 }
1032 if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
1033 (so = solisten(pData, 0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1034 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
1035 return 1;
1036
1037 case EMU_IRC:
1038 /*
1039 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
1040 */
1041 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
1042 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
1043 return 1;
1044
1045 /* The %256s is for the broken mIRC */
1046 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1047 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1048 return 1;
1049
1050 m->m_len = bptr - m->m_data; /* Adjust length */
1051 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1052 (unsigned long)ntohl(so->so_faddr.s_addr),
1053 ntohs(so->so_fport), 1);
1054 } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1055 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1056 return 1;
1057
1058 m->m_len = bptr - m->m_data; /* Adjust length */
1059 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
1060 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1061 ntohs(so->so_fport), n1, 1);
1062 } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1063 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1064 return 1;
1065
1066 m->m_len = bptr - m->m_data; /* Adjust length */
1067 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1068 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1069 ntohs(so->so_fport), n1, 1);
1070 }
1071 return 1;
1072
1073#ifdef VBOX
1074 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
1075 * This is not legal when more than one slirp instance is active. */
1076#else /* !VBOX */
1077 case EMU_REALAUDIO:
1078 /*
1079 * RealAudio emulation - JP. We must try to parse the incoming
1080 * data and try to find the two characters that contain the
1081 * port number. Then we redirect an udp port and replace the
1082 * number with the real port we got.
1083 *
1084 * The 1.0 beta versions of the player are not supported
1085 * any more.
1086 *
1087 * A typical packet for player version 1.0 (release version):
1088 *
1089 * 0000:50 4E 41 00 05
1090 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
1091 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
1092 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
1093 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
1094 *
1095 * Now the port number 0x1BD7 is found at offset 0x04 of the
1096 * Now the port number 0x1BD7 is found at offset 0x04 of the
1097 * second packet. This time we received five bytes first and
1098 * then the rest. You never know how many bytes you get.
1099 *
1100 * A typical packet for player version 2.0 (beta):
1101 *
1102 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
1103 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
1104 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
1105 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
1106 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
1107 *
1108 * Port number 0x1BC1 is found at offset 0x0d.
1109 *
1110 * This is just a horrible switch statement. Variable ra tells
1111 * us where we're going.
1112 */
1113
1114 bptr = m->m_data;
1115 while (bptr < m->m_data + m->m_len) {
1116 u_short p;
1117 static int ra = 0;
1118 char ra_tbl[4];
1119
1120 ra_tbl[0] = 0x50;
1121 ra_tbl[1] = 0x4e;
1122 ra_tbl[2] = 0x41;
1123 ra_tbl[3] = 0;
1124
1125 switch (ra) {
1126 case 0:
1127 case 2:
1128 case 3:
1129 if (*bptr++ != ra_tbl[ra]) {
1130 ra = 0;
1131 continue;
1132 }
1133 break;
1134
1135 case 1:
1136 /*
1137 * We may get 0x50 several times, ignore them
1138 */
1139 if (*bptr == 0x50) {
1140 ra = 1;
1141 bptr++;
1142 continue;
1143 } else if (*bptr++ != ra_tbl[ra]) {
1144 ra = 0;
1145 continue;
1146 }
1147 break;
1148
1149 case 4:
1150 /*
1151 * skip version number
1152 */
1153 bptr++;
1154 break;
1155
1156 case 5:
1157 /*
1158 * The difference between versions 1.0 and
1159 * 2.0 is here. For future versions of
1160 * the player this may need to be modified.
1161 */
1162 if (*(bptr + 1) == 0x02)
1163 bptr += 8;
1164 else
1165 bptr += 4;
1166 break;
1167
1168 case 6:
1169 /* This is the field containing the port
1170 * number that RA-player is listening to.
1171 */
1172 lport = (((u_char*)bptr)[0] << 8)
1173 + ((u_char *)bptr)[1];
1174 if (lport < 6970)
1175 lport += 256; /* don't know why */
1176 if (lport < 6970 || lport > 7170)
1177 return 1; /* failed */
1178
1179 /* try to get udp port between 6970 - 7170 */
1180 for (p = 6970; p < 7071; p++) {
1181 if (udp_listen( htons(p),
1182 so->so_laddr.s_addr,
1183 htons(lport),
1184 SS_FACCEPTONCE)) {
1185 break;
1186 }
1187 }
1188 if (p == 7071)
1189 p = 0;
1190 *(u_char *)bptr++ = (p >> 8) & 0xff;
1191 *(u_char *)bptr++ = p & 0xff;
1192 ra = 0;
1193 return 1; /* port redirected, we're done */
1194 break;
1195
1196 default:
1197 ra = 0;
1198 }
1199 ra++;
1200 }
1201 return 1;
1202#endif /* !VBOX */
1203
1204 default:
1205 /* Ooops, not emulated, won't call tcp_emu again */
1206 so->so_emu = 0;
1207 return 1;
1208 }
1209}
1210
1211/*
1212 * Do misc. config of SLiRP while its running.
1213 * Return 0 if this connections is to be closed, 1 otherwise,
1214 * return 2 if this is a command-line connection
1215 */
1216int
1217tcp_ctl(PNATState pData, struct socket *so)
1218{
1219 struct sbuf *sb = &so->so_snd;
1220 int command;
1221 struct ex_list *ex_ptr;
1222 int do_pty;
1223 /* struct socket *tmpso; */
1224
1225 DEBUG_CALL("tcp_ctl");
1226 DEBUG_ARG("so = %lx", (long )so);
1227
1228#if 0
1229 /*
1230 * Check if they're authorised
1231 */
1232 if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
1233 sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
1234 sb->sb_wptr += sb->sb_cc;
1235 return 0;
1236 }
1237#endif
1238 command = (ntohl(so->so_faddr.s_addr) & 0xff);
1239
1240 switch(command) {
1241 default: /* Check for exec's */
1242
1243 /*
1244 * Check if it's pty_exec
1245 */
1246 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1247 if (ex_ptr->ex_fport == so->so_fport &&
1248 command == ex_ptr->ex_addr) {
1249 do_pty = ex_ptr->ex_pty;
1250 goto do_exec;
1251 }
1252 }
1253
1254 /*
1255 * Nothing bound..
1256 */
1257 /* tcp_fconnect(so); */
1258
1259 /* FALLTHROUGH */
1260 case CTL_ALIAS:
1261 sb->sb_cc = sprintf(sb->sb_wptr,
1262 "Error: No application configured.\r\n");
1263 sb->sb_wptr += sb->sb_cc;
1264 return(0);
1265
1266 do_exec:
1267 DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
1268 return(fork_exec(pData, so, ex_ptr->ex_exec, do_pty));
1269
1270#if 0
1271 case CTL_CMD:
1272 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
1273 if (tmpso->so_emu == EMU_CTL &&
1274 !(tmpso->so_tcpcb?
1275 (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
1276 :0)) {
1277 /* Ooops, control connection already active */
1278 sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
1279 sb->sb_wptr += sb->sb_cc;
1280 return 0;
1281 }
1282 }
1283 so->so_emu = EMU_CTL;
1284 ctl_password_ok = 0;
1285 sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
1286 sb->sb_wptr += sb->sb_cc;
1287 do_echo=-1;
1288 return(2);
1289#endif
1290 }
1291}
1292
1293#if SIZEOF_CHAR_P != 4
1294/**
1295 * Slow pointer hashing that deals with automatic inserting and collisions.
1296 */
1297uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
1298{
1299 uint32_t i;
1300 if (pv == NULL)
1301 i = 0;
1302 else
1303 {
1304 const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1305 if (pData->apvHash[i1] == pv)
1306 i = i1;
1307 else
1308 {
1309 /*
1310 * Try up to 10 times then assume it's an insertion.
1311 * If we didn't find a free entry by then, try another 100 times.
1312 * If that fails, give up.
1313 */
1314 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1315 uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
1316 int cTries = 10;
1317 int cTries2 = 100;
1318
1319 i = i1;
1320 for (;;)
1321 {
1322 /* check if we should give in.*/
1323 if (--cTries > 0)
1324 {
1325 if (i1stFree != 0)
1326 {
1327 i = i1stFree;
1328 pData->apvHash[i] = pv;
1329 pData->cpvHashUsed++;
1330 if (i != i1)
1331 pData->cpvHashCollisions++;
1332 pData->cpvHashInserts++;
1333 break;
1334 }
1335 if (!cTries2)
1336 {
1337 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
1338 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1339 i = 0;
1340 break;
1341 }
1342 cTries = cTries2;
1343 cTries2 = 0;
1344 }
1345
1346 /* advance to the next hash entry and test it. */
1347 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1348 while (RT_UNLIKELY(!i))
1349 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1350 if (pData->apvHash[i] == pv)
1351 break;
1352 if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
1353 i1stFree = i;
1354 }
1355 }
1356 }
1357 return i;
1358}
1359
1360
1361/**
1362 * Removes the pointer from the hash table.
1363 */
1364void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
1365{
1366 /* We don't count NULL pointers. */
1367 if (pv == NULL)
1368 return;
1369 pData->cpvHashDone++;
1370
1371 /* try the hint */
1372 if ( iHint
1373 && iHint < RT_ELEMENTS(pData->apvHash)
1374 && pData->apvHash[iHint] == pv)
1375 {
1376 pData->apvHash[iHint] = NULL;
1377 pData->cpvHashUsed--;
1378 return;
1379 }
1380
1381 iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1382 if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
1383 {
1384 /*
1385 * Try up to 120 times then assert.
1386 */
1387 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1388 int cTries = 120;
1389 for (;;)
1390 {
1391 /* advance to the next hash entry and test it. */
1392 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1393 while (RT_UNLIKELY(!iHint))
1394 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1395 if (pData->apvHash[iHint] == pv)
1396 break;
1397
1398 /* check if we should give in.*/
1399 if (--cTries > 0)
1400 {
1401 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
1402 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1403 return;
1404 }
1405 }
1406 }
1407
1408 /* found it */
1409 pData->apvHash[iHint] = NULL;
1410 pData->cpvHashUsed--;
1411}
1412
1413#endif
Note: See TracBrowser for help on using the repository browser.

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