Statistics
| Branch: | Revision:

root / slirp / tcp_subr.c @ 70ead434

History | View | Annotate | Download (34.7 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 f0cbd3ec bellard
 * 
41 f0cbd3ec bellard
 * 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 f0cbd3ec bellard
int         tcp_mssdflt = TCP_MSS;
50 f0cbd3ec bellard
int         tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
51 f0cbd3ec bellard
int        tcp_do_rfc1323 = 0;        /* Don't do rfc1323 performance enhancements */
52 f0cbd3ec bellard
int        tcp_rcvspace;        /* You may want to change this */
53 f0cbd3ec bellard
int        tcp_sndspace;        /* Keep small if you have an error prone link */
54 f0cbd3ec bellard
55 f0cbd3ec bellard
/*
56 f0cbd3ec bellard
 * Tcp initialization
57 f0cbd3ec bellard
 */
58 f0cbd3ec bellard
void
59 f0cbd3ec bellard
tcp_init()
60 f0cbd3ec bellard
{
61 f0cbd3ec bellard
        tcp_iss = 1;                /* wrong */
62 f0cbd3ec bellard
        tcb.so_next = tcb.so_prev = &tcb;
63 f0cbd3ec bellard
        
64 f0cbd3ec bellard
        /* tcp_rcvspace = our Window we advertise to the remote */
65 f0cbd3ec bellard
        tcp_rcvspace = TCP_RCVSPACE;
66 f0cbd3ec bellard
        tcp_sndspace = TCP_SNDSPACE;
67 f0cbd3ec bellard
        
68 f0cbd3ec bellard
        /* Make sure tcp_sndspace is at least 2*MSS */
69 f0cbd3ec bellard
        if (tcp_sndspace < 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr)))
70 f0cbd3ec bellard
                tcp_sndspace = 2*(min(if_mtu, if_mru) - sizeof(struct tcpiphdr));
71 f0cbd3ec bellard
}
72 f0cbd3ec bellard
73 f0cbd3ec bellard
/*
74 f0cbd3ec bellard
 * Create template to be used to send tcp packets on a connection.
75 f0cbd3ec bellard
 * Call after host entry created, fills
76 f0cbd3ec bellard
 * in a skeletal tcp/ip header, minimizing the amount of work
77 f0cbd3ec bellard
 * necessary when the connection is used.
78 f0cbd3ec bellard
 */
79 f0cbd3ec bellard
/* struct tcpiphdr * */
80 f0cbd3ec bellard
void
81 f0cbd3ec bellard
tcp_template(tp)
82 f0cbd3ec bellard
        struct tcpcb *tp;
83 f0cbd3ec bellard
{
84 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
85 f0cbd3ec bellard
        register struct tcpiphdr *n = &tp->t_template;
86 f0cbd3ec bellard
87 f0cbd3ec bellard
        n->ti_next = n->ti_prev = 0;
88 f0cbd3ec bellard
        n->ti_x1 = 0;
89 f0cbd3ec bellard
        n->ti_pr = IPPROTO_TCP;
90 f0cbd3ec bellard
        n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
91 f0cbd3ec bellard
        n->ti_src = so->so_faddr;
92 f0cbd3ec bellard
        n->ti_dst = so->so_laddr;
93 f0cbd3ec bellard
        n->ti_sport = so->so_fport;
94 f0cbd3ec bellard
        n->ti_dport = so->so_lport;
95 f0cbd3ec bellard
        
96 f0cbd3ec bellard
        n->ti_seq = 0;
97 f0cbd3ec bellard
        n->ti_ack = 0;
98 f0cbd3ec bellard
        n->ti_x2 = 0;
99 f0cbd3ec bellard
        n->ti_off = 5;
100 f0cbd3ec bellard
        n->ti_flags = 0;
101 f0cbd3ec bellard
        n->ti_win = 0;
102 f0cbd3ec bellard
        n->ti_sum = 0;
103 f0cbd3ec bellard
        n->ti_urp = 0;
104 f0cbd3ec bellard
}
105 f0cbd3ec bellard
106 f0cbd3ec bellard
/*
107 f0cbd3ec bellard
 * Send a single message to the TCP at address specified by
108 f0cbd3ec bellard
 * the given TCP/IP header.  If m == 0, then we make a copy
109 f0cbd3ec bellard
 * of the tcpiphdr at ti and send directly to the addressed host.
110 f0cbd3ec bellard
 * This is used to force keep alive messages out using the TCP
111 f0cbd3ec bellard
 * template for a connection tp->t_template.  If flags are given
112 f0cbd3ec bellard
 * then we send a message back to the TCP which originated the
113 f0cbd3ec bellard
 * segment ti, and discard the mbuf containing it and any other
114 f0cbd3ec bellard
 * attached mbufs.
115 f0cbd3ec bellard
 *
116 f0cbd3ec bellard
 * In any case the ack and sequence number of the transmitted
117 f0cbd3ec bellard
 * segment are as specified by the parameters.
118 f0cbd3ec bellard
 */
119 f0cbd3ec bellard
void
120 f0cbd3ec bellard
tcp_respond(tp, ti, m, ack, seq, flags)
121 f0cbd3ec bellard
        struct tcpcb *tp;
122 f0cbd3ec bellard
        register struct tcpiphdr *ti;
123 f0cbd3ec bellard
        register struct mbuf *m;
124 f0cbd3ec bellard
        tcp_seq ack, seq;
125 f0cbd3ec bellard
        int flags;
126 f0cbd3ec bellard
{
127 f0cbd3ec bellard
        register int tlen;
128 f0cbd3ec bellard
        int win = 0;
129 f0cbd3ec bellard
130 f0cbd3ec bellard
        DEBUG_CALL("tcp_respond");
131 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
132 f0cbd3ec bellard
        DEBUG_ARG("ti = %lx", (long)ti);
133 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
134 f0cbd3ec bellard
        DEBUG_ARG("ack = %u", ack);
135 f0cbd3ec bellard
        DEBUG_ARG("seq = %u", seq);
136 f0cbd3ec bellard
        DEBUG_ARG("flags = %x", flags);
137 f0cbd3ec bellard
        
138 f0cbd3ec bellard
        if (tp)
139 f0cbd3ec bellard
                win = sbspace(&tp->t_socket->so_rcv);
140 f0cbd3ec bellard
        if (m == 0) {
141 f0cbd3ec bellard
                if ((m = m_get()) == NULL)
142 f0cbd3ec bellard
                        return;
143 f0cbd3ec bellard
#ifdef TCP_COMPAT_42
144 f0cbd3ec bellard
                tlen = 1;
145 f0cbd3ec bellard
#else
146 f0cbd3ec bellard
                tlen = 0;
147 f0cbd3ec bellard
#endif
148 f0cbd3ec bellard
                m->m_data += if_maxlinkhdr;
149 f0cbd3ec bellard
                *mtod(m, struct tcpiphdr *) = *ti;
150 f0cbd3ec bellard
                ti = mtod(m, struct tcpiphdr *);
151 f0cbd3ec bellard
                flags = TH_ACK;
152 f0cbd3ec bellard
        } else {
153 f0cbd3ec bellard
                /* 
154 f0cbd3ec bellard
                 * ti points into m so the next line is just making
155 f0cbd3ec bellard
                 * the mbuf point to ti
156 f0cbd3ec bellard
                 */
157 f0cbd3ec bellard
                m->m_data = (caddr_t)ti;
158 f0cbd3ec bellard
                
159 f0cbd3ec bellard
                m->m_len = sizeof (struct tcpiphdr);
160 f0cbd3ec bellard
                tlen = 0;
161 f0cbd3ec bellard
#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
162 f0cbd3ec bellard
                xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
163 f0cbd3ec bellard
                xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
164 f0cbd3ec bellard
#undef xchg
165 f0cbd3ec bellard
        }
166 f0cbd3ec bellard
        ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
167 f0cbd3ec bellard
        tlen += sizeof (struct tcpiphdr);
168 f0cbd3ec bellard
        m->m_len = tlen;
169 f0cbd3ec bellard
170 f0cbd3ec bellard
        ti->ti_next = ti->ti_prev = 0;
171 f0cbd3ec bellard
        ti->ti_x1 = 0;
172 f0cbd3ec bellard
        ti->ti_seq = htonl(seq);
173 f0cbd3ec bellard
        ti->ti_ack = htonl(ack);
174 f0cbd3ec bellard
        ti->ti_x2 = 0;
175 f0cbd3ec bellard
        ti->ti_off = sizeof (struct tcphdr) >> 2;
176 f0cbd3ec bellard
        ti->ti_flags = flags;
177 f0cbd3ec bellard
        if (tp)
178 f0cbd3ec bellard
                ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
179 f0cbd3ec bellard
        else
180 f0cbd3ec bellard
                ti->ti_win = htons((u_int16_t)win);
181 f0cbd3ec bellard
        ti->ti_urp = 0;
182 f0cbd3ec bellard
        ti->ti_sum = 0;
183 f0cbd3ec bellard
        ti->ti_sum = cksum(m, tlen);
184 f0cbd3ec bellard
        ((struct ip *)ti)->ip_len = tlen;
185 f0cbd3ec bellard
186 f0cbd3ec bellard
        if(flags & TH_RST) 
187 f0cbd3ec bellard
          ((struct ip *)ti)->ip_ttl = MAXTTL;
188 f0cbd3ec bellard
        else 
189 f0cbd3ec bellard
          ((struct ip *)ti)->ip_ttl = ip_defttl;
190 f0cbd3ec bellard
        
191 f0cbd3ec bellard
        (void) ip_output((struct socket *)0, m);
192 f0cbd3ec bellard
}
193 f0cbd3ec bellard
194 f0cbd3ec bellard
/*
195 f0cbd3ec bellard
 * Create a new TCP control block, making an
196 f0cbd3ec bellard
 * empty reassembly queue and hooking it to the argument
197 f0cbd3ec bellard
 * protocol control block.
198 f0cbd3ec bellard
 */
