VirtualBox

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

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

NAT:NSOK_INC/DEC added to count sockets number for allocation enough pollfd buffer
(and latter epoll_event (for Linux only)).

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