Statistics
| Branch: | Revision:

root / slirp / tcp_subr.c @ 7c829863

History | View | Annotate | Download (33.9 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 f0cbd3ec bellard
 * 3. All advertising materials mentioning features or use of this software
14 f0cbd3ec bellard
 *    must display the following acknowledgement:
15 f0cbd3ec bellard
 *        This product includes software developed by the University of
16 f0cbd3ec bellard
 *        California, Berkeley and its contributors.
17 f0cbd3ec bellard
 * 4. Neither the name of the University nor the names of its contributors
18 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
19 f0cbd3ec bellard
 *    without specific prior written permission.
20 f0cbd3ec bellard
 *
21 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 f0cbd3ec bellard
 * SUCH DAMAGE.
32 f0cbd3ec bellard
 *
33 f0cbd3ec bellard
 *        @(#)tcp_subr.c        8.1 (Berkeley) 6/10/93
34 f0cbd3ec bellard
 * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
35 f0cbd3ec bellard
 */
36 f0cbd3ec bellard
37 f0cbd3ec bellard
/*
38 f0cbd3ec bellard
 * Changes and additions relating to SLiRP
39 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
40 5fafdf24 ths
 *
41 5fafdf24 ths
 * Please read the file COPYRIGHT for the
42 f0cbd3ec bellard
 * terms and conditions of the copyright.
43 f0cbd3ec bellard
 */
44 f0cbd3ec bellard
45 f0cbd3ec bellard
#define WANT_SYS_IOCTL_H
46 f0cbd3ec bellard
#include <slirp.h>
47 f0cbd3ec bellard
48 f0cbd3ec bellard
/* patchable/settable parameters for tcp */
49 9634d903 blueswir1
/* Don't do rfc1323 performance enhancements */
50 9634d903 blueswir1
#define TCP_DO_RFC1323 0
51 f0cbd3ec bellard
52 f0cbd3ec bellard
/*
53 f0cbd3ec bellard
 * Tcp initialization
54 f0cbd3ec bellard
 */
55 f0cbd3ec bellard
void
56 f0cbd3ec bellard
tcp_init()
57 f0cbd3ec bellard
{
58 f0cbd3ec bellard
        tcp_iss = 1;                /* wrong */
59 f0cbd3ec bellard
        tcb.so_next = tcb.so_prev = &tcb;
60 f0cbd3ec bellard
}
61 f0cbd3ec bellard
62 f0cbd3ec bellard
/*
63 f0cbd3ec bellard
 * Create template to be used to send tcp packets on a connection.
64 f0cbd3ec bellard
 * Call after host entry created, fills
65 f0cbd3ec bellard
 * in a skeletal tcp/ip header, minimizing the amount of work
66 f0cbd3ec bellard
 * necessary when the connection is used.
67 f0cbd3ec bellard
 */
68 f0cbd3ec bellard
/* struct tcpiphdr * */
69 f0cbd3ec bellard
void
70 f0cbd3ec bellard
tcp_template(tp)
71 f0cbd3ec bellard
        struct tcpcb *tp;
72 f0cbd3ec bellard
{
73 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
74 f0cbd3ec bellard
        register struct tcpiphdr *n = &tp->t_template;
75 f0cbd3ec bellard
76 f0cbd3ec bellard
        n->ti_next = n->ti_prev = 0;
77 f0cbd3ec bellard
        n->ti_x1 = 0;
78 f0cbd3ec bellard
        n->ti_pr = IPPROTO_TCP;
79 f0cbd3ec bellard
        n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
80 f0cbd3ec bellard
        n->ti_src = so->so_faddr;
81 f0cbd3ec bellard
        n->ti_dst = so->so_laddr;
82 f0cbd3ec bellard
        n->ti_sport = so->so_fport;
83 f0cbd3ec bellard
        n->ti_dport = so->so_lport;
84 5fafdf24 ths
85 f0cbd3ec bellard
        n->ti_seq = 0;
86 f0cbd3ec bellard
        n->ti_ack = 0;
87 f0cbd3ec bellard
        n->ti_x2 = 0;
88 f0cbd3ec bellard
        n->ti_off = 5;
89 f0cbd3ec bellard
        n->ti_flags = 0;
90 f0cbd3ec bellard
        n->ti_win = 0;
91 f0cbd3ec bellard
        n->ti_sum = 0;
92 f0cbd3ec bellard
        n->ti_urp = 0;
93 f0cbd3ec bellard
}
94 f0cbd3ec bellard
95 f0cbd3ec bellard
/*
96 f0cbd3ec bellard
 * Send a single message to the TCP at address specified by
97 f0cbd3ec bellard
 * the given TCP/IP header.  If m == 0, then we make a copy
98 f0cbd3ec bellard
 * of the tcpiphdr at ti and send directly to the addressed host.
99 f0cbd3ec bellard
 * This is used to force keep alive messages out using the TCP
100 f0cbd3ec bellard
 * template for a connection tp->t_template.  If flags are given
101 f0cbd3ec bellard
 * then we send a message back to the TCP which originated the
102 f0cbd3ec bellard
 * segment ti, and discard the mbuf containing it and any other
103 f0cbd3ec bellard
 * attached mbufs.
104 f0cbd3ec bellard
 *
105 f0cbd3ec bellard
 * In any case the ack and sequence number of the transmitted
106 f0cbd3ec bellard
 * segment are as specified by the parameters.
107 f0cbd3ec bellard
 */
108 f0cbd3ec bellard
void
109 f0cbd3ec bellard
tcp_respond(tp, ti, m, ack, seq, flags)
110 f0cbd3ec bellard
        struct tcpcb *tp;
111 f0cbd3ec bellard
        register struct tcpiphdr *ti;
112 f0cbd3ec bellard
        register struct mbuf *m;
113 f0cbd3ec bellard
        tcp_seq ack, seq;
114 f0cbd3ec bellard
        int flags;
115 f0cbd3ec bellard
{
116 f0cbd3ec bellard
        register int tlen;
117 f0cbd3ec bellard
        int win = 0;
118 f0cbd3ec bellard
119 f0cbd3ec bellard
        DEBUG_CALL("tcp_respond");
120 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
121 f0cbd3ec bellard
        DEBUG_ARG("ti = %lx", (long)ti);
122 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
123 f0cbd3ec bellard
        DEBUG_ARG("ack = %u", ack);
124 f0cbd3ec bellard
        DEBUG_ARG("seq = %u", seq);
125 f0cbd3ec bellard
        DEBUG_ARG("flags = %x", flags);
126 5fafdf24 ths
127 f0cbd3ec bellard
        if (tp)
128 f0cbd3ec bellard
                win = sbspace(&tp->t_socket->so_rcv);
129 f0cbd3ec bellard
        if (m == 0) {
130 f0cbd3ec bellard
                if ((m = m_get()) == NULL)
131 f0cbd3ec bellard
                        return;
132 f0cbd3ec bellard
#ifdef TCP_COMPAT_42
133 f0cbd3ec bellard
                tlen = 1;
134 f0cbd3ec bellard
#else
135 f0cbd3ec bellard
                tlen = 0;
136 f0cbd3ec bellard
#endif
137 9634d903 blueswir1
                m->m_data += IF_MAXLINKHDR;
138 f0cbd3ec bellard
                *mtod(m, struct tcpiphdr *) = *ti;
139 f0cbd3ec bellard
                ti = mtod(m, struct tcpiphdr *);
140 f0cbd3ec bellard
                flags = TH_ACK;
141 f0cbd3ec bellard
        } else {
142 5fafdf24 ths
                /*
143 f0cbd3ec bellard
                 * ti points into m so the next line is just making
144 f0cbd3ec bellard
                 * the mbuf point to ti
145 f0cbd3ec bellard
                 */
146 f0cbd3ec bellard
                m->m_data = (caddr_t)ti;
147 3b46e624 ths
148 f0cbd3ec bellard
                m->m_len = sizeof (struct tcpiphdr);
149 f0cbd3ec bellard
                tlen = 0;
150 f0cbd3ec bellard
#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
151 f0cbd3ec bellard
                xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
152 f0cbd3ec bellard
                xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
153 f0cbd3ec bellard
#undef xchg
154 f0cbd3ec bellard
        }
155 f0cbd3ec bellard
        ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
156 f0cbd3ec bellard
        tlen += sizeof (struct tcpiphdr);
157 f0cbd3ec bellard
        m->m_len = tlen;
158 f0cbd3ec bellard
159 f0cbd3ec bellard
        ti->ti_next = ti->ti_prev = 0;
160 f0cbd3ec bellard
        ti->ti_x1 = 0;
161 f0cbd3ec bellard
        ti->ti_seq = htonl(seq);
162 f0cbd3ec bellard
        ti->ti_ack = htonl(ack);
163 f0cbd3ec bellard
        ti->ti_x2 = 0;
164 f0cbd3ec bellard
        ti->ti_off = sizeof (struct tcphdr) >> 2;
165 f0cbd3ec bellard
        ti->ti_flags = flags;
166 f0cbd3ec bellard
        if (tp)
167 f0cbd3ec bellard
                ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
168 f0cbd3ec bellard
        else
169 f0cbd3ec bellard
                ti->ti_win = htons((u_int16_t)win);
170 f0cbd3ec bellard
        ti->ti_urp = 0;
171 f0cbd3ec bellard
        ti->ti_sum = 0;
172 f0cbd3ec bellard
        ti->ti_sum = cksum(m, tlen);
173 f0cbd3ec bellard
        ((struct ip *)ti)->ip_len = tlen;
174 f0cbd3ec bellard
175 5fafdf24 ths
        if(flags & TH_RST)
176 f0cbd3ec bellard
          ((struct ip *)ti)->ip_ttl = MAXTTL;
177 5fafdf24 ths
        else
178 9634d903 blueswir1
          ((struct ip *)ti)->ip_ttl = IPDEFTTL;
179 5fafdf24 ths
180 f0cbd3ec bellard
        (void) ip_output((struct socket *)0, m);
181 f0cbd3ec bellard
}
182 f0cbd3ec bellard
183 f0cbd3ec bellard
/*
184 f0cbd3ec bellard
 * Create a new TCP control block, making an
185 f0cbd3ec bellard
 * empty reassembly queue and hooking it to the argument
186 f0cbd3ec bellard
 * protocol control block.
187 f0cbd3ec bellard
 */
188 f0cbd3ec bellard
struct tcpcb *
189 f0cbd3ec bellard
tcp_newtcpcb(so)
190 f0cbd3ec bellard
        struct socket *so;
191 f0cbd3ec bellard
{
192 f0cbd3ec bellard
        register struct tcpcb *tp;
193 5fafdf24 ths
194 f0cbd3ec bellard
        tp = (struct tcpcb *)malloc(sizeof(*tp));
195 f0cbd3ec bellard
        if (tp == NULL)
196 f0cbd3ec bellard
                return ((struct tcpcb *)0);
197 5fafdf24 ths
198 f0cbd3ec bellard
        memset((char *) tp, 0, sizeof(struct tcpcb));
199 f0cbd3ec bellard
        tp->seg_next = tp->seg_prev = (tcpiphdrp_32)tp;
200 9634d903 blueswir1
        tp->t_maxseg = TCP_MSS;
201 5fafdf24 ths
202 9634d903 blueswir1
        tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
203 f0cbd3ec bellard
        tp->t_socket = so;
204 5fafdf24 ths
205 f0cbd3ec bellard
        /*
206 f0cbd3ec bellard
         * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
207 f0cbd3ec bellard
         * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
208 f0cbd3ec bellard
         * reasonable initial retransmit time.
209 f0cbd3ec bellard
         */
210 f0cbd3ec bellard
        tp->t_srtt = TCPTV_SRTTBASE;
211 9634d903 blueswir1
        tp->t_rttvar = TCPTV_SRTTDFLT << 2;
212 f0cbd3ec bellard
        tp->t_rttmin = TCPTV_MIN;
213 f0cbd3ec bellard
214 5fafdf24 ths
        TCPT_RANGESET(tp->t_rxtcur,
215 f0cbd3ec bellard
            ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
216 f0cbd3ec bellard
            TCPTV_MIN, TCPTV_REXMTMAX);
217 f0cbd3ec bellard
218 f0cbd3ec bellard
        tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
219 f0cbd3ec bellard
        tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
220 f0cbd3ec bellard
        tp->t_state = TCPS_CLOSED;
221 5fafdf24 ths
222 f0cbd3ec bellard
        so->so_tcpcb = tp;
223 f0cbd3ec bellard
224 f0cbd3ec bellard
        return (tp);
225 f0cbd3ec bellard
}
226 f0cbd3ec bellard
227 f0cbd3ec bellard
/*
228 f0cbd3ec bellard
 * Drop a TCP connection, reporting
229 f0cbd3ec bellard
 * the specified error.  If connection is synchronized,
230 f0cbd3ec bellard
 * then send a RST to peer.
231 f0cbd3ec bellard
 */
232 5fafdf24 ths
struct tcpcb *tcp_drop(struct tcpcb *tp, int err)
233 f0cbd3ec bellard
{
234 f0cbd3ec bellard
/* tcp_drop(tp, errno)
235 f0cbd3ec bellard
        register struct tcpcb *tp;
236 f0cbd3ec bellard
        int errno;
237 f0cbd3ec bellard
{
238 f0cbd3ec bellard
*/
239 f0cbd3ec bellard
240 f0cbd3ec bellard
        DEBUG_CALL("tcp_drop");
241 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
242 f0cbd3ec bellard
        DEBUG_ARG("errno = %d", errno);
243 5fafdf24 ths
244 f0cbd3ec bellard
        if (TCPS_HAVERCVDSYN(tp->t_state)) {
245 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
246 f0cbd3ec bellard
                (void) tcp_output(tp);
247 31a60e22 blueswir1
                STAT(tcpstat.tcps_drops++);
248 f0cbd3ec bellard
        } else
249 31a60e22 blueswir1
                STAT(tcpstat.tcps_conndrops++);
250 f0cbd3ec bellard
/*        if (errno == ETIMEDOUT && tp->t_softerror)
251 f0cbd3ec bellard
 *                errno = tp->t_softerror;
252 f0cbd3ec bellard
 */
253 f0cbd3ec bellard
/*        so->so_error = errno; */
254 f0cbd3ec bellard
        return (tcp_close(tp));
255 f0cbd3ec bellard
}
256 f0cbd3ec bellard
257 f0cbd3ec bellard
/*
258 f0cbd3ec bellard
 * Close a TCP control block:
259 f0cbd3ec bellard
 *        discard all space held by the tcp
260 f0cbd3ec bellard
 *        discard internet protocol block
261 f0cbd3ec bellard
 *        wake up any sleepers
262 f0cbd3ec bellard
 */
263 f0cbd3ec bellard
struct tcpcb *
264 f0cbd3ec bellard
tcp_close(tp)
265 f0cbd3ec bellard
        register struct tcpcb *tp;
266 f0cbd3ec bellard
{
267 f0cbd3ec bellard
        register struct tcpiphdr *t;
268 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
269 f0cbd3ec bellard
        register struct mbuf *m;
270 f0cbd3ec bellard
271 f0cbd3ec bellard
        DEBUG_CALL("tcp_close");
272 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long )tp);
273 5fafdf24 ths
274 f0cbd3ec bellard
        /* free the reassembly queue, if any */
275 f0cbd3ec bellard
        t = (struct tcpiphdr *) tp->seg_next;
276 f0cbd3ec bellard
        while (t != (struct tcpiphdr *)tp) {
277 f0cbd3ec bellard
                t = (struct tcpiphdr *)t->ti_next;
278 f0cbd3ec bellard
                m = (struct mbuf *) REASS_MBUF((struct tcpiphdr *)t->ti_prev);
279 f0cbd3ec bellard
                remque_32((struct tcpiphdr *) t->ti_prev);
280 f0cbd3ec bellard
                m_freem(m);
281 f0cbd3ec bellard
        }
282 f0cbd3ec bellard
        /* It's static */
283 f0cbd3ec bellard
/*        if (tp->t_template)
284 f0cbd3ec bellard
 *                (void) m_free(dtom(tp->t_template));
285 f0cbd3ec bellard
 */
286 f0cbd3ec bellard
/*        free(tp, M_PCB);  */
287 f0cbd3ec bellard
        free(tp);
288 f0cbd3ec bellard
        so->so_tcpcb = 0;
289 f0cbd3ec bellard
        soisfdisconnected(so);
290 f0cbd3ec bellard
        /* clobber input socket cache if we're closing the cached connection */
291 f0cbd3ec bellard
        if (so == tcp_last_so)
292 f0cbd3ec bellard
                tcp_last_so = &tcb;
293 379ff53d bellard
        closesocket(so->s);
294 f0cbd3ec bellard
        sbfree(&so->so_rcv);
295 f0cbd3ec bellard
        sbfree(&so->so_snd);
296 f0cbd3ec bellard
        sofree(so);
297 31a60e22 blueswir1
        STAT(tcpstat.tcps_closed++);
298 f0cbd3ec bellard
        return ((struct tcpcb *)0);
299 f0cbd3ec bellard
}
300 f0cbd3ec bellard
301 9634d903 blueswir1
#ifdef notdef
302 f0cbd3ec bellard
void
303 f0cbd3ec bellard
tcp_drain()
304 f0cbd3ec bellard
{
305 f0cbd3ec bellard
        /* XXX */
306 f0cbd3ec bellard
}
307 f0cbd3ec bellard
308 f0cbd3ec bellard
/*
309 f0cbd3ec bellard
 * When a source quench is received, close congestion window
310 f0cbd3ec bellard
 * to one segment.  We will gradually open it again as we proceed.
311 f0cbd3ec bellard
 */
