Statistics
| Branch: | Revision:

root / slirp / tcp_subr.c @ 4ef7b894

History | View | Annotate | Download (26.6 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 2f5f8996 aliguori
 * 3. Neither the name of the University nor the names of its contributors
14 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
15 f0cbd3ec bellard
 *    without specific prior written permission.
16 f0cbd3ec bellard
 *
17 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 f0cbd3ec bellard
 * SUCH DAMAGE.
28 f0cbd3ec bellard
 *
29 f0cbd3ec bellard
 *        @(#)tcp_subr.c        8.1 (Berkeley) 6/10/93
30 f0cbd3ec bellard
 * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
31 f0cbd3ec bellard
 */
32 f0cbd3ec bellard
33 f0cbd3ec bellard
/*
34 f0cbd3ec bellard
 * Changes and additions relating to SLiRP
35 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
36 5fafdf24 ths
 *
37 5fafdf24 ths
 * Please read the file COPYRIGHT for the
38 f0cbd3ec bellard
 * terms and conditions of the copyright.
39 f0cbd3ec bellard
 */
40 f0cbd3ec bellard
41 f0cbd3ec bellard
#include <slirp.h>
42 f0cbd3ec bellard
43 f0cbd3ec bellard
/* patchable/settable parameters for tcp */
44 9634d903 blueswir1
/* Don't do rfc1323 performance enhancements */
45 9634d903 blueswir1
#define TCP_DO_RFC1323 0
46 f0cbd3ec bellard
47 f0cbd3ec bellard
/*
48 f0cbd3ec bellard
 * Tcp initialization
49 f0cbd3ec bellard
 */
50 f0cbd3ec bellard
void
51 460fec67 Jan Kiszka
tcp_init(Slirp *slirp)
52 f0cbd3ec bellard
{
53 460fec67 Jan Kiszka
    slirp->tcp_iss = 1;                /* wrong */
54 460fec67 Jan Kiszka
    slirp->tcb.so_next = slirp->tcb.so_prev = &slirp->tcb;
55 460fec67 Jan Kiszka
    slirp->tcp_last_so = &slirp->tcb;
56 f0cbd3ec bellard
}
57 f0cbd3ec bellard
58 a68adc22 Jan Kiszka
void tcp_cleanup(Slirp *slirp)
59 a68adc22 Jan Kiszka
{
60 a68adc22 Jan Kiszka
    while (slirp->tcb.so_next != &slirp->tcb) {
61 a68adc22 Jan Kiszka
        tcp_close(sototcpcb(slirp->tcb.so_next));
62 a68adc22 Jan Kiszka
    }
63 a68adc22 Jan Kiszka
}
64 a68adc22 Jan Kiszka
65 f0cbd3ec bellard
/*
66 f0cbd3ec bellard
 * Create template to be used to send tcp packets on a connection.
67 f0cbd3ec bellard
 * Call after host entry created, fills
68 f0cbd3ec bellard
 * in a skeletal tcp/ip header, minimizing the amount of work
69 f0cbd3ec bellard
 * necessary when the connection is used.
70 f0cbd3ec bellard
 */
71 f0cbd3ec bellard
void
72 511d2b14 blueswir1
tcp_template(struct tcpcb *tp)
73 f0cbd3ec bellard
{
74 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
75 f0cbd3ec bellard
        register struct tcpiphdr *n = &tp->t_template;
76 f0cbd3ec bellard
77 429d0a3d blueswir1
        n->ti_mbuf = NULL;
78 f0cbd3ec bellard
        n->ti_x1 = 0;
79 f0cbd3ec bellard
        n->ti_pr = IPPROTO_TCP;
80 f0cbd3ec bellard
        n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
81 f0cbd3ec bellard
        n->ti_src = so->so_faddr;
82 f0cbd3ec bellard
        n->ti_dst = so->so_laddr;
83 f0cbd3ec bellard
        n->ti_sport = so->so_fport;
84 f0cbd3ec bellard
        n->ti_dport = so->so_lport;
85 5fafdf24 ths
86 f0cbd3ec bellard
        n->ti_seq = 0;
87 f0cbd3ec bellard
        n->ti_ack = 0;
88 f0cbd3ec bellard
        n->ti_x2 = 0;
89 f0cbd3ec bellard
        n->ti_off = 5;
90 f0cbd3ec bellard
        n->ti_flags = 0;
91 f0cbd3ec bellard
        n->ti_win = 0;
92 f0cbd3ec bellard
        n->ti_sum = 0;
93 f0cbd3ec bellard
        n->ti_urp = 0;
94 f0cbd3ec bellard
}
95 f0cbd3ec bellard
96 f0cbd3ec bellard
/*
97 f0cbd3ec bellard
 * Send a single message to the TCP at address specified by
98 f0cbd3ec bellard
 * the given TCP/IP header.  If m == 0, then we make a copy
99 f0cbd3ec bellard
 * of the tcpiphdr at ti and send directly to the addressed host.
100 f0cbd3ec bellard
 * This is used to force keep alive messages out using the TCP
101 f0cbd3ec bellard
 * template for a connection tp->t_template.  If flags are given
102 f0cbd3ec bellard
 * then we send a message back to the TCP which originated the
103 f0cbd3ec bellard
 * segment ti, and discard the mbuf containing it and any other
104 f0cbd3ec bellard
 * attached mbufs.
105 f0cbd3ec bellard
 *
106 f0cbd3ec bellard
 * In any case the ack and sequence number of the transmitted
107 f0cbd3ec bellard
 * segment are as specified by the parameters.
108 f0cbd3ec bellard
 */
109 f0cbd3ec bellard
void
110 511d2b14 blueswir1
tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
111 511d2b14 blueswir1
            tcp_seq ack, tcp_seq seq, int flags)
112 f0cbd3ec bellard
{
113 f0cbd3ec bellard
        register int tlen;
114 f0cbd3ec bellard
        int win = 0;
115 f0cbd3ec bellard
116 f0cbd3ec bellard
        DEBUG_CALL("tcp_respond");
117 c4d12a74 Stefan Weil
        DEBUG_ARG("tp = %p", tp);
118 c4d12a74 Stefan Weil
        DEBUG_ARG("ti = %p", ti);
119 c4d12a74 Stefan Weil
        DEBUG_ARG("m = %p", m);
120 f0cbd3ec bellard
        DEBUG_ARG("ack = %u", ack);
121 f0cbd3ec bellard
        DEBUG_ARG("seq = %u", seq);
122 f0cbd3ec bellard
        DEBUG_ARG("flags = %x", flags);
123 5fafdf24 ths
124 f0cbd3ec bellard
        if (tp)
125 f0cbd3ec bellard
                win = sbspace(&tp->t_socket->so_rcv);
126 511d2b14 blueswir1
        if (m == NULL) {
127 e56afbc5 Stefan Weil
                if (!tp || (m = m_get(tp->t_socket->slirp)) == NULL)
128 f0cbd3ec bellard
                        return;
129 f0cbd3ec bellard
                tlen = 0;
130 9634d903 blueswir1
                m->m_data += IF_MAXLINKHDR;
131 f0cbd3ec bellard
                *mtod(m, struct tcpiphdr *) = *ti;
132 f0cbd3ec bellard
                ti = mtod(m, struct tcpiphdr *);
133 f0cbd3ec bellard
                flags = TH_ACK;
134 f0cbd3ec bellard
        } else {
135 5fafdf24 ths
                /*
136 f0cbd3ec bellard
                 * ti points into m so the next line is just making
137 f0cbd3ec bellard
                 * the mbuf point to ti
138 f0cbd3ec bellard
                 */
139 f0cbd3ec bellard
                m->m_data = (caddr_t)ti;
140 3b46e624 ths
141 f0cbd3ec bellard
                m->m_len = sizeof (struct tcpiphdr);
142 f0cbd3ec bellard
                tlen = 0;
143 f0cbd3ec bellard
#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
144 b6dce92e Stefan Weil
                xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, uint32_t);
145 b6dce92e Stefan Weil
                xchg(ti->ti_dport, ti->ti_sport, uint16_t);
146 f0cbd3ec bellard
#undef xchg
147 f0cbd3ec bellard
        }
148 f0cbd3ec bellard
        ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
149 f0cbd3ec bellard
        tlen += sizeof (struct tcpiphdr);
150 f0cbd3ec bellard
        m->m_len = tlen;
151 f0cbd3ec bellard
152 511d2b14 blueswir1
        ti->ti_mbuf = NULL;
153 f0cbd3ec bellard
        ti->ti_x1 = 0;
154 f0cbd3ec bellard
        ti->ti_seq = htonl(seq);
155 f0cbd3ec bellard
        ti->ti_ack = htonl(ack);
156 f0cbd3ec bellard
        ti->ti_x2 = 0;
157 f0cbd3ec bellard
        ti->ti_off = sizeof (struct tcphdr) >> 2;
158 f0cbd3ec bellard
        ti->ti_flags = flags;
159 f0cbd3ec bellard
        if (tp)
160 b6dce92e Stefan Weil
                ti->ti_win = htons((uint16_t) (win >> tp->rcv_scale));
161 f0cbd3ec bellard
        else
162 b6dce92e Stefan Weil
                ti->ti_win = htons((uint16_t)win);
163 f0cbd3ec bellard
        ti->ti_urp = 0;
164 f0cbd3ec bellard
        ti->ti_sum = 0;
165 f0cbd3ec bellard
        ti->ti_sum = cksum(m, tlen);
166 f0cbd3ec bellard
        ((struct ip *)ti)->ip_len = tlen;
167 f0cbd3ec bellard
168 5fafdf24 ths
        if(flags & TH_RST)
169 f0cbd3ec bellard
          ((struct ip *)ti)->ip_ttl = MAXTTL;
170 5fafdf24 ths
        else
171 9634d903 blueswir1
          ((struct ip *)ti)->ip_ttl = IPDEFTTL;
172 5fafdf24 ths
173 f0cbd3ec bellard
        (void) ip_output((struct socket *)0, m);
174 f0cbd3ec bellard
}
175 f0cbd3ec bellard
176 f0cbd3ec bellard
/*
177 f0cbd3ec bellard
 * Create a new TCP control block, making an
178 f0cbd3ec bellard
 * empty reassembly queue and hooking it to the argument
179 f0cbd3ec bellard
 * protocol control block.
180 f0cbd3ec bellard
 */
