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