VirtualBox

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

最後變更 在這個檔案從15450是 15450,由 vboxsync 提交於 16 年 前

slirp: style, dead code

  • 屬性 svn:eol-style 設為 native
檔案大小: 35.1 KB
 
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#ifdef VBOX_WITH_BSD_REASS
59 tcp_reass_maxqlen = 48;
60 tcp_reass_maxseg = 256;
61#endif /* VBOX_WITH_BSD_REASS */
62}
63
64/*
65 * Create template to be used to send tcp packets on a connection.
66 * Call after host entry created, fills
67 * in a skeletal tcp/ip header, minimizing the amount of work
68 * necessary when the connection is used.
69 */
70/* struct tcpiphdr * */
71void
72tcp_template(struct tcpcb *tp)
73{
74 struct socket *so = tp->t_socket;
75 register struct tcpiphdr *n = &tp->t_template;
76
77#if !defined(VBOX_WITH_BSD_REASS)
78 n->ti_next = n->ti_prev = 0;
79 n->ti_x1 = 0;
80#else
81 memset(n->ti_x1, 0, 9);
82#endif
83 n->ti_pr = IPPROTO_TCP;
84 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
85 n->ti_src = so->so_faddr;
86 n->ti_dst = so->so_laddr;
87 n->ti_sport = so->so_fport;
88 n->ti_dport = so->so_lport;
89
90 n->ti_seq = 0;
91 n->ti_ack = 0;
92 n->ti_x2 = 0;
93 n->ti_off = 5;
94 n->ti_flags = 0;
95 n->ti_win = 0;
96 n->ti_sum = 0;
97 n->ti_urp = 0;
98}
99
100/*
101 * Send a single message to the TCP at address specified by
102 * the given TCP/IP header. If m == 0, then we make a copy
103 * of the tcpiphdr at ti and send directly to the addressed host.
104 * This is used to force keep alive messages out using the TCP
105 * template for a connection tp->t_template. If flags are given
106 * then we send a message back to the TCP which originated the
107 * segment ti, and discard the mbuf containing it and any other
108 * attached mbufs.
109 *
110 * In any case the ack and sequence number of the transmitted
111 * segment are as specified by the parameters.
112 */
113void
114tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
115{
116 register int tlen;
117 int win = 0;
118
119 DEBUG_CALL("tcp_respond");
120 DEBUG_ARG("tp = %lx", (long)tp);
121 DEBUG_ARG("ti = %lx", (long)ti);
122 DEBUG_ARG("m = %lx", (long)m);
123 DEBUG_ARG("ack = %u", ack);
124 DEBUG_ARG("seq = %u", seq);
125 DEBUG_ARG("flags = %x", flags);
126
127 if (tp)
128 win = sbspace(&tp->t_socket->so_rcv);
129 if (m == 0)
130 {
131 if ((m = m_get(pData)) == NULL)
132 return;
133#ifdef TCP_COMPAT_42
134 tlen = 1;
135#else
136 tlen = 0;
137#endif
138 m->m_data += if_maxlinkhdr;
139 *mtod(m, struct tcpiphdr *) = *ti;
140 ti = mtod(m, struct tcpiphdr *);
141 flags = TH_ACK;
142 }
143 else
144 {
145 /*
146 * ti points into m so the next line is just making
147 * the mbuf point to ti
148 */
149 m->m_data = (caddr_t)ti;
150
151 m->m_len = sizeof (struct tcpiphdr);
152 tlen = 0;
153#define xchg(a,b,type) { type t; t = a; a = b; b = t; }
154 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
155 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
156#undef xchg
157 }
158 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
159 tlen += sizeof (struct tcpiphdr);
160 m->m_len = tlen;
161
162#if !defined(VBOX_WITH_BSD_REASS)
163 ti->ti_next = ti->ti_prev = 0;
164 ti->ti_x1 = 0;
165#else
166 memset(ti->ti_x1, 0, 9);
167#endif
168 ti->ti_seq = htonl(seq);
169 ti->ti_ack = htonl(ack);
170 ti->ti_x2 = 0;
171 ti->ti_off = sizeof (struct tcphdr) >> 2;
172 ti->ti_flags = flags;
173 if (tp)
174 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
175 else
176 ti->ti_win = htons((u_int16_t)win);
177 ti->ti_urp = 0;
178 ti->ti_sum = 0;
179 ti->ti_sum = cksum(m, tlen);
180 ((struct ip *)ti)->ip_len = tlen;
181
182 if(flags & TH_RST)
183 ((struct ip *)ti)->ip_ttl = MAXTTL;
184 else
185 ((struct ip *)ti)->ip_ttl = ip_defttl;
186
187 (void) ip_output(pData, (struct socket *)0, m);
188}
189
190/*
191 * Create a new TCP control block, making an
192 * empty reassembly queue and hooking it to the argument
193 * protocol control block.
194 */
195struct tcpcb *
196tcp_newtcpcb(PNATState pData, struct socket *so)
197{
198 register struct tcpcb *tp;
199
200 tp = (struct tcpcb *)malloc(sizeof(*tp));
201 if (tp == NULL)
202 return ((struct tcpcb *)0);
203
204 memset((char *) tp, 0, sizeof(struct tcpcb));
205#ifndef VBOX_WITH_BSD_REASS
206 tp->seg_next = tp->seg_prev = ptr_to_u32(pData, (struct tcpiphdr *)tp);
207#endif /* !VBOX_WITH_BSD_REASS */
208 tp->t_maxseg = tcp_mssdflt;
209
210 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
211 tp->t_socket = so;
212
213 /*
214 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
215 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
216 * reasonable initial retransmit time.
217 */
218 tp->t_srtt = TCPTV_SRTTBASE;
219 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
220 tp->t_rttmin = TCPTV_MIN;
221
222 TCPT_RANGESET(tp->t_rxtcur,
223 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
224 TCPTV_MIN, TCPTV_REXMTMAX);
225
226 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
227 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
228 tp->t_state = TCPS_CLOSED;
229
230 so->so_tcpcb = tp;
231
232 return (tp);
233}
234
235/*
236 * Drop a TCP connection, reporting
237 * the specified error. If connection is synchronized,
238 * then send a RST to peer.
239 */
240struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
241{
242/* tcp_drop(tp, errno)
243 register struct tcpcb *tp;
244 int errno;
245{
246*/
247 DEBUG_CALL("tcp_drop");
248 DEBUG_ARG("tp = %lx", (long)tp);
249 DEBUG_ARG("errno = %d", errno);
250
251 if (TCPS_HAVERCVDSYN(tp->t_state))
252 {
253 tp->t_state = TCPS_CLOSED;
254 (void) tcp_output(pData, tp);
255 tcpstat.tcps_drops++;
256 }
257 else
258 tcpstat.tcps_conndrops++;
259#if 0
260 if (errno == ETIMEDOUT && tp->t_softerror)
261 errno = tp->t_softerror;
262
263 so->so_error = errno;
264#endif
265 return (tcp_close(pData, tp));
266}
267
268/*
269 * Close a TCP control block:
270 * discard all space held by the tcp
271 * discard internet protocol block
272 * wake up any sleepers
273 */
274struct tcpcb *
275tcp_close(PNATState pData, register struct tcpcb *tp)
276{
277 register struct tcpiphdr *t;
278 struct socket *so = tp->t_socket;
279 register struct mbuf *m;
280
281#ifndef VBOX_WITH_BSD_REASS
282 DEBUG_CALL("tcp_close");
283 DEBUG_ARG("tp = %lx", (long )tp);
284
285 /* free the reassembly queue, if any */
286 t = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *);
287 while (t != (struct tcpiphdr *)tp)
288 {
289 t = u32_to_ptr(pData, t->ti_next, struct tcpiphdr *);
290 m = REASS_MBUF_GET(u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
291 remque_32(pData, u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
292 m_freem(pData, m);
293 }
294 u32ptr_done(pData, ptr_to_u32(pData, tp), tp);
295#else /* VBOX_WITH_BSD_REASS */
296 struct tseg_qent *te;
297 DEBUG_CALL("tcp_close");
298 DEBUG_ARG("tp = %lx", (long )tp);
299 /*XXX: freeing the reassembly queue */
300 LIST_FOREACH(te, &tp->t_segq, tqe_q)
301 {
302 LIST_REMOVE(te, tqe_q);
303 m_freem(pData, te->tqe_m);
304 free(te);
305 tcp_reass_qsize--;
306 }
307#endif /* VBOX_WITH_BSD_REASS */
308 free(tp);
309 so->so_tcpcb = 0;
310 soisfdisconnected(so);
311 /* clobber input socket cache if we're closing the cached connection */
312 if (so == tcp_last_so)
313 tcp_last_so = &tcb;
314 closesocket(so->s);
315 sbfree(&so->so_rcv);
316 sbfree(&so->so_snd);
317 sofree(pData, so);
318 tcpstat.tcps_closed++;
319 return ((struct tcpcb *)0);
320}
321
322void
323tcp_drain()
324{
325 /* XXX */
326}
327
328/*
329 * When a source quench is received, close congestion window
330 * to one segment. We will gradually open it again as we proceed.
331 */
332
333#if 0
334
335void
336tcp_quench(i, int errno)
337{
338 struct tcpcb *tp = intotcpcb(inp);
339
340 if (tp)
341 tp->snd_cwnd = tp->t_maxseg;
342}
343
344#endif
345
346/*
347 * TCP protocol interface to socket abstraction.
348 */
349
350/*
351 * User issued close, and wish to trail through shutdown states:
352 * if never received SYN, just forget it. If got a SYN from peer,
353 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
354 * If already got a FIN from peer, then almost done; go to LAST_ACK
355 * state. In all other cases, have already sent FIN to peer (e.g.
356 * after PRU_SHUTDOWN), and just have to play tedious game waiting
357 * for peer to send FIN or not respond to keep-alives, etc.
358 * We can let the user exit from the close as soon as the FIN is acked.
359 */
360void
361tcp_sockclosed(PNATState pData, struct tcpcb *tp)
362{
363 DEBUG_CALL("tcp_sockclosed");
364 DEBUG_ARG("tp = %lx", (long)tp);
365
366 switch (tp->t_state)
367 {
368 case TCPS_CLOSED:
369 case TCPS_LISTEN:
370 case TCPS_SYN_SENT:
371 tp->t_state = TCPS_CLOSED;
372 tp = tcp_close(pData, tp);
373 break;
374
375 case TCPS_SYN_RECEIVED:
376 case TCPS_ESTABLISHED:
377 tp->t_state = TCPS_FIN_WAIT_1;
378 break;
379
380 case TCPS_CLOSE_WAIT:
381 tp->t_state = TCPS_LAST_ACK;
382 break;
383 }
384/* soisfdisconnecting(tp->t_socket); */
385 if ( tp
386 && tp->t_state >= TCPS_FIN_WAIT_2)
387 soisfdisconnected(tp->t_socket);
388 if (tp)
389 tcp_output(pData, tp);
390}
391
392/*
393 * Connect to a host on the Internet
394 * Called by tcp_input
395 * Only do a connect, the tcp fields will be set in tcp_input
396 * return 0 if there's a result of the connect,
397 * else return -1 means we're still connecting
398 * The return value is almost always -1 since the socket is
399 * nonblocking. Connect returns after the SYN is sent, and does
400 * not wait for ACK+SYN.
401 */
402int tcp_fconnect(PNATState pData, struct socket *so)
403{
404 int ret = 0;
405
406 DEBUG_CALL("tcp_fconnect");
407 DEBUG_ARG("so = %lx", (long )so);
408
409 if ((ret = so->s = socket(AF_INET,SOCK_STREAM,0)) >= 0)
410 {
411 int opt, s = so->s;
412 struct sockaddr_in addr;
413
414 fd_nonblock(s);
415 opt = 1;
416 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt, sizeof(opt));
417 opt = 1;
418 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt, sizeof(opt));
419
420 addr.sin_family = AF_INET;
421 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
422 {
423 /* It's an alias */
424 switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask)
425 {
426 case CTL_DNS:
427 if (!get_dns_addr(pData, &dns_addr))
428 addr.sin_addr = dns_addr;
429 else
430 addr.sin_addr = loopback_addr;
431 break;
432 case CTL_ALIAS:
433 default:
434 addr.sin_addr = loopback_addr;
435 break;
436 }
437 }
438 else
439 addr.sin_addr = so->so_faddr;
440 addr.sin_port = so->so_fport;
441
442 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
443 "addr.sin_addr.s_addr=%.16s\n",
444 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
445 /* We don't care what port we get */
446 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
447
448 /*
449 * If it's not in progress, it failed, so we just return 0,
450 * without clearing SS_NOFDREF
451 */
452 soisfconnecting(so);
453 }
454
455 return(ret);
456}
457
458/*
459 * Accept the socket and connect to the local-host
460 *
461 * We have a problem. The correct thing to do would be
462 * to first connect to the local-host, and only if the
463 * connection is accepted, then do an accept() here.
464 * But, a) we need to know who's trying to connect
465 * to the socket to be able to SYN the local-host, and
466 * b) we are already connected to the foreign host by
467 * the time it gets to accept(), so... We simply accept
468 * here and SYN the local-host.
469 */
470void
471tcp_connect(PNATState pData, struct socket *inso)
472{
473 struct socket *so;
474 struct sockaddr_in addr;
475 socklen_t addrlen = sizeof(struct sockaddr_in);
476 struct tcpcb *tp;
477 int s, opt;
478
479 DEBUG_CALL("tcp_connect");
480 DEBUG_ARG("inso = %lx", (long)inso);
481
482 /*
483 * If it's an SS_ACCEPTONCE socket, no need to socreate()
484 * another socket, just use the accept() socket.
485 */
486 if (inso->so_state & SS_FACCEPTONCE)
487 {
488 /* FACCEPTONCE already have a tcpcb */
489 so = inso;
490 }
491 else
492 {
493 if ((so = socreate()) == NULL)
494 {
495 /* If it failed, get rid of the pending connection */
496 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
497 return;
498 }
499 if (tcp_attach(pData, so) < 0)
500 {
501 free(so); /* NOT sofree */
502 return;
503 }
504 so->so_laddr = inso->so_laddr;
505 so->so_lport = inso->so_lport;
506 }
507
508 (void) tcp_mss(pData, sototcpcb(so), 0);
509
510 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0)
511 {
512 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
513 return;
514 }
515 fd_nonblock(s);
516 opt = 1;
517 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
518 opt = 1;
519 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
520 opt = 1;
521 setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
522
523 so->so_fport = addr.sin_port;
524 so->so_faddr = addr.sin_addr;
525 /* Translate connections from localhost to the real hostname */
526 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
527 so->so_faddr = alias_addr;
528
529 /* Close the accept() socket, set right state */
530 if (inso->so_state & SS_FACCEPTONCE)
531 {
532 closesocket(so->s); /* If we only accept once, close the accept() socket */
533 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
534 /* if it's not FACCEPTONCE, it's already NOFDREF */
535 }
536 so->s = s;
537
538 so->so_iptos = tcp_tos(so);
539 tp = sototcpcb(so);
540
541 tcp_template(tp);
542
543 /* Compute window scaling to request. */
544/* while (tp->request_r_scale < TCP_MAX_WINSHIFT
545 * && (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
546 * tp->request_r_scale++;
547 */
548
549/* soisconnecting(so); */ /* NOFDREF used instead */
550 tcpstat.tcps_connattempt++;
551
552 tp->t_state = TCPS_SYN_SENT;
553 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
554 tp->iss = tcp_iss;
555 tcp_iss += TCP_ISSINCR/2;
556 tcp_sendseqinit(tp);
557 tcp_output(pData, tp);
558}
559
560/*
561 * Attach a TCPCB to a socket.
562 */
563int
564tcp_attach(PNATState pData, struct socket *so)
565{
566 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
567 return -1;
568
569 insque(pData, so, &tcb);
570 return 0;
571}
572
573/*
574 * Set the socket's type of service field
575 */
576static const struct tos_t tcptos[] =
577{
578 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
579 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
580 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
581 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
582 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
583 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
584 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
585 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
586 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
587 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
588 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
589 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
590 {0, 0, 0, 0}
591};
592
593/*
594 * Return TOS according to the above table
595 */
596u_int8_t
597tcp_tos(struct socket *so)
598{
599 int i = 0;
600
601 while(tcptos[i].tos)
602 {
603 if ( (tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport))
604 || (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport)))
605 {
606 so->so_emu = tcptos[i].emu;
607 return tcptos[i].tos;
608 }
609 i++;
610 }
611
612 return 0;
613}
614
615/*
616 * Emulate programs that try and connect to us. This includes ftp (the data
617 * connection is initiated by the server) and IRC (DCC CHAT and DCC SEND)
618 * for now
619 *
620 * NOTE: It's possible to crash SLiRP by sending it unstandard strings to
621 * emulate... if this is a problem, more checks are needed here.
622 *
623 * XXX Assumes the whole command cames in one packet
624 *
625 * XXX Some ftp clients will have their TOS set to LOWDELAY and so Nagel will
626 * kick in. Because of this, we'll get the first letter, followed by the
627 * rest, so we simply scan for ORT instead of PORT... DCC doesn't have this
628 * problem because there's other stuff in the packet before the DCC command.
629 *
630 * Return 1 if the mbuf m is still valid and should be sbappend()ed
631 *
632 * NOTE: if you return 0 you MUST m_free() the mbuf!
633 */
634int
635tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
636{
637 u_int n1, n2, n3, n4, n5, n6;
638 char buff[256];
639 u_int32_t laddr;
640 u_int lport;
641 char *bptr;
642
643 DEBUG_CALL("tcp_emu");
644 DEBUG_ARG("so = %lx", (long)so);
645 DEBUG_ARG("m = %lx", (long)m);
646
647 switch(so->so_emu)
648 {
649 int x, i;
650
651 case EMU_IDENT:
652 /*
653 * Identification protocol as per rfc-1413
654 */
655 {
656 struct socket *tmpso;
657 struct sockaddr_in addr;
658 socklen_t addrlen = sizeof(struct sockaddr_in);
659 struct sbuf *so_rcv = &so->so_rcv;
660
661 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
662 so_rcv->sb_wptr += m->m_len;
663 so_rcv->sb_rptr += m->m_len;
664 m->m_data[m->m_len] = 0; /* NULL terminate */
665 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n'))
666 {
667 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2)
668 {
669 HTONS(n1);
670 HTONS(n2);
671 /* n2 is the one on our host */
672 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next)
673 {
674 if ( tmpso->so_laddr.s_addr == so->so_laddr.s_addr
675 && tmpso->so_lport == n2
676 && tmpso->so_faddr.s_addr == so->so_faddr.s_addr
677 && tmpso->so_fport == n1)
678 {
679 if (getsockname(tmpso->s,
680 (struct sockaddr *)&addr, &addrlen) == 0)
681 n2 = ntohs(addr.sin_port);
682 break;
683 }
684 }
685 }
686 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
687 so_rcv->sb_rptr = so_rcv->sb_data;
688 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
689 }
690 m_free(pData, m);
691 return 0;
692 }
693
694 case EMU_FTP:
695 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
696 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL)
697 {
698 /*
699 * Need to emulate the PORT command
700 */
701 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
702 &n1, &n2, &n3, &n4, &n5, &n6, buff);
703 if (x < 6)
704 return 1;
705
706 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
707 lport = htons((n5 << 8) | (n6));
708
709 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
710 return 1;
711
712 n6 = ntohs(so->so_fport);
713
714 n5 = (n6 >> 8) & 0xff;
715 n6 &= 0xff;
716
717 laddr = ntohl(so->so_faddr.s_addr);
718
719 n1 = ((laddr >> 24) & 0xff);
720 n2 = ((laddr >> 16) & 0xff);
721 n3 = ((laddr >> 8) & 0xff);
722 n4 = ( laddr & 0xff);
723
724 m->m_len = bptr - m->m_data; /* Adjust length */
725 m->m_len += sprintf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%s",
726 n1, n2, n3, n4, n5, n6, x==7?buff:"");
727 return 1;
728 }
729 else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL)
730 {
731 /*
732 * Need to emulate the PASV response
733 */
734 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
735 &n1, &n2, &n3, &n4, &n5, &n6, buff);
736 if (x < 6)
737 return 1;
738
739 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
740 lport = htons((n5 << 8) | (n6));
741
742 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
743 return 1;
744
745 n6 = ntohs(so->so_fport);
746
747 n5 = (n6 >> 8) & 0xff;
748 n6 &= 0xff;
749
750 laddr = ntohl(so->so_faddr.s_addr);
751
752 n1 = ((laddr >> 24) & 0xff);
753 n2 = ((laddr >> 16) & 0xff);
754 n3 = ((laddr >> 8) & 0xff);
755 n4 = (laddr & 0xff);
756
757 m->m_len = bptr - m->m_data; /* Adjust length */
758 m->m_len += sprintf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
759 n1, n2, n3, n4, n5, n6, x==7?buff:"");
760
761 return 1;
762 }
763 return 1;
764
765 case EMU_KSH:
766 /*
767 * The kshell (Kerberos rsh) and shell services both pass
768 * a local port port number to carry signals to the server
769 * and stderr to the client. It is passed at the beginning
770 * of the connection as a NUL-terminated decimal ASCII string.
771 */
772 so->so_emu = 0;
773 for (lport = 0, i = 0; i < m->m_len-1; ++i)
774 {
775 if (m->m_data[i] < '0' || m->m_data[i] > '9')
776 return 1; /* invalid number */
777 lport *= 10;
778 lport += m->m_data[i] - '0';
779 }
780 if ( m->m_data[m->m_len-1] == '\0'
781 && lport != 0
782 && (so = solisten(pData, 0, so->so_laddr.s_addr,
783 htons(lport), SS_FACCEPTONCE)) != NULL)
784 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
785 return 1;
786
787 case EMU_IRC:
788 /*
789 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
790 */
791 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
792 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
793 return 1;
794
795 /* The %256s is for the broken mIRC */
796 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3)
797 {
798 if ((so = solisten(pData, 0, htonl(laddr),
799 htons(lport), SS_FACCEPTONCE)) == NULL)
800 return 1;
801
802 m->m_len = bptr - m->m_data; /* Adjust length */
803 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
804 (unsigned long)ntohl(so->so_faddr.s_addr),
805 ntohs(so->so_fport), 1);
806 }
807 else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
808 {
809 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
810 return 1;
811
812 m->m_len = bptr - m->m_data; /* Adjust length */
813 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
814 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
815 ntohs(so->so_fport), n1, 1);
816 }
817 else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
818 {
819 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
820 return 1;
821
822 m->m_len = bptr - m->m_data; /* Adjust length */
823 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
824 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
825 ntohs(so->so_fport), n1, 1);
826 }
827 return 1;
828
829#ifdef VBOX
830 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
831 * This is not legal when more than one slirp instance is active. */
832#else /* !VBOX */
833 case EMU_REALAUDIO:
834 /*
835 * RealAudio emulation - JP. We must try to parse the incoming
836 * data and try to find the two characters that contain the
837 * port number. Then we redirect an udp port and replace the
838 * number with the real port we got.
839 *
840 * The 1.0 beta versions of the player are not supported
841 * any more.
842 *
843 * A typical packet for player version 1.0 (release version):
844 *
845 * 0000:50 4E 41 00 05
846 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
847 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
848 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
849 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
850 *
851 * Now the port number 0x1BD7 is found at offset 0x04 of the
852 * Now the port number 0x1BD7 is found at offset 0x04 of the
853 * second packet. This time we received five bytes first and
854 * then the rest. You never know how many bytes you get.
855 *
856 * A typical packet for player version 2.0 (beta):
857 *
858 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
859 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
860 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
861 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
862 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
863 *
864 * Port number 0x1BC1 is found at offset 0x0d.
865 *
866 * This is just a horrible switch statement. Variable ra tells
867 * us where we're going.
868 */
869
870 bptr = m->m_data;
871 while (bptr < m->m_data + m->m_len)
872 {
873 u_short p;
874 static int ra = 0;
875 char ra_tbl[4];
876
877 ra_tbl[0] = 0x50;
878 ra_tbl[1] = 0x4e;
879 ra_tbl[2] = 0x41;
880 ra_tbl[3] = 0;
881
882 switch (ra)
883 {
884 case 0:
885 case 2:
886 case 3:
887 if (*bptr++ != ra_tbl[ra])
888 {
889 ra = 0;
890 continue;
891 }
892 break;
893
894 case 1:
895 /*
896 * We may get 0x50 several times, ignore them
897 */
898 if (*bptr == 0x50)
899 {
900 ra = 1;
901 bptr++;
902 continue;
903 }
904 else if (*bptr++ != ra_tbl[ra])
905 {
906 ra = 0;
907 continue;
908 }
909 break;
910
911 case 4:
912 /*
913 * skip version number
914 */
915 bptr++;
916 break;
917
918 case 5:
919 /*
920 * The difference between versions 1.0 and
921 * 2.0 is here. For future versions of
922 * the player this may need to be modified.
923 */
924 if (*(bptr + 1) == 0x02)
925 bptr += 8;
926 else
927 bptr += 4;
928 break;
929
930 case 6:
931 /* This is the field containing the port
932 * number that RA-player is listening to.
933 */
934 lport = (((u_char*)bptr)[0] << 8)
935 + ((u_char *)bptr)[1];
936 if (lport < 6970)
937 lport += 256; /* don't know why */
938 if (lport < 6970 || lport > 7170)
939 return 1; /* failed */
940
941 /* try to get udp port between 6970 - 7170 */
942 for (p = 6970; p < 7071; p++)
943 {
944 if (udp_listen(htons(p),
945 so->so_laddr.s_addr,
946 htons(lport),
947 SS_FACCEPTONCE))
948 {
949 break;
950 }
951 }
952 if (p == 7071)
953 p = 0;
954 *(u_char *)bptr++ = (p >> 8) & 0xff;
955 *(u_char *)bptr++ = p & 0xff;
956 ra = 0;
957 return 1; /* port redirected, we're done */
958 break;
959
960 default:
961 ra = 0;
962 }
963 ra++;
964 }
965 return 1;
966#endif /* !VBOX */
967
968 default:
969 /* Ooops, not emulated, won't call tcp_emu again */
970 so->so_emu = 0;
971 return 1;
972 }
973}
974
975#if SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS)
976/**
977 * Slow pointer hashing that deals with automatic inserting and collisions.
978 */
979uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
980{
981 uint32_t i;
982 if (pv == NULL)
983 i = 0;
984 else
985 {
986 const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
987 if (pData->apvHash[i1] == pv)
988 i = i1;
989 else
990 {
991 /*
992 * Try up to 10 times then assume it's an insertion.
993 * If we didn't find a free entry by then, try another 100 times.
994 * If that fails, give up.
995 */
996 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
997 uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
998 int cTries = 10;
999 int cTries2 = 100;
1000
1001 i = i1;
1002 for (;;)
1003 {
1004 /* check if we should give in.*/
1005 if (--cTries > 0)
1006 {
1007 if (i1stFree != 0)
1008 {
1009 i = i1stFree;
1010 pData->apvHash[i] = pv;
1011 pData->cpvHashUsed++;
1012 if (i != i1)
1013 pData->cpvHashCollisions++;
1014 pData->cpvHashInserts++;
1015 break;
1016 }
1017 if (!cTries2)
1018 {
1019 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
1020 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1021 i = 0;
1022 break;
1023 }
1024 cTries = cTries2;
1025 cTries2 = 0;
1026 }
1027
1028 /* advance to the next hash entry and test it. */
1029 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1030 while (RT_UNLIKELY(!i))
1031 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1032 if (pData->apvHash[i] == pv)
1033 break;
1034 if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
1035 i1stFree = i;
1036 }
1037 }
1038 }
1039 return i;
1040}
1041
1042
1043/**
1044 * Removes the pointer from the hash table.
1045 */
1046void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
1047{
1048 /* We don't count NULL pointers. */
1049 if (pv == NULL)
1050 return;
1051 pData->cpvHashDone++;
1052
1053 /* try the hint */
1054 if ( iHint
1055 && iHint < RT_ELEMENTS(pData->apvHash)
1056 && pData->apvHash[iHint] == pv)
1057 {
1058 pData->apvHash[iHint] = NULL;
1059 pData->cpvHashUsed--;
1060 return;
1061 }
1062
1063 iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1064 if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
1065 {
1066 /*
1067 * Try up to 120 times then assert.
1068 */
1069 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1070 int cTries = 120;
1071 for (;;)
1072 {
1073 /* advance to the next hash entry and test it. */
1074 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1075 while (RT_UNLIKELY(!iHint))
1076 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1077 if (pData->apvHash[iHint] == pv)
1078 break;
1079
1080 /* check if we should give in.*/
1081 if (--cTries > 0)
1082 {
1083 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
1084 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1085 return;
1086 }
1087 }
1088 }
1089
1090 /* found it */
1091 pData->apvHash[iHint] = NULL;
1092 pData->cpvHashUsed--;
1093}
1094
1095#endif /* SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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