181 f0cbd3ec bellard
struct tcpcb *
182 511d2b14 blueswir1
tcp_newtcpcb(struct socket *so)
183 f0cbd3ec bellard
{
184 f0cbd3ec bellard
        register struct tcpcb *tp;
185 5fafdf24 ths
186 f0cbd3ec bellard
        tp = (struct tcpcb *)malloc(sizeof(*tp));
187 f0cbd3ec bellard
        if (tp == NULL)
188 f0cbd3ec bellard
                return ((struct tcpcb *)0);
189 5fafdf24 ths
190 f0cbd3ec bellard
        memset((char *) tp, 0, sizeof(struct tcpcb));
191 429d0a3d blueswir1
        tp->seg_next = tp->seg_prev = (struct tcpiphdr*)tp;
192 9634d903 blueswir1
        tp->t_maxseg = TCP_MSS;
193 5fafdf24 ths
194 9634d903 blueswir1
        tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
195 f0cbd3ec bellard
        tp->t_socket = so;
196 5fafdf24 ths
197 f0cbd3ec bellard
        /*
198 f0cbd3ec bellard
         * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
199 f0cbd3ec bellard
         * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
200 f0cbd3ec bellard
         * reasonable initial retransmit time.
201 f0cbd3ec bellard
         */
202 f0cbd3ec bellard
        tp->t_srtt = TCPTV_SRTTBASE;
203 9634d903 blueswir1
        tp->t_rttvar = TCPTV_SRTTDFLT << 2;
204 f0cbd3ec bellard
        tp->t_rttmin = TCPTV_MIN;
205 f0cbd3ec bellard
206 5fafdf24 ths
        TCPT_RANGESET(tp->t_rxtcur,
207 f0cbd3ec bellard
            ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
208 f0cbd3ec bellard
            TCPTV_MIN, TCPTV_REXMTMAX);
209 f0cbd3ec bellard
210 f0cbd3ec bellard
        tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
211 f0cbd3ec bellard
        tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
212 f0cbd3ec bellard
        tp->t_state = TCPS_CLOSED;
213 5fafdf24 ths
214 f0cbd3ec bellard
        so->so_tcpcb = tp;
215 f0cbd3ec bellard
216 f0cbd3ec bellard
        return (tp);
217 f0cbd3ec bellard
}
218 f0cbd3ec bellard
219 f0cbd3ec bellard
/*
220 f0cbd3ec bellard
 * Drop a TCP connection, reporting
221 f0cbd3ec bellard
 * the specified error.  If connection is synchronized,
222 f0cbd3ec bellard
 * then send a RST to peer.
223 f0cbd3ec bellard
 */
224 5fafdf24 ths
struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
225 f0cbd3ec bellard
{
226 f0cbd3ec bellard
        DEBUG_CALL("tcp_drop");
227 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
228 f0cbd3ec bellard
        DEBUG_ARG("errno = %d", errno);
229 5fafdf24 ths
230 f0cbd3ec bellard
        if (TCPS_HAVERCVDSYN(tp->t_state)) {
231 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
232 f0cbd3ec bellard
                (void) tcp_output(tp);
233 0fe6a7f2 Jan Kiszka
        }
234 f0cbd3ec bellard
        return (tcp_close(tp));
235 f0cbd3ec bellard
}
236 f0cbd3ec bellard
237 f0cbd3ec bellard
/*
238 f0cbd3ec bellard
 * Close a TCP control block:
239 f0cbd3ec bellard
 *        discard all space held by the tcp
240 f0cbd3ec bellard
 *        discard internet protocol block
241 f0cbd3ec bellard
 *        wake up any sleepers
242 f0cbd3ec bellard
 */
