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