Skip to content

Commit 31abf24

Browse files
committed
Implement initial connection protocol.
1 parent 572318c commit 31abf24

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

src/ncp.c

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
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+
5762
#define CONNECTIONS 20
5863

5964
static int fd;
@@ -71,6 +76,7 @@ struct
7176
{
7277
client_t client;
7378
int host;
79+
int icp;
7480
struct { int link, size; uint32_t lsock, rsock; } rcv, snd;
7581
} connection[CONNECTIONS];
7682

@@ -168,6 +174,7 @@ static void destroy (int i)
168174
connection[i].snd.size = connection[i].rcv.size = -1;
169175
connection[i].rcv.lsock = connection[i].rcv.rsock =
170176
connection[i].snd.lsock = connection[i].snd.rsock = 0;
177+
connection[i].icp = ICP_NONE;
171178
}
172179

173180
static void send_imp (int flags, int type, int destination, int link, int id,
@@ -277,7 +284,7 @@ void ncp_rts (uint8_t destination, uint32_t lsock, uint32_t rsock, uint8_t link)
277284
void ncp_all (uint8_t destination, uint8_t link, uint16_t msg_space, uint32_t bit_space)
278285
{
279286
packet[22] = link;
280-
packet[23] = msg_space >> 16;
287+
packet[23] = msg_space >> 8;
281288
packet[24] = msg_space;
282289
packet[25] = bit_space >> 24;
283290
packet[26] = bit_space >> 16;
@@ -438,13 +445,16 @@ static int process_rts (uint8_t source, uint8_t *data)
438445
{
439446
int i;
440447
uint32_t lsock, rsock;
448+
uint8_t link;
449+
441450
rsock = sock (&data[0]);
442451
lsock = sock (&data[4]);
452+
link = data[8];
443453

444-
fprintf (stderr, "NCP: Recieved RTS %u:%u from %03o.\n",
445-
lsock, rsock, source);
454+
fprintf (stderr, "NCP: Received RTS sockets %u:%u link %u from %03o.\n",
455+
lsock, rsock, link, source);
446456

447-
if (data[8] < LINK_MIN || data[8] > LINK_MAX) {
457+
if (link < LINK_MIN || link > LINK_MAX) {
448458
ncp_err (source, ERR_PARAM, data - 1, 10);
449459
return 9;
450460
}
@@ -468,18 +478,23 @@ static int process_rts (uint8_t source, uint8_t *data)
468478
fprintf (stderr, "NCP: Listening to %u: connection %d.\n", lsock, i);
469479
}
470480
}
471-
connection[i].snd.link = data[8]; //Send link.
472-
if (connection[i].rcv.size == -1) {
481+
connection[i].snd.link = link; //Send link.
482+
if (connection[i].icp == ICP_SERVER_LISTEN) {
483+
connection[i].rcv.size = 32; //Send byte size for ICP.
484+
connection[i].icp = ICP_SERVER_STR;
485+
ncp_str (connection[i].host, lsock, rsock, connection[i].rcv.size);
486+
}
487+
else if (connection[i].rcv.size == -1) {
473488
connection[i].rcv.size = 8; //Send byte size.
474489
ncp_str (connection[i].host, lsock, rsock, connection[i].rcv.size);
475490
if (connection[i].rcv.link != -1) {
476491
fprintf (stderr, "NCP: Completing incoming RFC.\n");
477-
reply_listen (source, connection[i].snd.lsock, i);
492+
//reply_listen (source, connection[i].snd.lsock, i);
478493
}
479494
} else {
480495
if (connection[i].snd.size != -1) {
481496
fprintf (stderr, "NCP: Completing outgoing RFC.\n");
482-
reply_open (source, connection[i].rcv.rsock, i);
497+
//reply_open (source, connection[i].rcv.rsock, i);
483498
}
484499
}
485500

@@ -490,17 +505,15 @@ static int process_str (uint8_t source, uint8_t *data)
490505
{
491506
int i;
492507
uint32_t lsock, rsock;
508+
uint8_t size;
509+
493510
rsock = sock (&data[0]);
494511
lsock = sock (&data[4]);
512+
size = data[8];
495513

496514
fprintf (stderr, "NCP: Recieved STR %u:%u from %03o.\n",
497515
lsock, rsock, source);
498516

499-
if (data[8] < LINK_MIN || data[8] > LINK_MAX) {
500-
ncp_err (source, ERR_PARAM, data - 1, 10);
501-
return 9;
502-
}
503-
504517
if (find_listen (lsock) == -1) {
505518
i = find_sockets (source, lsock, rsock);
506519
if (i == -1) {
@@ -520,7 +533,7 @@ static int process_str (uint8_t source, uint8_t *data)
520533
fprintf (stderr, "NCP: Listening to %u: connection %d.\n", lsock, i);
521534
}
522535
}
523-
connection[i].snd.size = data[8]; //Receive byte size.
536+
connection[i].snd.size = size; //Receive byte size.
524537
if (connection[i].rcv.link == -1) {
525538
connection[i].rcv.link = 42; //Receive link.
526539
ncp_rts (connection[i].host, lsock, rsock, connection[i].rcv.link);
@@ -578,12 +591,26 @@ static int process_cls (uint8_t source, uint8_t *data)
578591
static int process_all (uint8_t source, uint8_t *data)
579592
{
580593
int i;
594+
uint8_t link = data[0];
595+
581596
fprintf (stderr, "NCP: Recieved ALL from %03o, link %u.\n",
582-
source, data[0]);
583-
i = find_link (source, data[0]);
584-
if (i == -1)
597+
source, link);
598+
i = find_link (source, link);
599+
if (i == -1) {
585600
ncp_err (source, ERR_SOCKET, data - 1, 10);
586-
return 9;
601+
} else if (connection[i].icp == ICP_SERVER_STR) {
602+
uint8_t tmp[4];
603+
uint32_t s = 0200;
604+
tmp[0] = (s >> 24) & 0xFF;
605+
tmp[1] = (s >> 16) & 0xFF;
606+
tmp[2] = (s >> 8) & 0xFF;
607+
tmp[3] = (s >> 0) & 0xFF;
608+
fprintf (stderr, "NCP: Send socket %u for ICP.\n", s);
609+
send_imp (0, IMP_REGULAR, connection[i].host, connection[i].snd.link,
610+
0, 0, &tmp, 4);
611+
connection[i].icp = ICP_SERVER_SOCKET;
612+
}
613+
return 7;
587614
}
588615

589616
static int process_gvb (uint8_t source, uint8_t *data)
@@ -761,13 +788,17 @@ static int (*ncp_messages[]) (uint8_t source, uint8_t *data) =
761788
static void process_ncp (uint8_t source, uint8_t *data, uint16_t count)
762789
{
763790
int i = 0, n;
791+
fprintf (stderr, "Process NCP: %u bytes.\n", count);
764792
while (i < count) {
765793
uint8_t type = data[i++];
794+
fprintf (stderr, "Process NCP: type %u.\n", type);
766795
if (type > NCP_MAX) {
796+
fprintf (stderr, "Process NCP: bad type.\n");
767797
ncp_err (source, ERR_OPCODE, data - 1, 10);
768798
return;
769799
}
770800
n = ncp_messages[type] (source, &data[i]);
801+
fprintf (stderr, "Process NCP: %d bytes data => %d.\n", n, i + n);
771802
if (i + n > count)
772803
ncp_err (source, ERR_SHORT, data - 1, count - i + 1);
773804
i += n;
@@ -1013,6 +1044,12 @@ static void app_open (void)
10131044
memcpy (&connection[i].client.addr, &client, len);
10141045
connection[i].client.len = len;
10151046

1047+
// Send ICP RTS.
1048+
// Wait for STR.
1049+
// Send ALL.
1050+
// Wait for data with remote socket.
1051+
// Wait for CLS / send CLS.
1052+
10161053
// Send RFC messages.
10171054
ncp_rts (connection[i].host, connection[i].rcv.lsock,
10181055
connection[i].rcv.rsock, connection[i].rcv.link);
@@ -1041,6 +1078,7 @@ static void app_listen (void)
10411078
listening[i].sock = socket;
10421079
memcpy (&connection[i].client.addr, &client, len);
10431080
connection[i].client.len = len;
1081+
connection[i].icp = ICP_SERVER_LISTEN;
10441082
}
10451083

10461084
static void app_read (void)

0 commit comments

Comments
 (0)