243 f0cbd3ec bellard
struct tcpcb *
244 511d2b14 blueswir1
tcp_close(struct tcpcb *tp)
245 f0cbd3ec bellard
{
246 f0cbd3ec bellard
        register struct tcpiphdr *t;
247 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
248 460fec67 Jan Kiszka
        Slirp *slirp = so->slirp;
249 f0cbd3ec bellard
        register struct mbuf *m;
250 f0cbd3ec bellard
251 f0cbd3ec bellard
        DEBUG_CALL("tcp_close");
252 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long )tp);
253 5fafdf24 ths
254 f0cbd3ec bellard
        /* free the reassembly queue, if any */
255 429d0a3d blueswir1
        t = tcpfrag_list_first(tp);
256 429d0a3d blueswir1
        while (!tcpfrag_list_end(t, tp)) {
257 429d0a3d blueswir1
                t = tcpiphdr_next(t);
258 429d0a3d blueswir1
                m = tcpiphdr_prev(t)->ti_mbuf;
259 429d0a3d blueswir1
                remque(tcpiphdr2qlink(tcpiphdr_prev(t)));
260 3acccfc6 Jan Kiszka
                m_free(m);
261 f0cbd3ec bellard
        }
262 f0cbd3ec bellard
        free(tp);
263 511d2b14 blueswir1
        so->so_tcpcb = NULL;
264 f0cbd3ec bellard
        /* clobber input socket cache if we're closing the cached connection */
265 460fec67 Jan Kiszka
        if (so == slirp->tcp_last_so)
266 460fec67 Jan Kiszka
                slirp->tcp_last_so = &slirp->tcb;
267 379ff53d bellard
        closesocket(so->s);
268 f0cbd3ec bellard
        sbfree(&so->so_rcv);
269 f0cbd3ec bellard
        sbfree(&so->so_snd);
270 f0cbd3ec bellard
        sofree(so);
271 f0cbd3ec bellard
        return ((struct tcpcb *)0);
272 f0cbd3ec bellard
}
273 f0cbd3ec bellard
274 f0cbd3ec bellard
/*
275 f0cbd3ec bellard
 * TCP protocol interface to socket abstraction.
276 f0cbd3ec bellard
 */
277 f0cbd3ec bellard
278 f0cbd3ec bellard
/*
279 f0cbd3ec bellard
 * User issued close, and wish to trail through shutdown states:
280 f0cbd3ec bellard
 * if never received SYN, just forget it.  If got a SYN from peer,
281 f0cbd3ec bellard
 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
282 f0cbd3ec bellard
 * If already got a FIN from peer, then almost done; go to LAST_ACK
283 f0cbd3ec bellard
 * state.  In all other cases, have already sent FIN to peer (e.g.
284 f0cbd3ec bellard
 * after PRU_SHUTDOWN), and just have to play tedious game waiting
285 f0cbd3ec bellard
 * for peer to send FIN or not respond to keep-alives, etc.
286 f0cbd3ec bellard
 * We can let the user exit from the close as soon as the FIN is acked.
287 f0cbd3ec bellard
 */
288 f0cbd3ec bellard
void
289 511d2b14 blueswir1
tcp_sockclosed(struct tcpcb *tp)
290 f0cbd3ec bellard
{
291 f0cbd3ec bellard
292 f0cbd3ec bellard
        DEBUG_CALL("tcp_sockclosed");
293 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
294 5fafdf24 ths
295 f0cbd3ec bellard
        switch (tp->t_state) {
296 f0cbd3ec bellard
297 f0cbd3ec bellard
        case TCPS_CLOSED:
298 f0cbd3ec bellard
        case TCPS_LISTEN:
299 f0cbd3ec bellard
        case TCPS_SYN_SENT:
300 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
301 f0cbd3ec bellard
                tp = tcp_close(tp);
302 f0cbd3ec bellard
                break;
303 f0cbd3ec bellard
304 f0cbd3ec bellard
        case TCPS_SYN_RECEIVED:
305 f0cbd3ec bellard
        case TCPS_ESTABLISHED:
306 f0cbd3ec bellard
                tp->t_state = TCPS_FIN_WAIT_1;
307 f0cbd3ec bellard
                break;
308 f0cbd3ec bellard
309 f0cbd3ec bellard
        case TCPS_CLOSE_WAIT:
310 f0cbd3ec bellard
                tp->t_state = TCPS_LAST_ACK;
311 f0cbd3ec bellard
                break;
312 f0cbd3ec bellard
        }
313 f0cbd3ec bellard
        if (tp)
314 f0cbd3ec bellard
                tcp_output(tp);
315 f0cbd3ec bellard
}
316 f0cbd3ec bellard
317 5fafdf24 ths
/*
318 f0cbd3ec bellard
 * Connect to a host on the Internet
319 f0cbd3ec bellard
 * Called by tcp_input
320 f0cbd3ec bellard
 * Only do a connect, the tcp fields will be set in tcp_input
321 f0cbd3ec bellard
 * return 0 if there's a result of the connect,
322 f0cbd3ec bellard
 * else return -1 means we're still connecting
323 f0cbd3ec bellard
 * The return value is almost always -1 since the socket is
324 5fafdf24 ths
 * nonblocking.  Connect returns after the SYN is sent, and does
325 f0cbd3ec bellard
 * not wait for ACK+SYN.
326 f0cbd3ec bellard
 */
327 511d2b14 blueswir1
int tcp_fconnect(struct socket *so)
328 f0cbd3ec bellard
{
329 460fec67 Jan Kiszka
  Slirp *slirp = so->slirp;
330 f0cbd3ec bellard
  int ret=0;
331 3b46e624 ths
332 f0cbd3ec bellard
  DEBUG_CALL("tcp_fconnect");
333 f0cbd3ec bellard
  DEBUG_ARG("so = %lx", (long )so);
334 f0cbd3ec bellard
335 40ff6d7e Kevin Wolf
  if( (ret = so->s = qemu_socket(AF_INET,SOCK_STREAM,0)) >= 0) {
336 f0cbd3ec bellard
    int opt, s=so->s;
337 f0cbd3ec bellard
    struct sockaddr_in addr;
338 f0cbd3ec bellard
339 1c5970a8 Paolo Bonzini
    socket_set_nonblock(s);
340 f0cbd3ec bellard
    opt = 1;
341 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
342 f0cbd3ec bellard
    opt = 1;
343 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
344 3b46e624 ths
345 f0cbd3ec bellard
    addr.sin_family = AF_INET;
346 460fec67 Jan Kiszka
    if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
347 460fec67 Jan Kiszka
        slirp->vnetwork_addr.s_addr) {
348 f0cbd3ec bellard
      /* It's an alias */
349 460fec67 Jan Kiszka
      if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
350 df7a86ed Ed Swierk
        if (get_dns_addr(&addr.sin_addr) < 0)
351 df7a86ed Ed Swierk
          addr.sin_addr = loopback_addr;
352 a13a4126 Jan Kiszka
      } else {
353 f0cbd3ec bellard
        addr.sin_addr = loopback_addr;
354 f0cbd3ec bellard
      }
355 f0cbd3ec bellard
    } else
356 f0cbd3ec bellard
      addr.sin_addr = so->so_faddr;
357 f0cbd3ec bellard
    addr.sin_port = so->so_fport;