199 f0cbd3ec bellard
struct tcpcb *
200 f0cbd3ec bellard
tcp_newtcpcb(so)
201 f0cbd3ec bellard
        struct socket *so;
202 f0cbd3ec bellard
{
203 f0cbd3ec bellard
        register struct tcpcb *tp;
204 f0cbd3ec bellard
        
205 f0cbd3ec bellard
        tp = (struct tcpcb *)malloc(sizeof(*tp));
206 f0cbd3ec bellard
        if (tp == NULL)
207 f0cbd3ec bellard
                return ((struct tcpcb *)0);
208 f0cbd3ec bellard
        
209 f0cbd3ec bellard
        memset((char *) tp, 0, sizeof(struct tcpcb));
210 f0cbd3ec bellard
        tp->seg_next = tp->seg_prev = (tcpiphdrp_32)tp;
211 f0cbd3ec bellard
        tp->t_maxseg = tcp_mssdflt;
212 f0cbd3ec bellard
        
213 f0cbd3ec bellard
        tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
214 f0cbd3ec bellard
        tp->t_socket = so;
215 f0cbd3ec bellard
        
216 f0cbd3ec bellard
        /*
217 f0cbd3ec bellard
         * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
218 f0cbd3ec bellard
         * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
219 f0cbd3ec bellard
         * reasonable initial retransmit time.
220 f0cbd3ec bellard
         */
221 f0cbd3ec bellard
        tp->t_srtt = TCPTV_SRTTBASE;
222 f0cbd3ec bellard
        tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
223 f0cbd3ec bellard
        tp->t_rttmin = TCPTV_MIN;
224 f0cbd3ec bellard
225 f0cbd3ec bellard
        TCPT_RANGESET(tp->t_rxtcur, 
226 f0cbd3ec bellard
            ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
227 f0cbd3ec bellard
            TCPTV_MIN, TCPTV_REXMTMAX);
228 f0cbd3ec bellard
229 f0cbd3ec bellard
        tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
230 f0cbd3ec bellard
        tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
231 f0cbd3ec bellard
        tp->t_state = TCPS_CLOSED;
232 f0cbd3ec bellard
        
233 f0cbd3ec bellard
        so->so_tcpcb = tp;
234 f0cbd3ec bellard
235 f0cbd3ec bellard
        return (tp);
236 f0cbd3ec bellard
}
237 f0cbd3ec bellard
238 f0cbd3ec bellard
/*
239 f0cbd3ec bellard
 * Drop a TCP connection, reporting
240 f0cbd3ec bellard
 * the specified error.  If connection is synchronized,
241 f0cbd3ec bellard
 * then send a RST to peer.
242 f0cbd3ec bellard
 */
243 9fafc9ea bellard
struct tcpcb *tcp_drop(struct tcpcb *tp, int err) 
244 f0cbd3ec bellard
{
245 f0cbd3ec bellard
/* tcp_drop(tp, errno)
246 f0cbd3ec bellard
        register struct tcpcb *tp;
247 f0cbd3ec bellard
        int errno;
248 f0cbd3ec bellard
{
249 f0cbd3ec bellard
*/
250 f0cbd3ec bellard
251 f0cbd3ec bellard
        DEBUG_CALL("tcp_drop");
252 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
253 f0cbd3ec bellard
        DEBUG_ARG("errno = %d", errno);
254 f0cbd3ec bellard
        
255 f0cbd3ec bellard
        if (TCPS_HAVERCVDSYN(tp->t_state)) {
256 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
257 f0cbd3ec bellard
                (void) tcp_output(tp);
258 f0cbd3ec bellard
                tcpstat.tcps_drops++;
259 f0cbd3ec bellard
        } else
260 f0cbd3ec bellard
                tcpstat.tcps_conndrops++;
261 f0cbd3ec bellard
/*        if (errno == ETIMEDOUT && tp->t_softerror)
262 f0cbd3ec bellard
 *                errno = tp->t_softerror;
263 f0cbd3ec bellard
 */
264 f0cbd3ec bellard
/*        so->so_error = errno; */
265 f0cbd3ec bellard
        return (tcp_close(tp));
266 f0cbd3ec bellard
}
267 f0cbd3ec bellard
268 f0cbd3ec bellard
/*
269 f0cbd3ec bellard
 * Close a TCP control block:
270 f0cbd3ec bellard
 *        discard all space held by the tcp
271 f0cbd3ec bellard
 *        discard internet protocol block
272 f0cbd3ec bellard
 *        wake up any sleepers
273 f0cbd3ec bellard
 */
274 f0cbd3ec bellard
struct tcpcb *
275 f0cbd3ec bellard
tcp_close(tp)
276 f0cbd3ec bellard
        register struct tcpcb *tp;
277 f0cbd3ec bellard
{
278 f0cbd3ec bellard
        register struct tcpiphdr *t;
279 f0cbd3ec bellard
        struct socket *so = tp->t_socket;
280 f0cbd3ec bellard
        register struct mbuf *m;
281 f0cbd3ec bellard
282 f0cbd3ec bellard
        DEBUG_CALL("tcp_close");
283 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long )tp);
284 f0cbd3ec bellard
        
285 f0cbd3ec bellard
        /* free the reassembly queue, if any */
286 f0cbd3ec bellard
        t = (struct tcpiphdr *) tp->seg_next;
287 f0cbd3ec bellard
        while (t != (struct tcpiphdr *)tp) {
288 f0cbd3ec bellard
                t = (struct tcpiphdr *)t->ti_next;
289 f0cbd3ec bellard
                m = (struct mbuf *) REASS_MBUF((struct tcpiphdr *)t->ti_prev);
290 f0cbd3ec bellard
                remque_32((struct tcpiphdr *) t->ti_prev);
291 f0cbd3ec bellard
                m_freem(m);
292 f0cbd3ec bellard
        }
293 f0cbd3ec bellard
        /* It's static */
294 f0cbd3ec bellard
/*        if (tp->t_template)
295 f0cbd3ec bellard
 *                (void) m_free(dtom(tp->t_template));
296 f0cbd3ec bellard
 */
297 f0cbd3ec bellard
/*        free(tp, M_PCB);  */
298 f0cbd3ec bellard
        free(tp);
299 f0cbd3ec bellard
        so->so_tcpcb = 0;
300 f0cbd3ec bellard
        soisfdisconnected(so);
301 f0cbd3ec bellard
        /* clobber input socket cache if we're closing the cached connection */
302 f0cbd3ec bellard
        if (so == tcp_last_so)
303 f0cbd3ec bellard
                tcp_last_so = &tcb;
304 379ff53d bellard
        closesocket(so->s);
305 f0cbd3ec bellard
        sbfree(&so->so_rcv);
306 f0cbd3ec bellard
        sbfree(&so->so_snd);
307 f0cbd3ec bellard
        sofree(so);
308 f0cbd3ec bellard
        tcpstat.tcps_closed++;
309 f0cbd3ec bellard
        return ((struct tcpcb *)0);
310 f0cbd3ec bellard
}
311 f0cbd3ec bellard
312 f0cbd3ec bellard
void
313 f0cbd3ec bellard
tcp_drain()
314 f0cbd3ec bellard
{
315 f0cbd3ec bellard
        /* XXX */
316 f0cbd3ec bellard
}
317 f0cbd3ec bellard
318 f0cbd3ec bellard
/*
319 f0cbd3ec bellard
 * When a source quench is received, close congestion window
320 f0cbd3ec bellard
 * to one segment.  We will gradually open it again as we proceed.
321 f0cbd3ec bellard
 */
322 f0cbd3ec bellard
323 f0cbd3ec bellard
#ifdef notdef
324 f0cbd3ec bellard
325 f0cbd3ec bellard
void
326 f0cbd3ec bellard
tcp_quench(i, errno)
327 f0cbd3ec bellard
328 f0cbd3ec bellard
        int errno;
329 f0cbd3ec bellard
{
330 f0cbd3ec bellard
        struct tcpcb *tp = intotcpcb(inp);
331 f0cbd3ec bellard
332 f0cbd3ec bellard
        if (tp)
333 f0cbd3ec bellard
                tp->snd_cwnd = tp->t_maxseg;
334 f0cbd3ec bellard
}
335 f0cbd3ec bellard
336 f0cbd3ec bellard
#endif /* notdef */
337 f0cbd3ec bellard
338 f0cbd3ec bellard
/*
339 f0cbd3ec bellard
 * TCP protocol interface to socket abstraction.
340 f0cbd3ec bellard
 */
341 f0cbd3ec bellard
342 f0cbd3ec bellard
/*
343 f0cbd3ec bellard
 * User issued close, and wish to trail through shutdown states:
344 f0cbd3ec bellard
 * if never received SYN, just forget it.  If got a SYN from peer,
345 f0cbd3ec bellard
 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
346 f0cbd3ec bellard
 * If already got a FIN from peer, then almost done; go to LAST_ACK
347 f0cbd3ec bellard
 * state.  In all other cases, have already sent FIN to peer (e.g.
348 f0cbd3ec bellard
 * after PRU_SHUTDOWN), and just have to play tedious game waiting
349 f0cbd3ec bellard
 * for peer to send FIN or not respond to keep-alives, etc.
350 f0cbd3ec bellard
 * We can let the user exit from the close as soon as the FIN is acked.
351 f0cbd3ec bellard
 */
352 f0cbd3ec bellard
void
353 f0cbd3ec bellard
tcp_sockclosed(tp)
354 f0cbd3ec bellard
        struct tcpcb *tp;
