VirtualBox

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

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

slirp: removed dead code

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

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