VirtualBox

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

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

slirp:typo

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

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