VirtualBox

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

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

slirp: better readability

  • Property svn:eol-style set to native
File size: 35.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#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 /* It's static */
295#if 0
296 if (tp->t_template)
297 (void) m_free(dtom(tp->t_template));
298
299 free(tp, M_PCB);
300#endif
301 u32ptr_done(pData, ptr_to_u32(pData, tp), tp);
302#else /* VBOX_WITH_BSD_REASS */
303 struct tseg_qent *te;
304 DEBUG_CALL("tcp_close");
305 DEBUG_ARG("tp = %lx", (long )tp);
306 /*XXX: freeing the reassembly queue */
307 LIST_FOREACH(te, &tp->t_segq, tqe_q)
308 {
309 LIST_REMOVE(te, tqe_q);
310 m_freem(pData, te->tqe_m);
311 free(te);
312 tcp_reass_qsize--;
313 }
314#endif /* VBOX_WITH_BSD_REASS */
315 free(tp);
316 so->so_tcpcb = 0;
317 soisfdisconnected(so);
318 /* clobber input socket cache if we're closing the cached connection */
319 if (so == tcp_last_so)
320 tcp_last_so = &tcb;
321 closesocket(so->s);
322 sbfree(&so->so_rcv);
323 sbfree(&so->so_snd);
324 sofree(pData, so);
325 tcpstat.tcps_closed++;
326 return ((struct tcpcb *)0);
327}
328
329void
330tcp_drain()
331{
332 /* XXX */
333}
334
335/*
336 * When a source quench is received, close congestion window
337 * to one segment. We will gradually open it again as we proceed.
338 */
339
340#if 0
341
342void
343tcp_quench(i, int errno)
344{
345 struct tcpcb *tp = intotcpcb(inp);
346
347 if (tp)
348 tp->snd_cwnd = tp->t_maxseg;
349}
350
351#endif
352
353/*
354 * TCP protocol interface to socket abstraction.
355 */
356
357/*
358 * User issued close, and wish to trail through shutdown states:
359 * if never received SYN, just forget it. If got a SYN from peer,
360 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
361 * If already got a FIN from peer, then almost done; go to LAST_ACK
362 * state. In all other cases, have already sent FIN to peer (e.g.
363 * after PRU_SHUTDOWN), and just have to play tedious game waiting
364 * for peer to send FIN or not respond to keep-alives, etc.
365 * We can let the user exit from the close as soon as the FIN is acked.
366 */
367void
368tcp_sockclosed(PNATState pData, struct tcpcb *tp)
369{
370 DEBUG_CALL("tcp_sockclosed");
371 DEBUG_ARG("tp = %lx", (long)tp);
372
373 switch (tp->t_state)
374 {
375 case TCPS_CLOSED:
376 case TCPS_LISTEN:
377 case TCPS_SYN_SENT:
378 tp->t_state = TCPS_CLOSED;
379 tp = tcp_close(pData, tp);
380 break;
381
382 case TCPS_SYN_RECEIVED:
383 case TCPS_ESTABLISHED:
384 tp->t_state = TCPS_FIN_WAIT_1;
385 break;
386
387 case TCPS_CLOSE_WAIT:
388 tp->t_state = TCPS_LAST_ACK;
389 break;
390 }
391/* soisfdisconnecting(tp->t_socket); */
392 if ( tp
393 && tp->t_state >= TCPS_FIN_WAIT_2)
394 soisfdisconnected(tp->t_socket);
395 if (tp)
396 tcp_output(pData, tp);
397}
398
399/*
400 * Connect to a host on the Internet
401 * Called by tcp_input
402 * Only do a connect, the tcp fields will be set in tcp_input
403 * return 0 if there's a result of the connect,
404 * else return -1 means we're still connecting
405 * The return value is almost always -1 since the socket is
406 * nonblocking. Connect returns after the SYN is sent, and does
407 * not wait for ACK+SYN.
408 */
409int tcp_fconnect(PNATState pData, struct socket *so)
410{
411 int ret=0;
412
413 DEBUG_CALL("tcp_fconnect");
414 DEBUG_ARG("so = %lx", (long )so);
415
416 if ((ret = so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0)
417 {
418 int opt, s=so->s;
419 struct sockaddr_in addr;
420
421 fd_nonblock(s);
422 opt = 1;
423 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt, sizeof(opt));
424 opt = 1;
425 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt, sizeof(opt));
426
427 addr.sin_family = AF_INET;
428 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
429 {
430 /* It's an alias */
431 switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask)
432 {
433 case CTL_DNS:
434 if (!get_dns_addr(pData, &dns_addr))
435 addr.sin_addr = dns_addr;
436 else
437 addr.sin_addr = loopback_addr;
438 break;
439 case CTL_ALIAS:
440 default:
441 addr.sin_addr = loopback_addr;
442 break;
443 }
444 }
445 else
446 addr.sin_addr = so->so_faddr;
447 addr.sin_port = so->so_fport;
448
449 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
450 "addr.sin_addr.s_addr=%.16s\n",
451 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
452 /* We don't care what port we get */
453 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
454
455 /*
456 * If it's not in progress, it failed, so we just return 0,
457 * without clearing SS_NOFDREF
458 */
459 soisfconnecting(so);
460 }
461
462 return(ret);
463}
464
465/*
466 * Accept the socket and connect to the local-host
467 *
468 * We have a problem. The correct thing to do would be
469 * to first connect to the local-host, and only if the
470 * connection is accepted, then do an accept() here.
471 * But, a) we need to know who's trying to connect
472 * to the socket to be able to SYN the local-host, and
473 * b) we are already connected to the foreign host by
474 * the time it gets to accept(), so... We simply accept
475 * here and SYN the local-host.
476 */
477void
478tcp_connect(PNATState pData, struct socket *inso)
479{
480 struct socket *so;
481 struct sockaddr_in addr;
482 socklen_t addrlen = sizeof(struct sockaddr_in);
483 struct tcpcb *tp;
484 int s, opt;
485
486 DEBUG_CALL("tcp_connect");
487 DEBUG_ARG("inso = %lx", (long)inso);
488
489 /*
490 * If it's an SS_ACCEPTONCE socket, no need to socreate()
491 * another socket, just use the accept() socket.
492 */
493 if (inso->so_state & SS_FACCEPTONCE)
494 {
495 /* FACCEPTONCE already have a tcpcb */
496 so = inso;
497 }
498 else
499 {
500 if ((so = socreate()) == NULL)
501 {
502 /* If it failed, get rid of the pending connection */
503 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
504 return;
505 }
506 if (tcp_attach(pData, so) < 0)
507 {
508 free(so); /* NOT sofree */
509 return;
510 }
511 so->so_laddr = inso->so_laddr;
512 so->so_lport = inso->so_lport;
513 }
514
515 (void) tcp_mss(pData, sototcpcb(so), 0);
516
517 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0)
518 {
519 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
520 return;
521 }
522 fd_nonblock(s);
523 opt = 1;
524 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
525 opt = 1;
526 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
527 opt = 1;
528 setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
529
530 so->so_fport = addr.sin_port;
531 so->so_faddr = addr.sin_addr;
532 /* Translate connections from localhost to the real hostname */
533 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
534 so->so_faddr = alias_addr;
535
536 /* Close the accept() socket, set right state */
537 if (inso->so_state & SS_FACCEPTONCE)
538 {
539 closesocket(so->s); /* If we only accept once, close the accept() socket */
540 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
541 /* if it's not FACCEPTONCE, it's already NOFDREF */
542 }
543 so->s = s;
544
545 so->so_iptos = tcp_tos(so);
546 tp = sototcpcb(so);
547
548 tcp_template(tp);
549
550 /* Compute window scaling to request. */
551/* while (tp->request_r_scale < TCP_MAX_WINSHIFT
552 * && (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
553 * tp->request_r_scale++;
554 */
555
556/* soisconnecting(so); */ /* NOFDREF used instead */
557 tcpstat.tcps_connattempt++;
558
559 tp->t_state = TCPS_SYN_SENT;
560 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
561 tp->iss = tcp_iss;
562 tcp_iss += TCP_ISSINCR/2;
563 tcp_sendseqinit(tp);
564 tcp_output(pData, tp);
565}
566
567/*
568 * Attach a TCPCB to a socket.
569 */
570int
571tcp_attach(PNATState pData, struct socket *so)
572{
573 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
574 return -1;
575
576 insque(pData, so, &tcb);
577 return 0;
578}
579
580/*
581 * Set the socket's type of service field
582 */
583static const struct tos_t tcptos[] =
584{
585 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
586 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
587 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
588 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
589 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
590 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
591 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
592 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
593 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
594 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
595 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
596 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
597 {0, 0, 0, 0}
598};
599
600/*
601 * Return TOS according to the above table
602 */
603u_int8_t
604tcp_tos(struct socket *so)
605{
606 int i = 0;
607
608 while(tcptos[i].tos)
609 {
610 if ( (tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport))
611 || (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport)))
612 {
613 so->so_emu = tcptos[i].emu;
614 return tcptos[i].tos;
615 }
616 i++;
617 }
618
619 return 0;
620}
621
622/*
623 * Emulate programs that try and connect to us. This includes ftp (the data
624 * connection is initiated by the server) and IRC (DCC CHAT and DCC SEND)
625 * for now
626 *
627 * NOTE: It's possible to crash SLiRP by sending it unstandard strings to
628 * emulate... if this is a problem, more checks are needed here.
629 *
630 * XXX Assumes the whole command cames in one packet
631 *
632 * XXX Some ftp clients will have their TOS set to LOWDELAY and so Nagel will
633 * kick in. Because of this, we'll get the first letter, followed by the
634 * rest, so we simply scan for ORT instead of PORT... DCC doesn't have this
635 * problem because there's other stuff in the packet before the DCC command.
636 *
637 * Return 1 if the mbuf m is still valid and should be sbappend()ed
638 *
639 * NOTE: if you return 0 you MUST m_free() the mbuf!
640 */
641int
642tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
643{
644 u_int n1, n2, n3, n4, n5, n6;
645 char buff[256];
646 u_int32_t laddr;
647 u_int lport;
648 char *bptr;
649
650 DEBUG_CALL("tcp_emu");
651 DEBUG_ARG("so = %lx", (long)so);
652 DEBUG_ARG("m = %lx", (long)m);
653
654 switch(so->so_emu)
655 {
656 int x, i;
657
658 case EMU_IDENT:
659 /*
660 * Identification protocol as per rfc-1413
661 */
662 {
663 struct socket *tmpso;
664 struct sockaddr_in addr;
665 socklen_t addrlen = sizeof(struct sockaddr_in);
666 struct sbuf *so_rcv = &so->so_rcv;
667
668 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
669 so_rcv->sb_wptr += m->m_len;
670 so_rcv->sb_rptr += m->m_len;
671 m->m_data[m->m_len] = 0; /* NULL terminate */
672 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n'))
673 {
674 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2)
675 {
676 HTONS(n1);
677 HTONS(n2);
678 /* n2 is the one on our host */
679 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next)
680 {
681 if ( tmpso->so_laddr.s_addr == so->so_laddr.s_addr
682 && tmpso->so_lport == n2
683 && tmpso->so_faddr.s_addr == so->so_faddr.s_addr
684 && tmpso->so_fport == n1)
685 {
686 if (getsockname(tmpso->s,
687 (struct sockaddr *)&addr, &addrlen) == 0)
688 n2 = ntohs(addr.sin_port);
689 break;
690 }
691 }
692 }
693 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
694 so_rcv->sb_rptr = so_rcv->sb_data;
695 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
696 }
697 m_free(pData, m);
698 return 0;
699 }
700
701 case EMU_FTP:
702 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
703 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL)
704 {
705 /*
706 * Need to emulate the PORT command
707 */
708 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
709 &n1, &n2, &n3, &n4, &n5, &n6, buff);
710 if (x < 6)
711 return 1;
712
713 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
714 lport = htons((n5 << 8) | (n6));
715
716 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
717 return 1;
718
719 n6 = ntohs(so->so_fport);
720
721 n5 = (n6 >> 8) & 0xff;
722 n6 &= 0xff;
723
724 laddr = ntohl(so->so_faddr.s_addr);
725
726 n1 = ((laddr >> 24) & 0xff);
727 n2 = ((laddr >> 16) & 0xff);
728 n3 = ((laddr >> 8) & 0xff);
729 n4 = ( laddr & 0xff);
730
731 m->m_len = bptr - m->m_data; /* Adjust length */
732 m->m_len += sprintf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%s",
733 n1, n2, n3, n4, n5, n6, x==7?buff:"");
734 return 1;
735 }
736 else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL)
737 {
738 /*
739 * Need to emulate the PASV response
740 */
741 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
742 &n1, &n2, &n3, &n4, &n5, &n6, buff);
743 if (x < 6)
744 return 1;
745
746 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
747 lport = htons((n5 << 8) | (n6));
748
749 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
750 return 1;
751
752 n6 = ntohs(so->so_fport);
753
754 n5 = (n6 >> 8) & 0xff;
755 n6 &= 0xff;
756
757 laddr = ntohl(so->so_faddr.s_addr);
758
759 n1 = ((laddr >> 24) & 0xff);
760 n2 = ((laddr >> 16) & 0xff);
761 n3 = ((laddr >> 8) & 0xff);
762 n4 = (laddr & 0xff);
763
764 m->m_len = bptr - m->m_data; /* Adjust length */
765 m->m_len += sprintf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
766 n1, n2, n3, n4, n5, n6, x==7?buff:"");
767
768 return 1;
769 }
770 return 1;
771
772 case EMU_KSH:
773 /*
774 * The kshell (Kerberos rsh) and shell services both pass
775 * a local port port number to carry signals to the server
776 * and stderr to the client. It is passed at the beginning
777 * of the connection as a NUL-terminated decimal ASCII string.
778 */
779 so->so_emu = 0;
780 for (lport = 0, i = 0; i < m->m_len-1; ++i)
781 {
782 if (m->m_data[i] < '0' || m->m_data[i] > '9')
783 return 1; /* invalid number */
784 lport *= 10;
785 lport += m->m_data[i] - '0';
786 }
787 if ( m->m_data[m->m_len-1] == '\0'
788 && lport != 0
789 && (so = solisten(pData, 0, so->so_laddr.s_addr,
790 htons(lport), SS_FACCEPTONCE)) != NULL)
791 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
792 return 1;
793
794 case EMU_IRC:
795 /*
796 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
797 */
798 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
799 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
800 return 1;
801
802 /* The %256s is for the broken mIRC */
803 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3)
804 {
805 if ((so = solisten(pData, 0, htonl(laddr),
806 htons(lport), SS_FACCEPTONCE)) == NULL)
807 return 1;
808
809 m->m_len = bptr - m->m_data; /* Adjust length */
810 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
811 (unsigned long)ntohl(so->so_faddr.s_addr),
812 ntohs(so->so_fport), 1);
813 }
814 else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
815 {
816 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
817 return 1;
818
819 m->m_len = bptr - m->m_data; /* Adjust length */
820 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
821 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
822 ntohs(so->so_fport), n1, 1);
823 }
824 else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
825 {
826 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
827 return 1;
828
829 m->m_len = bptr - m->m_data; /* Adjust length */
830 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
831 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
832 ntohs(so->so_fport), n1, 1);
833 }
834 return 1;
835
836#ifdef VBOX
837 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
838 * This is not legal when more than one slirp instance is active. */
839#else /* !VBOX */
840 case EMU_REALAUDIO:
841 /*
842 * RealAudio emulation - JP. We must try to parse the incoming
843 * data and try to find the two characters that contain the
844 * port number. Then we redirect an udp port and replace the
845 * number with the real port we got.
846 *
847 * The 1.0 beta versions of the player are not supported
848 * any more.
849 *
850 * A typical packet for player version 1.0 (release version):
851 *
852 * 0000:50 4E 41 00 05
853 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
854 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
855 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
856 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
857 *
858 * Now the port number 0x1BD7 is found at offset 0x04 of the
859 * Now the port number 0x1BD7 is found at offset 0x04 of the
860 * second packet. This time we received five bytes first and
861 * then the rest. You never know how many bytes you get.
862 *
863 * A typical packet for player version 2.0 (beta):
864 *
865 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
866 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
867 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
868 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
869 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
870 *
871 * Port number 0x1BC1 is found at offset 0x0d.
872 *
873 * This is just a horrible switch statement. Variable ra tells
874 * us where we're going.
875 */
876
877 bptr = m->m_data;
878 while (bptr < m->m_data + m->m_len)
879 {
880 u_short p;
881 static int ra = 0;
882 char ra_tbl[4];
883
884 ra_tbl[0] = 0x50;
885 ra_tbl[1] = 0x4e;
886 ra_tbl[2] = 0x41;
887 ra_tbl[3] = 0;
888
889 switch (ra)
890 {
891 case 0:
892 case 2:
893 case 3:
894 if (*bptr++ != ra_tbl[ra])
895 {
896 ra = 0;
897 continue;
898 }
899 break;
900
901 case 1:
902 /*
903 * We may get 0x50 several times, ignore them
904 */
905 if (*bptr == 0x50)
906 {
907 ra = 1;
908 bptr++;
909 continue;
910 }
911 else if (*bptr++ != ra_tbl[ra])
912 {
913 ra = 0;
914 continue;
915 }
916 break;
917
918 case 4:
919 /*
920 * skip version number
921 */
922 bptr++;
923 break;
924
925 case 5:
926 /*
927 * The difference between versions 1.0 and
928 * 2.0 is here. For future versions of
929 * the player this may need to be modified.
930 */
931 if (*(bptr + 1) == 0x02)
932 bptr += 8;
933 else
934 bptr += 4;
935 break;
936
937 case 6:
938 /* This is the field containing the port
939 * number that RA-player is listening to.
940 */
941 lport = (((u_char*)bptr)[0] << 8)
942 + ((u_char *)bptr)[1];
943 if (lport < 6970)
944 lport += 256; /* don't know why */
945 if (lport < 6970 || lport > 7170)
946 return 1; /* failed */
947
948 /* try to get udp port between 6970 - 7170 */
949 for (p = 6970; p < 7071; p++)
950 {
951 if (udp_listen(htons(p),
952 so->so_laddr.s_addr,
953 htons(lport),
954 SS_FACCEPTONCE))
955 {
956 break;
957 }
958 }
959 if (p == 7071)
960 p = 0;
961 *(u_char *)bptr++ = (p >> 8) & 0xff;
962 *(u_char *)bptr++ = p & 0xff;
963 ra = 0;
964 return 1; /* port redirected, we're done */
965 break;
966
967 default:
968 ra = 0;
969 }
970 ra++;
971 }
972 return 1;
973#endif /* !VBOX */
974
975 default:
976 /* Ooops, not emulated, won't call tcp_emu again */
977 so->so_emu = 0;
978 return 1;
979 }
980}
981
982#if SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS)
983/**
984 * Slow pointer hashing that deals with automatic inserting and collisions.
985 */
986uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
987{
988 uint32_t i;
989 if (pv == NULL)
990 i = 0;
991 else
992 {
993 const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
994 if (pData->apvHash[i1] == pv)
995 i = i1;
996 else
997 {
998 /*
999 * Try up to 10 times then assume it's an insertion.
1000 * If we didn't find a free entry by then, try another 100 times.
1001 * If that fails, give up.
1002 */
1003 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1004 uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
1005 int cTries = 10;
1006 int cTries2 = 100;
1007
1008 i = i1;
1009 for (;;)
1010 {
1011 /* check if we should give in.*/
1012 if (--cTries > 0)
1013 {
1014 if (i1stFree != 0)
1015 {
1016 i = i1stFree;
1017 pData->apvHash[i] = pv;
1018 pData->cpvHashUsed++;
1019 if (i != i1)
1020 pData->cpvHashCollisions++;
1021 pData->cpvHashInserts++;
1022 break;
1023 }
1024 if (!cTries2)
1025 {
1026 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
1027 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1028 i = 0;
1029 break;
1030 }
1031 cTries = cTries2;
1032 cTries2 = 0;
1033 }
1034
1035 /* advance to the next hash entry and test it. */
1036 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1037 while (RT_UNLIKELY(!i))
1038 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1039 if (pData->apvHash[i] == pv)
1040 break;
1041 if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
1042 i1stFree = i;
1043 }
1044 }
1045 }
1046 return i;
1047}
1048
1049
1050/**
1051 * Removes the pointer from the hash table.
1052 */
1053void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
1054{
1055 /* We don't count NULL pointers. */
1056 if (pv == NULL)
1057 return;
1058 pData->cpvHashDone++;
1059
1060 /* try the hint */
1061 if ( iHint
1062 && iHint < RT_ELEMENTS(pData->apvHash)
1063 && pData->apvHash[iHint] == pv)
1064 {
1065 pData->apvHash[iHint] = NULL;
1066 pData->cpvHashUsed--;
1067 return;
1068 }
1069
1070 iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1071 if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
1072 {
1073 /*
1074 * Try up to 120 times then assert.
1075 */
1076 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1077 int cTries = 120;
1078 for (;;)
1079 {
1080 /* advance to the next hash entry and test it. */
1081 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1082 while (RT_UNLIKELY(!iHint))
1083 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1084 if (pData->apvHash[iHint] == pv)
1085 break;
1086
1087 /* check if we should give in.*/
1088 if (--cTries > 0)
1089 {
1090 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
1091 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1092 return;
1093 }
1094 }
1095 }
1096
1097 /* found it */
1098 pData->apvHash[iHint] = NULL;
1099 pData->cpvHashUsed--;
1100}
1101
1102#endif /* SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS */
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