Skip to content

Commit b60c36f

Browse files
committed
Implement initial connection protocol.
1 parent 32ddabb commit b60c36f

File tree

1 file changed

+110
-32
lines changed

1 file changed

+110
-32
lines changed

src/ncp.c

Lines changed: 110 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@
5454
#define ERR_SOCKET 4 //Request on a non-existent socket.
5555
#define ERR_CONNECT 5 //Socket (link) not connected.
5656

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+
5766
#define CONNECTIONS 20
5867

5968
static int fd;
@@ -71,6 +80,7 @@ struct
7180
{
7281
client_t client;
7382
int host;
83+
int icp;
7484
struct { int link, size; uint32_t lsock, rsock; } rcv, snd;
7585
} connection[CONNECTIONS];
7686

@@ -168,6 +178,7 @@ static void destroy (int i)
168178
connection[i].snd.size = connection[i].rcv.size = -1;
169179
connection[i].rcv.lsock = connection[i].rcv.rsock =
170180
connection[i].snd.lsock = connection[i].snd.rsock = 0;
181+
connection[i].icp = ICP_NONE;
171182
}
172183

173184
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)
277288
void ncp_all (uint8_t destination, uint8_t link, uint16_t msg_space, uint32_t bit_space)
278289
{
279290
packet[22] = link;
280-
packet[23] = msg_space >> 16;
291+
packet[23] = msg_space >> 8;
281292
packet[24] = msg_space;
282293
packet[25] = bit_space >> 24;
283294
packet[26] = bit_space >> 16;
@@ -438,20 +449,34 @@ static int process_rts (uint8_t source, uint8_t *data)
438449
{
439450
int i;
440451
uint32_t lsock, rsock;
452+
uint8_t link;
453+
441454
rsock = sock (&data[0]);
442455
lsock = sock (&data[4]);
456+
link = data[8];
443457

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);
446460

447-
if (data[8] < LINK_MIN || data[8] > LINK_MAX) {
461+
if (link < LINK_MIN || link > LINK_MAX) {
448462
ncp_err (source, ERR_PARAM, data - 1, 10);
449463
return 9;
450464
}
451465

452466
if (find_listen (lsock) == -1) {
453467
i = find_sockets (source, lsock, rsock);
454468
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+
455480
fprintf (stderr, "NCP: Not listening to %u, no outgoing RFC, rejecting.\n", lsock);
456481
ncp_cls (source, lsock, rsock);
457482
return 9;
@@ -468,18 +493,23 @@ static int process_rts (uint8_t source, uint8_t *data)
468493
fprintf (stderr, "NCP: Listening to %u: connection %d.\n", lsock, i);
469494
}
470495
}
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) {
473503
connection[i].rcv.size = 8; //Send byte size.
474504
ncp_str (connection[i].host, lsock, rsock, connection[i].rcv.size);
475505
if (connection[i].rcv.link != -1) {
476506
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);
478508
}
479509
} else {
480510
if (connection[i].snd.size != -1) {
481511
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);
483513
}
484514
}
485515

@@ -490,25 +520,39 @@ static int process_str (uint8_t source, uint8_t *data)
490520
{
491521
int i;
492522
uint32_t lsock, rsock;
523+
uint8_t size;
524+
493525
rsock = sock (&data[0]);
494526
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];
503528

504529
if (find_listen (lsock) == -1) {
505530
i = find_sockets (source, lsock, rsock);
506531
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+
507544
fprintf (stderr, "NCP: Not listening to %u, no outgoing RFC, rejecting.\n", lsock);
508545
ncp_cls (source, lsock, rsock);
509546
return 9;
510547
}
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+
}
512556
} else {
513557
i = find_socket (source, lsock - 1);
514558
if (i == -1) {
@@ -520,7 +564,7 @@ static int process_str (uint8_t source, uint8_t *data)
520564
fprintf (stderr, "NCP: Listening to %u: connection %d.\n", lsock, i);
521565
}
522566
}
523-
connection[i].snd.size = data[8]; //Receive byte size.
567+
connection[i].snd.size = size; //Receive byte size.
524568
if (connection[i].rcv.link == -1) {
525569
connection[i].rcv.link = 42; //Receive link.
526570
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)
531575
} else {
532576
if (connection[i].snd.link != -1) {
533577
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);
535579
}
536580
}
537581