358 3b46e624 ths
359 f0cbd3ec bellard
    DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
360 5fafdf24 ths
                "addr.sin_addr.s_addr=%.16s\n",
361 f0cbd3ec bellard
                ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
362 f0cbd3ec bellard
    /* We don't care what port we get */
363 f0cbd3ec bellard
    ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
364 3b46e624 ths
365 f0cbd3ec bellard
    /*
366 f0cbd3ec bellard
     * If it's not in progress, it failed, so we just return 0,
367 f0cbd3ec bellard
     * without clearing SS_NOFDREF
368 f0cbd3ec bellard
     */
369 f0cbd3ec bellard
    soisfconnecting(so);
370 f0cbd3ec bellard
  }
371 f0cbd3ec bellard
372 f0cbd3ec bellard
  return(ret);
373 f0cbd3ec bellard
}
374 f0cbd3ec bellard
375 f0cbd3ec bellard
/*
376 f0cbd3ec bellard
 * Accept the socket and connect to the local-host
377 5fafdf24 ths
 *
378 f0cbd3ec bellard
 * We have a problem. The correct thing to do would be
379 f0cbd3ec bellard
 * to first connect to the local-host, and only if the
380 f0cbd3ec bellard
 * connection is accepted, then do an accept() here.
381 5fafdf24 ths
 * But, a) we need to know who's trying to connect
382 f0cbd3ec bellard
 * to the socket to be able to SYN the local-host, and
383 f0cbd3ec bellard
 * b) we are already connected to the foreign host by
384 f0cbd3ec bellard
 * the time it gets to accept(), so... We simply accept
385 f0cbd3ec bellard
 * here and SYN the local-host.
386 5fafdf24 ths
 */
387 4ef7b894 MORITA Kazutaka
void tcp_connect(struct socket *inso)
388 f0cbd3ec bellard
{
389 4ef7b894 MORITA Kazutaka
    Slirp *slirp = inso->slirp;
390 4ef7b894 MORITA Kazutaka
    struct socket *so;
391 4ef7b894 MORITA Kazutaka
    struct sockaddr_in addr;
392 4ef7b894 MORITA Kazutaka
    socklen_t addrlen = sizeof(struct sockaddr_in);
393 4ef7b894 MORITA Kazutaka
    struct tcpcb *tp;
394 4ef7b894 MORITA Kazutaka
    int s, opt;
395 f0cbd3ec bellard
396 4ef7b894 MORITA Kazutaka
    DEBUG_CALL("tcp_connect");
397 4ef7b894 MORITA Kazutaka
    DEBUG_ARG("inso = %lx", (long)inso);
398 5fafdf24 ths
399 4ef7b894 MORITA Kazutaka
    /*
400 4ef7b894 MORITA Kazutaka
     * If it's an SS_ACCEPTONCE socket, no need to socreate()
401 4ef7b894 MORITA Kazutaka
     * another socket, just use the accept() socket.
402 4ef7b894 MORITA Kazutaka
     */
403 4ef7b894 MORITA Kazutaka
    if (inso->so_state & SS_FACCEPTONCE) {
404 4ef7b894 MORITA Kazutaka
        /* FACCEPTONCE already have a tcpcb */
405 4ef7b894 MORITA Kazutaka
        so = inso;
406 4ef7b894 MORITA Kazutaka
    } else {
407 4ef7b894 MORITA Kazutaka
        so = socreate(slirp);
408 4ef7b894 MORITA Kazutaka
        if (so == NULL) {
409 4ef7b894 MORITA Kazutaka
            /* If it failed, get rid of the pending connection */
410 4ef7b894 MORITA Kazutaka
            closesocket(accept(inso->s, (struct sockaddr *)&addr, &addrlen));
411 4ef7b894 MORITA Kazutaka
            return;
412 4ef7b894 MORITA Kazutaka
        }
413 4ef7b894 MORITA Kazutaka
        if (tcp_attach(so) < 0) {
414 4ef7b894 MORITA Kazutaka
            free(so); /* NOT sofree */
415 4ef7b894 MORITA Kazutaka
            return;
416 4ef7b894 MORITA Kazutaka
        }
417 4ef7b894 MORITA Kazutaka
        so->so_laddr = inso->so_laddr;
418 4ef7b894 MORITA Kazutaka
        so->so_lport = inso->so_lport;
419 4ef7b894 MORITA Kazutaka
    }
420 5fafdf24 ths
421 4ef7b894 MORITA Kazutaka
    tcp_mss(sototcpcb(so), 0);
422 f0cbd3ec bellard
423 4ef7b894 MORITA Kazutaka
    s = accept(inso->s, (struct sockaddr *)&addr, &addrlen);
424 4ef7b894 MORITA Kazutaka
    if (s < 0) {
425 4ef7b894 MORITA Kazutaka
        tcp_close(sototcpcb(so)); /* This will sofree() as well */
426 4ef7b894 MORITA Kazutaka
        return;
427 4ef7b894 MORITA Kazutaka
    }
428 4ef7b894 MORITA Kazutaka
    socket_set_nonblock(s);
429 4ef7b894 MORITA Kazutaka
    opt = 1;
430 4ef7b894 MORITA Kazutaka
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(int));
431 4ef7b894 MORITA Kazutaka
    opt = 1;
432 4ef7b894 MORITA Kazutaka
    setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(int));
433 4ef7b894 MORITA Kazutaka
    opt = 1;
434 4ef7b894 MORITA Kazutaka
    setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(int));
435 4ef7b894 MORITA Kazutaka
436 4ef7b894 MORITA Kazutaka
    so->so_fport = addr.sin_port;
437 4ef7b894 MORITA Kazutaka
    so->so_faddr = addr.sin_addr;
438 4ef7b894 MORITA Kazutaka
    /* Translate connections from localhost to the real hostname */
439 4ef7b894 MORITA Kazutaka
    if (so->so_faddr.s_addr == 0 ||
440 4ef7b894 MORITA Kazutaka
        (so->so_faddr.s_addr & loopback_mask) ==
441 4ef7b894 MORITA Kazutaka
        (loopback_addr.s_addr & loopback_mask)) {
442 4ef7b894 MORITA Kazutaka
        so->so_faddr = slirp->vhost_addr;
443 4ef7b894 MORITA Kazutaka
    }
444 5fafdf24 ths
445 4ef7b894 MORITA Kazutaka
    /* Close the accept() socket, set right state */
446 4ef7b894 MORITA Kazutaka
    if (inso->so_state & SS_FACCEPTONCE) {
447 4ef7b894 MORITA Kazutaka
        /* If we only accept once, close the accept() socket */
448 4ef7b894 MORITA Kazutaka
        closesocket(so->s);
449 4ef7b894 MORITA Kazutaka
450 4ef7b894 MORITA Kazutaka
        /* Don't select it yet, even though we have an FD */
451 4ef7b894 MORITA Kazutaka
        /* if it's not FACCEPTONCE, it's already NOFDREF */
452 4ef7b894 MORITA Kazutaka
        so->so_state = SS_NOFDREF;
453 4ef7b894 MORITA Kazutaka
    }