355 f0cbd3ec bellard
{
356 f0cbd3ec bellard
357 f0cbd3ec bellard
        DEBUG_CALL("tcp_sockclosed");
358 f0cbd3ec bellard
        DEBUG_ARG("tp = %lx", (long)tp);
359 f0cbd3ec bellard
        
360 f0cbd3ec bellard
        switch (tp->t_state) {
361 f0cbd3ec bellard
362 f0cbd3ec bellard
        case TCPS_CLOSED:
363 f0cbd3ec bellard
        case TCPS_LISTEN:
364 f0cbd3ec bellard
        case TCPS_SYN_SENT:
365 f0cbd3ec bellard
                tp->t_state = TCPS_CLOSED;
366 f0cbd3ec bellard
                tp = tcp_close(tp);
367 f0cbd3ec bellard
                break;
368 f0cbd3ec bellard
369 f0cbd3ec bellard
        case TCPS_SYN_RECEIVED:
370 f0cbd3ec bellard
        case TCPS_ESTABLISHED:
371 f0cbd3ec bellard
                tp->t_state = TCPS_FIN_WAIT_1;
372 f0cbd3ec bellard
                break;
373 f0cbd3ec bellard
374 f0cbd3ec bellard
        case TCPS_CLOSE_WAIT:
375 f0cbd3ec bellard
                tp->t_state = TCPS_LAST_ACK;
376 f0cbd3ec bellard
                break;
377 f0cbd3ec bellard
        }
378 f0cbd3ec bellard
/*        soisfdisconnecting(tp->t_socket); */
379 f0cbd3ec bellard
        if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
380 f0cbd3ec bellard
                soisfdisconnected(tp->t_socket);
381 f0cbd3ec bellard
        if (tp)
382 f0cbd3ec bellard
                tcp_output(tp);
383 f0cbd3ec bellard
}
384 f0cbd3ec bellard
385 f0cbd3ec bellard
/* 
386 f0cbd3ec bellard
 * Connect to a host on the Internet
387 f0cbd3ec bellard
 * Called by tcp_input
388 f0cbd3ec bellard
 * Only do a connect, the tcp fields will be set in tcp_input
389 f0cbd3ec bellard
 * return 0 if there's a result of the connect,
390 f0cbd3ec bellard
 * else return -1 means we're still connecting
391 f0cbd3ec bellard
 * The return value is almost always -1 since the socket is
392 f0cbd3ec bellard
 * nonblocking.  Connect returns after the SYN is sent, and does 
393 f0cbd3ec bellard
 * not wait for ACK+SYN.
394 f0cbd3ec bellard
 */
395 f0cbd3ec bellard
int tcp_fconnect(so)
396 f0cbd3ec bellard
     struct socket *so;
397 f0cbd3ec bellard
{
398 f0cbd3ec bellard
  int ret=0;
399 f0cbd3ec bellard
  
400 f0cbd3ec bellard
  DEBUG_CALL("tcp_fconnect");
401 f0cbd3ec bellard
  DEBUG_ARG("so = %lx", (long )so);
402 f0cbd3ec bellard
403 f0cbd3ec bellard
  if( (ret=so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0) {
404 f0cbd3ec bellard
    int opt, s=so->s;
405 f0cbd3ec bellard
    struct sockaddr_in addr;
406 f0cbd3ec bellard
407 f0cbd3ec bellard
    fd_nonblock(s);
408 f0cbd3ec bellard
    opt = 1;
409 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
410 f0cbd3ec bellard
    opt = 1;
411 f0cbd3ec bellard
    setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
412 f0cbd3ec bellard
    
413 f0cbd3ec bellard
    addr.sin_family = AF_INET;
414 f0cbd3ec bellard
    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
415 f0cbd3ec bellard
      /* It's an alias */
416 f0cbd3ec bellard
      switch(ntohl(so->so_faddr.s_addr) & 0xff) {
417 f0cbd3ec bellard
      case CTL_DNS:
418 f0cbd3ec bellard
        addr.sin_addr = dns_addr;
419 f0cbd3ec bellard
        break;
420 f0cbd3ec bellard
      case CTL_ALIAS:
421 f0cbd3ec bellard
      default:
422 f0cbd3ec bellard
        addr.sin_addr = loopback_addr;
423 f0cbd3ec bellard
        break;
424 f0cbd3ec bellard
      }
425 f0cbd3ec bellard
    } else
426 f0cbd3ec bellard
      addr.sin_addr = so->so_faddr;
427 f0cbd3ec bellard
    addr.sin_port = so->so_fport;
428 f0cbd3ec bellard
    
429 f0cbd3ec bellard
    DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
430 f0cbd3ec bellard
                "addr.sin_addr.s_addr=%.16s\n", 
431 f0cbd3ec bellard
                ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
432 f0cbd3ec bellard
    /* We don't care what port we get */
433 f0cbd3ec bellard
    ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
434 f0cbd3ec bellard
    
435 f0cbd3ec bellard
    /*
436 f0cbd3ec bellard
     * If it's not in progress, it failed, so we just return 0,
437 f0cbd3ec bellard
     * without clearing SS_NOFDREF
438 f0cbd3ec bellard
     */
439 f0cbd3ec bellard
    soisfconnecting(so);
440 f0cbd3ec bellard
  }
441 f0cbd3ec bellard
442 f0cbd3ec bellard
  return(ret);
443 f0cbd3ec bellard
}
444 f0cbd3ec bellard
445 f0cbd3ec bellard
/*
446 f0cbd3ec bellard
 * Accept the socket and connect to the local-host
447 f0cbd3ec bellard
 * 
448 f0cbd3ec bellard
 * We have a problem. The correct thing to do would be
449 f0cbd3ec bellard
 * to first connect to the local-host, and only if the
450 f0cbd3ec bellard
 * connection is accepted, then do an accept() here.
451 f0cbd3ec bellard
 * But, a) we need to know who's trying to connect 
452 f0cbd3ec bellard
 * to the socket to be able to SYN the local-host, and
453 f0cbd3ec bellard
 * b) we are already connected to the foreign host by
454 f0cbd3ec bellard
 * the time it gets to accept(), so... We simply accept
455 f0cbd3ec bellard
 * here and SYN the local-host.
456 f0cbd3ec bellard
 */ 
457 f0cbd3ec bellard
void
458 f0cbd3ec bellard
tcp_connect(inso)
459 f0cbd3ec bellard
        struct socket *inso;
460 f0cbd3ec bellard
{
461 f0cbd3ec bellard
        struct socket *so;
462 f0cbd3ec bellard
        struct sockaddr_in addr;
463 f0cbd3ec bellard
        int addrlen = sizeof(struct sockaddr_in);
464 f0cbd3ec bellard
        struct tcpcb *tp;
465 f0cbd3ec bellard
        int s, opt;
466 f0cbd3ec bellard
467 f0cbd3ec bellard
        DEBUG_CALL("tcp_connect");
468 f0cbd3ec bellard
        DEBUG_ARG("inso = %lx", (long)inso);
469 f0cbd3ec bellard
        
470 f0cbd3ec bellard
        /*
471 f0cbd3ec bellard
         * If it's an SS_ACCEPTONCE socket, no need to socreate()
472 f0cbd3ec bellard
         * another socket, just use the accept() socket.
473 f0cbd3ec bellard
         */
474 f0cbd3ec bellard
        if (inso->so_state & SS_FACCEPTONCE) {
475 f0cbd3ec bellard
                /* FACCEPTONCE already have a tcpcb */
476 f0cbd3ec bellard
                so = inso;
477 f0cbd3ec bellard
        } else {
478 f0cbd3ec bellard
                if ((so = socreate()) == NULL) {
479 f0cbd3ec bellard
                        /* If it failed, get rid of the pending connection */
480 379ff53d bellard
                        closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
481 f0cbd3ec bellard
                        return;
482 f0cbd3ec bellard
                }
483 f0cbd3ec bellard
                if (tcp_attach(so) < 0) {
484 f0cbd3ec bellard
                        free(so); /* NOT sofree */
485 f0cbd3ec bellard
                        return;
486 f0cbd3ec bellard
                }
487 f0cbd3ec bellard
                so->so_laddr = inso->so_laddr;
488 f0cbd3ec bellard
                so->so_lport = inso->so_lport;
489 f0cbd3ec bellard
        }
490 f0cbd3ec bellard
        
491 f0cbd3ec bellard
        (void) tcp_mss(sototcpcb(so), 0);
492 f0cbd3ec bellard
493 f0cbd3ec bellard
        if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
494 f0cbd3ec bellard
                tcp_close(sototcpcb(so)); /* This will sofree() as well */
495 f0cbd3ec bellard
                return;
496 f0cbd3ec bellard
        }
497 f0cbd3ec bellard
        fd_nonblock(s);
498 f0cbd3ec bellard
        opt = 1;
499 f0cbd3ec bellard
        setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
500 f0cbd3ec bellard
        opt = 1;
501 f0cbd3ec bellard
        setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
502 eaf7e70b ths
        opt = 1;
503 eaf7e70b ths
        setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
504 f0cbd3ec bellard
        
505 f0cbd3ec bellard
        so->so_fport = addr.sin_port;
506 f0cbd3ec bellard
        so->so_faddr = addr.sin_addr;
507 f0cbd3ec bellard
        /* Translate connections from localhost to the real hostname */
508 f0cbd3ec bellard
        if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
509 8dbca8dd bellard
           so->so_faddr = alias_addr;
510 f0cbd3ec bellard
        
511 f0cbd3ec bellard
        /* Close the accept() socket, set right state */
512 f0cbd3ec bellard
        if (inso->so_state & SS_FACCEPTONCE) {
513 379ff53d bellard
                closesocket(so->s); /* If we only accept once, close the accept() socket */
514 f0cbd3ec bellard
                so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
515 f0cbd3ec bellard
                                           /* if it's not FACCEPTONCE, it's already NOFDREF */
516 f0cbd3ec bellard
        }
517 f0cbd3ec bellard
        so->s = s;
518 f0cbd3ec bellard
        
519 f0cbd3ec bellard
        so->so_iptos = tcp_tos(so);
520 f0cbd3ec bellard
        tp = sototcpcb(so);
521 f0cbd3ec bellard
522 f0cbd3ec bellard
        tcp_template(tp);
523 f0cbd3ec bellard
        
524 f0cbd3ec bellard
        /* Compute window scaling to request.  */