@@ -546,9 +590,21 @@ static int process_cls (uint8_t source, uint8_t *data)
546590
lsock = sock (&data[4]);
547591
i = find_sockets (source, lsock, rsock);
548592
if (i == -1) {
593+
fprintf (stderr, "NCP: Remote tried to close %u:%u which does not exist.\n",
594+
lsock, rsock);
549595
ncp_err (source, ERR_SOCKET, data - 1, 9);
550596
return 8;
551597
}
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+
552608
if (connection[i].rcv.lsock == lsock)
553609
connection[i].rcv.lsock = connection[i].rcv.rsock = 0;
554610
if (connection[i].snd.lsock == lsock)
@@ -568,7 +624,7 @@ static int process_cls (uint8_t source, uint8_t *data)
568624
if (connection[i].rcv.lsock == 0 && connection[i].snd.lsock == 0) {
569625
fprintf (stderr, "NCP: Connection %u closed by remote.\n", i);
570626
destroy (i);
571-
reply_close (i);
627+
//reply_close (i);
572628
}
573629
}
574630

@@ -578,12 +634,26 @@ static int process_cls (uint8_t source, uint8_t *data)
578634
static int process_all (uint8_t source, uint8_t *data)
579635
{
580636
int i;
637+
uint8_t link = data[0];
638+
581639
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) {
585643
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;
587657
}
588658

589659
static int process_gvb (uint8_t source, uint8_t *data)
@@ -656,7 +726,6 @@ static int process_erp (uint8_t source, uint8_t *data)
656726
{
657727
fprintf (stderr, "NCP: recieved ERP %03o from %03o.\n",
658728
*data, source);
659-
hosts[source].flags |= HOST_ALIVE;
660729
reply_echo (source, *data, 0x10);
661730
hosts[source].echo.len = 0;
662731
return 1;
@@ -804,6 +873,16 @@ static void process_regular (uint8_t *packet, int length)
804873
}
805874
length = 2 * (length - 2);
806875
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+
807886
reply_read (i, packet + 4, length);
808887
}
809888
}
@@ -997,8 +1076,8 @@ static void app_open (void)
9971076
int i;
9981077

9991078
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);
10021081

10031082
if ((hosts[host].flags & HOST_ALIVE) == 0) {
10041083
// We haven't communicated with this host yet.
@@ -1007,17 +1086,15 @@ static void app_open (void)
10071086
}
10081087

10091088
// Initiate a connection.
1010-
i = make_open (host, 1002, socket, 1003, socket+1);
1089+
i = make_open (host, 1002, socket, 0, 0);
10111090
connection[i].rcv.link = 42; //Receive link.
1012-
connection[i].rcv.size = 8; //Send byte size.
10131091
memcpy (&connection[i].client.addr, &client, len);
10141092
connection[i].client.len = len;
10151093

1016-
// Send RFC messages.
1094+
// Send first ICP RTS.
10171095
ncp_rts (connection[i].host, connection[i].rcv.lsock,
10181096
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;
10211098
}
10221099

10231100
static void app_listen (void)
@@ -1041,6 +1118,7 @@ static void app_listen (void)
10411118
listening[i].sock = socket;
10421119
memcpy (&connection[i].client.addr, &client, len);
10431120
connection[i].client.len = len;
1121+
connection[i].icp = ICP_SERVER_LISTEN;
10441122
}
10451123

10461124
static void app_read (void)

0 commit comments

Comments
 (0)