312 f0cbd3ec bellard
void
313 f0cbd3ec bellard
tcp_quench(i, errno)
314 f0cbd3ec bellard
315 f0cbd3ec bellard
        int errno;
316 f0cbd3ec bellard
{
317 f0cbd3ec bellard
        struct tcpcb *tp = intotcpcb(inp);
318 f0cbd3ec bellard
319 f0cbd3ec bellard
        if (tp)
320 f0cbd3ec bellard
                tp->snd_cwnd = tp->t_maxseg;
321 f0cbd3ec bellard
}
322 f0cbd3ec bellard
323 f0cbd3ec bellard
#endif /* notdef */
324 f0cbd3ec bellard
325 f0cbd3ec bellard
/*
326 f0cbd3ec bellard
 * TCP protocol interface to socket abstraction.
327 f0cbd3ec bellard
 */
328 f0cbd3ec bellard
329 f0cbd3ec bellard
/*
330 f0cbd3ec bellard
 * User issued close, and wish to trail through shutdown states:
331 f0cbd3ec bellard
 * if never received SYN, just forget it.  If got a SYN from peer,
332 f0cbd3ec bellard
 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
333 f0cbd3ec bellard
 * If already got a FIN from peer, then almost done; go to LAST_ACK
334 f0cbd3ec bellard
 * state.  In all other cases, have already sent FIN to peer (e.g.
335 f0cbd3ec bellard
 * after PRU_SHUTDOWN), and just have to play tedious game waiting
336 f0cbd3ec bellard
 * for peer to send FIN or not respond to keep-alives, etc.
337 f0cbd3ec bellard
 * We can let the user exit from the close as soon as the FIN is acked.
338 f0cbd3ec bellard
 */