525 f0cbd3ec bellard
/*        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
526 f0cbd3ec bellard
 *                (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
527 f0cbd3ec bellard
 *                tp->request_r_scale++;
528 f0cbd3ec bellard
 */
529 f0cbd3ec bellard
530 f0cbd3ec bellard
/*        soisconnecting(so); */ /* NOFDREF used instead */
531 f0cbd3ec bellard
        tcpstat.tcps_connattempt++;
532 f0cbd3ec bellard
        
533 f0cbd3ec bellard
        tp->t_state = TCPS_SYN_SENT;
534 f0cbd3ec bellard
        tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
535 f0cbd3ec bellard
        tp->iss = tcp_iss; 
536 f0cbd3ec bellard
        tcp_iss += TCP_ISSINCR/2;
537 f0cbd3ec bellard
        tcp_sendseqinit(tp);
538 f0cbd3ec bellard
        tcp_output(tp);
539 f0cbd3ec bellard
}
540 f0cbd3ec bellard
541 f0cbd3ec bellard
/*
542 f0cbd3ec bellard
 * Attach a TCPCB to a socket.
543 f0cbd3ec bellard
 */
544 f0cbd3ec bellard
int
545 f0cbd3ec bellard
tcp_attach(so)
546 f0cbd3ec bellard
        struct socket *so;
547 f0cbd3ec bellard
{
548 f0cbd3ec bellard
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL)
549 f0cbd3ec bellard
           return -1;
550 f0cbd3ec bellard
        
551 f0cbd3ec bellard
        insque(so, &tcb);
552 f0cbd3ec bellard
553 f0cbd3ec bellard
        return 0;
554 f0cbd3ec bellard
}
555 f0cbd3ec bellard
556 f0cbd3ec bellard
/*
557 f0cbd3ec bellard
 * Set the socket's type of service field
558 f0cbd3ec bellard
 */
