54
54
#define ERR_SOCKET 4 //Request on a non-existent socket.
55
55
#define ERR_CONNECT 5 //Socket (link) not connected.
56
56
57
+ #define ICP_NONE 0
58
+ #define ICP_SERVER_LISTEN 1
59
+ #define ICP_SERVER_STR 2
60
+ #define ICP_SERVER_SOCKET 3
61
+ #define ICP_CLIENT_RTS 11
62
+ #define ICP_CLIENT_ALL 12
63
+ #define ICP_CLIENT_CLS 13
64
+ #define ICP_CLIENT_RTS2 14
65
+
57
66
#define CONNECTIONS 20
58
67
59
68
static int fd ;
71
80
{
72
81
client_t client ;
73
82
int host ;
83
+ int icp ;
74
84
struct { int link , size ; uint32_t lsock , rsock ; } rcv , snd ;
75
85
} connection [CONNECTIONS ];
76
86
@@ -168,6 +178,7 @@ static void destroy (int i)
168
178
connection [i ].snd .size = connection [i ].rcv .size = -1 ;
169
179
connection [i ].rcv .lsock = connection [i ].rcv .rsock =
170
180
connection [i ].snd .lsock = connection [i ].snd .rsock = 0 ;
181
+ connection [i ].icp = ICP_NONE ;
171
182
}
172
183
173
184
static void send_imp (int flags , int type , int destination , int link , int id ,
@@ -277,7 +288,7 @@ void ncp_rts (uint8_t destination, uint32_t lsock, uint32_t rsock, uint8_t link)
277
288
void ncp_all (uint8_t destination , uint8_t link , uint16_t msg_space , uint32_t bit_space )
278
289
{
279
290
packet [22 ] = link ;
280
- packet [23 ] = msg_space >> 16 ;
291
+ packet [23 ] = msg_space >> 8 ;
281
292
packet [24 ] = msg_space ;
282
293
packet [25 ] = bit_space >> 24 ;
283
294
packet [26 ] = bit_space >> 16 ;
@@ -438,20 +449,34 @@ static int process_rts (uint8_t source, uint8_t *data)
438
449
{
439
450
int i ;
440
451
uint32_t lsock , rsock ;
452
+ uint8_t link ;
453
+
441
454
rsock = sock (& data [0 ]);
442
455
lsock = sock (& data [4 ]);
456
+ link = data [8 ];
443
457
444
- fprintf (stderr , "NCP: Received RTS %u:%u from %03o.\n" ,
445
- lsock , rsock , source );
458
+ fprintf (stderr , "NCP: Received RTS sockets %u:%u link %u from %03o.\n" ,
459
+ rsock , lsock , link , source );
446
460
447
- if (data [ 8 ] < LINK_MIN || data [ 8 ] > LINK_MAX ) {
461
+ if (link < LINK_MIN || link > LINK_MAX ) {
448
462
ncp_err (source , ERR_PARAM , data - 1 , 10 );
449
463
return 9 ;
450
464
}
451
465
452
466
if (find_listen (lsock ) == -1 ) {
453
467
i = find_sockets (source , lsock , rsock );
454
468
if (i == -1 ) {
469
+ i = find_socket (source , lsock - 3 );
470
+ if (i != -1 ) {
471
+ fprintf (stderr , "NCP: ICP from %03o, RTS %u:%u link %u.\n" ,
472
+ source , rsock , lsock , link );
473
+ i = make_open (source , 0 , 0 , lsock , rsock );
474
+ connection [i ].snd .link = link ;
475
+ connection [i ].snd .size = 8 ;
476
+ ncp_str (source , lsock , rsock , connection [i ].snd .size );
477
+ return 9 ;
478
+ }
479
+
455
480
fprintf (stderr , "NCP: Not listening to %u, no outgoing RFC, rejecting.\n" , lsock );
456
481
ncp_cls (source , lsock , rsock );
457
482
return 9 ;
@@ -468,18 +493,23 @@ static int process_rts (uint8_t source, uint8_t *data)
468
493
fprintf (stderr , "NCP: Listening to %u: connection %d.\n" , lsock , i );
469
494
}
470
495
}
471
- connection [i ].snd .link = data [8 ]; //Send link.
472
- if (connection [i ].rcv .size == -1 ) {
496
+ connection [i ].snd .link = link ; //Send link.
497
+ if (connection [i ].icp == ICP_SERVER_LISTEN ) {
498
+ connection [i ].rcv .size = 32 ; //Send byte size for ICP.
499
+ connection [i ].icp = ICP_SERVER_STR ;
500
+ ncp_str (connection [i ].host , lsock , rsock , connection [i ].rcv .size );
501
+ }
502
+ else if (connection [i ].rcv .size == -1 ) {
473
503
connection [i ].rcv .size = 8 ; //Send byte size.
474
504
ncp_str (connection [i ].host , lsock , rsock , connection [i ].rcv .size );
475
505
if (connection [i ].rcv .link != -1 ) {
476
506
fprintf (stderr , "NCP: Completing incoming RFC.\n" );
477
- reply_listen (source , connection [i ].snd .lsock , i );
507
+ // reply_listen (source, connection[i].snd.lsock, i);
478
508
}
479
509
} else {
480
510
if (connection [i ].snd .size != -1 ) {
481
511
fprintf (stderr , "NCP: Completing outgoing RFC.\n" );
482
- reply_open (source , connection [i ].rcv .rsock , i );
512
+ // reply_open (source, connection[i].rcv.rsock, i);
483
513
}
484
514
}
485
515
@@ -490,25 +520,39 @@ static int process_str (uint8_t source, uint8_t *data)
490
520
{
491
521
int i ;
492
522
uint32_t lsock , rsock ;
523
+ uint8_t size ;
524
+
493
525
rsock = sock (& data [0 ]);
494
526
lsock = sock (& data [4 ]);
495
-
496
- fprintf (stderr , "NCP: Received STR %u:%u from %03o.\n" ,
497
- lsock , rsock , source );
498
-
499
- if (data [8 ] < LINK_MIN || data [8 ] > LINK_MAX ) {
500
- ncp_err (source , ERR_PARAM , data - 1 , 10 );
501
- return 9 ;
502
- }
527
+ size = data [8 ];
503
528
504
529
if (find_listen (lsock ) == -1 ) {
505
530
i = find_sockets (source , lsock , rsock );
506
531
if (i == -1 ) {
532
+ i = find_socket (source , lsock - 2 );
533
+ if (i != -1 ) {
534
+ fprintf (stderr , "NCP: ICP STR %u:%u size %u.\n" ,
535
+ rsock , lsock , size );
536
+ i = make_open (source , lsock , rsock , 0 , 0 );
537
+ connection [i ].rcv .size = size ;
538
+ connection [i ].rcv .link = 43 ;
539
+ ncp_rts (source , lsock , rsock , connection [i ].rcv .link );
540
+ reply_open (source , 23 , i );
541
+ return 9 ;
542
+ }
543
+
507
544
fprintf (stderr , "NCP: Not listening to %u, no outgoing RFC, rejecting.\n" , lsock );
508
545
ncp_cls (source , lsock , rsock );
509
546
return 9 ;
510
547
}
511
- fprintf (stderr , "NCP: Outgoing RFC socket %u.\n" , lsock );
548
+ if (connection [i ].icp == ICP_CLIENT_RTS ) {
549
+ fprintf (stderr , "NCP: Received from %03o ICP STR %u:%u, size %u.\n" ,
550
+ source , rsock , lsock , size );
551
+ connection [i ].rcv .size = size ;
552
+ connection [i ].icp = ICP_CLIENT_ALL ;
553
+ ncp_all (source , connection [i ].rcv .link , 1 , 200 );
554
+ return 9 ;
555
+ }
512
556
} else {
513
557
i = find_socket (source , lsock - 1 );
514
558
if (i == -1 ) {
@@ -520,7 +564,7 @@ static int process_str (uint8_t source, uint8_t *data)
520
564
fprintf (stderr , "NCP: Listening to %u: connection %d.\n" , lsock , i );
521
565
}
522
566
}
523
- connection [i ].snd .size = data [ 8 ] ; //Receive byte size.
567
+ connection [i ].snd .size = size ; //Receive byte size.
524
568
if (connection [i ].rcv .link == -1 ) {
525
569
connection [i ].rcv .link = 42 ; //Receive link.
526
570
ncp_rts (connection [i ].host , lsock , rsock , connection [i ].rcv .link );
@@ -531,7 +575,7 @@ static int process_str (uint8_t source, uint8_t *data)
531
575
} else {
532
576
if (connection [i ].snd .link != -1 ) {
533
577
fprintf (stderr , "NCP: Completing outgoing RFC.\n" );
534
- reply_open (source , connection [i ].rcv .rsock , i );
578
+ // reply_open (source, connection[i].rcv.rsock, i);
535
579
}
536
580
}
537
581
@@ -546,9 +590,21 @@ static int process_cls (uint8_t source, uint8_t *data)
546
590
lsock = sock (& data [4 ]);
547
591
i = find_sockets (source , lsock , rsock );
548
592
if (i == -1 ) {
593
+ fprintf (stderr , "NCP: Remote tried to close %u:%u which does not exist.\n" ,
594
+ lsock , rsock );
549
595
ncp_err (source , ERR_SOCKET , data - 1 , 9 );
550
596
return 8 ;
551
597
}
598
+
599
+ if (connection [i ].icp == ICP_CLIENT_CLS ) {
600
+ ncp_rts (source ,
601
+ connection [i ].rcv .lsock ,
602
+ connection [i ].rcv .rsock ,
603
+ connection [i ].rcv .link );
604
+ connection [i ].icp = ICP_CLIENT_RTS2 ;
605
+ return 8 ;
606
+ }
607
+
552
608
if (connection [i ].rcv .lsock == lsock )
553
609
connection [i ].rcv .lsock = connection [i ].rcv .rsock = 0 ;
554
610
if (connection [i ].snd .lsock == lsock )
@@ -568,7 +624,7 @@ static int process_cls (uint8_t source, uint8_t *data)
568
624
if (connection [i ].rcv .lsock == 0 && connection [i ].snd .lsock == 0 ) {
569
625
fprintf (stderr , "NCP: Connection %u closed by remote.\n" , i );
570
626
destroy (i );
571
- reply_close (i );
627
+ // reply_close (i);
572
628
}
573
629
}
574
630
@@ -578,12 +634,26 @@ static int process_cls (uint8_t source, uint8_t *data)
578
634
static int process_all (uint8_t source , uint8_t * data )
579
635
{
580
636
int i ;
637
+ uint8_t link = data [0 ];
638
+
581
639
fprintf (stderr , "NCP: Received ALL from %03o, link %u.\n" ,
582
- source , data [ 0 ] );
583
- i = find_link (source , data [ 0 ] );
584
- if (i == -1 )
640
+ source , link );
641
+ i = find_link (source , link );
642
+ if (i == -1 ) {
585
643
ncp_err (source , ERR_SOCKET , data - 1 , 10 );
586
- return 9 ;
644
+ } else if (connection [i ].icp == ICP_SERVER_STR ) {
645
+ uint8_t tmp [4 ];
646
+ uint32_t s = 0200 ;
647
+ tmp [0 ] = (s >> 24 ) & 0xFF ;
648
+ tmp [1 ] = (s >> 16 ) & 0xFF ;
649
+ tmp [2 ] = (s >> 8 ) & 0xFF ;
650
+ tmp [3 ] = (s >> 0 ) & 0xFF ;
651
+ fprintf (stderr , "NCP: Send socket %u for ICP.\n" , s );
652
+ send_imp (0 , IMP_REGULAR , connection [i ].host , connection [i ].snd .link ,
653
+ 0 , 0 , & tmp , 4 );
654
+ connection [i ].icp = ICP_SERVER_SOCKET ;
655
+ }
656
+ return 7 ;
587
657
}
588
658
589
659
static int process_gvb (uint8_t source , uint8_t * data )
@@ -656,7 +726,6 @@ static int process_erp (uint8_t source, uint8_t *data)
656
726
{
657
727
fprintf (stderr , "NCP: recieved ERP %03o from %03o.\n" ,
658
728
* data , source );
659
- hosts [source ].flags |= HOST_ALIVE ;
660
729
reply_echo (source , * data , 0x10 );
661
730
hosts [source ].echo .len = 0 ;
662
731
return 1 ;
@@ -804,6 +873,16 @@ static void process_regular (uint8_t *packet, int length)
804
873
}
805
874
length = 2 * (length - 2 );
806
875
fprintf (stderr , "NCP: Connection %u, length %u.\n" , i , length );
876
+
877
+ if (connection [i ].icp == ICP_CLIENT_ALL ) {
878
+ uint32_t s = sock (& packet [5 ]);
879
+ fprintf (stderr , "NCP: ICP link %u socket %u.\n" , link , s );
880
+ ncp_cls (source , connection [i ].rcv .lsock , connection [i ].rcv .rsock );
881
+ connection [i ].icp = ICP_CLIENT_CLS ;
882
+ connection [i ].snd .rsock = s ;
883
+ return ;
884
+ }
885
+
807
886
reply_read (i , packet + 4 , length );
808
887
}
809
888
}
@@ -997,8 +1076,8 @@ static void app_open (void)
997
1076
int i ;
998
1077
999
1078
socket = app [2 ] << 24 | app [3 ] << 16 | app [4 ] << 8 | app [5 ];
1000
- fprintf (stderr , "NCP: Application open sockets %u, %u on host %03o.\n" ,
1001
- socket , socket + 1 , host );
1079
+ fprintf (stderr , "NCP: Application open socket %u on host %03o.\n" ,
1080
+ socket , host );
1002
1081
1003
1082
if ((hosts [host ].flags & HOST_ALIVE ) == 0 ) {
1004
1083
// We haven't communicated with this host yet.
@@ -1007,17 +1086,15 @@ static void app_open (void)
1007
1086
}
1008
1087
1009
1088
// Initiate a connection.
1010
- i = make_open (host , 1002 , socket , 1003 , socket + 1 );
1089
+ i = make_open (host , 1002 , socket , 0 , 0 );
1011
1090
connection [i ].rcv .link = 42 ; //Receive link.
1012
- connection [i ].rcv .size = 8 ; //Send byte size.
1013
1091
memcpy (& connection [i ].client .addr , & client , len );
1014
1092
connection [i ].client .len = len ;
1015
1093
1016
- // Send RFC messages .
1094
+ // Send first ICP RTS .
1017
1095
ncp_rts (connection [i ].host , connection [i ].rcv .lsock ,
1018
1096
connection [i ].rcv .rsock , connection [i ].rcv .link );
1019
- ncp_str (connection [i ].host , connection [i ].snd .lsock ,
1020
- connection [i ].snd .rsock , connection [i ].rcv .size );
1097
+ connection [i ].icp = ICP_CLIENT_RTS ;
1021
1098
}
1022
1099
1023
1100
static void app_listen (void )
@@ -1041,6 +1118,7 @@ static void app_listen (void)
1041
1118
listening [i ].sock = socket ;
1042
1119
memcpy (& connection [i ].client .addr , & client , len );
1043
1120
connection [i ].client .len = len ;
1121
+ connection [i ].icp = ICP_SERVER_LISTEN ;
1044
1122
}
1045
1123
1046
1124
static void app_read (void )
0 commit comments