339 f0cbd3ec bellard
void
340 f0cbd3ec bellard
tcp_sockclosed(tp)
341 f0cbd3ec bellard
        struct tcpcb *tp;
342 f0cbd3ec bellard
{
343 f0cbd3ec bellard
344 f0cbd3ec bellard
        DEBUG_CALL("tcp_sockclosed");
345 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
346 5fafdf24 ths
347 f0cbd3ec bellard
        switch (tp->t_state) {
348 f0cbd3ec bellard
349 f0cbd3ec bellard
        case TCPS_CLOSED:
350 f0cbd3ec bellard
        case TCPS_LISTEN:
351 f0cbd3ec bellard
        case TCPS_SYN_SENT:
352 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
353 f0cbd3ec bellard
                tp = tcp_close(tp);
354 f0cbd3ec bellard
                break;
355 f0cbd3ec bellard
356 f0cbd3ec bellard
        case TCPS_SYN_RECEIVED:
357 f0cbd3ec bellard
        case TCPS_ESTABLISHED:
358 f0cbd3ec bellard
                tp->t_state = TCPS_FIN_WAIT_1;
359 f0cbd3ec bellard
                break;
360 f0cbd3ec bellard
361 f0cbd3ec bellard
        case TCPS_CLOSE_WAIT:
362 f0cbd3ec bellard
                tp->t_state = TCPS_LAST_ACK;
363 f0cbd3ec bellard
                break;
364 f0cbd3ec bellard
        }
365 f0cbd3ec bellard
/*        soisfdisconnecting(tp->t_socket); */
366 f0cbd3ec bellard
        if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
367 f0cbd3ec bellard
                soisfdisconnected(tp->t_socket);
368 f0cbd3ec bellard
        if (tp)
369 f0cbd3ec bellard
                tcp_output(tp);
370 f0cbd3ec bellard
}
371 f0cbd3ec bellard
372 5fafdf24 ths
/*
373 f0cbd3ec bellard
 * Connect to a host on the Internet
374 f0cbd3ec bellard
 * Called by tcp_input
375 f0cbd3ec bellard
 * Only do a connect, the tcp fields will be set in tcp_input
376 f0cbd3ec bellard
 * return 0 if there's a result of the connect,
377 f0cbd3ec bellard
 * else return -1 means we're still connecting
378 f0cbd3ec bellard
 * The return value is almost always -1 since the socket is
379 5fafdf24 ths
 * nonblocking.  Connect returns after the SYN is sent, and does
380 f0cbd3ec bellard
 * not wait for ACK+SYN.
381 f0cbd3ec bellard
 */
382 f0cbd3ec bellard
int tcp_fconnect(so)
383 f0cbd3ec bellard
     struct socket *so;
384 f0cbd3ec bellard
{
385 f0cbd3ec bellard
  int ret=0;
386 3b46e624 ths
387 f0cbd3ec bellard
  DEBUG_CALL("tcp_fconnect");
388 f0cbd3ec bellard
  DEBUG_ARG("so = %lx", (long )so);
389 f0cbd3ec bellard
390 f0cbd3ec bellard
  if( (ret=so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0) {
391 f0cbd3ec bellard
    int opt, s=so->s;
392 f0cbd3ec bellard
    struct sockaddr_in addr;
393 f0cbd3ec bellard
394 f0cbd3ec bellard
    fd_nonblock(s);
395 f0cbd3ec bellard
    opt = 1;
396 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
397 f0cbd3ec bellard
    opt = 1;
398 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
399 3b46e624 ths
400 f0cbd3ec bellard
    addr.sin_family = AF_INET;
401 f0cbd3ec bellard
    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
402 f0cbd3ec bellard
      /* It's an alias */
403 f0cbd3ec bellard
      switch(ntohl(so->so_faddr.s_addr) & 0xff) {
404 f0cbd3ec bellard
      case CTL_DNS:
405 f0cbd3ec bellard
        addr.sin_addr = dns_addr;
406 f0cbd3ec bellard
        break;
407 f0cbd3ec bellard
      case CTL_ALIAS:
408 f0cbd3ec bellard
      default:
409 f0cbd3ec bellard
        addr.sin_addr = loopback_addr;
410 f0cbd3ec bellard
        break;
411 f0cbd3ec bellard
      }
412 f0cbd3ec bellard
    } else
413 f0cbd3ec bellard
      addr.sin_addr = so->so_faddr;
414 f0cbd3ec bellard
    addr.sin_port = so->so_fport;
415 3b46e624 ths
416 f0cbd3ec bellard
    DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
417 5fafdf24 ths
                "addr.sin_addr.s_addr=%.16s\n",
418 f0cbd3ec bellard
                ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
419 f0cbd3ec bellard
    /* We don't care what port we get */
420 f0cbd3ec bellard
    ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
421 3b46e624 ths
422 f0cbd3ec bellard
    /*
423 f0cbd3ec bellard
     * If it's not in progress, it failed, so we just return 0,
424 f0cbd3ec bellard
     * without clearing SS_NOFDREF
425 f0cbd3ec bellard
     */
426 f0cbd3ec bellard
    soisfconnecting(so);
427 f0cbd3ec bellard
  }
428 f0cbd3ec bellard
429 f0cbd3ec bellard
  return(ret);
430 f0cbd3ec bellard
}
431 f0cbd3ec bellard
432 f0cbd3ec bellard
/*
433 f0cbd3ec bellard
 * Accept the socket and connect to the local-host
434 5fafdf24 ths
 *
435 f0cbd3ec bellard
 * We have a problem. The correct thing to do would be
436 f0cbd3ec bellard
 * to first connect to the local-host, and only if the
437 f0cbd3ec bellard
 * connection is accepted, then do an accept() here.
438 5fafdf24 ths
 * But, a) we need to know who's trying to connect
439 f0cbd3ec bellard
 * to the socket to be able to SYN the local-host, and
440 f0cbd3ec bellard
 * b) we are already connected to the foreign host by
441 f0cbd3ec bellard
 * the time it gets to accept(), so... We simply accept
442 f0cbd3ec bellard
 * here and SYN the local-host.
443 5fafdf24 ths
 */
444 f0cbd3ec bellard
void
445 f0cbd3ec bellard
tcp_connect(inso)
446 f0cbd3ec bellard
        struct socket *inso;
447 f0cbd3ec bellard
{
448 f0cbd3ec bellard
        struct socket *so;
449 f0cbd3ec bellard
        struct sockaddr_in addr;
450 f0cbd3ec bellard
        int addrlen = sizeof(struct sockaddr_in);
451 f0cbd3ec bellard
        struct tcpcb *tp;
452 f0cbd3ec bellard
        int s, opt;
453 f0cbd3ec bellard
454 f0cbd3ec bellard
        DEBUG_CALL("tcp_connect");
455 f0cbd3ec bellard
        DEBUG_ARG("inso = %lx", (long)inso);
456 5fafdf24 ths
457 f0cbd3ec bellard
        /*
458 f0cbd3ec bellard
         * If it's an SS_ACCEPTONCE socket, no need to socreate()
459 f0cbd3ec bellard
         * another socket, just use the accept() socket.
460 f0cbd3ec bellard
         */
461 f0cbd3ec bellard
        if (inso->so_state & SS_FACCEPTONCE) {
462 f0cbd3ec bellard
                /* FACCEPTONCE already have a tcpcb */
463 f0cbd3ec bellard
                so = inso;
464 f0cbd3ec bellard
        } else {
465 f0cbd3ec bellard
                if ((so = socreate()) == NULL) {
466 f0cbd3ec bellard
                        /* If it failed, get rid of the pending connection */
467 379ff53d bellard
                        closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
468 f0cbd3ec bellard
                        return;
469 f0cbd3ec bellard
                }
470 f0cbd3ec bellard
                if (tcp_attach(so) < 0) {
471 f0cbd3ec bellard
                        free(so); /* NOT sofree */
472 f0cbd3ec bellard
                        return;
473 f0cbd3ec bellard
                }
474 f0cbd3ec bellard
                so->so_laddr = inso->so_laddr;
475 f0cbd3ec bellard
                so->so_lport = inso->so_lport;
476 f0cbd3ec bellard
        }
477 5fafdf24 ths
478 f0cbd3ec bellard
        (void) tcp_mss(sototcpcb(so), 0);
479 f0cbd3ec bellard
480 f0cbd3ec bellard
        if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
481 f0cbd3ec bellard
                tcp_close(sototcpcb(so)); /* This will sofree() as well */
482 f0cbd3ec bellard
                return;
483 f0cbd3ec bellard
        }
484 f0cbd3ec bellard
        fd_nonblock(s);
485 f0cbd3ec bellard
        opt = 1;
486 f0cbd3ec bellard
        setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
487 f0cbd3ec bellard
        opt = 1;
488 f0cbd3ec bellard
        setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
489 eaf7e70b ths
        opt = 1;
490 eaf7e70b ths
        setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