559 f0cbd3ec bellard
struct tos_t tcptos[] = {
560 f0cbd3ec bellard
          {0, 20, IPTOS_THROUGHPUT, 0},        /* ftp data */
561 f0cbd3ec bellard
          {21, 21, IPTOS_LOWDELAY,  EMU_FTP},        /* ftp control */
562 f0cbd3ec bellard
          {0, 23, IPTOS_LOWDELAY, 0},        /* telnet */
563 f0cbd3ec bellard
          {0, 80, IPTOS_THROUGHPUT, 0},        /* WWW */
564 f0cbd3ec bellard
          {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT},        /* rlogin */
565 f0cbd3ec bellard
          {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT},        /* shell */
566 f0cbd3ec bellard
          {0, 544, IPTOS_LOWDELAY, EMU_KSH},                /* kshell */
567 f0cbd3ec bellard
          {0, 543, IPTOS_LOWDELAY, 0},        /* klogin */
568 f0cbd3ec bellard
          {0, 6667, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC */
569 f0cbd3ec bellard
          {0, 6668, IPTOS_THROUGHPUT, EMU_IRC},        /* IRC undernet */
570 f0cbd3ec bellard
          {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
571 f0cbd3ec bellard
          {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
572 f0cbd3ec bellard
          {0, 0, 0, 0}
573 f0cbd3ec bellard
};
574 f0cbd3ec bellard
575 f0cbd3ec bellard
struct emu_t *tcpemu = 0;
576 f0cbd3ec bellard
                
577 f0cbd3ec bellard
/*
578 f0cbd3ec bellard
 * Return TOS according to the above table
579 f0cbd3ec bellard
 */
580 f0cbd3ec bellard
u_int8_t
581 f0cbd3ec bellard
tcp_tos(so)
582 f0cbd3ec bellard
        struct socket *so;
583 f0cbd3ec bellard
{
584 f0cbd3ec bellard
        int i = 0;
585 f0cbd3ec bellard
        struct emu_t *emup;
586 f0cbd3ec bellard
        
587 f0cbd3ec bellard
        while(tcptos[i].tos) {
588 f0cbd3ec bellard
                if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
589 f0cbd3ec bellard
                    (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
590 f0cbd3ec bellard
                        so->so_emu = tcptos[i].emu;
591 f0cbd3ec bellard
                        return tcptos[i].tos;
592 f0cbd3ec bellard
                }
593 f0cbd3ec bellard
                i++;
594 f0cbd3ec bellard
        }
595 f0cbd3ec bellard
        
596 f0cbd3ec bellard
        /* Nope, lets see if there's a user-added one */
597 f0cbd3ec bellard
        for (emup = tcpemu; emup; emup = emup->next) {
598 f0cbd3ec bellard
                if ((emup->fport && (ntohs(so->so_fport) == emup->fport)) ||
599 f0cbd3ec bellard
                    (emup->lport && (ntohs(so->so_lport) == emup->lport))) {
600 f0cbd3ec bellard
                        so->so_emu = emup->emu;
601 f0cbd3ec bellard
                        return emup->tos;
602 f0cbd3ec bellard
                }
603 f0cbd3ec bellard
        }
604 f0cbd3ec bellard
        
605 f0cbd3ec bellard
        return 0;
606 f0cbd3ec bellard
}
607 f0cbd3ec bellard
608 f0cbd3ec bellard
int do_echo = -1;
609 f0cbd3ec bellard
610 f0cbd3ec bellard
/*
611 f0cbd3ec bellard
 * Emulate programs that try and connect to us
612 f0cbd3ec bellard
 * This includes ftp (the data connection is
613 f0cbd3ec bellard
 * initiated by the server) and IRC (DCC CHAT and
614 f0cbd3ec bellard
 * DCC SEND) for now
615 f0cbd3ec bellard
 * 
616 f0cbd3ec bellard
 * NOTE: It's possible to crash SLiRP by sending it
617 f0cbd3ec bellard
 * unstandard strings to emulate... if this is a problem,
618 f0cbd3ec bellard
 * more checks are needed here
619 f0cbd3ec bellard
 *
620 f0cbd3ec bellard
 * XXX Assumes the whole command came in one packet
621 f0cbd3ec bellard
 *                                            
622 f0cbd3ec bellard
 * XXX Some ftp clients will have their TOS set to
623 f0cbd3ec bellard
 * LOWDELAY and so Nagel will kick in.  Because of this,
624 f0cbd3ec bellard
 * we'll get the first letter, followed by the rest, so
625 f0cbd3ec bellard
 * we simply scan for ORT instead of PORT...
626 f0cbd3ec bellard
 * DCC doesn't have this problem because there's other stuff
627 f0cbd3ec bellard
 * in the packet before the DCC command.
628 f0cbd3ec bellard
 * 
629 f0cbd3ec bellard
 * Return 1 if the mbuf m is still valid and should be 
630 f0cbd3ec bellard
 * sbappend()ed
631 f0cbd3ec bellard
 * 
632 f0cbd3ec bellard
 * NOTE: if you return 0 you MUST m_free() the mbuf!
633 f0cbd3ec bellard
 */
634 f0cbd3ec bellard
int
635 f0cbd3ec bellard
tcp_emu(so, m)
636 f0cbd3ec bellard
        struct socket *so;
637 f0cbd3ec bellard
        struct mbuf *m;
638 f0cbd3ec bellard
{
639 f0cbd3ec bellard
        u_int n1, n2, n3, n4, n5, n6;
640 f0cbd3ec bellard
        char buff[256];
641 f0cbd3ec bellard
        u_int32_t laddr;
642 f0cbd3ec bellard
        u_int lport;
643 f0cbd3ec bellard
        char *bptr;
644 f0cbd3ec bellard
        
645 f0cbd3ec bellard
        DEBUG_CALL("tcp_emu");
646 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
647 f0cbd3ec bellard
        DEBUG_ARG("m = %lx", (long)m);
648 f0cbd3ec bellard
        
649 f0cbd3ec bellard
        switch(so->so_emu) {
650 f0cbd3ec bellard
                int x, i;
651 f0cbd3ec bellard
                
652 f0cbd3ec bellard
         case EMU_IDENT:
653 f0cbd3ec bellard
                /*
654 f0cbd3ec bellard
                 * Identification protocol as per rfc-1413
655 f0cbd3ec bellard
                 */
656 f0cbd3ec bellard
                
657 f0cbd3ec bellard
                {
658 f0cbd3ec bellard
                        struct socket *tmpso;
659 f0cbd3ec bellard
                        struct sockaddr_in addr;
660 f0cbd3ec bellard
                        int addrlen = sizeof(struct sockaddr_in);
661 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
662 f0cbd3ec bellard
                        
663 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
664 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
665 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
666 f0cbd3ec bellard
                        m->m_data[m->m_len] = 0; /* NULL terminate */
667 f0cbd3ec bellard
                        if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
668 f0cbd3ec bellard
                                if (sscanf(so_rcv->sb_data, "%d%*[ ,]%d", &n1, &n2) == 2) {
669 f0cbd3ec bellard
                                        HTONS(n1);
670 f0cbd3ec bellard
                                        HTONS(n2);
671 f0cbd3ec bellard
                                        /* n2 is the one on our host */
672 f0cbd3ec bellard
                                        for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
673 f0cbd3ec bellard
                                                if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
674 f0cbd3ec bellard
                                                    tmpso->so_lport == n2 &&
675 f0cbd3ec bellard
                                                    tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
676 f0cbd3ec bellard
                                                    tmpso->so_fport == n1) {
677 f0cbd3ec bellard
                                                        if (getsockname(tmpso->s,
678 f0cbd3ec bellard
                                                                (struct sockaddr *)&addr, &addrlen) == 0)
679 f0cbd3ec bellard
                                                           n2 = ntohs(addr.sin_port);
680 f0cbd3ec bellard
                                                        break;
681 f0cbd3ec bellard
                                                }
682 f0cbd3ec bellard
                                        }
683 f0cbd3ec bellard
                                }
684 f0cbd3ec bellard
                                so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
685 f0cbd3ec bellard
                                so_rcv->sb_rptr = so_rcv->sb_data;
686 f0cbd3ec bellard
                                so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
687 f0cbd3ec bellard
                        }
688 f0cbd3ec bellard
                        m_free(m);
689 f0cbd3ec bellard
                        return 0;
690 f0cbd3ec bellard
                }
691 f0cbd3ec bellard
                
692 f0cbd3ec bellard
#if 0
693 f0cbd3ec bellard
         case EMU_RLOGIN:
694 f0cbd3ec bellard
                /*
695 f0cbd3ec bellard
                 * Rlogin emulation
696 f0cbd3ec bellard
                 * First we accumulate all the initial option negotiation,
697 f0cbd3ec bellard
                 * then fork_exec() rlogin according to the  options
698 f0cbd3ec bellard
                 */
699 f0cbd3ec bellard
                {
700 f0cbd3ec bellard
                        int i, i2, n;
701 f0cbd3ec bellard
                        char *ptr;
702 f0cbd3ec bellard
                        char args[100];
703 f0cbd3ec bellard
                        char term[100];
704 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
705 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
706 f0cbd3ec bellard
                        
707 f0cbd3ec bellard
                        /* First check if they have a priveladged port, or too much data has arrived */
708 f0cbd3ec bellard
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
709 f0cbd3ec bellard
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
710 f0cbd3ec bellard
                                memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
711 f0cbd3ec bellard
                                so_snd->sb_wptr += 18;
712 f0cbd3ec bellard
                                so_snd->sb_cc += 18;
713 f0cbd3ec bellard
                                tcp_sockclosed(sototcpcb(so));
714 f0cbd3ec bellard
                                m_free(m);
715 f0cbd3ec bellard
                                return 0;
716 f0cbd3ec bellard
                        }
717 f0cbd3ec bellard
                        
718 f0cbd3ec bellard
                        /* Append the current data */
719 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
720 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
721 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
722 f0cbd3ec bellard
                        m_free(m);
723 f0cbd3ec bellard
                        
724 f0cbd3ec bellard
                        /*
725 f0cbd3ec bellard
                         * Check if we have all the initial options,
726 f0cbd3ec bellard
                         * and build argument list to rlogin while we're here
727 f0cbd3ec bellard
                         */
728 f0cbd3ec bellard
                        n = 0;
729 f0cbd3ec bellard
                        ptr = so_rcv->sb_data;
730 f0cbd3ec bellard
                        args[0] = 0;
731 f0cbd3ec bellard
                        term[0] = 0;
732 f0cbd3ec bellard
                        while (ptr < so_rcv->sb_wptr) {
733 f0cbd3ec bellard
                                if (*ptr++ == 0) {
734 f0cbd3ec bellard
                                        n++;
735 f0cbd3ec bellard
                                        if (n == 2) {
736 f0cbd3ec bellard
                                                sprintf(args, "rlogin -l %s %s",
737 f0cbd3ec bellard
                                                        ptr, inet_ntoa(so->so_faddr));
738 f0cbd3ec bellard
                                        } else if (n == 3) {
739 f0cbd3ec bellard
                                                i2 = so_rcv->sb_wptr - ptr;
740 f0cbd3ec bellard
                                                for (i = 0; i < i2; i++) {
741 f0cbd3ec bellard
                                                        if (ptr[i] == '/') {
742 f0cbd3ec bellard
                                                                ptr[i] = 0;
743 f0cbd3ec bellard
#ifdef HAVE_SETENV
744 f0cbd3ec bellard
                                                                sprintf(term, "%s", ptr);
745 f0cbd3ec bellard
#else
746 f0cbd3ec bellard
                                                                sprintf(term, "TERM=%s", ptr);
747 f0cbd3ec bellard
#endif
748 f0cbd3ec bellard
                                                                ptr[i] = '/';
749 f0cbd3ec bellard
                                                                break;
750 f0cbd3ec bellard
                                                        }
751 f0cbd3ec bellard
                                                }
752 f0cbd3ec bellard
                                        }
753 f0cbd3ec bellard
                                }
754 f0cbd3ec bellard
                        }
755 f0cbd3ec bellard
                        
756 f0cbd3ec bellard
                        if (n != 4)
757 f0cbd3ec bellard
                           return 0;
758 f0cbd3ec bellard
                        
759 f0cbd3ec bellard
                        /* We have it, set our term variable and fork_exec() */
760 f0cbd3ec bellard
#ifdef HAVE_SETENV
761 f0cbd3ec bellard
                        setenv("TERM", term, 1);
762 f0cbd3ec bellard
#else
763 f0cbd3ec bellard
                        putenv(term);
764 f0cbd3ec bellard
#endif
765 f0cbd3ec bellard
                        fork_exec(so, args, 2);
766 f0cbd3ec bellard
                        term[0] = 0;
767 f0cbd3ec bellard
                        so->so_emu = 0;
768 f0cbd3ec bellard
                        
769 f0cbd3ec bellard
                        /* And finally, send the client a 0 character */
770 f0cbd3ec bellard
                        so_snd->sb_wptr[0] = 0;
771 f0cbd3ec bellard
                        so_snd->sb_wptr++;
772 f0cbd3ec bellard
                        so_snd->sb_cc++;
773 f0cbd3ec bellard
                        
774 f0cbd3ec bellard
                        return 0;
775 f0cbd3ec bellard
                }
776 f0cbd3ec bellard
                
777 f0cbd3ec bellard
         case EMU_RSH:
778 f0cbd3ec bellard
                /*
779 f0cbd3ec bellard
                 * rsh emulation
780 f0cbd3ec bellard
                 * First we accumulate all the initial option negotiation,
781 f0cbd3ec bellard
                 * then rsh_exec() rsh according to the  options
782 f0cbd3ec bellard
                 */
783 f0cbd3ec bellard
                {
784 f0cbd3ec bellard
                        int  n;
785 f0cbd3ec bellard
                        char *ptr;
786 f0cbd3ec bellard
                        char *user;
787 f0cbd3ec bellard
                        char *args;
788 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
789 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
790 f0cbd3ec bellard
                        
791 f0cbd3ec bellard
                        /* First check if they have a priveladged port, or too much data has arrived */
792 f0cbd3ec bellard
                        if (ntohs(so->so_lport) > 1023 || ntohs(so->so_lport) < 512 ||
793 f0cbd3ec bellard
                            (m->m_len + so_rcv->sb_wptr) > (so_rcv->sb_data + so_rcv->sb_datalen)) {
794 f0cbd3ec bellard
                                memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
795 f0cbd3ec bellard
                                so_snd->sb_wptr += 18;
796 f0cbd3ec bellard
                                so_snd->sb_cc += 18;
797 f0cbd3ec bellard
                                tcp_sockclosed(sototcpcb(so));
798 f0cbd3ec bellard
                                m_free(m);
799 f0cbd3ec bellard
                                return 0;
800 f0cbd3ec bellard
                        }
801 f0cbd3ec bellard
                        
802 f0cbd3ec bellard
                        /* Append the current data */
803 f0cbd3ec bellard
                        memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
804 f0cbd3ec bellard
                        so_rcv->sb_wptr += m->m_len;
805 f0cbd3ec bellard
                        so_rcv->sb_rptr += m->m_len;
806 f0cbd3ec bellard
                        m_free(m);
807 f0cbd3ec bellard
                        
808 f0cbd3ec bellard
                        /*
809 f0cbd3ec bellard
                         * Check if we have all the initial options,
810 f0cbd3ec bellard
                         * and build argument list to rlogin while we're here
811 f0cbd3ec bellard
                         */
812 f0cbd3ec bellard
                        n = 0;
813 f0cbd3ec bellard
                        ptr = so_rcv->sb_data;
814 f0cbd3ec bellard
                        user="";
815 f0cbd3ec bellard
                        args="";
816 f0cbd3ec bellard
                        if (so->extra==NULL) {
817 f0cbd3ec bellard
                                struct socket *ns;
818 f0cbd3ec bellard
                                struct tcpcb* tp;
819 f0cbd3ec bellard
                                int port=atoi(ptr);
820 f0cbd3ec bellard
                                if (port <= 0) return 0;
821 f0cbd3ec bellard
                if (port > 1023 || port < 512) {
822 f0cbd3ec bellard
                  memcpy(so_snd->sb_wptr, "Permission denied\n", 18);
823 f0cbd3ec bellard
                  so_snd->sb_wptr += 18;
824 f0cbd3ec bellard
                  so_snd->sb_cc += 18;
825 f0cbd3ec bellard
                  tcp_sockclosed(sototcpcb(so));
826 f0cbd3ec bellard
                  return 0;
827 f0cbd3ec bellard
                }
828 f0cbd3ec bellard
                                if ((ns=socreate()) == NULL)
829 f0cbd3ec bellard
                  return 0;
830 f0cbd3ec bellard
                                if (tcp_attach(ns)<0) {
831 f0cbd3ec bellard
                  free(ns);
832 f0cbd3ec bellard
                  return 0;
833 f0cbd3ec bellard
                                }
834 f0cbd3ec bellard
835 f0cbd3ec bellard
                                ns->so_laddr=so->so_laddr;
836 f0cbd3ec bellard
                                ns->so_lport=htons(port);
837 f0cbd3ec bellard
838 f0cbd3ec bellard
                                (void) tcp_mss(sototcpcb(ns), 0);
839 f0cbd3ec bellard
840 f0cbd3ec bellard
                                ns->so_faddr=so->so_faddr;
841 f0cbd3ec bellard
                                ns->so_fport=htons(IPPORT_RESERVED-1); /* Use a fake port. */
842 f0cbd3ec bellard
843 f0cbd3ec bellard
                                if (ns->so_faddr.s_addr == 0 || 
844 f0cbd3ec bellard
                                        ns->so_faddr.s_addr == loopback_addr.s_addr)
845 8dbca8dd bellard
                  ns->so_faddr = alias_addr;
846 f0cbd3ec bellard
847 f0cbd3ec bellard
                                ns->so_iptos = tcp_tos(ns);
848 f0cbd3ec bellard
                                tp = sototcpcb(ns);
849 f0cbd3ec bellard
                
850 f0cbd3ec bellard
                                tcp_template(tp);
851 f0cbd3ec bellard
                
852 f0cbd3ec bellard
                                /* Compute window scaling to request.  */
853 f0cbd3ec bellard
                                /*        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
854 f0cbd3ec bellard
                                 *                (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
855 f0cbd3ec bellard
                                 *                tp->request_r_scale++;
856 f0cbd3ec bellard
                                 */
857 f0cbd3ec bellard
858 f0cbd3ec bellard
                /*soisfconnecting(ns);*/
859 f0cbd3ec bellard
860 f0cbd3ec bellard
                                tcpstat.tcps_connattempt++;
861 f0cbd3ec bellard
                                        
862 f0cbd3ec bellard
                                tp->t_state = TCPS_SYN_SENT;
863 f0cbd3ec bellard
                                tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
864 f0cbd3ec bellard
                                tp->iss = tcp_iss; 
865 f0cbd3ec bellard
                                tcp_iss += TCP_ISSINCR/2;
866 f0cbd3ec bellard
                                tcp_sendseqinit(tp);
867 f0cbd3ec bellard
                                tcp_output(tp);
868 f0cbd3ec bellard
                                so->extra=ns;
869 f0cbd3ec bellard
                        }
870 f0cbd3ec bellard
                        while (ptr < so_rcv->sb_wptr) {
871 f0cbd3ec bellard
              if (*ptr++ == 0) {
872 f0cbd3ec bellard
                n++;
873 f0cbd3ec bellard
                if (n == 2) {
874 f0cbd3ec bellard
                  user=ptr;
875 f0cbd3ec bellard
                } else if (n == 3) {
876 f0cbd3ec bellard
                  args=ptr;
877 f0cbd3ec bellard
                }
878 f0cbd3ec bellard
              }
879 f0cbd3ec bellard
                        }
880 f0cbd3ec bellard
                        
881 f0cbd3ec bellard
                        if (n != 4)
882 f0cbd3ec bellard
              return 0;
883 f0cbd3ec bellard
                        
884 f0cbd3ec bellard
                        rsh_exec(so,so->extra, user, inet_ntoa(so->so_faddr), args);
885 f0cbd3ec bellard
                        so->so_emu = 0;
886 f0cbd3ec bellard
                        so->extra=NULL;
887 f0cbd3ec bellard
                        
888 f0cbd3ec bellard
                        /* And finally, send the client a 0 character */
889 f0cbd3ec bellard
                        so_snd->sb_wptr[0] = 0;
890 f0cbd3ec bellard
                        so_snd->sb_wptr++;
891 f0cbd3ec bellard
                        so_snd->sb_cc++;
892 f0cbd3ec bellard
                        
893 f0cbd3ec bellard
                        return 0;
894 f0cbd3ec bellard
                }
895 f0cbd3ec bellard
896 f0cbd3ec bellard
         case EMU_CTL:
897 f0cbd3ec bellard
                {
898 f0cbd3ec bellard
                        int num;
899 f0cbd3ec bellard
                        struct sbuf *so_snd = &so->so_snd;
900 f0cbd3ec bellard
                        struct sbuf *so_rcv = &so->so_rcv;
901 f0cbd3ec bellard
                        
902 f0cbd3ec bellard
                        /*
903 f0cbd3ec bellard
                         * If there is binary data here, we save it in so->so_m
904 f0cbd3ec bellard
                         */
905 f0cbd3ec bellard
                        if (!so->so_m) {
906 f0cbd3ec bellard
                          int rxlen;
907 f0cbd3ec bellard
                          char *rxdata;
908 f0cbd3ec bellard
                          rxdata=mtod(m, char *);
909 f0cbd3ec bellard
                          for (rxlen=m->m_len; rxlen; rxlen--) {
910 f0cbd3ec bellard
                            if (*rxdata++ & 0x80) {
911 f0cbd3ec bellard
                              so->so_m = m;
912 f0cbd3ec bellard
                              return 0;
913 f0cbd3ec bellard
                            }
914 f0cbd3ec bellard
                          }
915 f0cbd3ec bellard
                        } /* if(so->so_m==NULL) */
916 f0cbd3ec bellard
                        
917 f0cbd3ec bellard
                        /*
918 f0cbd3ec bellard
                         * Append the line
919 f0cbd3ec bellard
                         */
920 f0cbd3ec bellard
                        sbappendsb(so_rcv, m);
921 f0cbd3ec bellard
                        
922 f0cbd3ec bellard
                        /* To avoid going over the edge of the buffer, we reset it */
923 f0cbd3ec bellard
                        if (so_snd->sb_cc == 0)
924 f0cbd3ec bellard
                           so_snd->sb_wptr = so_snd->sb_rptr = so_snd->sb_data;
925 f0cbd3ec bellard
                        
926 f0cbd3ec bellard
                        /*
927 f0cbd3ec bellard
                         * A bit of a hack:
928 f0cbd3ec bellard
                         * If the first packet we get here is 1 byte long, then it
929 f0cbd3ec bellard
                         * was done in telnet character mode, therefore we must echo
930 f0cbd3ec bellard
                         * the characters as they come.  Otherwise, we echo nothing,
931 f0cbd3ec bellard
                         * because in linemode, the line is already echoed
932 f0cbd3ec bellard
                         * XXX two or more control connections won't work
933 f0cbd3ec bellard
                         */
934 f0cbd3ec bellard
                        if (do_echo == -1) {
935 f0cbd3ec bellard
                                if (m->m_len == 1) do_echo = 1;
936 f0cbd3ec bellard
                                else do_echo = 0;
937 f0cbd3ec bellard
                        }
938 f0cbd3ec bellard
                        if (do_echo) {
939 f0cbd3ec bellard
                          sbappendsb(so_snd, m);
940 f0cbd3ec bellard
                          m_free(m);
941 f0cbd3ec bellard
                          tcp_output(sototcpcb(so)); /* XXX */
942 f0cbd3ec bellard
                        } else
943 f0cbd3ec bellard
                          m_free(m);
944 f0cbd3ec bellard
                        
945 f0cbd3ec bellard
                        num = 0;
946 f0cbd3ec bellard
                        while (num < so->so_rcv.sb_cc) {
947 f0cbd3ec bellard
                                if (*(so->so_rcv.sb_rptr + num) == '\n' ||
948 f0cbd3ec bellard
                                    *(so->so_rcv.sb_rptr + num) == '\r') {
949 f0cbd3ec bellard
                                        int n;
950 f0cbd3ec bellard
                                        
951 f0cbd3ec bellard
                                        *(so_rcv->sb_rptr + num) = 0;
952 f0cbd3ec bellard
                                        if (ctl_password && !ctl_password_ok) {
953 f0cbd3ec bellard
                                                /* Need a password */
954 f0cbd3ec bellard
                                                if (sscanf(so_rcv->sb_rptr, "pass %256s", buff) == 1) {
955 f0cbd3ec bellard
                                                        if (strcmp(buff, ctl_password) == 0) {
956 f0cbd3ec bellard
                                                                ctl_password_ok = 1;
957 f0cbd3ec bellard
                                                                n = sprintf(so_snd->sb_wptr,
958 f0cbd3ec bellard
                                                                            "Password OK.\r\n");
959 f0cbd3ec bellard
                                                                goto do_prompt;
960 f0cbd3ec bellard
                                                        }
961 f0cbd3ec bellard
                                                }
962 f0cbd3ec bellard
                                                n = sprintf(so_snd->sb_wptr,
963 f0cbd3ec bellard
                                         "Error: Password required, log on with \"pass PASSWORD\"\r\n");
964 f0cbd3ec bellard
                                                goto do_prompt;
965 f0cbd3ec bellard
                                        }
966 f0cbd3ec bellard
                                        cfg_quitting = 0;
967 f0cbd3ec bellard
                                        n = do_config(so_rcv->sb_rptr, so, PRN_SPRINTF);
968 f0cbd3ec bellard
                                        if (!cfg_quitting) {
969 f0cbd3ec bellard
                                                /* Register the printed data */
970 f0cbd3ec bellard
do_prompt:
971 f0cbd3ec bellard
                                                so_snd->sb_cc += n;
972 f0cbd3ec bellard
                                                so_snd->sb_wptr += n;
973 f0cbd3ec bellard
                                                /* Add prompt */
974 f0cbd3ec bellard
                                                n = sprintf(so_snd->sb_wptr, "Slirp> ");
975 f0cbd3ec bellard
                                                so_snd->sb_cc += n;
976 f0cbd3ec bellard
                                                so_snd->sb_wptr += n;
977 f0cbd3ec bellard
                                        }
978 f0cbd3ec bellard
                                        /* Drop so_rcv data */
979 f0cbd3ec bellard
                                        so_rcv->sb_cc = 0;
980 f0cbd3ec bellard
                                        so_rcv->sb_wptr = so_rcv->sb_rptr = so_rcv->sb_data;
981 f0cbd3ec bellard
                                        tcp_output(sototcpcb(so)); /* Send the reply */
982 f0cbd3ec bellard
                                }
983 f0cbd3ec bellard
                                num++;
984 f0cbd3ec bellard
                        }
985 f0cbd3ec bellard
                        return 0;
986 f0cbd3ec bellard
                }
987 f0cbd3ec bellard
#endif                
988 f0cbd3ec bellard
        case EMU_FTP: /* ftp */
989 f0cbd3ec bellard
                *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
990 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
991 f0cbd3ec bellard
                        /*
992 f0cbd3ec bellard
                         * Need to emulate the PORT command
993 f0cbd3ec bellard
                         */                        
994 f0cbd3ec bellard
                        x = sscanf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%256[^\177]", 
995 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
996 f0cbd3ec bellard
                        if (x < 6)
997 f0cbd3ec bellard
                           return 1;
998 f0cbd3ec bellard
                        
999 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1000 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
1001 f0cbd3ec bellard
                        
1002 f0cbd3ec bellard
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1003 f0cbd3ec bellard
                           return 1;
1004 f0cbd3ec bellard
                        
1005 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
1006 f0cbd3ec bellard
                        
1007 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
1008 f0cbd3ec bellard
                        n6 &= 0xff;
1009 f0cbd3ec bellard
                        
1010 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
1011 f0cbd3ec bellard
                        
1012 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
1013 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
1014 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
1015 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
1016 f0cbd3ec bellard
                        
1017 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1018 f0cbd3ec bellard
                        m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s", 
1019 f0cbd3ec bellard
                                            n1, n2, n3, n4, n5, n6, x==7?buff:"");
