Statistics
| Branch: | Revision:

root / slirp / tcp_subr.c @ cf2846b5

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