491 5fafdf24 ths
492 f0cbd3ec bellard
        so->so_fport = addr.sin_port;
493 f0cbd3ec bellard
        so->so_faddr = addr.sin_addr;
494 f0cbd3ec bellard
        /* Translate connections from localhost to the real hostname */
495 f0cbd3ec bellard
        if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
496 8dbca8dd bellard
           so->so_faddr = alias_addr;
497 5fafdf24 ths
498 f0cbd3ec bellard
        /* Close the accept() socket, set right state */
499 f0cbd3ec bellard
        if (inso->so_state & SS_FACCEPTONCE) {
500 379ff53d bellard
                closesocket(so->s); /* If we only accept once, close the accept() socket */
501 f0cbd3ec bellard
                so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
502 f0cbd3ec bellard
                                           /* if it's not FACCEPTONCE, it's already NOFDREF */
503 f0cbd3ec bellard
        }
504 f0cbd3ec bellard
        so->s = s;
505 5fafdf24 ths
506 f0cbd3ec bellard
        so->so_iptos = tcp_tos(so);
507 f0cbd3ec bellard
        tp = sototcpcb(so);
508 f0cbd3ec bellard
509 f0cbd3ec bellard
        tcp_template(tp);
510 5fafdf24 ths
511 f0cbd3ec bellard
        /* Compute window scaling to request.  */
512 f0cbd3ec bellard
/*        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
513 f0cbd3ec bellard
 *                (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
514 f0cbd3ec bellard
 *                tp->request_r_scale++;
515 f0cbd3ec bellard
 */
516 f0cbd3ec bellard
517 f0cbd3ec bellard
/*        soisconnecting(so); */ /* NOFDREF used instead */
518 31a60e22 blueswir1
        STAT(tcpstat.tcps_connattempt++);
519 5fafdf24 ths
520 f0cbd3ec bellard
        tp->t_state = TCPS_SYN_SENT;
521 f0cbd3ec bellard
        tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
522 5fafdf24 ths
        tp->iss = tcp_iss;
523 f0cbd3ec bellard
        tcp_iss += TCP_ISSINCR/2;
524 f0cbd3ec bellard
        tcp_sendseqinit(tp);
525 f0cbd3ec bellard
        tcp_output(tp);
526 f0cbd3ec bellard
}
527 f0cbd3ec bellard
528 f0cbd3ec bellard
/*
529 f0cbd3ec bellard
 * Attach a TCPCB to a socket.
530 f0cbd3ec bellard
 */
531 f0cbd3ec bellard
int
532 f0cbd3ec bellard
tcp_attach(so)
533 f0cbd3ec bellard
        struct socket *so;
534 f0cbd3ec bellard
{
535 f0cbd3ec bellard
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL)
536 f0cbd3ec bellard
           return -1;
537 5fafdf24 ths
538 f0cbd3ec bellard
        insque(so, &tcb);
539 f0cbd3ec bellard
540 f0cbd3ec bellard
        return 0;
541 f0cbd3ec bellard
}
542 f0cbd3ec bellard
543 f0cbd3ec bellard
/*
544 f0cbd3ec bellard
 * Set the socket's type of service field
545 f0cbd3ec bellard
 */
