VirtualBox

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

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

silly typo

  • 屬性 svn:eol-style 設為 native
檔案大小: 42.3 KB
 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
34 * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
35 */
36
37/*
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
40 *
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
43 */
44
45#define WANT_SYS_IOCTL_H
46#include <slirp.h>
47
48#ifndef VBOX
49/* patchable/settable parameters for tcp */
50int tcp_mssdflt = TCP_MSS;
51int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
52int tcp_do_rfc1323 = 0; /* Don't do rfc1323 performance enhancements */
53int tcp_rcvspace; /* You may want to change this */
54int tcp_sndspace; /* Keep small if you have an error prone link */
55#endif /* !VBOX */
56
57/*
58 * Tcp initialization
59 */
60void
61#ifdef VBOX
62tcp_init(PNATState pData)
63#else /* !VBOX */
64tcp_init()
65#endif /* !VBOX */
66{
67 tcp_iss = 1; /* wrong */
68 tcb.so_next = tcb.so_prev = &tcb;
69#ifdef VBOX
70 tcp_last_so = &tcb;
71#endif /* VBOX */
72
73#ifndef VBOX
74 /* tcp_rcvspace = our Window we advertise to the remote */
75 tcp_rcvspace = TCP_RCVSPACE;
76 tcp_sndspace = TCP_SNDSPACE;
77
78 /* Make sure tcp_sndspace is at least 2*MSS */
79 if (tcp_sndspace < 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr)))
80 tcp_sndspace = 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr));
81#endif /* !VBOX */
82}
83
84/*
85 * Create template to be used to send tcp packets on a connection.
86 * Call after host entry created, fills
87 * in a skeletal tcp/ip header, minimizing the amount of work
88 * necessary when the connection is used.
89 */
90/* struct tcpiphdr * */
91void
92tcp_template(tp)
93 struct tcpcb *tp;
94{
95 struct socket *so = tp->t_socket;
96 register struct tcpiphdr *n = &tp->t_template;
97
98 n->ti_next = n->ti_prev = 0;
99 n->ti_x1 = 0;
100 n->ti_pr = IPPROTO_TCP;
101 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
102 n->ti_src = so->so_faddr;
103 n->ti_dst = so->so_laddr;
104 n->ti_sport = so->so_fport;
105 n->ti_dport = so->so_lport;
106
107 n->ti_seq = 0;
108 n->ti_ack = 0;
109 n->ti_x2 = 0;
110 n->ti_off = 5;
111 n->ti_flags = 0;
112 n->ti_win = 0;
113 n->ti_sum = 0;
114 n->ti_urp = 0;
115}
116
117/*
118 * Send a single message to the TCP at address specified by
119 * the given TCP/IP header. If m == 0, then we make a copy
120 * of the tcpiphdr at ti and send directly to the addressed host.
121 * This is used to force keep alive messages out using the TCP
122 * template for a connection tp->t_template. If flags are given
123 * then we send a message back to the TCP which originated the
124 * segment ti, and discard the mbuf containing it and any other
125 * attached mbufs.
126 *
127 * In any case the ack and sequence number of the transmitted
128 * segment are as specified by the parameters.
129 */
130void
131#ifdef VBOX
132tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
133#else /* !VBOX */
134tcp_respond(tp, ti, m, ack, seq, flags)
135 struct tcpcb *tp;
136 register struct tcpiphdr *ti;
137 register struct mbuf *m;
138 tcp_seq ack, seq;
139 int flags;
140#endif /* !VBOX */
141{
142 register int tlen;
143 int win = 0;
144
145 DEBUG_CALL("tcp_respond");
146 DEBUG_ARG("tp = %lx", (long)tp);
147 DEBUG_ARG("ti = %lx", (long)ti);
148 DEBUG_ARG("m = %lx", (long)m);
149 DEBUG_ARG("ack = %u", ack);
150 DEBUG_ARG("seq = %u", seq);
151 DEBUG_ARG("flags = %x", flags);
152
153 if (tp)
154 win = sbspace(&tp->t_socket->so_rcv);
155 if (m == 0) {
156#ifdef VBOX
157 if ((m = m_get(pData)) == NULL)
158#else /* !VBOX */
159 if ((m = m_get()) == NULL)
160#endif /* !VBOX */
161 return;
162#ifdef TCP_COMPAT_42
163 tlen = 1;
164#else
165 tlen = 0;
166#endif
167 m->m_data += if_maxlinkhdr;
168 *mtod(m, struct tcpiphdr *) = *ti;
169 ti = mtod(m, struct tcpiphdr *);
170 flags = TH_ACK;
171 } else {
172 /*
173 * ti points into m so the next line is just making
174 * the mbuf point to ti
175 */
176 m->m_data = (caddr_t)ti;
177
178 m->m_len = sizeof (struct tcpiphdr);
179 tlen = 0;
180#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
181 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
182 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
183#undef xchg
184 }
185 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
186 tlen += sizeof (struct tcpiphdr);
187 m->m_len = tlen;
188
189 ti->ti_next = ti->ti_prev = 0;
190 ti->ti_x1 = 0;
191 ti->ti_seq = htonl(seq);
192 ti->ti_ack = htonl(ack);
193 ti->ti_x2 = 0;
194 ti->ti_off = sizeof (struct tcphdr) >> 2;
195 ti->ti_flags = flags;
196 if (tp)
197 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
198 else
199 ti->ti_win = htons((u_int16_t)win);
200 ti->ti_urp = 0;
201 ti->ti_sum = 0;
202 ti->ti_sum = cksum(m, tlen);
203 ((struct ip *)ti)->ip_len = tlen;
204
205 if(flags & TH_RST)
206 ((struct ip *)ti)->ip_ttl = MAXTTL;
207 else
208 ((struct ip *)ti)->ip_ttl = ip_defttl;
209
210#ifdef VBOX
211 (void) ip_output(pData, (struct socket *)0, m);
212#else /* !VBOX */
213 (void) ip_output((struct socket *)0, m);
214#endif /* !VBOX */
215}
216
217/*
218 * Create a new TCP control block, making an
219 * empty reassembly queue and hooking it to the argument
220 * protocol control block.
221 */
222struct tcpcb *
223tcp_newtcpcb(PNATState pData, struct socket *so)
224{
225 register struct tcpcb *tp;
226
227 tp = (struct tcpcb *)malloc(sizeof(*tp));
228 if (tp == NULL)
229 return ((struct tcpcb *)0);
230
231 memset((char *) tp, 0, sizeof(struct tcpcb));
232 tp->seg_next = tp->seg_prev = ptr_to_u32(pData, (struct tcpiphdr *)tp);
233 tp->t_maxseg = tcp_mssdflt;
234
235 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
236 tp->t_socket = so;
237
238 /*
239 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
240 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
241 * reasonable initial retransmit time.
242 */
243 tp->t_srtt = TCPTV_SRTTBASE;
244 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
245 tp->t_rttmin = TCPTV_MIN;
246
247 TCPT_RANGESET(tp->t_rxtcur,
248 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
249 TCPTV_MIN, TCPTV_REXMTMAX);
250
251 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
252 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
253 tp->t_state = TCPS_CLOSED;
254
255 so->so_tcpcb = tp;
256
257 return (tp);
258}
259
260/*
261 * Drop a TCP connection, reporting
262 * the specified error. If connection is synchronized,
263 * then send a RST to peer.
264 */
265#ifdef VBOX
266struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
267#else /* !VBOX */
268struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
269#endif /* !VBOX */
270{
271/* tcp_drop(tp, errno)
272 register struct tcpcb *tp;
273 int errno;
274{
275*/
276
277 DEBUG_CALL("tcp_drop");
278 DEBUG_ARG("tp = %lx", (long)tp);
279 DEBUG_ARG("errno = %d", errno);
280
281 if (TCPS_HAVERCVDSYN(tp->t_state)) {
282 tp->t_state = TCPS_CLOSED;
283#ifdef VBOX
284 (void) tcp_output(pData, tp);
285#else /* !VBOX */
286 (void) tcp_output(tp);
287#endif /* !VBOX */
288 tcpstat.tcps_drops++;
289 } else
290 tcpstat.tcps_conndrops++;
291/* if (errno == ETIMEDOUT && tp->t_softerror)
292 * errno = tp->t_softerror;
293 */
294/* so->so_error = errno; */
295#ifdef VBOX
296 return (tcp_close(pData, tp));
297#else /* !VBOX */
298 return (tcp_close(tp));
299#endif /* !VBOX */
300}
301
302/*
303 * Close a TCP control block:
304 * discard all space held by the tcp
305 * discard internet protocol block
306 * wake up any sleepers
307 */
308struct tcpcb *
309#ifdef VBOX
310tcp_close(PNATState pData, register struct tcpcb *tp)
311#else /* !VBOX */
312tcp_close(tp)
313 register struct tcpcb *tp;
314#endif /* !VBOX */
315{
316 register struct tcpiphdr *t;
317 struct socket *so = tp->t_socket;
318 register struct mbuf *m;
319
320 DEBUG_CALL("tcp_close");
321 DEBUG_ARG("tp = %lx", (long )tp);
322
323 /* free the reassembly queue, if any */
324 t = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *);
325 while (t != (struct tcpiphdr *)tp) {
326 t = u32_to_ptr(pData, t->ti_next, struct tcpiphdr *);
327 m = REASS_MBUF_GET(u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
328 remque_32(pData, u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
329#ifdef VBOX
330 m_freem(pData, m);
331#else /* !VBOX */
332 m_freem(m);
333#endif /* !VBOX */
334 }
335 /* It's static */
336/* if (tp->t_template)
337 * (void) m_free(dtom(tp->t_template));
338 */
339/* free(tp, M_PCB); */
340 u32ptr_done(pData, ptr_to_u32(pData, tp), tp);
341 free(tp);
342 so->so_tcpcb = 0;
343 soisfdisconnected(so);
344 /* clobber input socket cache if we're closing the cached connection */
345 if (so == tcp_last_so)
346 tcp_last_so = &tcb;
347 closesocket(so->s);
348 sbfree(&so->so_rcv);
349 sbfree(&so->so_snd);
350#ifdef VBOX
351 sofree(pData, so);
352#else /* !VBOX */
353 sofree(so);
354#endif /* !VBOX */
355 tcpstat.tcps_closed++;
356 return ((struct tcpcb *)0);
357}
358
359void
360tcp_drain()
361{
362 /* XXX */
363}
364
365/*
366 * When a source quench is received, close congestion window
367 * to one segment. We will gradually open it again as we proceed.
368 */
369
370#ifdef notdef
371
372void
373tcp_quench(i, errno)
374
375 int errno;
376{
377 struct tcpcb *tp = intotcpcb(inp);
378
379 if (tp)
380 tp->snd_cwnd = tp->t_maxseg;
381}
382
383#endif /* notdef */
384
385/*
386 * TCP protocol interface to socket abstraction.
387 */
388
389/*
390 * User issued close, and wish to trail through shutdown states:
391 * if never received SYN, just forget it. If got a SYN from peer,
392 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
393 * If already got a FIN from peer, then almost done; go to LAST_ACK
394 * state. In all other cases, have already sent FIN to peer (e.g.
395 * after PRU_SHUTDOWN), and just have to play tedious game waiting
396 * for peer to send FIN or not respond to keep-alives, etc.
397 * We can let the user exit from the close as soon as the FIN is acked.
398 */
399void
400#ifdef VBOX
401tcp_sockclosed(PNATState pData, struct tcpcb *tp)
402#else /* !VBOX */
403tcp_sockclosed(tp)
404 struct tcpcb *tp;
405#endif /* !VBOX */
406{
407
408 DEBUG_CALL("tcp_sockclosed");
409 DEBUG_ARG("tp = %lx", (long)tp);
410
411 switch (tp->t_state) {
412
413 case TCPS_CLOSED:
414 case TCPS_LISTEN:
415 case TCPS_SYN_SENT:
416 tp->t_state = TCPS_CLOSED;
417#ifdef VBOX
418 tp = tcp_close(pData, tp);
419#else /* !VBOX */
420 tp = tcp_close(tp);
421#endif /* !VBOX */
422 break;
423
424 case TCPS_SYN_RECEIVED:
425 case TCPS_ESTABLISHED:
426 tp->t_state = TCPS_FIN_WAIT_1;
427 break;
428
429 case TCPS_CLOSE_WAIT:
430 tp->t_state = TCPS_LAST_ACK;
431 break;
432 }
433/* soisfdisconnecting(tp->t_socket); */
434 if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
435 soisfdisconnected(tp->t_socket);
436 if (tp)
437#ifdef VBOX
438 tcp_output(pData, tp);
439#else /* !VBOX */
440 tcp_output(tp);
441#endif /* !VBOX */
442}
443
444/*
445 * Connect to a host on the Internet
446 * Called by tcp_input
447 * Only do a connect, the tcp fields will be set in tcp_input
448 * return 0 if there's a result of the connect,
449 * else return -1 means we're still connecting
450 * The return value is almost always -1 since the socket is
451 * nonblocking. Connect returns after the SYN is sent, and does
452 * not wait for ACK+SYN.
453 */
454#ifdef VBOX
455int tcp_fconnect(PNATState pData, struct socket *so)
456#else /* !VBOX */
457int tcp_fconnect(so)
458 struct socket *so;
459#endif /* !VBOX */
460{
461 int ret=0;
462
463 DEBUG_CALL("tcp_fconnect");
464 DEBUG_ARG("so = %lx", (long )so);
465
466 if( (ret=so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0) {
467 int opt, s=so->s;
468 struct sockaddr_in addr;
469
470 fd_nonblock(s);
471 opt = 1;
472 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
473 opt = 1;
474 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
475
476 addr.sin_family = AF_INET;
477 if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
478 /* It's an alias */
479 switch(ntohl(so->so_faddr.s_addr) & 0xff) {
480 case CTL_DNS:
481 addr.sin_addr = dns_addr;
482 break;
483 case CTL_ALIAS:
484 default:
485 addr.sin_addr = loopback_addr;
486 break;
487 }
488 } else
489 addr.sin_addr = so->so_faddr;
490 addr.sin_port = so->so_fport;
491
492 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
493 "addr.sin_addr.s_addr=%.16s\n",
494 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
495 /* We don't care what port we get */
496 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
497
498 /*
499 * If it's not in progress, it failed, so we just return 0,
500 * without clearing SS_NOFDREF
501 */
502 soisfconnecting(so);
503 }
504
505 return(ret);
506}
507
508/*
509 * Accept the socket and connect to the local-host
510 *
511 * We have a problem. The correct thing to do would be
512 * to first connect to the local-host, and only if the
513 * connection is accepted, then do an accept() here.
514 * But, a) we need to know who's trying to connect
515 * to the socket to be able to SYN the local-host, and
516 * b) we are already connected to the foreign host by
517 * the time it gets to accept(), so... We simply accept
518 * here and SYN the local-host.
519 */
520void
521#ifdef VBOX
522tcp_connect(PNATState pData, struct socket *inso)
523#else /* !VBOX */
524tcp_connect(inso)
525 struct socket *inso;
526#endif /* !VBOX */
527{
528 struct socket *so;
529 struct sockaddr_in addr;
530#ifndef VBOX
531 int addrlen = sizeof(struct sockaddr_in);
532#else /* VBOX */
533 socklen_t addrlen = sizeof(struct sockaddr_in);
534#endif /* VBOX */
535 struct tcpcb *tp;
536 int s, opt;
537
538 DEBUG_CALL("tcp_connect");
539 DEBUG_ARG("inso = %lx", (long)inso);
540
541 /*
542 * If it's an SS_ACCEPTONCE socket, no need to socreate()
543 * another socket, just use the accept() socket.
544 */
545 if (inso->so_state & SS_FACCEPTONCE) {
546 /* FACCEPTONCE already have a tcpcb */
547 so = inso;
548 } else {
549 if ((so = socreate()) == NULL) {
550 /* If it failed, get rid of the pending connection */
551 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
552 return;
553 }
554#ifdef VBOX
555 if (tcp_attach(pData, so) < 0) {
556#else /* !VBOX */
557 if (tcp_attach(so) < 0) {
558#endif /* !VBOX */
559 free(so); /* NOT sofree */
560 return;
561 }
562 so->so_laddr = inso->so_laddr;
563 so->so_lport = inso->so_lport;
564 }
565
566#ifdef VBOX
567 (void) tcp_mss(pData, sototcpcb(so), 0);
568#else /* !VBOX */
569 (void) tcp_mss(sototcpcb(so), 0);
570#endif /* !VBOX */
571
572 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
573#ifdef VBOX
574 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
575#else /* !VBOX */
576 tcp_close(sototcpcb(so)); /* This will sofree() as well */
577#endif /* !VBOX */
578 return;
579 }
580 fd_nonblock(s);
581 opt = 1;
582 setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
583 opt = 1;
584 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
585 opt = 1;
586 setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
587
588 so->so_fport = addr.sin_port;
589 so->so_faddr = addr.sin_addr;
590 /* Translate connections from localhost to the real hostname */
591 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
592 so->so_faddr = alias_addr;
593
594 /* Close the accept() socket, set right state */
595 if (inso->so_state & SS_FACCEPTONCE) {
596 closesocket(so->s); /* If we only accept once, close the accept() socket */
597 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
598 /* if it's not FACCEPTONCE, it's already NOFDREF */
599 }
600 so->s = s;
601
602 so->so_iptos = tcp_tos(so);
603 tp = sototcpcb(so);
604
605 tcp_template(tp);
606
607 /* Compute window scaling to request. */
608/* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
609 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
610 * tp->request_r_scale++;
611 */
612
613/* soisconnecting(so); */ /* NOFDREF used instead */
614 tcpstat.tcps_connattempt++;
615
616 tp->t_state = TCPS_SYN_SENT;
617 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
618 tp->iss = tcp_iss;
619 tcp_iss += TCP_ISSINCR/2;
620 tcp_sendseqinit(tp);
621#ifdef VBOX
622 tcp_output(pData, tp);
623#else /* !VBOX */
624 tcp_output(tp);
625#endif /* !VBOX */
626}
627
628/*
629 * Attach a TCPCB to a socket.
630 */
631int
632#ifdef VBOX
633tcp_attach(PNATState pData, struct socket *so)
634#else /* !VBOX */
635tcp_attach(so)
636 struct socket *so;
637#endif /* !VBOX */
638{
639 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
640 return -1;
641
642 insque(pData, so, &tcb);
643
644 return 0;
645}
646
647/*
648 * Set the socket's type of service field
649 */
650#ifdef VBOX
651static const struct tos_t tcptos[] = {
652#else /* !VBOX */
653struct tos_t tcptos[] = {
654#endif /* !VBOX */
655 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
656 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
657 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
658 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
659 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
660 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
661 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
662 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
663 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
664 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
665 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
666 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
667 {0, 0, 0, 0}
668};
669
670#ifndef VBOX
671struct emu_t *tcpemu = 0;
672#endif /* !VBOX */
673
674/*
675 * Return TOS according to the above table
676 */
677u_int8_t
678tcp_tos(so)
679 struct socket *so;
680{
681 int i = 0;
682#ifndef VBOX
683 struct emu_t *emup;
684#endif /* !VBOX */
685
686 while(tcptos[i].tos) {
687 if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
688 (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
689 so->so_emu = tcptos[i].emu;
690 return tcptos[i].tos;
691 }
692 i++;
693 }
694
695#ifdef VBOX
696 /* No user-added emulators supported. */
697#else /* !VBOX */
698 /* Nope, lets see if there's a user-added one */
699 for (emup = tcpemu; emup; emup = emup->next) {
700 if ((emup->fport && (ntohs(so->so_fport) == emup->fport)) ||
701 (emup->lport && (ntohs(so->so_lport) == emup->lport))) {
702 so->so_emu = emup->emu;
703 return emup->tos;
704 }
705 }
706#endif /* !VBOX */
707
708 return 0;
709}
710
711#ifndef VBOX
712int do_echo = -1;
713#endif /* !VBOX */
714
715/*
716 * Emulate programs that try and connect to us
717 * This includes ftp (the data connection is
718 * initiated by the server) and IRC (DCC CHAT and
719 * DCC SEND) for now
720 *
721 * NOTE: It's possible to crash SLiRP by sending it
722 * unstandard strings to emulate... if this is a problem,
723 * more checks are needed here
724 *
725 * XXX Assumes the whole command came in one packet
726 *
727 * XXX Some ftp clients will have their TOS set to
728 * LOWDELAY and so Nagel will kick in. Because of this,
729 * we'll get the first letter, followed by the rest, so
730 * we simply scan for ORT instead of PORT...
731 * DCC doesn't have this problem because there's other stuff
732 * in the packet before the DCC command.
733 *
734 * Return 1 if the mbuf m is still valid and should be
735 * sbappend()ed
736 *
737 * NOTE: if you return 0 you MUST m_free() the mbuf!
738 */
739int
740#ifdef VBOX
741tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
742#else /* !VBOX */
743tcp_emu(so, m)
744 struct socket *so;
745 struct mbuf *m;
746#endif /* !VBOX */
747{
748 u_int n1, n2, n3, n4, n5, n6;
749 char buff[256];
750 u_int32_t laddr;
751 u_int lport;
752 char *bptr;
753
754 DEBUG_CALL("tcp_emu");
755 DEBUG_ARG("so = %lx", (long)so);
756 DEBUG_ARG("m = %lx", (long)m);
757
758 switch(so->so_emu) {
759 int x, i;
760
761 case EMU_IDENT:
762 /*
763 * Identification protocol as per rfc-1413
764 */
765
766 {
767 struct socket *tmpso;
768 struct sockaddr_in addr;
769#ifndef VBOX
770 int addrlen = sizeof(struct sockaddr_in);
771#else /* VBOX */
772 socklen_t addrlen = sizeof(struct sockaddr_in);
773#endif /* VBOX */
774 struct sbuf *so_rcv = &so->so_rcv;
775
776 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
777 so_rcv->sb_wptr += m->m_len;
778 so_rcv->sb_rptr += m->m_len;
779 m->m_data[m->m_len] = 0; /* NULL terminate */
780 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
781#ifdef VBOX
782 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
783#else
784 if (sscanf(so_rcv->sb_data, "%d%*[ ,]%d", &n1, &n2) == 2) {
785#endif
786 HTONS(n1);
787 HTONS(n2);
788 /* n2 is the one on our host */
789 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
790 if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
791 tmpso->so_lport == n2 &&
792 tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
793 tmpso->so_fport == n1) {
794 if (getsockname(tmpso->s,
795 (struct sockaddr *)&addr, &addrlen) == 0)
796 n2 = ntohs(addr.sin_port);
797 break;
798 }
799 }
800 }
801 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
802 so_rcv->sb_rptr = so_rcv->sb_data;
803 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
804 }
805#ifdef VBOX
806 m_free(pData, m);
807#else /* !VBOX */
808 m_free(m);
809#endif /* !VBOX */
810 return 0;
811 }
812
813#if 0
814 case EMU_RLOGIN:
815 /*
816 * Rlogin emulation
817 * First we accumulate all the initial option negotiation,
818 * then fork_exec() rlogin according to the options
819 */
820 {
821 int i, i2, n;
822 char *ptr;
823 char args[100];
824 char term[100];
825 struct sbuf *so_snd = &so->so_snd;
826 struct sbuf *so_rcv = &so->so_rcv;
827
828 /* First check if they have a priveladged port, or too much data has arrived */
829 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
830 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
831 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
832 so_snd->sb_wptr += 18;
833 so_snd->sb_cc += 18;
834 tcp_sockclosed(sototcpcb(so));
835 m_free(m);
836 return 0;
837 }
838
839 /* Append the current data */
840 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
841 so_rcv->sb_wptr += m->m_len;
842 so_rcv->sb_rptr += m->m_len;
843 m_free(m);
844
845 /*
846 * Check if we have all the initial options,
847 * and build argument list to rlogin while we're here
848 */
849 n = 0;
850 ptr = so_rcv->sb_data;
851 args[0] = 0;
852 term[0] = 0;
853 while (ptr < so_rcv->sb_wptr) {
854 if (*ptr++ == 0) {
855 n++;
856 if (n == 2) {
857 sprintf(args, "rlogin -l %s %s",
858 ptr, inet_ntoa(so->so_faddr));
859 } else if (n == 3) {
860 i2 = so_rcv->sb_wptr - ptr;
861 for (i = 0; i < i2; i++) {
862 if (ptr[i] == '/') {
863 ptr[i] = 0;
864#ifdef HAVE_SETENV
865 sprintf(term, "%s", ptr);
866#else
867 sprintf(term, "TERM=%s", ptr);
868#endif
869 ptr[i] = '/';
870 break;
871 }
872 }
873 }
874 }
875 }
876
877 if (n != 4)
878 return 0;
879
880 /* We have it, set our term variable and fork_exec() */
881#ifdef HAVE_SETENV
882 setenv("TERM", term, 1);
883#else
884 putenv(term);
885#endif
886 fork_exec(so, args, 2);
887 term[0] = 0;
888 so->so_emu = 0;
889
890 /* And finally, send the client a 0 character */
891 so_snd->sb_wptr[0] = 0;
892 so_snd->sb_wptr++;
893 so_snd->sb_cc++;
894
895 return 0;
896 }
897
898 case EMU_RSH:
899 /*
900 * rsh emulation
901 * First we accumulate all the initial option negotiation,
902 * then rsh_exec() rsh according to the options
903 */
904 {
905 int n;
906 char *ptr;
907 char *user;
908 char *args;
909 struct sbuf *so_snd = &so->so_snd;
910 struct sbuf *so_rcv = &so->so_rcv;
911
912 /* First check if they have a priveladged port, or too much data has arrived */
913 if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
914 (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
915 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
916 so_snd->sb_wptr += 18;
917 so_snd->sb_cc += 18;
918 tcp_sockclosed(sototcpcb(so));
919 m_free(m);
920 return 0;
921 }
922
923 /* Append the current data */
924 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
925 so_rcv->sb_wptr += m->m_len;
926 so_rcv->sb_rptr += m->m_len;
927 m_free(m);
928
929 /*
930 * Check if we have all the initial options,
931 * and build argument list to rlogin while we're here
932 */
933 n = 0;
934 ptr = so_rcv->sb_data;
935 user="";
936 args="";
937 if (so->extra==NULL) {
938 struct socket *ns;
939 struct tcpcb* tp;
940 int port=atoi(ptr);
941 if (port <= 0) return 0;
942 if (port > 1023 || port < 512) {
943 memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
944 so_snd->sb_wptr += 18;
945 so_snd->sb_cc += 18;
946 tcp_sockclosed(sototcpcb(so));
947 return 0;
948 }
949 if ((ns=socreate()) == NULL)
950 return 0;
951 if (tcp_attach(ns)<0) {
952 free(ns);
953 return 0;
954 }
955
956 ns->so_laddr=so->so_laddr;
957 ns->so_lport=htons(port);
958
959 (void) tcp_mss(sototcpcb(ns), 0);
960
961 ns->so_faddr=so->so_faddr;
962 ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
963
964 if (ns->so_faddr.s_addr == 0 ||
965 ns->so_faddr.s_addr == loopback_addr.s_addr)
966 ns->so_faddr = alias_addr;
967
968 ns->so_iptos = tcp_tos(ns);
969 tp = sototcpcb(ns);
970
971 tcp_template(tp);
972
973 /* Compute window scaling to request. */
974 /* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
975 * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
976 * tp->request_r_scale++;
977 */
978
979 /*soisfconnecting(ns);*/
980
981 tcpstat.tcps_connattempt++;
982
983 tp->t_state = TCPS_SYN_SENT;
984 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
985 tp->iss = tcp_iss;
986 tcp_iss += TCP_ISSINCR/2;
987 tcp_sendseqinit(tp);
988 tcp_output(tp);
989 so->extra=ns;
990 }
991 while (ptr < so_rcv->sb_wptr) {
992 if (*ptr++ == 0) {
993 n++;
994 if (n == 2) {
995 user=ptr;
996 } else if (n == 3) {
997 args=ptr;
998 }
999 }
1000 }
1001
1002 if (n != 4)
1003 return 0;
1004
1005 rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
1006 so->so_emu = 0;
1007 so->extra=NULL;
1008
1009 /* And finally, send the client a 0 character */
1010 so_snd->sb_wptr[0] = 0;
1011 so_snd->sb_wptr++;
1012 so_snd->sb_cc++;
1013
1014 return 0;
1015 }
1016
1017 case EMU_CTL:
1018 {
1019 int num;
1020 struct sbuf *so_snd = &so->so_snd;
1021 struct sbuf *so_rcv = &so->so_rcv;
1022
1023 /*
1024 * If there is binary data here, we save it in so->so_m
1025 */
1026 if (!so->so_m) {
1027 int rxlen;
1028 char *rxdata;
1029 rxdata=mtod(m, char *);
1030 for (rxlen=m->m_len; rxlen; rxlen--) {
1031 if (*rxdata++ & 0x80) {
1032 so->so_m = m;
1033 return 0;
1034 }
1035 }
1036 } /* if(so->so_m==NULL) */
1037
1038 /*
1039 * Append the line
1040 */
1041 sbappendsb(so_rcv, m);
1042
1043 /* To avoid going over the edge of the buffer, we reset it */
1044 if (so_snd->sb_cc == 0)
1045 so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
1046
1047 /*
1048 * A bit of a hack:
1049 * If the first packet we get here is 1 byte long, then it
1050 * was done in telnet character mode, therefore we must echo
1051 * the characters as they come. Otherwise, we echo nothing,
1052 * because in linemode, the line is already echoed
1053 * XXX two or more control connections won't work
1054 */
1055 if (do_echo == -1) {
1056 if (m->m_len == 1) do_echo = 1;
1057 else do_echo = 0;
1058 }
1059 if (do_echo) {
1060 sbappendsb(so_snd, m);
1061 m_free(m);
1062 tcp_output(sototcpcb(so)); /* XXX */
1063 } else
1064 m_free(m);
1065
1066 num = 0;
1067 while (num < so->so_rcv.sb_cc) {
1068 if (*(so->so_rcv.sb_rptr + num) == '\n' ||
1069 *(so->so_rcv.sb_rptr + num) == '\r') {
1070 int n;
1071
1072 *(so_rcv->sb_rptr + num) = 0;
1073 if (ctl_password && !ctl_password_ok) {
1074 /* Need a password */
1075 if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
1076 if (strcmp(buff, ctl_password) == 0) {
1077 ctl_password_ok = 1;
1078 n = sprintf(so_snd->sb_wptr,
1079 "Password OK.\r\n");
1080 goto do_prompt;
1081 }
1082 }
1083 n = sprintf(so_snd->sb_wptr,
1084 "Error: Password required, log on with \"pass PASSWORD\"\r\n");
1085 goto do_prompt;
1086 }
1087 cfg_quitting = 0;
1088 n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
1089 if (!cfg_quitting) {
1090 /* Register the printed data */
1091do_prompt:
1092 so_snd->sb_cc += n;
1093 so_snd->sb_wptr += n;
1094 /* Add prompt */
1095 n = sprintf(so_snd->sb_wptr, "Slirp> ");
1096 so_snd->sb_cc += n;
1097 so_snd->sb_wptr += n;
1098 }
1099 /* Drop so_rcv data */
1100 so_rcv->sb_cc = 0;
1101 so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
1102 tcp_output(sototcpcb(so)); /* Send the reply */
1103 }
1104 num++;
1105 }
1106 return 0;
1107 }
1108#endif
1109 case EMU_FTP: /* ftp */
1110 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
1111 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
1112 /*
1113 * Need to emulate the PORT command
1114 */
1115#ifdef VBOX
1116 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
1117 &n1, &n2, &n3, &n4, &n5, &n6, buff);
1118#else
1119 x = sscanf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%256[^\177]",
1120 &n1, &n2, &n3, &n4, &n5, &n6, buff);
1121#endif
1122 if (x < 6)
1123 return 1;
1124
1125 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1126 lport = htons((n5 << 8) | (n6));
1127
1128#ifdef VBOX
1129 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1130#else /* !VBOX */
1131 if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1132#endif /* !VBOX */
1133 return 1;
1134
1135 n6 = ntohs(so->so_fport);
1136
1137 n5 = (n6 >> 8) & 0xff;
1138 n6 &= 0xff;
1139
1140 laddr = ntohl(so->so_faddr.s_addr);
1141
1142 n1 = ((laddr >> 24) & 0xff);
1143 n2 = ((laddr >> 16) & 0xff);
1144 n3 = ((laddr >> 8) & 0xff);
1145 n4 = (laddr & 0xff);
1146
1147 m->m_len = bptr - m->m_data; /* Adjust length */
1148 m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
1149 n1, n2, n3, n4, n5, n6, x==7?buff:"");
1150 return 1;
1151 } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
1152 /*
1153 * Need to emulate the PASV response
1154 */
1155#ifdef VBOX
1156 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
1157 &n1, &n2, &n3, &n4, &n5, &n6, buff);
1158#else
1159 x = sscanf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%256[^\177]",
1160 &n1, &n2, &n3, &n4, &n5, &n6, buff);
1161#endif
1162 if (x < 6)
1163 return 1;
1164
1165 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1166 lport = htons((n5 << 8) | (n6));
1167
1168#ifdef VBOX
1169 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1170#else /* !VBOX */
1171 if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1172#endif /* !VBOX */
1173 return 1;
1174
1175 n6 = ntohs(so->so_fport);
1176
1177 n5 = (n6 >> 8) & 0xff;
1178 n6 &= 0xff;
1179
1180 laddr = ntohl(so->so_faddr.s_addr);
1181
1182 n1 = ((laddr >> 24) & 0xff);
1183 n2 = ((laddr >> 16) & 0xff);
1184 n3 = ((laddr >> 8) & 0xff);
1185 n4 = (laddr & 0xff);
1186
1187 m->m_len = bptr - m->m_data; /* Adjust length */
1188 m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
1189 n1, n2, n3, n4, n5, n6, x==7?buff:"");
1190
1191 return 1;
1192 }
1193
1194 return 1;
1195
1196 case EMU_KSH:
1197 /*
1198 * The kshell (Kerberos rsh) and shell services both pass
1199 * a local port port number to carry signals to the server
1200 * and stderr to the client. It is passed at the beginning
1201 * of the connection as a NUL-terminated decimal ASCII string.
1202 */
1203 so->so_emu = 0;
1204 for (lport = 0, i = 0; i < m->m_len-1; ++i) {
1205 if (m->m_data[i] < '0' || m->m_data[i] > '9')
1206 return 1; /* invalid number */
1207 lport *= 10;
1208 lport += m->m_data[i] - '0';
1209 }
1210 if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
1211#ifdef VBOX
1212 (so = solisten(pData, 0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1213#else /* !VBOX */
1214 (so = solisten(0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1215#endif /* !VBOX */
1216 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
1217 return 1;
1218
1219 case EMU_IRC:
1220 /*
1221 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
1222 */
1223 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
1224 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
1225 return 1;
1226
1227 /* The %256s is for the broken mIRC */
1228 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1229#ifdef VBOX
1230 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1231#else /* !VBOX */
1232 if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1233#endif /* !VBOX */
1234 return 1;
1235
1236 m->m_len = bptr - m->m_data; /* Adjust length */
1237 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1238 (unsigned long)ntohl(so->so_faddr.s_addr),
1239 ntohs(so->so_fport), 1);
1240 } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1241#ifdef VBOX
1242 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1243#else /* !VBOX */
1244 if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1245#endif /* !VBOX */
1246 return 1;
1247
1248 m->m_len = bptr - m->m_data; /* Adjust length */
1249 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
1250 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1251 ntohs(so->so_fport), n1, 1);
1252 } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1253#ifdef VBOX
1254 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1255#else /* !VBOX */
1256 if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1257#endif /* !VBOX */
1258 return 1;
1259
1260 m->m_len = bptr - m->m_data; /* Adjust length */
1261 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1262 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1263 ntohs(so->so_fport), n1, 1);
1264 }
1265 return 1;
1266
1267#ifdef VBOX
1268 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
1269 * This is not legal when more than one slirp instance is active. */
1270#else /* !VBOX */
1271 case EMU_REALAUDIO:
1272 /*
1273 * RealAudio emulation - JP. We must try to parse the incoming
1274 * data and try to find the two characters that contain the
1275 * port number. Then we redirect an udp port and replace the
1276 * number with the real port we got.
1277 *
1278 * The 1.0 beta versions of the player are not supported
1279 * any more.
1280 *
1281 * A typical packet for player version 1.0 (release version):
1282 *
1283 * 0000:50 4E 41 00 05
1284 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
1285 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
1286 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
1287 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
1288 *
1289 * Now the port number 0x1BD7 is found at offset 0x04 of the
1290 * Now the port number 0x1BD7 is found at offset 0x04 of the
1291 * second packet. This time we received five bytes first and
1292 * then the rest. You never know how many bytes you get.
1293 *
1294 * A typical packet for player version 2.0 (beta):
1295 *
1296 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
1297 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
1298 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
1299 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
1300 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
1301 *
1302 * Port number 0x1BC1 is found at offset 0x0d.
1303 *
1304 * This is just a horrible switch statement. Variable ra tells
1305 * us where we're going.
1306 */
1307
1308 bptr = m->m_data;
1309 while (bptr < m->m_data + m->m_len) {
1310 u_short p;
1311 static int ra = 0;
1312 char ra_tbl[4];
1313
1314 ra_tbl[0] = 0x50;
1315 ra_tbl[1] = 0x4e;
1316 ra_tbl[2] = 0x41;
1317 ra_tbl[3] = 0;
1318
1319 switch (ra) {
1320 case 0:
1321 case 2:
1322 case 3:
1323 if (*bptr++ != ra_tbl[ra]) {
1324 ra = 0;
1325 continue;
1326 }
1327 break;
1328
1329 case 1:
1330 /*
1331 * We may get 0x50 several times, ignore them
1332 */
1333 if (*bptr == 0x50) {
1334 ra = 1;
1335 bptr++;
1336 continue;
1337 } else if (*bptr++ != ra_tbl[ra]) {
1338 ra = 0;
1339 continue;
1340 }
1341 break;
1342
1343 case 4:
1344 /*
1345 * skip version number
1346 */
1347 bptr++;
1348 break;
1349
1350 case 5:
1351 /*
1352 * The difference between versions 1.0 and
1353 * 2.0 is here. For future versions of
1354 * the player this may need to be modified.
1355 */
1356 if (*(bptr + 1) == 0x02)
1357 bptr += 8;
1358 else
1359 bptr += 4;
1360 break;
1361
1362 case 6:
1363 /* This is the field containing the port
1364 * number that RA-player is listening to.
1365 */
1366 lport = (((u_char*)bptr)[0] << 8)
1367 + ((u_char *)bptr)[1];
1368 if (lport < 6970)
1369 lport += 256; /* don't know why */
1370 if (lport < 6970 || lport > 7170)
1371 return 1; /* failed */
1372
1373 /* try to get udp port between 6970 - 7170 */
1374 for (p = 6970; p < 7071; p++) {
1375 if (udp_listen( htons(p),
1376 so->so_laddr.s_addr,
1377 htons(lport),
1378 SS_FACCEPTONCE)) {
1379 break;
1380 }
1381 }
1382 if (p == 7071)
1383 p = 0;
1384 *(u_char *)bptr++ = (p >> 8) & 0xff;
1385 *(u_char *)bptr++ = p & 0xff;
1386 ra = 0;
1387 return 1; /* port redirected, we're done */
1388 break;
1389
1390 default:
1391 ra = 0;
1392 }
1393 ra++;
1394 }
1395 return 1;
1396#endif /* !VBOX */
1397
1398 default:
1399 /* Ooops, not emulated, won't call tcp_emu again */
1400 so->so_emu = 0;
1401 return 1;
1402 }
1403}
1404
1405/*
1406 * Do misc. config of SLiRP while its running.
1407 * Return 0 if this connections is to be closed, 1 otherwise,
1408 * return 2 if this is a command-line connection
1409 */
1410int
1411#ifdef VBOX
1412tcp_ctl(PNATState pData, struct socket *so)
1413#else /* !VBOX */
1414tcp_ctl(so)
1415 struct socket *so;
1416#endif /* !VBOX */
1417{
1418 struct sbuf *sb = &so->so_snd;
1419 int command;
1420 struct ex_list *ex_ptr;
1421 int do_pty;
1422 /* struct socket *tmpso; */
1423
1424 DEBUG_CALL("tcp_ctl");
1425 DEBUG_ARG("so = %lx", (long )so);
1426
1427#if 0
1428 /*
1429 * Check if they're authorised
1430 */
1431 if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
1432 sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
1433 sb->sb_wptr += sb->sb_cc;
1434 return 0;
1435 }
1436#endif
1437 command = (ntohl(so->so_faddr.s_addr) & 0xff);
1438
1439 switch(command) {
1440 default: /* Check for exec's */
1441
1442 /*
1443 * Check if it's pty_exec
1444 */
1445 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1446 if (ex_ptr->ex_fport == so->so_fport &&
1447 command == ex_ptr->ex_addr) {
1448 do_pty = ex_ptr->ex_pty;
1449 goto do_exec;
1450 }
1451 }
1452
1453 /*
1454 * Nothing bound..
1455 */
1456 /* tcp_fconnect(so); */
1457
1458 /* FALLTHROUGH */
1459 case CTL_ALIAS:
1460 sb->sb_cc = sprintf(sb->sb_wptr,
1461 "Error: No application configured.\r\n");
1462 sb->sb_wptr += sb->sb_cc;
1463 return(0);
1464
1465 do_exec:
1466 DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
1467#ifdef VBOX
1468 return(fork_exec(pData, so, ex_ptr->ex_exec, do_pty));
1469#else /* !VBOX */
1470 return(fork_exec(so, ex_ptr->ex_exec, do_pty));
1471#endif /* !VBOX */
1472
1473#if 0
1474 case CTL_CMD:
1475 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
1476 if (tmpso->so_emu == EMU_CTL &&
1477 !(tmpso->so_tcpcb?
1478 (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
1479 :0)) {
1480 /* Ooops, control connection already active */
1481 sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
1482 sb->sb_wptr += sb->sb_cc;
1483 return 0;
1484 }
1485 }
1486 so->so_emu = EMU_CTL;
1487 ctl_password_ok = 0;
1488 sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
1489 sb->sb_wptr += sb->sb_cc;
1490 do_echo=-1;
1491 return(2);
1492#endif
1493 }
1494}
1495
1496#if defined(VBOX) && SIZEOF_CHAR_P != 4
1497/**
1498 * Slow pointer hashing that deals with automatic inserting and collisions.
1499 */
1500uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
1501{
1502 uint32_t i;
1503 if (pv == NULL)
1504 i = 0;
1505 else
1506 {
1507 const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1508 if (pData->apvHash[i1] == pv)
1509 i = i1;
1510 else
1511 {
1512 /*
1513 * Try up to 10 times then assume it's an insertion.
1514 * If we didn't find a free entry by then, try another 100 times.
1515 * If that fails, give up.
1516 */
1517 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1518 uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
1519 int cTries = 10;
1520 int cTries2 = 100;
1521
1522 i = i1;
1523 for (;;)
1524 {
1525 /* check if we should give in.*/
1526 if (--cTries > 0)
1527 {
1528 if (i1stFree != 0)
1529 {
1530 i = i1stFree;
1531 pData->apvHash[i] = pv;
1532 pData->cpvHashUsed++;
1533 if (i != i1)
1534 pData->cpvHashCollisions++;
1535 pData->cpvHashInserts++;
1536 break;
1537 }
1538 if (!cTries2)
1539 {
1540 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
1541 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1542 i = 0;
1543 break;
1544 }
1545 cTries = cTries2;
1546 cTries2 = 0;
1547 }
1548
1549 /* advance to the next hash entry and test it. */
1550 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1551 while (RT_UNLIKELY(!i))
1552 i = (i + i2) % RT_ELEMENTS(pData->apvHash);
1553 if (pData->apvHash[i] == pv)
1554 break;
1555 if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
1556 i1stFree = i;
1557 }
1558 }
1559 }
1560 return i;
1561}
1562
1563
1564/**
1565 * Removes the pointer from the hash table.
1566 */
1567void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
1568{
1569 /* We don't count NULL pointers. */
1570 if (pv == NULL)
1571 return;
1572 pData->cpvHashDone++;
1573
1574 /* try the hint */
1575 if ( iHint
1576 && iHint < RT_ELEMENTS(pData->apvHash)
1577 && pData->apvHash[iHint] == pv)
1578 {
1579 pData->apvHash[iHint] = NULL;
1580 pData->cpvHashUsed--;
1581 return;
1582 }
1583
1584 iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
1585 if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
1586 {
1587 /*
1588 * Try up to 120 times then assert.
1589 */
1590 const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
1591 int cTries = 120;
1592 for (;;)
1593 {
1594 /* advance to the next hash entry and test it. */
1595 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1596 while (RT_UNLIKELY(!iHint))
1597 iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
1598 if (pData->apvHash[iHint] == pv)
1599 break;
1600
1601 /* check if we should give in.*/
1602 if (--cTries > 0)
1603 {
1604 AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
1605 pv, pData->cpvHashUsed, pData->cpvHashCollisions));
1606 return;
1607 }
1608 }
1609 }
1610
1611 /* found it */
1612 pData->apvHash[iHint] = NULL;
1613 pData->cpvHashUsed--;
1614}
1615
1616#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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