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