546 9634d903 blueswir1
static const struct tos_t tcptos[] = {
547 f0cbd3ec bellard
          {0, 20, IPTOS_THROUGHPUT, 0},        /* ftp data */
548 f0cbd3ec bellard
          {21, 21, IPTOS_LOWDELAY,  EMU_FTP},        /* ftp control */
549 f0cbd3ec bellard
          {0, 23, IPTOS_LOWDELAY, 0},        /* telnet */
550 f0cbd3ec bellard
          {0, 80, IPTOS_THROUGHPUT, 0},        /* WWW */
551 f0cbd3ec bellard
          {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT},        /* rlogin */
552 f0cbd3ec bellard
          {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT},        /* shell */
553 f0cbd3ec bellard
          {0, 544, IPTOS_LOWDELAY, EMU_KSH},                /* kshell */
554 f0cbd3ec bellard
          {0, 543, IPTOS_LOWDELAY, 0},        /* klogin */
555 f0cbd3ec bellard
          {0, 6667, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC */
556 f0cbd3ec bellard
          {0, 6668, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC undernet */
557 f0cbd3ec bellard
          {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
558 f0cbd3ec bellard
          {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
559 f0cbd3ec bellard
          {0, 0, 0, 0}
560 f0cbd3ec bellard
};
561 f0cbd3ec bellard
562 7c829863 blueswir1
#ifdef CONFIG_QEMU
563 7c829863 blueswir1
static
564 7c829863 blueswir1
#endif
565 7c829863 blueswir1
struct emu_t *tcpemu = 0;
566 3b46e624 ths
567 f0cbd3ec bellard
/*
568 f0cbd3ec bellard
 * Return TOS according to the above table
569 f0cbd3ec bellard
 */
570 f0cbd3ec bellard
u_int8_t
571 f0cbd3ec bellard
tcp_tos(so)
572 f0cbd3ec bellard
        struct socket *so;
573 f0cbd3ec bellard
{
574 f0cbd3ec bellard
        int i = 0;
575 f0cbd3ec bellard
        struct emu_t *emup;
576 5fafdf24 ths
577 f0cbd3ec bellard
        while(tcptos[i].tos) {
578 f0cbd3ec bellard
                if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
579 f0cbd3ec bellard
                    (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
580 f0cbd3ec bellard
                        so->so_emu = tcptos[i].emu;
581 f0cbd3ec bellard
                        return tcptos[i].tos;
582 f0cbd3ec bellard
                }
583 f0cbd3ec bellard
                i++;
584 f0cbd3ec bellard
        }
585 5fafdf24 ths
586 f0cbd3ec bellard
        /* Nope, lets see if there's a user-added one */
587 f0cbd3ec bellard
        for (emup = tcpemu; emup; emup = emup->next) {
588 f0cbd3ec bellard
                if ((emup->fport && (ntohs(so->so_fport) == emup->fport)) ||
589 f0cbd3ec bellard
                    (emup->lport && (ntohs(so->so_lport) == emup->lport))) {
590 f0cbd3ec bellard
                        so->so_emu = emup->emu;
591 f0cbd3ec bellard
                        return emup->tos;
592 f0cbd3ec bellard
                }
593 f0cbd3ec bellard
        }
594 5fafdf24 ths
595 f0cbd3ec bellard
        return 0;
596 f0cbd3ec bellard
}
597 f0cbd3ec bellard
598 7878ff6b blueswir1
#if 0
599 f0cbd3ec bellard
int do_echo = -1;
600 7878ff6b blueswir1
#endif
601 f0cbd3ec bellard
602 f0cbd3ec bellard
/*
603 f0cbd3ec bellard
 * Emulate programs that try and connect to us
604 f0cbd3ec bellard
 * This includes ftp (the data connection is
605 f0cbd3ec bellard
 * initiated by the server) and IRC (DCC CHAT and
606 f0cbd3ec bellard
 * DCC SEND) for now
607 5fafdf24 ths
 *
608 f0cbd3ec bellard
 * NOTE: It's possible to crash SLiRP by sending it
609 f0cbd3ec bellard
 * unstandard strings to emulate... if this is a problem,
610 f0cbd3ec bellard
 * more checks are needed here
611 f0cbd3ec bellard
 *
612 f0cbd3ec bellard
 * XXX Assumes the whole command came in one packet
613 3b46e624 ths
 *
614 f0cbd3ec bellard
 * XXX Some ftp clients will have their TOS set to
615 f0cbd3ec bellard
 * LOWDELAY and so Nagel will kick in.  Because of this,
616 f0cbd3ec bellard
 * we'll get the first letter, followed by the rest, so
617 f0cbd3ec bellard
 * we simply scan for ORT instead of PORT...
618 f0cbd3ec bellard
 * DCC doesn't have this problem because there's other stuff
619 f0cbd3ec bellard
 * in the packet before the DCC command.
620 5fafdf24 ths
 *
621 5fafdf24 ths
 * Return 1 if the mbuf m is still valid and should be
622 f0cbd3ec bellard
 * sbappend()ed
623 5fafdf24 ths
 *
624 f0cbd3ec bellard
 * NOTE: if you return 0 you MUST m_free() the mbuf!
625 f0cbd3ec bellard
 */
626 f0cbd3ec bellard
int
627 f0cbd3ec bellard
tcp_emu(so, m)
628 f0cbd3ec bellard
        struct socket *so;
629 f0cbd3ec bellard
        struct mbuf *m;
630 f0cbd3ec bellard
{
631 f0cbd3ec bellard
        u_int n1, n2, n3, n4, n5, n6;
632 f0cbd3ec bellard
        char buff[256];
633 f0cbd3ec bellard
        u_int32_t laddr;
634 f0cbd3ec bellard
        u_int lport;
635 f0cbd3ec bellard
        char *bptr;
636 5fafdf24 ths
637 f0cbd3ec bellard
        DEBUG_CALL("tcp_emu");
638 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
639 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
640 5fafdf24 ths
641 f0cbd3ec bellard
        switch(so->so_emu) {
642 f0cbd3ec bellard
                int x, i;
643 3b46e624 ths
644 f0cbd3ec bellard
         case EMU_IDENT:
645 f0cbd3ec bellard
                /*
646 f0cbd3ec bellard
                 * Identification protocol as per rfc-1413
647 f0cbd3ec bellard
                 */
648 3b46e624 ths
649 f0cbd3ec bellard
                {
650 f0cbd3ec bellard
                        struct socket *tmpso;
651 f0cbd3ec bellard
                        struct sockaddr_in addr;
652 f0cbd3ec bellard
                        int addrlen = sizeof(struct sockaddr_in);
653 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
654 3b46e624 ths
655 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
656 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
657 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
658 f0cbd3ec bellard
                        m->m_data[m->m_len] = 0; /* NULL terminate */
659 f0cbd3ec bellard
                        if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
660 9634d903 blueswir1
                                if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
661 f0cbd3ec bellard
                                        HTONS(n1);
662 f0cbd3ec bellard
                                        HTONS(n2);
663 f0cbd3ec bellard
                                        /* n2 is the one on our host */
664 f0cbd3ec bellard
                                        for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
665 f0cbd3ec bellard
                                                if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
666 f0cbd3ec bellard
                                                    tmpso->so_lport == n2 &&
667 f0cbd3ec bellard
                                                    tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
668 f0cbd3ec bellard
                                                    tmpso->so_fport == n1) {
669 f0cbd3ec bellard
                                                        if (getsockname(tmpso->s,
670 f0cbd3ec bellard
                                                                (struct sockaddr *)&addr, &addrlen) == 0)
671 f0cbd3ec bellard
                                                           n2 = ntohs(addr.sin_port);
672 f0cbd3ec bellard
                                                        break;
673 f0cbd3ec bellard
                                                }
674 f0cbd3ec bellard
                                        }
675 f0cbd3ec bellard
                                }
676 f0cbd3ec bellard
                                so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
677 f0cbd3ec bellard
                                so_rcv->sb_rptr = so_rcv->sb_data;
678 f0cbd3ec bellard
                                so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
679 f0cbd3ec bellard
                        }
680 f0cbd3ec bellard
                        m_free(m);
681 f0cbd3ec bellard
                        return 0;
682 f0cbd3ec bellard
                }
683 3b46e624 ths
684 f0cbd3ec bellard
#if 0
685 f0cbd3ec bellard
         case EMU_RLOGIN:
686 f0cbd3ec bellard
                /*
687 f0cbd3ec bellard
                 * Rlogin emulation
688 f0cbd3ec bellard
                 * First we accumulate all the initial option negotiation,
689 f0cbd3ec bellard
                 * then fork_exec() rlogin according to the  options
690 f0cbd3ec bellard
                 */
691 f0cbd3ec bellard
                {
692 f0cbd3ec bellard
                        int i, i2, n;
693 f0cbd3ec bellard
                        char *ptr;
694 f0cbd3ec bellard
                        char args[100];
695 f0cbd3ec bellard
                        char term[100];
696 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
697 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
698 3b46e624 ths

699 f0cbd3ec bellard
                        /* First check if they have a priveladged port, or too much data has arrived */
700 f0cbd3ec bellard
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
701 f0cbd3ec bellard
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
702 f0cbd3ec bellard
                                memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
703 f0cbd3ec bellard
                                so_snd->sb_wptr += 18;
704 f0cbd3ec bellard
                                so_snd->sb_cc += 18;
705 f0cbd3ec bellard
                                tcp_sockclosed(sototcpcb(so));
706 f0cbd3ec bellard
                                m_free(m);
707 f0cbd3ec bellard
                                return 0;
708 f0cbd3ec bellard
                        }
709 3b46e624 ths

710 f0cbd3ec bellard
                        /* Append the current data */
711 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
712 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
713 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
714 f0cbd3ec bellard
                        m_free(m);
715 3b46e624 ths

716 f0cbd3ec bellard
                        /*
717 f0cbd3ec bellard
                         * Check if we have all the initial options,
718 f0cbd3ec bellard
                         * and build argument list to rlogin while we're here
719 f0cbd3ec bellard
                         */
720 f0cbd3ec bellard
                        n = 0;
721 f0cbd3ec bellard
                        ptr = so_rcv->sb_data;
722 f0cbd3ec bellard
                        args[0] = 0;
723 f0cbd3ec bellard
                        term[0] = 0;
724 f0cbd3ec bellard
                        while (ptr < so_rcv->sb_wptr) {
725 f0cbd3ec bellard
                                if (*ptr++ == 0) {
726 f0cbd3ec bellard
                                        n++;
727 f0cbd3ec bellard
                                        if (n == 2) {
728 f0cbd3ec bellard
                                                sprintf(args, "rlogin -l %s %s",
729 f0cbd3ec bellard
                                                        ptr, inet_ntoa(so->so_faddr));
730 f0cbd3ec bellard
                                        } else if (n == 3) {
731 f0cbd3ec bellard
                                                i2 = so_rcv->sb_wptr - ptr;
732 f0cbd3ec bellard
                                                for (i = 0; i < i2; i++) {
733 f0cbd3ec bellard
                                                        if (ptr[i] == '/') {
734 f0cbd3ec bellard
                                                                ptr[i] = 0;
735 f0cbd3ec bellard
#ifdef HAVE_SETENV
736 f0cbd3ec bellard
                                                                sprintf(term, "%s", ptr);
737 f0cbd3ec bellard
#else
738 f0cbd3ec bellard
                                                                sprintf(term, "TERM=%s", ptr);
739 f0cbd3ec bellard
#endif
740 f0cbd3ec bellard
                                                                ptr[i] = '/';
741 f0cbd3ec bellard
                                                                break;
742 f0cbd3ec bellard
                                                        }
743 f0cbd3ec bellard
                                                }
744 f0cbd3ec bellard
                                        }
745 f0cbd3ec bellard
                                }
746 f0cbd3ec bellard
                        }
747 3b46e624 ths
748 f0cbd3ec bellard
                        if (n != 4)
749 f0cbd3ec bellard
                           return 0;
750 3b46e624 ths
751 f0cbd3ec bellard
                        /* We have it, set our term variable and fork_exec() */
752 f0cbd3ec bellard
#ifdef HAVE_SETENV
753 f0cbd3ec bellard
                        setenv("TERM", term, 1);
754 f0cbd3ec bellard
#else
755 f0cbd3ec bellard
                        putenv(term);
756 f0cbd3ec bellard
#endif
757 f0cbd3ec bellard
                        fork_exec(so, args, 2);
758 f0cbd3ec bellard
                        term[0] = 0;
759 f0cbd3ec bellard
                        so->so_emu = 0;
760 3b46e624 ths
761 f0cbd3ec bellard
                        /* And finally, send the client a 0 character */
762 f0cbd3ec bellard
                        so_snd->sb_wptr[0] = 0;
763 f0cbd3ec bellard
                        so_snd->sb_wptr++;
764 f0cbd3ec bellard
                        so_snd->sb_cc++;
765 3b46e624 ths
766 f0cbd3ec bellard
                        return 0;
767 f0cbd3ec bellard
                }
768 3b46e624 ths
769 f0cbd3ec bellard
         case EMU_RSH:
770 f0cbd3ec bellard
                /*
771 f0cbd3ec bellard
                 * rsh emulation
772 f0cbd3ec bellard
                 * First we accumulate all the initial option negotiation,
773 f0cbd3ec bellard
                 * then rsh_exec() rsh according to the  options
774 f0cbd3ec bellard
                 */
775 f0cbd3ec bellard
                {
776 f0cbd3ec bellard
                        int  n;
777 f0cbd3ec bellard
                        char *ptr;
778 f0cbd3ec bellard
                        char *user;
779 f0cbd3ec bellard
                        char *args;
780 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
781 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
782 3b46e624 ths
783 f0cbd3ec bellard
                        /* First check if they have a priveladged port, or too much data has arrived */
784 f0cbd3ec bellard
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
785 f0cbd3ec bellard
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
786 f0cbd3ec bellard
                                memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
787 f0cbd3ec bellard
                                so_snd->sb_wptr += 18;
788 f0cbd3ec bellard
                                so_snd->sb_cc += 18;
789 f0cbd3ec bellard
                                tcp_sockclosed(sototcpcb(so));
790 f0cbd3ec bellard
                                m_free(m);
791 f0cbd3ec bellard
                                return 0;
792 f0cbd3ec bellard
                        }
793 3b46e624 ths
794 f0cbd3ec bellard
                        /* Append the current data */
795 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
796 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
797 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
798 f0cbd3ec bellard
                        m_free(m);
799 3b46e624 ths
800 f0cbd3ec bellard
                        /*
801 f0cbd3ec bellard
                         * Check if we have all the initial options,
802 f0cbd3ec bellard
                         * and build argument list to rlogin while we're here
803 f0cbd3ec bellard
                         */
804 f0cbd3ec bellard
                        n = 0;
805 f0cbd3ec bellard
                        ptr = so_rcv->sb_data;
806 f0cbd3ec bellard
                        user="";
807 f0cbd3ec bellard
                        args="";
808 f0cbd3ec bellard
                        if (so->extra==NULL) {
809 f0cbd3ec bellard
                                struct socket *ns;
810 f0cbd3ec bellard
                                struct tcpcb* tp;
811 f0cbd3ec bellard
                                int port=atoi(ptr);
812 f0cbd3ec bellard
                                if (port <= 0) return 0;
813 f0cbd3ec bellard
                if (port > 1023 || port < 512) {
814 f0cbd3ec bellard
                  memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
815 f0cbd3ec bellard
                  so_snd->sb_wptr += 18;
816 f0cbd3ec bellard
                  so_snd->sb_cc += 18;
817 f0cbd3ec bellard
                  tcp_sockclosed(sototcpcb(so));
818 f0cbd3ec bellard
                  return 0;
819 f0cbd3ec bellard
                }
820 f0cbd3ec bellard
                                if ((ns=socreate()) == NULL)
821 f0cbd3ec bellard
                  return 0;
822 f0cbd3ec bellard
                                if (tcp_attach(ns)<0) {
823 f0cbd3ec bellard
                  free(ns);
824 f0cbd3ec bellard
                  return 0;
825 f0cbd3ec bellard
                                }
826 f0cbd3ec bellard
827 f0cbd3ec bellard
                                ns->so_laddr=so->so_laddr;
828 f0cbd3ec bellard
                                ns->so_lport=htons(port);
829 f0cbd3ec bellard
830 f0cbd3ec bellard
                                (void) tcp_mss(sototcpcb(ns), 0);
831 f0cbd3ec bellard
832 f0cbd3ec bellard
                                ns->so_faddr=so->so_faddr;
833 f0cbd3ec bellard
                                ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
834 f0cbd3ec bellard
835 5fafdf24 ths
                                if (ns->so_faddr.s_addr == 0 ||
836 f0cbd3ec bellard
                                        ns->so_faddr.s_addr == loopback_addr.s_addr)
837 8dbca8dd bellard
                  ns->so_faddr = alias_addr;
838 f0cbd3ec bellard
839 f0cbd3ec bellard
                                ns->so_iptos = tcp_tos(ns);
840 f0cbd3ec bellard
                                tp = sototcpcb(ns);
841 3b46e624 ths
842 f0cbd3ec bellard
                                tcp_template(tp);
843 3b46e624 ths
844 f0cbd3ec bellard
                                /* Compute window scaling to request.  */
845 f0cbd3ec bellard
                                /*        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
846 f0cbd3ec bellard
                                 *                (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
847 f0cbd3ec bellard
                                 *                tp->request_r_scale++;
848 f0cbd3ec bellard
                                 */
849 f0cbd3ec bellard
850 f0cbd3ec bellard
                /*soisfconnecting(ns);*/
851 f0cbd3ec bellard
852 31a60e22 blueswir1
                                STAT(tcpstat.tcps_connattempt++);
853 3b46e624 ths
854 f0cbd3ec bellard
                                tp->t_state = TCPS_SYN_SENT;
855 f0cbd3ec bellard
                                tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
856 5fafdf24 ths
                                tp->iss = tcp_iss;
857 f0cbd3ec bellard
                                tcp_iss += TCP_ISSINCR/2;
858 f0cbd3ec bellard
                                tcp_sendseqinit(tp);
859 f0cbd3ec bellard
                                tcp_output(tp);
860 f0cbd3ec bellard
                                so->extra=ns;
861 f0cbd3ec bellard
                        }
862 f0cbd3ec bellard
                        while (ptr < so_rcv->sb_wptr) {
863 f0cbd3ec bellard
              if (*ptr++ == 0) {
864 f0cbd3ec bellard
                n++;
865 f0cbd3ec bellard
                if (n == 2) {
866 f0cbd3ec bellard
                  user=ptr;
867 f0cbd3ec bellard
                } else if (n == 3) {
868 f0cbd3ec bellard
                  args=ptr;
869 f0cbd3ec bellard
                }
870 f0cbd3ec bellard
              }
871 f0cbd3ec bellard
                        }
872 3b46e624 ths
873 f0cbd3ec bellard
                        if (n != 4)
874 f0cbd3ec bellard
              return 0;
875 3b46e624 ths
876 f0cbd3ec bellard
                        rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
877 f0cbd3ec bellard
                        so->so_emu = 0;
878 f0cbd3ec bellard
                        so->extra=NULL;
879 3b46e624 ths
880 f0cbd3ec bellard
                        /* And finally, send the client a 0 character */
881 f0cbd3ec bellard
                        so_snd->sb_wptr[0] = 0;
882 f0cbd3ec bellard
                        so_snd->sb_wptr++;
883 f0cbd3ec bellard
                        so_snd->sb_cc++;
884 3b46e624 ths
885 f0cbd3ec bellard
                        return 0;
886 f0cbd3ec bellard
                }
