VirtualBox

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

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

removed unused global tcbhead

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