2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1988, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
Remove the advertising clause from the slirp license
According to the FSF, the 4-clause BSD license, which slirp is covered under,
is not compatible with the GPL or LGPL[1].
[1] http://www.fsf.org/licensing/licenses/index_html#GPLIncompatibleLicenses
There are three declared copyright holders in slirp that use the 4-clause
BSD license, the Regents of UC Berkley, Danny Gasparovski, and Kelly Price.
Below are the appropriate permissions to remove the advertise clause from slirp
from each party.
Special thanks go to Richard Fontana from Red Hat for contacting all of the
necessary authors to resolve this issue!
Regents of UC Berkley:
From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
July 22, 1999
To All Licensees, Distributors of Any Version of BSD:
As you know, certain of the Berkeley Software Distribution ("BSD") source
code files require that further distributions of products containing all or
portions of the software, acknowledge within their advertising materials
that such products contain software developed by UC Berkeley and its
contributors.
Specifically, the provision reads:
" * 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors."
Effective immediately, licensees and distributors are no longer required to
include the acknowledgement within advertising materials. Accordingly, the
foregoing paragraph of those BSD Unix files containing it is hereby deleted
in its entirety.
William Hoskins
Director, Office of Technology Licensing
University of California, Berkeley
Danny Gasparovski:
Subject: RE: Slirp license
Date: Thu, 8 Jan 2009 10:51:00 +1100
From: "Gasparovski, Daniel" <Daniel.Gasparovski@ato.gov.au>
To: "Richard Fontana" <rfontana@redhat.com>
Hi Richard,
I have no objection to having Slirp code in QEMU be licensed under the
3-clause BSD license.
Thanks for taking the effort to consult me about this.
Dan ...
Kelly Price:
Date: Thu, 8 Jan 2009 19:38:56 -0500
From: "Kelly Price" <strredwolf@gmail.com>
To: "Richard Fontana" <rfontana@redhat.com>
Subject: Re: Slirp license
Thanks for contacting me, Richard. I'm glad you were able to find
Dan, as I've been "keeping the light on" for Slirp. I have no use for
it now, and I have little time for it (now holding onto Keenspot's
Comic Genesis and having a regular US state government position). If
Dan would like to return to the project, I'd love to give it back to
him.
As for copyright, I don't own all of it. Dan does, so I will defer to
him. Any of my patches I will gladly license to the 3-part BSD
license. My interest in re-licensing was because we didn't have ready
info to contact Dan. If Dan would like to port Slirp back out of
QEMU, a lot of us 64-bit users would be grateful.
Feel free to share this email address with Dan. I will be glad to
effect a transfer of the project to him and Mr. Bellard of the QEMU
project.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6451 c046a42c-6fe2-441c-8c8c-71466251a162
2009-01-27 03:37:41 +08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2004-04-22 08:10:48 +08:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
|
|
|
|
* tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Changes and additions relating to SLiRP
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski.
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
|
|
|
* Please read the file COPYRIGHT for the
|
2004-04-22 08:10:48 +08:00
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <slirp.h>
|
|
|
|
|
|
|
|
/* patchable/settable parameters for tcp */
|
2007-10-27 03:01:16 +08:00
|
|
|
/* Don't do rfc1323 performance enhancements */
|
|
|
|
#define TCP_DO_RFC1323 0
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tcp initialization
|
|
|
|
*/
|
|
|
|
void
|
2009-06-24 20:42:31 +08:00
|
|
|
tcp_init(Slirp *slirp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
slirp->tcp_iss = 1; /* wrong */
|
|
|
|
slirp->tcb.so_next = slirp->tcb.so_prev = &slirp->tcb;
|
|
|
|
slirp->tcp_last_so = &slirp->tcb;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-01 02:14:23 +08:00
|
|
|
void tcp_cleanup(Slirp *slirp)
|
|
|
|
{
|
|
|
|
while (slirp->tcb.so_next != &slirp->tcb) {
|
|
|
|
tcp_close(sototcpcb(slirp->tcb.so_next));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Create template to be used to send tcp packets on a connection.
|
|
|
|
* Call after host entry created, fills
|
|
|
|
* in a skeletal tcp/ip header, minimizing the amount of work
|
|
|
|
* necessary when the connection is used.
|
|
|
|
*/
|
|
|
|
void
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_template(struct tcpcb *tp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct socket *so = tp->t_socket;
|
|
|
|
register struct tcpiphdr *n = &tp->t_template;
|
|
|
|
|
2009-01-14 03:48:42 +08:00
|
|
|
n->ti_mbuf = NULL;
|
2004-04-22 08:10:48 +08:00
|
|
|
n->ti_x1 = 0;
|
|
|
|
n->ti_pr = IPPROTO_TCP;
|
|
|
|
n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
|
|
|
|
n->ti_src = so->so_faddr;
|
|
|
|
n->ti_dst = so->so_laddr;
|
|
|
|
n->ti_sport = so->so_fport;
|
|
|
|
n->ti_dport = so->so_lport;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
n->ti_seq = 0;
|
|
|
|
n->ti_ack = 0;
|
|
|
|
n->ti_x2 = 0;
|
|
|
|
n->ti_off = 5;
|
|
|
|
n->ti_flags = 0;
|
|
|
|
n->ti_win = 0;
|
|
|
|
n->ti_sum = 0;
|
|
|
|
n->ti_urp = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a single message to the TCP at address specified by
|
|
|
|
* the given TCP/IP header. If m == 0, then we make a copy
|
|
|
|
* of the tcpiphdr at ti and send directly to the addressed host.
|
|
|
|
* This is used to force keep alive messages out using the TCP
|
|
|
|
* template for a connection tp->t_template. If flags are given
|
|
|
|
* then we send a message back to the TCP which originated the
|
|
|
|
* segment ti, and discard the mbuf containing it and any other
|
|
|
|
* attached mbufs.
|
|
|
|
*
|
|
|
|
* In any case the ack and sequence number of the transmitted
|
|
|
|
* segment are as specified by the parameters.
|
|
|
|
*/
|
|
|
|
void
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
|
|
|
|
tcp_seq ack, tcp_seq seq, int flags)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
register int tlen;
|
|
|
|
int win = 0;
|
|
|
|
|
|
|
|
DEBUG_CALL("tcp_respond");
|
2012-09-05 05:20:35 +08:00
|
|
|
DEBUG_ARG("tp = %p", tp);
|
|
|
|
DEBUG_ARG("ti = %p", ti);
|
|
|
|
DEBUG_ARG("m = %p", m);
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_ARG("ack = %u", ack);
|
|
|
|
DEBUG_ARG("seq = %u", seq);
|
|
|
|
DEBUG_ARG("flags = %x", flags);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (tp)
|
|
|
|
win = sbspace(&tp->t_socket->so_rcv);
|
2009-03-07 23:32:56 +08:00
|
|
|
if (m == NULL) {
|
2012-09-05 05:20:36 +08:00
|
|
|
if (!tp || (m = m_get(tp->t_socket->slirp)) == NULL)
|
2004-04-22 08:10:48 +08:00
|
|
|
return;
|
|
|
|
tlen = 0;
|
2007-10-27 03:01:16 +08:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-04-22 08:10:48 +08:00
|
|
|
*mtod(m, struct tcpiphdr *) = *ti;
|
|
|
|
ti = mtod(m, struct tcpiphdr *);
|
|
|
|
flags = TH_ACK;
|
|
|
|
} else {
|
2007-09-17 05:08:06 +08:00
|
|
|
/*
|
2004-04-22 08:10:48 +08:00
|
|
|
* ti points into m so the next line is just making
|
|
|
|
* the mbuf point to ti
|
|
|
|
*/
|
|
|
|
m->m_data = (caddr_t)ti;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = sizeof (struct tcpiphdr);
|
|
|
|
tlen = 0;
|
|
|
|
#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
|
2010-07-23 04:15:23 +08:00
|
|
|
xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, uint32_t);
|
|
|
|
xchg(ti->ti_dport, ti->ti_sport, uint16_t);
|
2004-04-22 08:10:48 +08:00
|
|
|
#undef xchg
|
|
|
|
}
|
|
|
|
ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
|
|
|
|
tlen += sizeof (struct tcpiphdr);
|
|
|
|
m->m_len = tlen;
|
|
|
|
|
2009-03-07 23:32:56 +08:00
|
|
|
ti->ti_mbuf = NULL;
|
2004-04-22 08:10:48 +08:00
|
|
|
ti->ti_x1 = 0;
|
|
|
|
ti->ti_seq = htonl(seq);
|
|
|
|
ti->ti_ack = htonl(ack);
|
|
|
|
ti->ti_x2 = 0;
|
|
|
|
ti->ti_off = sizeof (struct tcphdr) >> 2;
|
|
|
|
ti->ti_flags = flags;
|
|
|
|
if (tp)
|
2010-07-23 04:15:23 +08:00
|
|
|
ti->ti_win = htons((uint16_t) (win >> tp->rcv_scale));
|
2004-04-22 08:10:48 +08:00
|
|
|
else
|
2010-07-23 04:15:23 +08:00
|
|
|
ti->ti_win = htons((uint16_t)win);
|
2004-04-22 08:10:48 +08:00
|
|
|
ti->ti_urp = 0;
|
|
|
|
ti->ti_sum = 0;
|
|
|
|
ti->ti_sum = cksum(m, tlen);
|
|
|
|
((struct ip *)ti)->ip_len = tlen;
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
if(flags & TH_RST)
|
2004-04-22 08:10:48 +08:00
|
|
|
((struct ip *)ti)->ip_ttl = MAXTTL;
|
2007-09-17 05:08:06 +08:00
|
|
|
else
|
2007-10-27 03:01:16 +08:00
|
|
|
((struct ip *)ti)->ip_ttl = IPDEFTTL;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
(void) ip_output((struct socket *)0, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new TCP control block, making an
|
|
|
|
* empty reassembly queue and hooking it to the argument
|
|
|
|
* protocol control block.
|
|
|
|
*/
|
|
|
|
struct tcpcb *
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_newtcpcb(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
register struct tcpcb *tp;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
tp = (struct tcpcb *)malloc(sizeof(*tp));
|
|
|
|
if (tp == NULL)
|
|
|
|
return ((struct tcpcb *)0);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
memset((char *) tp, 0, sizeof(struct tcpcb));
|
2009-01-14 03:48:42 +08:00
|
|
|
tp->seg_next = tp->seg_prev = (struct tcpiphdr*)tp;
|
2007-10-27 03:01:16 +08:00
|
|
|
tp->t_maxseg = TCP_MSS;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2007-10-27 03:01:16 +08:00
|
|
|
tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
|
2004-04-22 08:10:48 +08:00
|
|
|
tp->t_socket = so;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
|
|
|
|
* rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
|
|
|
|
* reasonable initial retransmit time.
|
|
|
|
*/
|
|
|
|
tp->t_srtt = TCPTV_SRTTBASE;
|
2007-10-27 03:01:16 +08:00
|
|
|
tp->t_rttvar = TCPTV_SRTTDFLT << 2;
|
2004-04-22 08:10:48 +08:00
|
|
|
tp->t_rttmin = TCPTV_MIN;
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
TCPT_RANGESET(tp->t_rxtcur,
|
2004-04-22 08:10:48 +08:00
|
|
|
((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
|
|
|
|
TCPTV_MIN, TCPTV_REXMTMAX);
|
|
|
|
|
|
|
|
tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
|
|
|
tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
|
|
|
tp->t_state = TCPS_CLOSED;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_tcpcb = tp;
|
|
|
|
|
|
|
|
return (tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop a TCP connection, reporting
|
|
|
|
* the specified error. If connection is synchronized,
|
|
|
|
* then send a RST to peer.
|
|
|
|
*/
|
2007-09-17 05:08:06 +08:00
|
|
|
struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
DEBUG_CALL("tcp_drop");
|
|
|
|
DEBUG_ARG("tp = %lx", (long)tp);
|
|
|
|
DEBUG_ARG("errno = %d", errno);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
if (TCPS_HAVERCVDSYN(tp->t_state)) {
|
|
|
|
tp->t_state = TCPS_CLOSED;
|
|
|
|
(void) tcp_output(tp);
|
2009-06-24 20:42:29 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
return (tcp_close(tp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a TCP control block:
|
|
|
|
* discard all space held by the tcp
|
|
|
|
* discard internet protocol block
|
|
|
|
* wake up any sleepers
|
|
|
|
*/
|
|
|
|
struct tcpcb *
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_close(struct tcpcb *tp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
register struct tcpiphdr *t;
|
|
|
|
struct socket *so = tp->t_socket;
|
2009-06-24 20:42:31 +08:00
|
|
|
Slirp *slirp = so->slirp;
|
2004-04-22 08:10:48 +08:00
|
|
|
register struct mbuf *m;
|
|
|
|
|
|
|
|
DEBUG_CALL("tcp_close");
|
|
|
|
DEBUG_ARG("tp = %lx", (long )tp);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* free the reassembly queue, if any */
|
2009-01-14 03:48:42 +08:00
|
|
|
t = tcpfrag_list_first(tp);
|
|
|
|
while (!tcpfrag_list_end(t, tp)) {
|
|
|
|
t = tcpiphdr_next(t);
|
|
|
|
m = tcpiphdr_prev(t)->ti_mbuf;
|
|
|
|
remque(tcpiphdr2qlink(tcpiphdr_prev(t)));
|
2011-07-20 18:20:16 +08:00
|
|
|
m_free(m);
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
free(tp);
|
2009-03-07 23:32:56 +08:00
|
|
|
so->so_tcpcb = NULL;
|
2004-04-22 08:10:48 +08:00
|
|
|
/* clobber input socket cache if we're closing the cached connection */
|
2009-06-24 20:42:31 +08:00
|
|
|
if (so == slirp->tcp_last_so)
|
|
|
|
slirp->tcp_last_so = &slirp->tcb;
|
2004-07-13 06:33:07 +08:00
|
|
|
closesocket(so->s);
|
2004-04-22 08:10:48 +08:00
|
|
|
sbfree(&so->so_rcv);
|
|
|
|
sbfree(&so->so_snd);
|
|
|
|
sofree(so);
|
|
|
|
return ((struct tcpcb *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TCP protocol interface to socket abstraction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User issued close, and wish to trail through shutdown states:
|
|
|
|
* if never received SYN, just forget it. If got a SYN from peer,
|
|
|
|
* but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
|
|
|
|
* If already got a FIN from peer, then almost done; go to LAST_ACK
|
|
|
|
* state. In all other cases, have already sent FIN to peer (e.g.
|
|
|
|
* after PRU_SHUTDOWN), and just have to play tedious game waiting
|
|
|
|
* for peer to send FIN or not respond to keep-alives, etc.
|
|
|
|
* We can let the user exit from the close as soon as the FIN is acked.
|
|
|
|
*/
|
|
|
|
void
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_sockclosed(struct tcpcb *tp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
DEBUG_CALL("tcp_sockclosed");
|
|
|
|
DEBUG_ARG("tp = %lx", (long)tp);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
switch (tp->t_state) {
|
|
|
|
|
|
|
|
case TCPS_CLOSED:
|
|
|
|
case TCPS_LISTEN:
|
|
|
|
case TCPS_SYN_SENT:
|
|
|
|
tp->t_state = TCPS_CLOSED;
|
|
|
|
tp = tcp_close(tp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCPS_SYN_RECEIVED:
|
|
|
|
case TCPS_ESTABLISHED:
|
|
|
|
tp->t_state = TCPS_FIN_WAIT_1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCPS_CLOSE_WAIT:
|
|
|
|
tp->t_state = TCPS_LAST_ACK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tp)
|
|
|
|
tcp_output(tp);
|
|
|
|
}
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
/*
|
2004-04-22 08:10:48 +08:00
|
|
|
* Connect to a host on the Internet
|
|
|
|
* Called by tcp_input
|
|
|
|
* Only do a connect, the tcp fields will be set in tcp_input
|
|
|
|
* return 0 if there's a result of the connect,
|
|
|
|
* else return -1 means we're still connecting
|
|
|
|
* The return value is almost always -1 since the socket is
|
2007-09-17 05:08:06 +08:00
|
|
|
* nonblocking. Connect returns after the SYN is sent, and does
|
2004-04-22 08:10:48 +08:00
|
|
|
* not wait for ACK+SYN.
|
|
|
|
*/
|
2009-03-07 23:32:56 +08:00
|
|
|
int tcp_fconnect(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
Slirp *slirp = so->slirp;
|
2004-04-22 08:10:48 +08:00
|
|
|
int ret=0;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_CALL("tcp_fconnect");
|
|
|
|
DEBUG_ARG("so = %lx", (long )so);
|
|
|
|
|
2009-12-02 19:24:42 +08:00
|
|
|
if( (ret = so->s = qemu_socket(AF_INET,SOCK_STREAM,0)) >= 0) {
|
2004-04-22 08:10:48 +08:00
|
|
|
int opt, s=so->s;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
2012-03-22 08:02:52 +08:00
|
|
|
socket_set_nonblock(s);
|
2004-04-22 08:10:48 +08:00
|
|
|
opt = 1;
|
2013-03-09 02:58:32 +08:00
|
|
|
qemu_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
2004-04-22 08:10:48 +08:00
|
|
|
opt = 1;
|
2013-03-09 02:58:32 +08:00
|
|
|
qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
addr.sin_family = AF_INET;
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
|
|
|
|
slirp->vnetwork_addr.s_addr) {
|
2004-04-22 08:10:48 +08:00
|
|
|
/* It's an alias */
|
2009-06-24 20:42:31 +08:00
|
|
|
if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
|
2009-08-21 10:00:31 +08:00
|
|
|
if (get_dns_addr(&addr.sin_addr) < 0)
|
|
|
|
addr.sin_addr = loopback_addr;
|
2009-06-24 20:42:28 +08:00
|
|
|
} else {
|
2004-04-22 08:10:48 +08:00
|
|
|
addr.sin_addr = loopback_addr;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
addr.sin_addr = so->so_faddr;
|
|
|
|
addr.sin_port = so->so_fport;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
|
2007-09-17 05:08:06 +08:00
|
|
|
"addr.sin_addr.s_addr=%.16s\n",
|
2004-04-22 08:10:48 +08:00
|
|
|
ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
|
|
|
/* We don't care what port we get */
|
|
|
|
ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* If it's not in progress, it failed, so we just return 0,
|
|
|
|
* without clearing SS_NOFDREF
|
|
|
|
*/
|
|
|
|
soisfconnecting(so);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept the socket and connect to the local-host
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* We have a problem. The correct thing to do would be
|
|
|
|
* to first connect to the local-host, and only if the
|
|
|
|
* connection is accepted, then do an accept() here.
|
2007-09-17 05:08:06 +08:00
|
|
|
* But, a) we need to know who's trying to connect
|
2004-04-22 08:10:48 +08:00
|
|
|
* to the socket to be able to SYN the local-host, and
|
|
|
|
* b) we are already connected to the foreign host by
|
|
|
|
* the time it gets to accept(), so... We simply accept
|
|
|
|
* here and SYN the local-host.
|
2007-09-17 05:08:06 +08:00
|
|
|
*/
|
2013-02-22 11:39:49 +08:00
|
|
|
void tcp_connect(struct socket *inso)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2013-02-22 11:39:49 +08:00
|
|
|
Slirp *slirp = inso->slirp;
|
|
|
|
struct socket *so;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_in);
|
|
|
|
struct tcpcb *tp;
|
|
|
|
int s, opt;
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
DEBUG_CALL("tcp_connect");
|
|
|
|
DEBUG_ARG("inso = %lx", (long)inso);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
/*
|
|
|
|
* If it's an SS_ACCEPTONCE socket, no need to socreate()
|
|
|
|
* another socket, just use the accept() socket.
|
|
|
|
*/
|
|
|
|
if (inso->so_state & SS_FACCEPTONCE) {
|
|
|
|
/* FACCEPTONCE already have a tcpcb */
|
|
|
|
so = inso;
|
|
|
|
} else {
|
|
|
|
so = socreate(slirp);
|
|
|
|
if (so == NULL) {
|
|
|
|
/* If it failed, get rid of the pending connection */
|
|
|
|
closesocket(accept(inso->s, (struct sockaddr *)&addr, &addrlen));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tcp_attach(so) < 0) {
|
|
|
|
free(so); /* NOT sofree */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
so->so_laddr = inso->so_laddr;
|
|
|
|
so->so_lport = inso->so_lport;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
tcp_mss(sototcpcb(so), 0);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
s = accept(inso->s, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
if (s < 0) {
|
|
|
|
tcp_close(sototcpcb(so)); /* This will sofree() as well */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket_set_nonblock(s);
|
|
|
|
opt = 1;
|
2013-03-09 02:58:32 +08:00
|
|
|
qemu_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
|
2013-02-22 11:39:49 +08:00
|
|
|
opt = 1;
|
2013-03-09 02:58:32 +08:00
|
|
|
qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
|
2013-02-22 11:39:50 +08:00
|
|
|
socket_set_nodelay(s);
|
2013-02-22 11:39:49 +08:00
|
|
|
|
|
|
|
so->so_fport = addr.sin_port;
|
|
|
|
so->so_faddr = addr.sin_addr;
|
|
|
|
/* Translate connections from localhost to the real hostname */
|
|
|
|
if (so->so_faddr.s_addr == 0 ||
|
|
|
|
(so->so_faddr.s_addr & loopback_mask) ==
|
|
|
|
(loopback_addr.s_addr & loopback_mask)) {
|
|
|
|
so->so_faddr = slirp->vhost_addr;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
/* Close the accept() socket, set right state */
|
|
|
|
if (inso->so_state & SS_FACCEPTONCE) {
|
|
|
|
/* If we only accept once, close the accept() socket */
|
|
|
|
closesocket(so->s);
|
|
|
|
|
|
|
|
/* Don't select it yet, even though we have an FD */
|
|
|
|
/* if it's not FACCEPTONCE, it's already NOFDREF */
|
|
|
|
so->so_state = SS_NOFDREF;
|
|
|
|
}
|
|
|
|
so->s = s;
|
|
|
|
so->so_state |= SS_INCOMING;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
so->so_iptos = tcp_tos(so);
|
|
|
|
tp = sototcpcb(so);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
tcp_template(tp);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2013-02-22 11:39:49 +08:00
|
|
|
tp->t_state = TCPS_SYN_SENT;
|
|
|
|
tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
|
|
|
tp->iss = slirp->tcp_iss;
|
|
|
|
slirp->tcp_iss += TCP_ISSINCR/2;
|
|
|
|
tcp_sendseqinit(tp);
|
|
|
|
tcp_output(tp);
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach a TCPCB to a socket.
|
|
|
|
*/
|
|
|
|
int
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_attach(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL)
|
|
|
|
return -1;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
insque(so, &so->slirp->tcb);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the socket's type of service field
|
|
|
|
*/
|
2007-10-27 03:01:16 +08:00
|
|
|
static const struct tos_t tcptos[] = {
|
2004-04-22 08:10:48 +08:00
|
|
|
{0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
|
|
|
|
{21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
|
|
|
|
{0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
|
|
|
|
{0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
|
|
|
|
{0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
|
|
|
|
{0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
|
|
|
|
{0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
|
|
|
|
{0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
|
|
|
|
{0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
|
|
|
|
{0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
|
|
|
|
{0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
|
|
|
|
{0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2009-06-24 20:42:29 +08:00
|
|
|
static struct emu_t *tcpemu = NULL;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* Return TOS according to the above table
|
|
|
|
*/
|
2010-07-23 04:15:23 +08:00
|
|
|
uint8_t
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_tos(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
struct emu_t *emup;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
while(tcptos[i].tos) {
|
|
|
|
if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
|
|
|
|
(tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
|
|
|
|
so->so_emu = tcptos[i].emu;
|
|
|
|
return tcptos[i].tos;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* Nope, lets see if there's a user-added one */
|
|
|
|
for (emup = tcpemu; emup; emup = emup->next) {
|
|
|
|
if ((emup->fport && (ntohs(so->so_fport) == emup->fport)) ||
|
|
|
|
(emup->lport && (ntohs(so->so_lport) == emup->lport))) {
|
|
|
|
so->so_emu = emup->emu;
|
|
|
|
return emup->tos;
|
|
|
|
}
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emulate programs that try and connect to us
|
|
|
|
* This includes ftp (the data connection is
|
|
|
|
* initiated by the server) and IRC (DCC CHAT and
|
|
|
|
* DCC SEND) for now
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* NOTE: It's possible to crash SLiRP by sending it
|
|
|
|
* unstandard strings to emulate... if this is a problem,
|
|
|
|
* more checks are needed here
|
|
|
|
*
|
|
|
|
* XXX Assumes the whole command came in one packet
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* XXX Some ftp clients will have their TOS set to
|
|
|
|
* LOWDELAY and so Nagel will kick in. Because of this,
|
|
|
|
* we'll get the first letter, followed by the rest, so
|
|
|
|
* we simply scan for ORT instead of PORT...
|
|
|
|
* DCC doesn't have this problem because there's other stuff
|
|
|
|
* in the packet before the DCC command.
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
|
|
|
* Return 1 if the mbuf m is still valid and should be
|
2004-04-22 08:10:48 +08:00
|
|
|
* sbappend()ed
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* NOTE: if you return 0 you MUST m_free() the mbuf!
|
|
|
|
*/
|
|
|
|
int
|
2009-03-07 23:32:56 +08:00
|
|
|
tcp_emu(struct socket *so, struct mbuf *m)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
Slirp *slirp = so->slirp;
|
2004-04-22 08:10:48 +08:00
|
|
|
u_int n1, n2, n3, n4, n5, n6;
|
2008-08-22 01:58:08 +08:00
|
|
|
char buff[257];
|
2010-07-23 04:15:23 +08:00
|
|
|
uint32_t laddr;
|
2004-04-22 08:10:48 +08:00
|
|
|
u_int lport;
|
|
|
|
char *bptr;
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
DEBUG_CALL("tcp_emu");
|
|
|
|
DEBUG_ARG("so = %lx", (long)so);
|
|
|
|
DEBUG_ARG("m = %lx", (long)m);
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
switch(so->so_emu) {
|
|
|
|
int x, i;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case EMU_IDENT:
|
|
|
|
/*
|
|
|
|
* Identification protocol as per rfc-1413
|
|
|
|
*/
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
struct socket *tmpso;
|
|
|
|
struct sockaddr_in addr;
|
2008-09-20 16:07:15 +08:00
|
|
|
socklen_t addrlen = sizeof(struct sockaddr_in);
|
2004-04-22 08:10:48 +08:00
|
|
|
struct sbuf *so_rcv = &so->so_rcv;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
|
|
|
|
so_rcv->sb_wptr += m->m_len;
|
|
|
|
so_rcv->sb_rptr += m->m_len;
|
|
|
|
m->m_data[m->m_len] = 0; /* NULL terminate */
|
|
|
|
if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
|
2007-10-27 03:01:16 +08:00
|
|
|
if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
|
2004-04-22 08:10:48 +08:00
|
|
|
HTONS(n1);
|
|
|
|
HTONS(n2);
|
|
|
|
/* n2 is the one on our host */
|
2009-06-24 20:42:31 +08:00
|
|
|
for (tmpso = slirp->tcb.so_next;
|
|
|
|
tmpso != &slirp->tcb;
|
|
|
|
tmpso = tmpso->so_next) {
|
2004-04-22 08:10:48 +08:00
|
|
|
if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
|
|
|
|
tmpso->so_lport == n2 &&
|
|
|
|
tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
|
|
|
|
tmpso->so_fport == n1) {
|
|
|
|
if (getsockname(tmpso->s,
|
|
|
|
(struct sockaddr *)&addr, &addrlen) == 0)
|
|
|
|
n2 = ntohs(addr.sin_port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 01:58:08 +08:00
|
|
|
so_rcv->sb_cc = snprintf(so_rcv->sb_data,
|
|
|
|
so_rcv->sb_datalen,
|
|
|
|
"%d,%d\r\n", n1, n2);
|
2004-04-22 08:10:48 +08:00
|
|
|
so_rcv->sb_rptr = so_rcv->sb_data;
|
|
|
|
so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
|
|
|
|
}
|
|
|
|
m_free(m);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case EMU_FTP: /* ftp */
|
2009-03-07 23:32:56 +08:00
|
|
|
*(m->m_data+m->m_len) = 0; /* NUL terminate for strstr */
|
2004-04-22 08:10:48 +08:00
|
|
|
if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
|
|
|
|
/*
|
|
|
|
* Need to emulate the PORT command
|
2007-09-17 16:09:54 +08:00
|
|
|
*/
|
2007-10-27 03:01:16 +08:00
|
|
|
x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
|
2004-04-22 08:10:48 +08:00
|
|
|
&n1, &n2, &n3, &n4, &n5, &n6, buff);
|
|
|
|
if (x < 6)
|
|
|
|
return 1;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
|
|
|
|
lport = htons((n5 << 8) | (n6));
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so = tcp_listen(slirp, INADDR_ANY, 0, laddr,
|
|
|
|
lport, SS_FACCEPTONCE)) == NULL) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
n6 = ntohs(so->so_fport);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
n5 = (n6 >> 8) & 0xff;
|
|
|
|
n6 &= 0xff;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
laddr = ntohl(so->so_faddr.s_addr);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
n1 = ((laddr >> 24) & 0xff);
|
|
|
|
n2 = ((laddr >> 16) & 0xff);
|
|
|
|
n3 = ((laddr >> 8) & 0xff);
|
|
|
|
n4 = (laddr & 0xff);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = bptr - m->m_data; /* Adjust length */
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
|
|
|
|
"ORT %d,%d,%d,%d,%d,%d\r\n%s",
|
|
|
|
n1, n2, n3, n4, n5, n6, x==7?buff:"");
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
|
|
|
} else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
|
|
|
|
/*
|
|
|
|
* Need to emulate the PASV response
|
|
|
|
*/
|
2007-10-27 03:01:16 +08:00
|
|
|
x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
|
2004-04-22 08:10:48 +08:00
|
|
|
&n1, &n2, &n3, &n4, &n5, &n6, buff);
|
|
|
|
if (x < 6)
|
|
|
|
return 1;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
|
|
|
|
lport = htons((n5 << 8) | (n6));
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so = tcp_listen(slirp, INADDR_ANY, 0, laddr,
|
|
|
|
lport, SS_FACCEPTONCE)) == NULL) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
n6 = ntohs(so->so_fport);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
n5 = (n6 >> 8) & 0xff;
|
|
|
|
n6 &= 0xff;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
laddr = ntohl(so->so_faddr.s_addr);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
n1 = ((laddr >> 24) & 0xff);
|
|
|
|
n2 = ((laddr >> 16) & 0xff);
|
|
|
|
n3 = ((laddr >> 8) & 0xff);
|
|
|
|
n4 = (laddr & 0xff);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = bptr - m->m_data; /* Adjust length */
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
|
|
|
|
"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
|
|
|
|
n1, n2, n3, n4, n5, n6, x==7?buff:"");
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case EMU_KSH:
|
|
|
|
/*
|
|
|
|
* The kshell (Kerberos rsh) and shell services both pass
|
|
|
|
* a local port port number to carry signals to the server
|
|
|
|
* and stderr to the client. It is passed at the beginning
|
|
|
|
* of the connection as a NUL-terminated decimal ASCII string.
|
|
|
|
*/
|
|
|
|
so->so_emu = 0;
|
|
|
|
for (lport = 0, i = 0; i < m->m_len-1; ++i) {
|
|
|
|
if (m->m_data[i] < '0' || m->m_data[i] > '9')
|
|
|
|
return 1; /* invalid number */
|
|
|
|
lport *= 10;
|
|
|
|
lport += m->m_data[i] - '0';
|
|
|
|
}
|
|
|
|
if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
|
2009-06-24 20:42:31 +08:00
|
|
|
(so = tcp_listen(slirp, INADDR_ANY, 0, so->so_laddr.s_addr,
|
|
|
|
htons(lport), SS_FACCEPTONCE)) != NULL)
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len = snprintf(m->m_data, m->m_hdr.mh_size, "%d",
|
|
|
|
ntohs(so->so_fport)) + 1;
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case EMU_IRC:
|
|
|
|
/*
|
|
|
|
* Need to emulate DCC CHAT, DCC SEND and DCC MOVE
|
|
|
|
*/
|
|
|
|
*(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
|
|
|
|
if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
|
|
|
|
return 1;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* The %256s is for the broken mIRC */
|
|
|
|
if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
|
|
|
|
htonl(laddr), htons(lport),
|
|
|
|
SS_FACCEPTONCE)) == NULL) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = bptr - m->m_data; /* Adjust length */
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
|
|
|
|
"DCC CHAT chat %lu %u%c\n",
|
|
|
|
(unsigned long)ntohl(so->so_faddr.s_addr),
|
|
|
|
ntohs(so->so_fport), 1);
|
2004-04-22 08:10:48 +08:00
|
|
|
} else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
|
|
|
|
htonl(laddr), htons(lport),
|
|
|
|
SS_FACCEPTONCE)) == NULL) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = bptr - m->m_data; /* Adjust length */
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
|
|
|
|
"DCC SEND %s %lu %u %u%c\n", buff,
|
|
|
|
(unsigned long)ntohl(so->so_faddr.s_addr),
|
|
|
|
ntohs(so->so_fport), n1, 1);
|
2004-04-22 08:10:48 +08:00
|
|
|
} else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
|
2009-06-24 20:42:31 +08:00
|
|
|
if ((so = tcp_listen(slirp, INADDR_ANY, 0,
|
|
|
|
htonl(laddr), htons(lport),
|
|
|
|
SS_FACCEPTONCE)) == NULL) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
m->m_len = bptr - m->m_data; /* Adjust length */
|
2008-08-22 01:58:08 +08:00
|
|
|
m->m_len += snprintf(bptr, m->m_hdr.mh_size,
|
|
|
|
"DCC MOVE %s %lu %u %u%c\n", buff,
|
|
|
|
(unsigned long)ntohl(so->so_faddr.s_addr),
|
|
|
|
ntohs(so->so_fport), n1, 1);
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case EMU_REALAUDIO:
|
2007-09-17 05:08:06 +08:00
|
|
|
/*
|
2004-04-22 08:10:48 +08:00
|
|
|
* RealAudio emulation - JP. We must try to parse the incoming
|
|
|
|
* data and try to find the two characters that contain the
|
|
|
|
* port number. Then we redirect an udp port and replace the
|
|
|
|
* number with the real port we got.
|
|
|
|
*
|
|
|
|
* The 1.0 beta versions of the player are not supported
|
|
|
|
* any more.
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* A typical packet for player version 1.0 (release version):
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2007-09-17 05:08:06 +08:00
|
|
|
* 0000:50 4E 41 00 05
|
2009-06-24 20:42:29 +08:00
|
|
|
* 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 ........g.l.c..P
|
2004-04-22 08:10:48 +08:00
|
|
|
* 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
|
|
|
|
* 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
|
|
|
|
* 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* Now the port number 0x1BD7 is found at offset 0x04 of the
|
|
|
|
* Now the port number 0x1BD7 is found at offset 0x04 of the
|
|
|
|
* second packet. This time we received five bytes first and
|
|
|
|
* then the rest. You never know how many bytes you get.
|
|
|
|
*
|
|
|
|
* A typical packet for player version 2.0 (beta):
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2009-06-24 20:42:29 +08:00
|
|
|
* 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA.............
|
|
|
|
* 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .gux.c..Win2.0.0
|
2004-04-22 08:10:48 +08:00
|
|
|
* 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
|
|
|
|
* 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
|
|
|
|
* 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* Port number 0x1BC1 is found at offset 0x0d.
|
2007-09-17 16:09:54 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* This is just a horrible switch statement. Variable ra tells
|
|
|
|
* us where we're going.
|
|
|
|
*/
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
bptr = m->m_data;
|
|
|
|
while (bptr < m->m_data + m->m_len) {
|
|
|
|
u_short p;
|
|
|
|
static int ra = 0;
|
2007-09-17 05:08:06 +08:00
|
|
|
char ra_tbl[4];
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
ra_tbl[0] = 0x50;
|
|
|
|
ra_tbl[1] = 0x4e;
|
|
|
|
ra_tbl[2] = 0x41;
|
|
|
|
ra_tbl[3] = 0;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
switch (ra) {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
if (*bptr++ != ra_tbl[ra]) {
|
|
|
|
ra = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case 1:
|
|
|
|
/*
|
|
|
|
* We may get 0x50 several times, ignore them
|
|
|
|
*/
|
|
|
|
if (*bptr == 0x50) {
|
|
|
|
ra = 1;
|
|
|
|
bptr++;
|
|
|
|
continue;
|
|
|
|
} else if (*bptr++ != ra_tbl[ra]) {
|
|
|
|
ra = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
case 4:
|
|
|
|
/*
|
2004-04-22 08:10:48 +08:00
|
|
|
* skip version number
|
|
|
|
*/
|
|
|
|
bptr++;
|
|
|
|
break;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
case 5:
|
2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* The difference between versions 1.0 and
|
|
|
|
* 2.0 is here. For future versions of
|
|
|
|
* the player this may need to be modified.
|
|
|
|
*/
|
|
|
|
if (*(bptr + 1) == 0x02)
|
|
|
|
bptr += 8;
|
|
|
|
else
|
|
|
|
bptr += 4;
|
2007-09-17 16:09:54 +08:00
|
|
|
break;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
case 6:
|
|
|
|
/* This is the field containing the port
|
|
|
|
* number that RA-player is listening to.
|
|
|
|
*/
|
2007-09-17 05:08:06 +08:00
|
|
|
lport = (((u_char*)bptr)[0] << 8)
|
2004-04-22 08:10:48 +08:00
|
|
|
+ ((u_char *)bptr)[1];
|
2007-09-17 16:09:54 +08:00
|
|
|
if (lport < 6970)
|
2004-04-22 08:10:48 +08:00
|
|
|
lport += 256; /* don't know why */
|
|
|
|
if (lport < 6970 || lport > 7170)
|
|
|
|
return 1; /* failed */
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
/* try to get udp port between 6970 - 7170 */
|
|
|
|
for (p = 6970; p < 7071; p++) {
|
2009-06-24 20:42:31 +08:00
|
|
|
if (udp_listen(slirp, INADDR_ANY,
|
2009-06-24 20:42:28 +08:00
|
|
|
htons(p),
|
2004-04-22 08:10:48 +08:00
|
|
|
so->so_laddr.s_addr,
|
|
|
|
htons(lport),
|
|
|
|
SS_FACCEPTONCE)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == 7071)
|
|
|
|
p = 0;
|
|
|
|
*(u_char *)bptr++ = (p >> 8) & 0xff;
|
2010-03-07 21:45:37 +08:00
|
|
|
*(u_char *)bptr = p & 0xff;
|
2007-09-17 05:08:06 +08:00
|
|
|
ra = 0;
|
2004-04-22 08:10:48 +08:00
|
|
|
return 1; /* port redirected, we're done */
|
2007-09-17 16:09:54 +08:00
|
|
|
break;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
default:
|
2007-09-17 16:09:54 +08:00
|
|
|
ra = 0;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
ra++;
|
|
|
|
}
|
2007-09-17 16:09:54 +08:00
|
|
|
return 1;
|
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
default:
|
|
|
|
/* Ooops, not emulated, won't call tcp_emu again */
|
|
|
|
so->so_emu = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do misc. config of SLiRP while its running.
|
|
|
|
* Return 0 if this connections is to be closed, 1 otherwise,
|
|
|
|
* return 2 if this is a command-line connection
|
|
|
|
*/
|
2009-06-24 20:42:28 +08:00
|
|
|
int tcp_ctl(struct socket *so)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-06-24 20:42:31 +08:00
|
|
|
Slirp *slirp = so->slirp;
|
2009-06-24 20:42:28 +08:00
|
|
|
struct sbuf *sb = &so->so_snd;
|
|
|
|
struct ex_list *ex_ptr;
|
|
|
|
int do_pty;
|
|
|
|
|
|
|
|
DEBUG_CALL("tcp_ctl");
|
|
|
|
DEBUG_ARG("so = %lx", (long )so);
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
|
2009-06-24 20:42:28 +08:00
|
|
|
/* Check if it's pty_exec */
|
2009-06-24 20:42:31 +08:00
|
|
|
for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
|
2009-06-24 20:42:28 +08:00
|
|
|
if (ex_ptr->ex_fport == so->so_fport &&
|
2009-06-24 20:42:28 +08:00
|
|
|
so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
|
2009-06-24 20:42:28 +08:00
|
|
|
if (ex_ptr->ex_pty == 3) {
|
|
|
|
so->s = -1;
|
|
|
|
so->extra = (void *)ex_ptr->ex_exec;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
do_pty = ex_ptr->ex_pty;
|
2011-09-13 04:33:01 +08:00
|
|
|
DEBUG_MISC((dfd, " executing %s\n", ex_ptr->ex_exec));
|
2009-06-24 20:42:28 +08:00
|
|
|
return fork_exec(so, ex_ptr->ex_exec, do_pty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb->sb_cc =
|
|
|
|
snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
|
|
|
|
"Error: No application configured.\r\n");
|
|
|
|
sb->sb_wptr += sb->sb_cc;
|
|
|
|
return 0;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|