887 f0cbd3ec bellard
888 f0cbd3ec bellard
         case EMU_CTL:
889 f0cbd3ec bellard
                {
890 f0cbd3ec bellard
                        int num;
891 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
892 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
893 3b46e624 ths
894 f0cbd3ec bellard
                        /*
895 f0cbd3ec bellard
                         * If there is binary data here, we save it in so->so_m
896 f0cbd3ec bellard
                         */
897 f0cbd3ec bellard
                        if (!so->so_m) {
898 f0cbd3ec bellard
                          int rxlen;
899 f0cbd3ec bellard
                          char *rxdata;
900 f0cbd3ec bellard
                          rxdata=mtod(m, char *);
901 f0cbd3ec bellard
                          for (rxlen=m->m_len; rxlen; rxlen--) {
902 f0cbd3ec bellard
                            if (*rxdata++ & 0x80) {
903 f0cbd3ec bellard
                              so->so_m = m;
904 f0cbd3ec bellard
                              return 0;
905 f0cbd3ec bellard
                            }
906 f0cbd3ec bellard
                          }
907 f0cbd3ec bellard
                        } /* if(so->so_m==NULL) */
908 3b46e624 ths
909 f0cbd3ec bellard
                        /*
910 f0cbd3ec bellard
                         * Append the line
911 f0cbd3ec bellard
                         */
912 f0cbd3ec bellard
                        sbappendsb(so_rcv, m);
913 3b46e624 ths
914 f0cbd3ec bellard
                        /* To avoid going over the edge of the buffer, we reset it */
915 f0cbd3ec bellard
                        if (so_snd->sb_cc == 0)
916 f0cbd3ec bellard
                           so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
917 3b46e624 ths
918 f0cbd3ec bellard
                        /*
919 f0cbd3ec bellard
                         * A bit of a hack:
920 f0cbd3ec bellard
                         * If the first packet we get here is 1 byte long, then it
921 f0cbd3ec bellard
                         * was done in telnet character mode, therefore we must echo
922 f0cbd3ec bellard
                         * the characters as they come.  Otherwise, we echo nothing,
923 f0cbd3ec bellard
                         * because in linemode, the line is already echoed
924 f0cbd3ec bellard
                         * XXX two or more control connections won't work
925 f0cbd3ec bellard
                         */
926 f0cbd3ec bellard
                        if (do_echo == -1) {
927 f0cbd3ec bellard
                                if (m->m_len == 1) do_echo = 1;
928 f0cbd3ec bellard
                                else do_echo = 0;
929 f0cbd3ec bellard
                        }
930 f0cbd3ec bellard
                        if (do_echo) {
931 f0cbd3ec bellard
                          sbappendsb(so_snd, m);
932 f0cbd3ec bellard
                          m_free(m);
933 f0cbd3ec bellard
                          tcp_output(sototcpcb(so)); /* XXX */
934 f0cbd3ec bellard
                        } else
935 f0cbd3ec bellard
                          m_free(m);
936 3b46e624 ths
937 f0cbd3ec bellard
                        num = 0;
938 f0cbd3ec bellard
                        while (num < so->so_rcv.sb_cc) {
939 f0cbd3ec bellard
                                if (*(so->so_rcv.sb_rptr + num) == '\n' ||
940 f0cbd3ec bellard
                                    *(so->so_rcv.sb_rptr + num) == '\r') {
941 f0cbd3ec bellard
                                        int n;
942 3b46e624 ths
943 f0cbd3ec bellard
                                        *(so_rcv->sb_rptr + num) = 0;
944 f0cbd3ec bellard
                                        if (ctl_password && !ctl_password_ok) {
945 f0cbd3ec bellard
                                                /* Need a password */
946 f0cbd3ec bellard
                                                if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
947 f0cbd3ec bellard
                                                        if (strcmp(buff, ctl_password) == 0) {
948 f0cbd3ec bellard
                                                                ctl_password_ok = 1;
949 f0cbd3ec bellard
                                                                n = sprintf(so_snd->sb_wptr,
950 f0cbd3ec bellard
                                                                            "Password OK.\r\n");
951 f0cbd3ec bellard
                                                                goto do_prompt;
952 f0cbd3ec bellard
                                                        }
953 f0cbd3ec bellard
                                                }
954 f0cbd3ec bellard
                                                n = sprintf(so_snd->sb_wptr,
955 f0cbd3ec bellard
                                         "Error: Password required, log on with \"pass PASSWORD\"\r\n");
956 f0cbd3ec bellard
                                                goto do_prompt;
957 f0cbd3ec bellard
                                        }
958 f0cbd3ec bellard
                                        cfg_quitting = 0;
959 f0cbd3ec bellard
                                        n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
960 f0cbd3ec bellard
                                        if (!cfg_quitting) {
961 f0cbd3ec bellard
                                                /* Register the printed data */
962 f0cbd3ec bellard
do_prompt:
963 f0cbd3ec bellard
                                                so_snd->sb_cc += n;
964 f0cbd3ec bellard
                                                so_snd->sb_wptr += n;
965 f0cbd3ec bellard
                                                /* Add prompt */
966 f0cbd3ec bellard
                                                n = sprintf(so_snd->sb_wptr, "Slirp> ");
967 f0cbd3ec bellard
                                                so_snd->sb_cc += n;
968 f0cbd3ec bellard
                                                so_snd->sb_wptr += n;
969 f0cbd3ec bellard
                                        }
970 f0cbd3ec bellard
                                        /* Drop so_rcv data */
971 f0cbd3ec bellard
                                        so_rcv->sb_cc = 0;
972 f0cbd3ec bellard
                                        so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
973 f0cbd3ec bellard
                                        tcp_output(sototcpcb(so)); /* Send the reply */
974 f0cbd3ec bellard
                                }
975 f0cbd3ec bellard
                                num++;
976 f0cbd3ec bellard
                        }
