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