454 4ef7b894 MORITA Kazutaka
    so->s = s;
455 4ef7b894 MORITA Kazutaka
    so->so_state |= SS_INCOMING;
456 5fafdf24 ths
457 4ef7b894 MORITA Kazutaka
    so->so_iptos = tcp_tos(so);
458 4ef7b894 MORITA Kazutaka
    tp = sototcpcb(so);
459 f0cbd3ec bellard
460 4ef7b894 MORITA Kazutaka
    tcp_template(tp);
461 5fafdf24 ths
462 4ef7b894 MORITA Kazutaka
    tp->t_state = TCPS_SYN_SENT;
463 4ef7b894 MORITA Kazutaka
    tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
464 4ef7b894 MORITA Kazutaka
    tp->iss = slirp->tcp_iss;
465 4ef7b894 MORITA Kazutaka
    slirp->tcp_iss += TCP_ISSINCR/2;
466 4ef7b894 MORITA Kazutaka
    tcp_sendseqinit(tp);
467 4ef7b894 MORITA Kazutaka
    tcp_output(tp);
468 f0cbd3ec bellard
}
469 f0cbd3ec bellard
470 f0cbd3ec bellard
/*
471 f0cbd3ec bellard
 * Attach a TCPCB to a socket.
472 f0cbd3ec bellard
 */
473 f0cbd3ec bellard
int
474 511d2b14 blueswir1
tcp_attach(struct socket *so)
475 f0cbd3ec bellard
{
476 f0cbd3ec bellard
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL)
477 f0cbd3ec bellard
           return -1;
478 5fafdf24 ths
479 460fec67 Jan Kiszka
        insque(so, &so->slirp->tcb);
480 f0cbd3ec bellard
481 f0cbd3ec bellard
        return 0;
482 f0cbd3ec bellard
}
483 f0cbd3ec bellard
484 f0cbd3ec bellard
/*
485 f0cbd3ec bellard
 * Set the socket's type of service field
486 f0cbd3ec bellard
 */