1020 f0cbd3ec bellard
                        return 1;
1021 f0cbd3ec bellard
                } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
1022 f0cbd3ec bellard
                        /*
1023 f0cbd3ec bellard
                         * Need to emulate the PASV response
1024 f0cbd3ec bellard
                         */
1025 f0cbd3ec bellard
                        x = sscanf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%256[^\177]",
1026 f0cbd3ec bellard
                                   &n1, &n2, &n3, &n4, &n5, &n6, buff);
1027 f0cbd3ec bellard
                        if (x < 6)
1028 f0cbd3ec bellard
                           return 1;
1029 f0cbd3ec bellard
                        
1030 f0cbd3ec bellard
                        laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
1031 f0cbd3ec bellard
                        lport = htons((n5 << 8) | (n6));
1032 f0cbd3ec bellard
                        
1033 f0cbd3ec bellard
                        if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL)
1034 f0cbd3ec bellard
                           return 1;
1035 f0cbd3ec bellard
                        
1036 f0cbd3ec bellard
                        n6 = ntohs(so->so_fport);
1037 f0cbd3ec bellard
                        
1038 f0cbd3ec bellard
                        n5 = (n6 >> 8) & 0xff;
1039 f0cbd3ec bellard
                        n6 &= 0xff;
1040 f0cbd3ec bellard
                        
