VirtualBox

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

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

heade of list of TCPCB was added and initialization code was injected

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