487 9634d903 blueswir1
static const struct tos_t tcptos[] = {
488 f0cbd3ec bellard
          {0, 20, IPTOS_THROUGHPUT, 0},        /* ftp data */
489 f0cbd3ec bellard
          {21, 21, IPTOS_LOWDELAY,  EMU_FTP},        /* ftp control */
490 f0cbd3ec bellard
          {0, 23, IPTOS_LOWDELAY, 0},        /* telnet */
491 f0cbd3ec bellard
          {0, 80, IPTOS_THROUGHPUT, 0},        /* WWW */
492 f0cbd3ec bellard
          {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT},        /* rlogin */
493 f0cbd3ec bellard
          {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT},        /* shell */
494 f0cbd3ec bellard
          {0, 544, IPTOS_LOWDELAY, EMU_KSH},                /* kshell */
495 f0cbd3ec bellard
          {0, 543, IPTOS_LOWDELAY, 0},        /* klogin */
496 f0cbd3ec bellard
          {0, 6667, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC */
497 f0cbd3ec bellard
          {0, 6668, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC undernet */
498 f0cbd3ec bellard
          {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
499 f0cbd3ec bellard
          {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
500 f0cbd3ec bellard
          {0, 0, 0, 0}
501 f0cbd3ec bellard
};
502 f0cbd3ec bellard
503 0d62c4cf Jan Kiszka
static struct emu_t *tcpemu = NULL;
504 3b46e624 ths
505 f0cbd3ec bellard
/*
506 f0cbd3ec bellard
 * Return TOS according to the above table
507 f0cbd3ec bellard
 */
508 b6dce92e Stefan Weil
uint8_t
509 511d2b14 blueswir1
tcp_tos(struct socket *so)
510 f0cbd3ec bellard
{
511 f0cbd3ec bellard
        int i = 0;
512 f0cbd3ec bellard
        struct emu_t *emup;
513 5fafdf24 ths
514 f0cbd3ec bellard
        while(tcptos[i].tos) {
515 f0cbd3ec bellard
                if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
516 f0cbd3ec bellard
                    (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
517 f0cbd3ec bellard
                        so->so_emu = tcptos[i].emu;
518 f0cbd3ec bellard
                        return tcptos[i].tos;
519 f0cbd3ec bellard
                }
520 f0cbd3ec bellard
                i++;
521 f0cbd3ec bellard
        }
522 5fafdf24 ths
523 f0cbd3ec bellard
        /* Nope, lets see if there's a user-added one */
524 f0cbd3ec bellard
        for (emup = tcpemu; emup; emup = emup->next) {
525 f0cbd3ec bellard
                if ((emup->fport && (ntohs(so->so_fport) == emup->fport)) ||
526 f0cbd3ec bellard
                    (emup->lport && (ntohs(so->so_lport) == emup->lport))) {
527 f0cbd3ec bellard
                        so->so_emu = emup->emu;
528 f0cbd3ec bellard
                        return emup->tos;
529 f0cbd3ec bellard
                }
530 f0cbd3ec bellard
        }
531 5fafdf24 ths
532 f0cbd3ec bellard
        return 0;
533 f0cbd3ec bellard
}
534 f0cbd3ec bellard
535 f0cbd3ec bellard
/*
536 f0cbd3ec bellard
 * Emulate programs that try and connect to us
537 f0cbd3ec bellard
 * This includes ftp (the data connection is
538 f0cbd3ec bellard
 * initiated by the server) and IRC (DCC CHAT and
539 f0cbd3ec bellard
 * DCC SEND) for now
540 5fafdf24 ths
 *
541 f0cbd3ec bellard
 * NOTE: It's possible to crash SLiRP by sending it
542 f0cbd3ec bellard
 * unstandard strings to emulate... if this is a problem,
543 f0cbd3ec bellard
 * more checks are needed here
544 f0cbd3ec bellard
 *
545 f0cbd3ec bellard
 * XXX Assumes the whole command came in one packet
546 3b46e624 ths
 *
547 f0cbd3ec bellard
 * XXX Some ftp clients will have their TOS set to
548 f0cbd3ec bellard
 * LOWDELAY and so Nagel will kick in.  Because of this,
549 f0cbd3ec bellard
 * we'll get the first letter, followed by the rest, so
550 f0cbd3ec bellard
 * we simply scan for ORT instead of PORT...
551 f0cbd3ec bellard
 * DCC doesn't have this problem because there's other stuff
552 f0cbd3ec bellard
 * in the packet before the DCC command.
553 5fafdf24 ths
 *
554 5fafdf24 ths
 * Return 1 if the mbuf m is still valid and should be
555 f0cbd3ec bellard
 * sbappend()ed
556 5fafdf24 ths
 *
557 f0cbd3ec bellard
 * NOTE: if you return 0 you MUST m_free() the mbuf!
558 f0cbd3ec bellard
 */
559 f0cbd3ec bellard
int
560 511d2b14 blueswir1
tcp_emu(struct socket *so, struct mbuf *m)
561 f0cbd3ec bellard
{
562 460fec67 Jan Kiszka
        Slirp *slirp = so->slirp;
563 f0cbd3ec bellard
        u_int n1, n2, n3, n4, n5, n6;
564 363a37d5 blueswir1
        char buff[257];
565 b6dce92e Stefan Weil
        uint32_t laddr;
566 f0cbd3ec bellard
        u_int lport;
567 f0cbd3ec bellard
        char *bptr;
568 5fafdf24 ths
569 f0cbd3ec bellard
        DEBUG_CALL("tcp_emu");
570 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
571 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
572 5fafdf24 ths
573 f0cbd3ec bellard
        switch(so->so_emu) {
574 f0cbd3ec bellard
                int x, i;
575 3b46e624 ths
576 f0cbd3ec bellard
         case EMU_IDENT:
577 f0cbd3ec bellard
                /*
578 f0cbd3ec bellard
                 * Identification protocol as per rfc-1413
579 f0cbd3ec bellard
                 */
580 3b46e624 ths
581 f0cbd3ec bellard
                {
582 f0cbd3ec bellard
                        struct socket *tmpso;
583 f0cbd3ec bellard
                        struct sockaddr_in addr;
584 b55266b5 blueswir1
                        socklen_t addrlen = sizeof(struct sockaddr_in);
585 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
586 3b46e624 ths
587 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
588 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
589 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
590 f0cbd3ec bellard
                        m->m_data[m->m_len] = 0; /* NULL terminate */
591 f0cbd3ec bellard
                        if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
592 9634d903 blueswir1
                                if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
593 f0cbd3ec bellard
                                        HTONS(n1);
594 f0cbd3ec bellard
                                        HTONS(n2);
595 f0cbd3ec bellard
                                        /* n2 is the one on our host */
596 460fec67 Jan Kiszka
                                        for (tmpso = slirp->tcb.so_next;
597 460fec67 Jan Kiszka
                                             tmpso != &slirp->tcb;
598 460fec67 Jan Kiszka
                                             tmpso = tmpso->so_next) {
599 f0cbd3ec bellard
                                                if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
600 f0cbd3ec bellard
                                                    tmpso->so_lport == n2 &&
601 f0cbd3ec bellard
                                                    tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
602 f0cbd3ec bellard
                                                    tmpso->so_fport == n1) {
603 f0cbd3ec bellard
                                                        if (getsockname(tmpso->s,
604 f0cbd3ec bellard
                                                                (struct sockaddr *)&addr, &addrlen) == 0)
605 f0cbd3ec bellard
                                                           n2 = ntohs(addr.sin_port);
606 f0cbd3ec bellard
                                                        break;
607 f0cbd3ec bellard
                                                }
608 f0cbd3ec bellard
                                        }
609 f0cbd3ec bellard
                                }
610 363a37d5 blueswir1
                                so_rcv->sb_cc = snprintf(so_rcv->sb_data,
611 363a37d5 blueswir1
                                                         so_rcv->sb_datalen,
612 363a37d5 blueswir1
                                                         "%d,%d\r\n", n1, n2);
613 f0cbd3ec bellard
                                so_rcv->sb_rptr = so_rcv->sb_data;
614 f0cbd3ec bellard
                                so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
615 f0cbd3ec bellard
                        }
616 f0cbd3ec bellard
                        m_free(m);
617 f0cbd3ec bellard
                        return 0;
618 f0cbd3ec bellard
                }
619 3b46e624 ths
620 f0cbd3ec bellard
        case EMU_FTP: /* ftp */
621 511d2b14 blueswir1
                *(m->m_data+m->m_len) = 0; /* NUL terminate for strstr */
622 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
623 f0cbd3ec bellard
                        /*
624 f0cbd3ec bellard
                         * Need to emulate the PORT command
625 3b46e624 ths
                         */
626 9634d903 blueswir1
                        x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
627 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
628 f0cbd3ec bellard
                        if (x < 6)
629 f0cbd3ec bellard
                           return 1;
630 3b46e624 ths
631 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
632 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
633 3b46e624 ths
634 460fec67 Jan Kiszka
                        if ((so = tcp_listen(slirp, INADDR_ANY, 0, laddr,
635 460fec67 Jan Kiszka
                                             lport, SS_FACCEPTONCE)) == NULL) {
636 f0cbd3ec bellard
                           return 1;
637 460fec67 Jan Kiszka
                        }
638 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
639 3b46e624 ths
640 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
641 f0cbd3ec bellard
                        n6 &= 0xff;
642 3b46e624 ths
643 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
644 3b46e624 ths
645 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
646 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
647 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
648 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
649 3b46e624 ths
650 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
651 363a37d5 blueswir1
                        m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
652 363a37d5 blueswir1
                                             "ORT %d,%d,%d,%d,%d,%d\r\n%s",
653 363a37d5 blueswir1
                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
654 f0cbd3ec bellard
                        return 1;
655 f0cbd3ec bellard
                } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
656 f0cbd3ec bellard
                        /*
657 f0cbd3ec bellard
                         * Need to emulate the PASV response
658 f0cbd3ec bellard
                         */
659 9634d903 blueswir1
                        x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
660 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
661 f0cbd3ec bellard
                        if (x < 6)
662 f0cbd3ec bellard
                           return 1;
663 3b46e624 ths
664 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
665 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
666 3b46e624 ths
667 460fec67 Jan Kiszka
                        if ((so = tcp_listen(slirp, INADDR_ANY, 0, laddr,
668 460fec67 Jan Kiszka
                                             lport, SS_FACCEPTONCE)) == NULL) {
669 f0cbd3ec bellard
                           return 1;
670 460fec67 Jan Kiszka
                        }
671 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
672 3b46e624 ths
673 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
674 f0cbd3ec bellard
                        n6 &= 0xff;
675 3b46e624 ths
676 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
677 3b46e624 ths
678 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
679 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
680 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
681 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
682 3b46e624 ths
683 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
684 363a37d5 blueswir1
                        m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len,
685 363a37d5 blueswir1
                                             "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
686 363a37d5 blueswir1
                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
687 3b46e624 ths
688 f0cbd3ec bellard
                        return 1;
689 f0cbd3ec bellard
                }
690 3b46e624 ths
691 f0cbd3ec bellard
                return 1;
692 3b46e624 ths
693 f0cbd3ec bellard
         case EMU_KSH:
694 f0cbd3ec bellard
                /*
695 f0cbd3ec bellard
                 * The kshell (Kerberos rsh) and shell services both pass
696 f0cbd3ec bellard
                 * a local port port number to carry signals to the server
697 f0cbd3ec bellard
                 * and stderr to the client.  It is passed at the beginning
698 f0cbd3ec bellard
                 * of the connection as a NUL-terminated decimal ASCII string.
699 f0cbd3ec bellard
                 */
700 f0cbd3ec bellard
                so->so_emu = 0;
701 f0cbd3ec bellard
                for (lport = 0, i = 0; i < m->m_len-1; ++i) {
702 f0cbd3ec bellard
                        if (m->m_data[i] < '0' || m->m_data[i] > '9')
703 f0cbd3ec bellard
                                return 1;       /* invalid number */
704 f0cbd3ec bellard
                        lport *= 10;
705 f0cbd3ec bellard
                        lport += m->m_data[i] - '0';
706 f0cbd3ec bellard
                }
707 f0cbd3ec bellard
                if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
708 460fec67 Jan Kiszka
                    (so = tcp_listen(slirp, INADDR_ANY, 0, so->so_laddr.s_addr,
709 460fec67 Jan Kiszka
                                     htons(lport), SS_FACCEPTONCE)) != NULL)