1041 f0cbd3ec bellard
                        laddr = ntohl(so->so_faddr.s_addr);
1042 f0cbd3ec bellard
                        
1043 f0cbd3ec bellard
                        n1 = ((laddr >> 24) & 0xff);
1044 f0cbd3ec bellard
                        n2 = ((laddr >> 16) & 0xff);
1045 f0cbd3ec bellard
                        n3 = ((laddr >> 8)  & 0xff);
1046 f0cbd3ec bellard
                        n4 =  (laddr & 0xff);
1047 f0cbd3ec bellard
                        
1048 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1049 f0cbd3ec bellard
                        m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
1050 f0cbd3ec bellard
                                            n1, n2, n3, n4, n5, n6, x==7?buff:"");
1051 f0cbd3ec bellard
                        
1052 f0cbd3ec bellard
                        return 1;
1053 f0cbd3ec bellard
                }
1054 f0cbd3ec bellard
                
1055 f0cbd3ec bellard
                return 1;
1056 f0cbd3ec bellard
                                   
1057 f0cbd3ec bellard
         case EMU_KSH:
1058 f0cbd3ec bellard
                /*
1059 f0cbd3ec bellard
                 * The kshell (Kerberos rsh) and shell services both pass
1060 f0cbd3ec bellard
                 * a local port port number to carry signals to the server
1061 f0cbd3ec bellard
                 * and stderr to the client.  It is passed at the beginning
1062 f0cbd3ec bellard
                 * of the connection as a NUL-terminated decimal ASCII string.
1063 f0cbd3ec bellard
                 */
1064 f0cbd3ec bellard
                so->so_emu = 0;
1065 f0cbd3ec bellard
                for (lport = 0, i = 0; i < m->m_len-1; ++i) {
1066 f0cbd3ec bellard
                        if (m->m_data[i] < '0' || m->m_data[i] > '9')
1067 f0cbd3ec bellard
                                return 1;       /* invalid number */
1068 f0cbd3ec bellard
                        lport *= 10;
1069 f0cbd3ec bellard
                        lport += m->m_data[i] - '0';
1070 f0cbd3ec bellard
                }
1071 f0cbd3ec bellard
                if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
