VirtualBox

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

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

The rest of macro replacment was added

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