977 f0cbd3ec bellard
                        return 0;
978 f0cbd3ec bellard
                }
979 3b46e624 ths
#endif
980 f0cbd3ec bellard
        case EMU_FTP: /* ftp */
981 f0cbd3ec bellard
                *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
982 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
983 f0cbd3ec bellard
                        /*
984 f0cbd3ec bellard
                         * Need to emulate the PORT command
985 3b46e624 ths
                         */
986 9634d903 blueswir1
                        x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
987 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
988 f0cbd3ec bellard
                        if (x < 6)
989 f0cbd3ec bellard
                           return 1;
990 3b46e624 ths
991 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
992 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
993 3b46e624 ths
994 f0cbd3ec bellard
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
995 f0cbd3ec bellard
                           return 1;
996 3b46e624 ths
997 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
998 3b46e624 ths
999 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
1000 f0cbd3ec bellard
                        n6 &= 0xff;
1001 3b46e624 ths
1002 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
1003 3b46e624 ths
1004 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
1005 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
1006 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
1007 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
1008 3b46e624 ths
1009 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1010 5fafdf24 ths
                        m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
1011 f0cbd3ec bellard
                                            n1, n2, n3, n4, n5, n6, x==7?buff:"");
1012 f0cbd3ec bellard
                        return 1;
1013 f0cbd3ec bellard
                } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
1014 f0cbd3ec bellard
                        /*
1015 f0cbd3ec bellard
                         * Need to emulate the PASV response
1016 f0cbd3ec bellard
                         */
1017 9634d903 blueswir1
                        x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
1018 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
1019 f0cbd3ec bellard
                        if (x < 6)
1020 f0cbd3ec bellard
                           return 1;
1021 3b46e624 ths
1022 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1023 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
1024 3b46e624 ths
1025 f0cbd3ec bellard
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1026 f0cbd3ec bellard
                           return 1;
1027 3b46e624 ths
1028 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
1029 3b46e624 ths
1030 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
1031 f0cbd3ec bellard
                        n6 &= 0xff;
1032 3b46e624 ths
1033 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
1034 3b46e624 ths
1035 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
1036 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
1037 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
1038 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
1039 3b46e624 ths
1040 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1041 f0cbd3ec bellard
                        m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
1042 f0cbd3ec bellard
                                            n1, n2, n3, n4, n5, n6, x==7?buff:"");
1043 3b46e624 ths
1044 f0cbd3ec bellard
                        return 1;
1045 f0cbd3ec bellard
                }
1046 3b46e624 ths
1047 f0cbd3ec bellard
                return 1;
1048 3b46e624 ths
1049 f0cbd3ec bellard
         case EMU_KSH:
1050 f0cbd3ec bellard
                /*
1051 f0cbd3ec bellard
                 * The kshell (Kerberos rsh) and shell services both pass
1052 f0cbd3ec bellard
                 * a local port port number to carry signals to the server
1053 f0cbd3ec bellard
                 * and stderr to the client.  It is passed at the beginning
1054 f0cbd3ec bellard
                 * of the connection as a NUL-terminated decimal ASCII string.
1055 f0cbd3ec bellard
                 */
1056 f0cbd3ec bellard
                so->so_emu = 0;
1057 f0cbd3ec bellard
                for (lport = 0, i = 0; i < m->m_len-1; ++i) {
1058 f0cbd3ec bellard
                        if (m->m_data[i] < '0' || m->m_data[i] > '9')
1059 f0cbd3ec bellard
                                return 1;       /* invalid number */
1060 f0cbd3ec bellard
                        lport *= 10;
1061 f0cbd3ec bellard
                        lport += m->m_data[i] - '0';
1062 f0cbd3ec bellard
                }
1063 f0cbd3ec bellard
                if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
1064 f0cbd3ec bellard
                    (so = solisten(0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1065 f0cbd3ec bellard
                        m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
1066 f0cbd3ec bellard
                return 1;
1067 3b46e624 ths
1068 f0cbd3ec bellard
         case EMU_IRC:
1069 f0cbd3ec bellard
                /*
1070 f0cbd3ec bellard
                 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
1071 f0cbd3ec bellard
                 */
1072 f0cbd3ec bellard
                *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
1073 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
1074 f0cbd3ec bellard
                         return 1;
1075 3b46e624 ths
1076 f0cbd3ec bellard
                /* The %256s is for the broken mIRC */
1077 f0cbd3ec bellard
                if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1078 f0cbd3ec bellard
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1079 f0cbd3ec bellard
                                return 1;
1080 3b46e624 ths
1081 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1082 f0cbd3ec bellard
                        m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1083 f0cbd3ec bellard
                             (unsigned long)ntohl(so->so_faddr.s_addr),
1084 f0cbd3ec bellard
                             ntohs(so->so_fport), 1);
1085 f0cbd3ec bellard
                } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1086 f0cbd3ec bellard
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1087 f0cbd3ec bellard
                                return 1;
1088 3b46e624 ths
1089 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1090 5fafdf24 ths
                        m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
1091 f0cbd3ec bellard
                              buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1092 f0cbd3ec bellard
                              ntohs(so->so_fport), n1, 1);
1093 f0cbd3ec bellard
                } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1094 f0cbd3ec bellard
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1095 f0cbd3ec bellard
                                return 1;
1096 3b46e624 ths
1097 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1098 f0cbd3ec bellard
                        m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1099 f0cbd3ec bellard
                              buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1100 f0cbd3ec bellard
                              ntohs(so->so_fport), n1, 1);
1101 f0cbd3ec bellard
                }
1102 f0cbd3ec bellard
                return 1;
1103 f0cbd3ec bellard
1104 f0cbd3ec bellard
         case EMU_REALAUDIO:
1105 5fafdf24 ths
                /*
1106 f0cbd3ec bellard
                 * RealAudio emulation - JP. We must try to parse the incoming
1107 f0cbd3ec bellard
                 * data and try to find the two characters that contain the
1108 f0cbd3ec bellard
                 * port number. Then we redirect an udp port and replace the
1109 f0cbd3ec bellard
                 * number with the real port we got.
1110 f0cbd3ec bellard
                 *
1111 f0cbd3ec bellard
                 * The 1.0 beta versions of the player are not supported
1112 f0cbd3ec bellard
                 * any more.
1113 5fafdf24 ths
                 *
1114 f0cbd3ec bellard
                 * A typical packet for player version 1.0 (release version):
1115 3b46e624 ths
                 *
1116 5fafdf24 ths
                 * 0000:50 4E 41 00 05
1117 f0cbd3ec bellard
                 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....?..g?l?c..P
1118 f0cbd3ec bellard
                 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
1119 f0cbd3ec bellard
                 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
1120 f0cbd3ec bellard
                 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
1121 3b46e624 ths
                 *
1122 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
1123 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
1124 f0cbd3ec bellard
                 * second packet. This time we received five bytes first and
1125 f0cbd3ec bellard
                 * then the rest. You never know how many bytes you get.
1126 f0cbd3ec bellard
                 *
1127 f0cbd3ec bellard
                 * A typical packet for player version 2.0 (beta):
1128 3b46e624 ths
                 *
1129 f0cbd3ec bellard
                 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........?.
1130 f0cbd3ec bellard
                 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .gux?c..Win2.0.0
1131 f0cbd3ec bellard
                 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
1132 f0cbd3ec bellard
                 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
1133 f0cbd3ec bellard
                 * 0040:65 2E 72 61 79 53 00 00 06 36 42                e.rayS...6B
1134 3b46e624 ths
                 *
1135 f0cbd3ec bellard
                 * Port number 0x1BC1 is found at offset 0x0d.
1136 3b46e624 ths
                 *
1137 f0cbd3ec bellard
                 * This is just a horrible switch statement. Variable ra tells
1138 f0cbd3ec bellard
                 * us where we're going.
1139 f0cbd3ec bellard
                 */
1140 3b46e624 ths
1141 f0cbd3ec bellard
                bptr = m->m_data;