1072 f0cbd3ec bellard
                    (so = solisten(0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
1073 f0cbd3ec bellard
                        m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
1074 f0cbd3ec bellard
                return 1;
1075 f0cbd3ec bellard
                
1076 f0cbd3ec bellard
         case EMU_IRC:
1077 f0cbd3ec bellard
                /*
1078 f0cbd3ec bellard
                 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
1079 f0cbd3ec bellard
                 */
1080 f0cbd3ec bellard
                *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
1081 f0cbd3ec bellard
                if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
1082 f0cbd3ec bellard
                         return 1;
1083 f0cbd3ec bellard
                
1084 f0cbd3ec bellard
                /* The %256s is for the broken mIRC */
1085 f0cbd3ec bellard
                if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
1086 f0cbd3ec bellard
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1087 f0cbd3ec bellard
                                return 1;
1088 f0cbd3ec bellard
                        
1089 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1090 f0cbd3ec bellard
                        m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
1091 f0cbd3ec bellard
                             (unsigned long)ntohl(so->so_faddr.s_addr),
1092 f0cbd3ec bellard
                             ntohs(so->so_fport), 1);
1093 f0cbd3ec bellard
                } else if (sscanf(bptr, "DCC SEND %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 f0cbd3ec bellard
                        
1097 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1098 f0cbd3ec bellard
                        m->m_len += sprintf(bptr, "DCC SEND %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
                } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
1102 f0cbd3ec bellard
                        if ((so = solisten(0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
1103 f0cbd3ec bellard
                                return 1;
1104 f0cbd3ec bellard
                        
1105 f0cbd3ec bellard
                        m->m_len = bptr - m->m_data; /* Adjust length */
1106 f0cbd3ec bellard
                        m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
1107 f0cbd3ec bellard
                              buff, (unsigned long)ntohl(so->so_faddr.s_addr),
1108 f0cbd3ec bellard
                              ntohs(so->so_fport), n1, 1);
1109 f0cbd3ec bellard
                }
1110 f0cbd3ec bellard
                return 1;
1111 f0cbd3ec bellard
1112 f0cbd3ec bellard
         case EMU_REALAUDIO:
1113 f0cbd3ec bellard
                /* 
1114 f0cbd3ec bellard
                 * RealAudio emulation - JP. We must try to parse the incoming
1115 f0cbd3ec bellard
                 * data and try to find the two characters that contain the
1116 f0cbd3ec bellard
                 * port number. Then we redirect an udp port and replace the
1117 f0cbd3ec bellard
                 * number with the real port we got.
1118 f0cbd3ec bellard
                 *
1119 f0cbd3ec bellard
                 * The 1.0 beta versions of the player are not supported
1120 f0cbd3ec bellard
                 * any more.
1121 f0cbd3ec bellard
                 * 
1122 f0cbd3ec bellard
                 * A typical packet for player version 1.0 (release version):
1123 f0cbd3ec bellard
                 *        
1124 f0cbd3ec bellard
                 * 0000:50 4E 41 00 05 
1125 f0cbd3ec bellard
                 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....?..g?l?c..P
1126 f0cbd3ec bellard
                 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
1127 f0cbd3ec bellard
                 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
1128 f0cbd3ec bellard
                 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
1129 f0cbd3ec bellard
                 *         
1130 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
1131 f0cbd3ec bellard
                 * Now the port number 0x1BD7 is found at offset 0x04 of the
1132 f0cbd3ec bellard
                 * second packet. This time we received five bytes first and
1133 f0cbd3ec bellard
                 * then the rest. You never know how many bytes you get.
1134 f0cbd3ec bellard
                 *
1135 f0cbd3ec bellard
                 * A typical packet for player version 2.0 (beta):
1136 f0cbd3ec bellard
                 *        
1137 f0cbd3ec bellard
                 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........?.
1138 f0cbd3ec bellard
                 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .gux?c..Win2.0.0
1139 f0cbd3ec bellard
                 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
1140 f0cbd3ec bellard
                 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
1141 f0cbd3ec bellard
                 * 0040:65 2E 72 61 79 53 00 00 06 36 42                e.rayS...6B
1142 f0cbd3ec bellard
                 *        
1143 f0cbd3ec bellard
                 * Port number 0x1BC1 is found at offset 0x0d.
1144 f0cbd3ec bellard
                 *      
1145 f0cbd3ec bellard
                 * This is just a horrible switch statement. Variable ra tells
1146 f0cbd3ec bellard
                 * us where we're going.
1147 f0cbd3ec bellard
                 */
1148 f0cbd3ec bellard
                
1149 f0cbd3ec bellard
                bptr = m->m_data;
1150 f0cbd3ec bellard
                while (bptr < m->m_data + m->m_len) {
1151 f0cbd3ec bellard
                        u_short p;
1152 f0cbd3ec bellard
                        static int ra = 0;
1153 f0cbd3ec bellard
                        char ra_tbl[4]; 
1154 f0cbd3ec bellard
                        
1155 f0cbd3ec bellard
                        ra_tbl[0] = 0x50;
1156 f0cbd3ec bellard
                        ra_tbl[1] = 0x4e;
1157 f0cbd3ec bellard
                        ra_tbl[2] = 0x41;
1158 f0cbd3ec bellard
                        ra_tbl[3] = 0;
1159 f0cbd3ec bellard
                        
1160 f0cbd3ec bellard
                        switch (ra) {
1161 f0cbd3ec bellard
                         case 0:
1162 f0cbd3ec bellard
                         case 2:
1163 f0cbd3ec bellard
                         case 3:
1164 f0cbd3ec bellard
                                if (*bptr++ != ra_tbl[ra]) {
1165 f0cbd3ec bellard
                                        ra = 0;
1166 f0cbd3ec bellard
                                        continue;
1167 f0cbd3ec bellard
                                }
1168 f0cbd3ec bellard
                                break;
1169 f0cbd3ec bellard
                                
1170 f0cbd3ec bellard
                         case 1:
1171 f0cbd3ec bellard
                                /*
1172 f0cbd3ec bellard
                                 * We may get 0x50 several times, ignore them
1173 f0cbd3ec bellard
                                 */
1174 f0cbd3ec bellard
                                if (*bptr == 0x50) {
1175 f0cbd3ec bellard
                                        ra = 1;
1176 f0cbd3ec bellard
                                        bptr++;
1177 f0cbd3ec bellard
                                        continue;
1178 f0cbd3ec bellard
                                } else if (*bptr++ != ra_tbl[ra]) {
1179 f0cbd3ec bellard
                                        ra = 0;
1180 f0cbd3ec bellard
                                        continue;
1181 f0cbd3ec bellard
                                }
1182 f0cbd3ec bellard
                                break;
1183 f0cbd3ec bellard
                                
1184 f0cbd3ec bellard
                         case 4: 
1185 f0cbd3ec bellard
                                /* 
1186 f0cbd3ec bellard
                                 * skip version number
1187 f0cbd3ec bellard
                                 */
1188 f0cbd3ec bellard
                                bptr++;
1189 f0cbd3ec bellard
                                break;
1190 f0cbd3ec bellard
                                
1191 f0cbd3ec bellard
                         case 5: 
1192 f0cbd3ec bellard
                                /*
1193 f0cbd3ec bellard
                                 * The difference between versions 1.0 and
1194 f0cbd3ec bellard
                                 * 2.0 is here. For future versions of
1195 f0cbd3ec bellard
                                 * the player this may need to be modified.
1196 f0cbd3ec bellard
                                 */
1197 f0cbd3ec bellard
                                if (*(bptr + 1) == 0x02)
1198 f0cbd3ec bellard
                                   bptr += 8;
1199 f0cbd3ec bellard
                                else
1200 f0cbd3ec bellard
                                   bptr += 4;
1201 f0cbd3ec bellard
                                break;                          
1202 f0cbd3ec bellard
                                
1203 f0cbd3ec bellard
                         case 6:
1204 f0cbd3ec bellard
                                /* This is the field containing the port
1205 f0cbd3ec bellard
                                 * number that RA-player is listening to.
1206 f0cbd3ec bellard
                                 */
1207 f0cbd3ec bellard
                                lport = (((u_char*)bptr)[0] << 8) 
1208 f0cbd3ec bellard
                                + ((u_char *)bptr)[1];
1209 f0cbd3ec bellard
                                if (lport < 6970)      
1210 f0cbd3ec bellard
                                   lport += 256;   /* don't know why */
1211 f0cbd3ec bellard
                                if (lport < 6970 || lport > 7170)
1212 f0cbd3ec bellard
                                   return 1;       /* failed */
1213 f0cbd3ec bellard
                                
1214 f0cbd3ec bellard
                                /* try to get udp port between 6970 - 7170 */
1215 f0cbd3ec bellard
                                for (p = 6970; p < 7071; p++) {
1216 f0cbd3ec bellard
                                        if (udp_listen( htons(p),
1217 f0cbd3ec bellard
                                                       so->so_laddr.s_addr,
1218 f0cbd3ec bellard
                                                       htons(lport),
1219 f0cbd3ec bellard
                                                       SS_FACCEPTONCE)) {
1220 f0cbd3ec bellard
                                                break;
1221 f0cbd3ec bellard
                                        }
1222 f0cbd3ec bellard
                                }
1223 f0cbd3ec bellard
                                if (p == 7071)
1224 f0cbd3ec bellard
                                   p = 0;
1225 f0cbd3ec bellard
                                *(u_char *)bptr++ = (p >> 8) & 0xff;
1226 f0cbd3ec bellard
                                *(u_char *)bptr++ = p & 0xff;
1227 f0cbd3ec bellard
                                ra = 0; 
1228 f0cbd3ec bellard
                                return 1;   /* port redirected, we're done */
1229 f0cbd3ec bellard
                                break;  
1230 f0cbd3ec bellard
                                
1231 f0cbd3ec bellard
                         default:
1232 f0cbd3ec bellard
                                ra = 0;                         
1233 f0cbd3ec bellard
                        }
1234 f0cbd3ec bellard
                        ra++;
1235 f0cbd3ec bellard
                }
1236 f0cbd3ec bellard
                return 1;                                
1237 f0cbd3ec bellard
                
1238 f0cbd3ec bellard
         default:
1239 f0cbd3ec bellard
                /* Ooops, not emulated, won't call tcp_emu again */
1240 f0cbd3ec bellard
                so->so_emu = 0;
1241 f0cbd3ec bellard
                return 1;
1242 f0cbd3ec bellard
        }
1243 f0cbd3ec bellard
}
1244 f0cbd3ec bellard
1245 f0cbd3ec bellard
/*
1246 f0cbd3ec bellard
 * Do misc. config of SLiRP while its running.
1247 f0cbd3ec bellard
 * Return 0 if this connections is to be closed, 1 otherwise,
1248 f0cbd3ec bellard
 * return 2 if this is a command-line connection
1249 f0cbd3ec bellard
 */
1250 f0cbd3ec bellard
int
1251 f0cbd3ec bellard
tcp_ctl(so)
1252 f0cbd3ec bellard
        struct socket *so;
1253 f0cbd3ec bellard
{
1254 f0cbd3ec bellard
        struct sbuf *sb = &so->so_snd;
1255 f0cbd3ec bellard
        int command;
1256 f0cbd3ec bellard
         struct ex_list *ex_ptr;
1257 f0cbd3ec bellard
        int do_pty;
1258 99679ece bellard
        //        struct socket *tmpso;
1259 f0cbd3ec bellard
        
1260 f0cbd3ec bellard
        DEBUG_CALL("tcp_ctl");
1261 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long )so);
1262 f0cbd3ec bellard
        
1263 a3d4af03 bellard
#if 0
1264 f0cbd3ec bellard
        /*
1265 f0cbd3ec bellard
         * Check if they're authorised
1266 f0cbd3ec bellard
         */
1267 f0cbd3ec bellard
        if (ctl_addr.s_addr && (ctl_addr.s_addr == -1 || (so->so_laddr.s_addr != ctl_addr.s_addr))) {
1268 f0cbd3ec bellard
                sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n");
1269 f0cbd3ec bellard
                sb->sb_wptr += sb->sb_cc;
1270 f0cbd3ec bellard
                return 0;
1271 f0cbd3ec bellard
        }
1272 a3d4af03 bellard
#endif        
1273 f0cbd3ec bellard
        command = (ntohl(so->so_faddr.s_addr) & 0xff);
1274 f0cbd3ec bellard
        
1275 f0cbd3ec bellard
        switch(command) {
1276 f0cbd3ec bellard
        default: /* Check for exec's */
1277 f0cbd3ec bellard
                
1278 f0cbd3ec bellard
                /*
1279 f0cbd3ec bellard
                 * Check if it's pty_exec
1280 f0cbd3ec bellard
                 */
1281 f0cbd3ec bellard
                for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1282 f0cbd3ec bellard
                        if (ex_ptr->ex_fport == so->so_fport &&
1283 f0cbd3ec bellard
                            command == ex_ptr->ex_addr) {
1284 f0cbd3ec bellard
                                do_pty = ex_ptr->ex_pty;
1285 f0cbd3ec bellard
                                goto do_exec;
1286 f0cbd3ec bellard
                        }
1287 f0cbd3ec bellard
                }
1288 f0cbd3ec bellard
                
1289 f0cbd3ec bellard
                /*
1290 f0cbd3ec bellard
                 * Nothing bound..
1291 f0cbd3ec bellard
                 */
1292 f0cbd3ec bellard
                /* tcp_fconnect(so); */
1293 f0cbd3ec bellard
                
1294 f0cbd3ec bellard
                /* FALLTHROUGH */
1295 f0cbd3ec bellard
        case CTL_ALIAS:
1296 f0cbd3ec bellard
          sb->sb_cc = sprintf(sb->sb_wptr,
1297 f0cbd3ec bellard
                              "Error: No application configured.\r\n");
1298 f0cbd3ec bellard
          sb->sb_wptr += sb->sb_cc;
1299 f0cbd3ec bellard
          return(0);
1300 f0cbd3ec bellard
1301 f0cbd3ec bellard
        do_exec:
1302 f0cbd3ec bellard
                DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec));
1303 f0cbd3ec bellard
                return(fork_exec(so, ex_ptr->ex_exec, do_pty));
1304 f0cbd3ec bellard
                
1305 a3d4af03 bellard
#if 0
1306 f0cbd3ec bellard
        case CTL_CMD:
1307 f0cbd3ec bellard
           for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
1308 f0cbd3ec bellard
             if (tmpso->so_emu == EMU_CTL && 
1309 f0cbd3ec bellard
                 !(tmpso->so_tcpcb? 
1310 f0cbd3ec bellard
                   (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK))
1311 f0cbd3ec bellard
                   :0)) {
1312 f0cbd3ec bellard
               /* Ooops, control connection already active */
1313 f0cbd3ec bellard
               sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n");
1314 f0cbd3ec bellard
               sb->sb_wptr += sb->sb_cc;
1315 f0cbd3ec bellard
               return 0;
1316 f0cbd3ec bellard
             }
1317 f0cbd3ec bellard
           }
1318 f0cbd3ec bellard
           so->so_emu = EMU_CTL;
1319 f0cbd3ec bellard
           ctl_password_ok = 0;
1320 f0cbd3ec bellard
           sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> ");
1321 f0cbd3ec bellard
           sb->sb_wptr += sb->sb_cc;
1322 f0cbd3ec bellard
           do_echo=-1;
1323 f0cbd3ec bellard
           return(2);
1324 f0cbd3ec bellard
#endif
1325 a3d4af03 bellard
        }
1326 f0cbd3ec bellard
}