710 363a37d5 blueswir1
                    m->m_len = snprintf(m->m_data, m->m_hdr.mh_size, "%d",
711 363a37d5 blueswir1
                                        ntohs(so->so_fport)) + 1;
712 f0cbd3ec bellard
                return 1;
713 3b46e624 ths
714 f0cbd3ec bellard
         case EMU_IRC:
715 f0cbd3ec bellard
                /*
716 f0cbd3ec bellard
                 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
717 f0cbd3ec bellard
                 */
718 f0cbd3ec bellard
                *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
719 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
720 f0cbd3ec bellard
                         return 1;
721 3b46e624 ths
722 f0cbd3ec bellard
                /* The %256s is for the broken mIRC */
723 f0cbd3ec bellard
                if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
724 460fec67 Jan Kiszka
                        if ((so = tcp_listen(slirp, INADDR_ANY, 0,
725 460fec67 Jan Kiszka
                                             htonl(laddr), htons(lport),
726 460fec67 Jan Kiszka
                                             SS_FACCEPTONCE)) == NULL) {
727 f0cbd3ec bellard
                                return 1;
728 460fec67 Jan Kiszka
                        }
729 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
730 363a37d5 blueswir1
                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
731 363a37d5 blueswir1
                                             "DCC CHAT chat %lu %u%c\n",
732 363a37d5 blueswir1
                                             (unsigned long)ntohl(so->so_faddr.s_addr),
733 363a37d5 blueswir1
                                             ntohs(so->so_fport), 1);
734 f0cbd3ec bellard
                } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
735 460fec67 Jan Kiszka
                        if ((so = tcp_listen(slirp, INADDR_ANY, 0,
736 460fec67 Jan Kiszka
                                             htonl(laddr), htons(lport),
737 460fec67 Jan Kiszka
                                             SS_FACCEPTONCE)) == NULL) {
738 f0cbd3ec bellard
                                return 1;
739 460fec67 Jan Kiszka
                        }
740 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
741 363a37d5 blueswir1
                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
742 363a37d5 blueswir1
                                             "DCC SEND %s %lu %u %u%c\n", buff,
743 363a37d5 blueswir1
                                             (unsigned long)ntohl(so->so_faddr.s_addr),
744 363a37d5 blueswir1
                                             ntohs(so->so_fport), n1, 1);
745 f0cbd3ec bellard
                } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
746 460fec67 Jan Kiszka
                        if ((so = tcp_listen(slirp, INADDR_ANY, 0,
747 460fec67 Jan Kiszka
                                             htonl(laddr), htons(lport),
748 460fec67 Jan Kiszka
                                             SS_FACCEPTONCE)) == NULL) {
749 f0cbd3ec bellard
                                return 1;
750 460fec67 Jan Kiszka
                        }
751 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
752 363a37d5 blueswir1
                        m->m_len += snprintf(bptr, m->m_hdr.mh_size,
753 363a37d5 blueswir1
                                             "DCC MOVE %s %lu %u %u%c\n", buff,
754 363a37d5 blueswir1
                                             (unsigned long)ntohl(so->so_faddr.s_addr),
755 363a37d5 blueswir1
                                             ntohs(so->so_fport), n1, 1);
756 f0cbd3ec bellard
                }
757 f0cbd3ec bellard
                return 1;
758 f0cbd3ec bellard
759 f0cbd3ec bellard
         case EMU_REALAUDIO:
760 5fafdf24 ths
                /*
761 f0cbd3ec bellard
                 * RealAudio emulation - JP. We must try to parse the incoming
762 f0cbd3ec bellard
                 * data and try to find the two characters that contain the
763 f0cbd3ec bellard
                 * port number. Then we redirect an udp port and replace the
764 f0cbd3ec bellard
                 * number with the real port we got.
765 f0cbd3ec bellard
                 *
766 f0cbd3ec bellard
                 * The 1.0 beta versions of the player are not supported
767 f0cbd3ec bellard
                 * any more.
768 5fafdf24 ths
                 *
769 f0cbd3ec bellard
                 * A typical packet for player version 1.0 (release version):
770 3b46e624 ths
                 *
771 5fafdf24 ths
                 * 0000:50 4E 41 00 05
772 0d62c4cf Jan Kiszka
                 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 ........g.l.c..P
773 f0cbd3ec bellard
                 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
774 f0cbd3ec bellard
                 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
775 f0cbd3ec bellard
                 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
776 3b46e624 ths
                 *
777 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
778 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
779 f0cbd3ec bellard
                 * second packet. This time we received five bytes first and
780 f0cbd3ec bellard
                 * then the rest. You never know how many bytes you get.
781 f0cbd3ec bellard
                 *
782 f0cbd3ec bellard
                 * A typical packet for player version 2.0 (beta):
783 3b46e624 ths
                 *
784 0d62c4cf Jan Kiszka
                 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA.............
785 0d62c4cf Jan Kiszka
                 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .gux.c..Win2.0.0
786 f0cbd3ec bellard
                 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
787 f0cbd3ec bellard
                 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
788 f0cbd3ec bellard
                 * 0040:65 2E 72 61 79 53 00 00 06 36 42                e.rayS...6B
789 3b46e624 ths
                 *
790 f0cbd3ec bellard
                 * Port number 0x1BC1 is found at offset 0x0d.
791 3b46e624 ths
                 *
792 f0cbd3ec bellard
                 * This is just a horrible switch statement. Variable ra tells
793 f0cbd3ec bellard
                 * us where we're going.
794 f0cbd3ec bellard
                 */
795 3b46e624 ths
796 f0cbd3ec bellard
                bptr = m->m_data;
