VirtualBox

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

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

TCP reassembling (compilable, but not working version)

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