1142 f0cbd3ec bellard
                while (bptr < m->m_data + m->m_len) {
1143 f0cbd3ec bellard
                        u_short p;
1144 f0cbd3ec bellard
                        static int ra = 0;
1145 5fafdf24 ths
                        char ra_tbl[4];
1146 3b46e624 ths
1147 f0cbd3ec bellard
                        ra_tbl[0] = 0x50;
1148 f0cbd3ec bellard
                        ra_tbl[1] = 0x4e;
1149 f0cbd3ec bellard
                        ra_tbl[2] = 0x41;
1150 f0cbd3ec bellard
                        ra_tbl[3] = 0;
1151 3b46e624 ths
1152 f0cbd3ec bellard
                        switch (ra) {
1153 f0cbd3ec bellard
                         case 0:
1154 f0cbd3ec bellard
                         case 2:
1155 f0cbd3ec bellard
                         case 3:
1156 f0cbd3ec bellard
                                if (*bptr++ != ra_tbl[ra]) {
1157 f0cbd3ec bellard
                                        ra = 0;
1158 f0cbd3ec bellard
                                        continue;
1159 f0cbd3ec bellard
                                }
1160 f0cbd3ec bellard
                                break;
1161 3b46e624 ths
1162 f0cbd3ec bellard
                         case 1:
1163 f0cbd3ec bellard
                                /*
1164 f0cbd3ec bellard
                                 * We may get 0x50 several times, ignore them
1165 f0cbd3ec bellard
                                 */
1166 f0cbd3ec bellard
                                if (*bptr == 0x50) {
1167 f0cbd3ec bellard
                                        ra = 1;
1168 f0cbd3ec bellard
                                        bptr++;
1169 f0cbd3ec bellard
                                        continue;
1170 f0cbd3ec bellard
                                } else if (*bptr++ != ra_tbl[ra]) {
1171 f0cbd3ec bellard
                                        ra = 0;
1172 f0cbd3ec bellard
                                        continue;
1173 f0cbd3ec bellard
                                }
1174 f0cbd3ec bellard
                                break;
1175 3b46e624 ths
1176 5fafdf24 ths
                         case 4:
1177 5fafdf24 ths
                                /*
1178 f0cbd3ec bellard
                                 * skip version number
1179 f0cbd3ec bellard
                                 */
1180 f0cbd3ec bellard
                                bptr++;
1181 f0cbd3ec bellard
                                break;
1182 3b46e624 ths
1183 5fafdf24 ths
                         case 5:
1184 f0cbd3ec bellard
                                /*
1185 f0cbd3ec bellard
                                 * The difference between versions 1.0 and
1186 f0cbd3ec bellard
                                 * 2.0 is here. For future versions of
1187 f0cbd3ec bellard
                                 * the player this may need to be modified.
1188 f0cbd3ec bellard
                                 */
1189 f0cbd3ec bellard
                                if (*(bptr + 1) == 0x02)
1190 f0cbd3ec bellard
                                   bptr += 8;
1191 f0cbd3ec bellard
                                else
1192 f0cbd3ec bellard
                                   bptr += 4;
1193 3b46e624 ths
                                break;
1194 3b46e624 ths
1195 f0cbd3ec bellard
                         case 6:
1196 f0cbd3ec bellard
                                /* This is the field containing the port
1197 f0cbd3ec bellard
                                 * number that RA-player is listening to.
1198 f0cbd3ec bellard
                                 */
1199 5fafdf24 ths
                                lport = (((u_char*)bptr)[0] << 8)
1200 f0cbd3ec bellard
                                + ((u_char *)bptr)[1];
1201 3b46e624 ths
                                if (lport < 6970)
1202 f0cbd3ec bellard
                                   lport += 256;   /* don't know why */
1203 f0cbd3ec bellard
                                if (lport < 6970 || lport > 7170)
1204 f0cbd3ec bellard
                                   return 1;       /* failed */
1205 3b46e624 ths
1206 f0cbd3ec bellard
                                /* try to get udp port between 6970 - 7170 */
1207 f0cbd3ec bellard
                                for (p = 6970; p < 7071; p++) {
1208 f0cbd3ec bellard
                                        if (udp_listen( htons(p),
1209 f0cbd3ec bellard
                                                       so->so_laddr.s_addr,
1210 f0cbd3ec bellard
                                                       htons(lport),
1211 f0cbd3ec bellard
                                                       SS_FACCEPTONCE)) {
1212 f0cbd3ec bellard
                                                break;
1213 f0cbd3ec bellard
                                        }
1214 f0cbd3ec bellard
                                }
1215 f0cbd3ec bellard
                                if (p == 7071)
1216 f0cbd3ec bellard
                                   p = 0;
1217 f0cbd3ec bellard
                                *(u_char *)bptr++ = (p >> 8) & 0xff;
1218 f0cbd3ec bellard
                                *(u_char *)bptr++ = p & 0xff;
1219 5fafdf24 ths
                                ra = 0;
1220 f0cbd3ec bellard
                                return 1;   /* port redirected, we're done */
1221 3b46e624 ths
                                break;
1222 3b46e624 ths
1223 f0cbd3ec bellard
                         default:
1224 3b46e624 ths
                                ra = 0;
1225 f0cbd3ec bellard
                        }
1226 f0cbd3ec bellard
                        ra++;
1227 f0cbd3ec bellard
                }
1228 3b46e624 ths
                return 1;
1229 3b46e624 ths
1230 f0cbd3ec bellard
         default:
1231 f0cbd3ec bellard
                /* Ooops, not emulated, won't call tcp_emu again */
1232 f0cbd3ec bellard
                so->so_emu = 0;
1233 f0cbd3ec bellard
                return 1;
1234 f0cbd3ec bellard
        }
1235 f0cbd3ec bellard
}
1236 f0cbd3ec bellard
1237 f0cbd3ec bellard
/*
1238 f0cbd3ec bellard
 * Do misc. config of SLiRP while its running.
1239 f0cbd3ec bellard
 * Return 0 if this connections is to be closed, 1 otherwise,
1240 f0cbd3ec bellard
 * return 2 if this is a command-line connection
1241 f0cbd3ec bellard
 */
1242 f0cbd3ec bellard
int
1243 f0cbd3ec bellard
tcp_ctl(so)
1244 f0cbd3ec bellard
        struct socket *so;
1245 f0cbd3ec bellard
{
1246 f0cbd3ec bellard
        struct sbuf *sb = &so->so_snd;
1247 f0cbd3ec bellard
        int command;
1248 f0cbd3ec bellard
         struct ex_list *ex_ptr;
1249 f0cbd3ec bellard
        int do_pty;
1250 99679ece bellard
        //        struct socket *tmpso;
1251 5fafdf24 ths
1252 f0cbd3ec bellard
        DEBUG_CALL("tcp_ctl");
1253 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long )so);
1254 5fafdf24 ths
1255 a3d4af03 bellard
#if 0
1256 f0cbd3ec bellard
        /*
1257 f0cbd3ec bellard
         * Check if they're authorised
1258 f0cbd3ec bellard
         */
1259 f0cbd3ec bellard
        if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
1260 f0cbd3ec bellard
                sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
1261 f0cbd3ec bellard
                sb->sb_wptr += sb->sb_cc;
1262 f0cbd3ec bellard
                return 0;
1263 f0cbd3ec bellard
        }
1264 5fafdf24 ths
#endif
1265 f0cbd3ec bellard
        command = (ntohl(so->so_faddr.s_addr) & 0xff);
1266 5fafdf24 ths
1267 f0cbd3ec bellard
        switch(command) {
1268 f0cbd3ec bellard
        default: /* Check for exec's */
1269 3b46e624 ths
1270 f0cbd3ec bellard
                /*
1271 f0cbd3ec bellard
                 * Check if it's pty_exec
1272 f0cbd3ec bellard
                 */
1273 f0cbd3ec bellard
                for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1274 f0cbd3ec bellard
                        if (ex_ptr->ex_fport == so->so_fport &&
1275 f0cbd3ec bellard
                            command == ex_ptr->ex_addr) {
1276 f0cbd3ec bellard
                                do_pty = ex_ptr->ex_pty;
1277 f0cbd3ec bellard
                                goto do_exec;
1278 f0cbd3ec bellard
                        }
1279 f0cbd3ec bellard
                }
1280 3b46e624 ths
1281 f0cbd3ec bellard
                /*
1282 f0cbd3ec bellard
                 * Nothing bound..
1283 f0cbd3ec bellard
                 */
1284 f0cbd3ec bellard
                /* tcp_fconnect(so); */
1285 3b46e624 ths
1286 f0cbd3ec bellard
                /* FALLTHROUGH */
1287 f0cbd3ec bellard
        case CTL_ALIAS:
1288 f0cbd3ec bellard
          sb->sb_cc = sprintf(sb->sb_wptr,
1289 f0cbd3ec bellard
                              "Error: No application configured.\r\n");
1290 f0cbd3ec bellard
          sb->sb_wptr += sb->sb_cc;
1291 f0cbd3ec bellard
          return(0);
1292 f0cbd3ec bellard
1293 f0cbd3ec bellard
        do_exec:
1294 f0cbd3ec bellard
                DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
1295 f0cbd3ec bellard
                return(fork_exec(so, ex_ptr->ex_exec, do_pty));
1296 3b46e624 ths
1297 a3d4af03 bellard
#if 0
1298 f0cbd3ec bellard
        case CTL_CMD:
1299 f0cbd3ec bellard
           for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
1300 5fafdf24 ths
             if (tmpso->so_emu == EMU_CTL &&
1301 5fafdf24 ths
                 !(tmpso->so_tcpcb?
1302 f0cbd3ec bellard
                   (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
1303 f0cbd3ec bellard
                   :0)) {
1304 f0cbd3ec bellard
               /* Ooops, control connection already active */
1305 f0cbd3ec bellard
               sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
1306 f0cbd3ec bellard
               sb->sb_wptr += sb->sb_cc;
1307 f0cbd3ec bellard
               return 0;
1308 f0cbd3ec bellard
             }
1309 f0cbd3ec bellard
           }
1310 f0cbd3ec bellard
           so->so_emu = EMU_CTL;
1311 f0cbd3ec bellard
           ctl_password_ok = 0;
1312 f0cbd3ec bellard
           sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
1313 f0cbd3ec bellard
           sb->sb_wptr += sb->sb_cc;
1314 f0cbd3ec bellard
           do_echo=-1;
1315 f0cbd3ec bellard
           return(2);
1316 f0cbd3ec bellard
#endif
1317 a3d4af03 bellard
        }
1318 f0cbd3ec bellard
}