797 f0cbd3ec bellard
                while (bptr < m->m_data + m->m_len) {
798 f0cbd3ec bellard
                        u_short p;
799 f0cbd3ec bellard
                        static int ra = 0;
800 5fafdf24 ths
                        char ra_tbl[4];
801 3b46e624 ths
802 f0cbd3ec bellard
                        ra_tbl[0] = 0x50;
803 f0cbd3ec bellard
                        ra_tbl[1] = 0x4e;
804 f0cbd3ec bellard
                        ra_tbl[2] = 0x41;
805 f0cbd3ec bellard
                        ra_tbl[3] = 0;
806 3b46e624 ths
807 f0cbd3ec bellard
                        switch (ra) {
808 f0cbd3ec bellard
                         case 0:
809 f0cbd3ec bellard
                         case 2:
810 f0cbd3ec bellard
                         case 3:
811 f0cbd3ec bellard
                                if (*bptr++ != ra_tbl[ra]) {
812 f0cbd3ec bellard
                                        ra = 0;
813 f0cbd3ec bellard
                                        continue;
814 f0cbd3ec bellard
                                }
815 f0cbd3ec bellard
                                break;
816 3b46e624 ths
817 f0cbd3ec bellard
                         case 1:
818 f0cbd3ec bellard
                                /*
819 f0cbd3ec bellard
                                 * We may get 0x50 several times, ignore them
820 f0cbd3ec bellard
                                 */
821 f0cbd3ec bellard
                                if (*bptr == 0x50) {
822 f0cbd3ec bellard
                                        ra = 1;
823 f0cbd3ec bellard
                                        bptr++;
824 f0cbd3ec bellard
                                        continue;
825 f0cbd3ec bellard
                                } else if (*bptr++ != ra_tbl[ra]) {
826 f0cbd3ec bellard
                                        ra = 0;
827 f0cbd3ec bellard
                                        continue;
828 f0cbd3ec bellard
                                }
829 f0cbd3ec bellard
                                break;
830 3b46e624 ths
831 5fafdf24 ths
                         case 4:
832 5fafdf24 ths
                                /*
833 f0cbd3ec bellard
                                 * skip version number
834 f0cbd3ec bellard
                                 */
835 f0cbd3ec bellard
                                bptr++;
836 f0cbd3ec bellard
                                break;
837 3b46e624 ths
838 5fafdf24 ths
                         case 5:
839 f0cbd3ec bellard
                                /*
840 f0cbd3ec bellard
                                 * The difference between versions 1.0 and
841 f0cbd3ec bellard
                                 * 2.0 is here. For future versions of
842 f0cbd3ec bellard
                                 * the player this may need to be modified.
843 f0cbd3ec bellard
                                 */
844 f0cbd3ec bellard
                                if (*(bptr + 1) == 0x02)
845 f0cbd3ec bellard
                                   bptr += 8;
846 f0cbd3ec bellard
                                else
847 f0cbd3ec bellard
                                   bptr += 4;
848 3b46e624 ths
                                break;
849 3b46e624 ths
850 f0cbd3ec bellard
                         case 6:
851 f0cbd3ec bellard
                                /* This is the field containing the port
852 f0cbd3ec bellard
                                 * number that RA-player is listening to.
853 f0cbd3ec bellard
                                 */
854 5fafdf24 ths
                                lport = (((u_char*)bptr)[0] << 8)
855 f0cbd3ec bellard
                                + ((u_char *)bptr)[1];
856 3b46e624 ths
                                if (lport < 6970)
857 f0cbd3ec bellard
                                   lport += 256;   /* don't know why */
858 f0cbd3ec bellard
                                if (lport < 6970 || lport > 7170)
859 f0cbd3ec bellard
                                   return 1;       /* failed */
860 3b46e624 ths
861 f0cbd3ec bellard
                                /* try to get udp port between 6970 - 7170 */
862 f0cbd3ec bellard
                                for (p = 6970; p < 7071; p++) {
863 460fec67 Jan Kiszka
                                        if (udp_listen(slirp, INADDR_ANY,
864 3c6a0580 Jan Kiszka
                                                       htons(p),
865 f0cbd3ec bellard
                                                       so->so_laddr.s_addr,
866 f0cbd3ec bellard
                                                       htons(lport),
867 f0cbd3ec bellard
                                                       SS_FACCEPTONCE)) {
868 f0cbd3ec bellard
                                                break;
869 f0cbd3ec bellard
                                        }
870 f0cbd3ec bellard
                                }
871 f0cbd3ec bellard
                                if (p == 7071)
872 f0cbd3ec bellard
                                   p = 0;
873 f0cbd3ec bellard
                                *(u_char *)bptr++ = (p >> 8) & 0xff;
874 369c86e7 Blue Swirl
                                *(u_char *)bptr = p & 0xff;
875 5fafdf24 ths
                                ra = 0;
876 f0cbd3ec bellard
                                return 1;   /* port redirected, we're done */
877 3b46e624 ths
                                break;
878 3b46e624 ths
879 f0cbd3ec bellard
                         default:
880 3b46e624 ths
                                ra = 0;
881 f0cbd3ec bellard
                        }
882 f0cbd3ec bellard
                        ra++;
883 f0cbd3ec bellard
                }
884 3b46e624 ths
                return 1;
885 3b46e624 ths
886 f0cbd3ec bellard
         default:
887 f0cbd3ec bellard
                /* Ooops, not emulated, won't call tcp_emu again */
888 f0cbd3ec bellard
                so->so_emu = 0;
889 f0cbd3ec bellard
                return 1;
890 f0cbd3ec bellard
        }
891 f0cbd3ec bellard
}
892 f0cbd3ec bellard
893 f0cbd3ec bellard
/*
894 f0cbd3ec bellard
 * Do misc. config of SLiRP while its running.
895 f0cbd3ec bellard
 * Return 0 if this connections is to be closed, 1 otherwise,
896 f0cbd3ec bellard
 * return 2 if this is a command-line connection
897 f0cbd3ec bellard
 */
898 b35725c5 Jan Kiszka
int tcp_ctl(struct socket *so)
899 f0cbd3ec bellard
{
900 460fec67 Jan Kiszka
    Slirp *slirp = so->slirp;
901 b35725c5 Jan Kiszka
    struct sbuf *sb = &so->so_snd;
902 b35725c5 Jan Kiszka
    struct ex_list *ex_ptr;
903 b35725c5 Jan Kiszka
    int do_pty;
904 b35725c5 Jan Kiszka
905 b35725c5 Jan Kiszka
    DEBUG_CALL("tcp_ctl");
906 b35725c5 Jan Kiszka
    DEBUG_ARG("so = %lx", (long )so);
907 b35725c5 Jan Kiszka
908 460fec67 Jan Kiszka
    if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
909 b35725c5 Jan Kiszka
        /* Check if it's pty_exec */
910 460fec67 Jan Kiszka
        for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
911 b35725c5 Jan Kiszka
            if (ex_ptr->ex_fport == so->so_fport &&
912 a13a4126 Jan Kiszka
                so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
913 b35725c5 Jan Kiszka
                if (ex_ptr->ex_pty == 3) {
914 b35725c5 Jan Kiszka
                    so->s = -1;
915 b35725c5 Jan Kiszka
                    so->extra = (void *)ex_ptr->ex_exec;
916 b35725c5 Jan Kiszka
                    return 1;
917 b35725c5 Jan Kiszka
                }
918 b35725c5 Jan Kiszka
                do_pty = ex_ptr->ex_pty;
919 b2bedb21 Stefan Weil
                DEBUG_MISC((dfd, " executing %s\n", ex_ptr->ex_exec));
920 b35725c5 Jan Kiszka
                return fork_exec(so, ex_ptr->ex_exec, do_pty);
921 b35725c5 Jan Kiszka
            }
922 b35725c5 Jan Kiszka
        }
923 b35725c5 Jan Kiszka
    }
924 b35725c5 Jan Kiszka
    sb->sb_cc =
925 b35725c5 Jan Kiszka
        snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data),
926 b35725c5 Jan Kiszka
                 "Error: No application configured.\r\n");
927 b35725c5 Jan Kiszka
    sb->sb_wptr += sb->sb_cc;
928 b35725c5 Jan Kiszka
    return 0;
929 f0cbd3ec bellard
}