Skip to content

Commit 05a2e67

Browse files
committed
Wait for RRP or RFNM to be received.
1 parent 570806a commit 05a2e67

File tree

1 file changed

+123
-39
lines changed

1 file changed

+123
-39
lines changed

src/ncp.c

Lines changed: 123 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
#define CONN_CLIENT 000001
5959
#define CONN_SERVER 000002
60+
#define CONN_SENT_RTS 000010
61+
#define CONN_SENT_STR 000020
6062
#define CONN_GOT_RTS 000100
6163
#define CONN_GOT_STR 000200
6264
#define CONN_GOT_SOCKET 000400
@@ -83,6 +85,8 @@ struct
8385
unsigned flags;
8486
int listen;
8587
struct { int link, size; uint32_t lsock, rsock; } rcv, snd;
88+
void (*rrp_callback) (int);
89+
void (*rfnm_callback) (int);
8690
} connection[CONNECTIONS];
8791

8892
struct
@@ -122,6 +126,48 @@ static const char *type_name[] =
122126
static uint8_t packet[200];
123127
static uint8_t app[200];
124128

129+
static void when_rrp (int i, void (*cb) (int))
130+
{
131+
connection[i].rrp_callback = cb;
132+
}
133+
134+
static void check_rrp (int host)
135+
{
136+
void (*cb) (int);
137+
int i;
138+
for (i = 0; i < CONNECTIONS; i++) {
139+
if (connection[i].host != host)
140+
continue;
141+
if (connection[i].rrp_callback == NULL)
142+
continue;
143+
cb = connection[i].rrp_callback;
144+
connection[i].rrp_callback = NULL;
145+
cb (i);
146+
}
147+
}
148+
149+
static void when_rfnm (int i, void (*cb) (int))
150+
{
151+
connection[i].rfnm_callback = cb;
152+
}
153+
154+
static void check_rfnm (int host)
155+
{
156+
void (*cb) (int);
157+
int i;
158+
for (i = 0; i < CONNECTIONS; i++) {
159+
if (connection[i].host != host)
160+
continue;
161+
if (connection[i].rfnm_callback == NULL)
162+
continue;
163+
if (hosts[connection[i].host].outstanding_rfnm >= 4)
164+
continue;
165+
cb = connection[i].rfnm_callback;
166+
connection[i].rfnm_callback = NULL;
167+
cb (i);
168+
}
169+
}
170+
125171
static int find_link (int host, int link)
126172
{
127173
int i;
@@ -188,6 +234,8 @@ static void destroy (int i)
188234
connection[i].rcv.lsock = connection[i].rcv.rsock =
189235
connection[i].snd.lsock = connection[i].snd.rsock = 0;
190236
connection[i].flags = 0;
237+
connection[i].rrp_callback = NULL;
238+
connection[i].rfnm_callback = NULL;
191239
}
192240

193241
static void send_imp (int flags, int type, int destination, int link, int id,
@@ -504,6 +552,7 @@ static int process_rts (uint8_t source, uint8_t *data)
504552
connection[i].snd.lsock,
505553
connection[i].snd.rsock,
506554
connection[i].snd.size);
555+
connection[i].flags |= CONN_SENT_STR;
507556
} else if ((i = find_snd_sockets (source, lsock, rsock)) != -1) {
508557
/* There already exists a connection for this socket pair, which
509558
means an STR was sent previously. */
@@ -527,6 +576,7 @@ static int process_rts (uint8_t source, uint8_t *data)
527576
connection[i].snd.lsock,
528577
connection[i].snd.rsock,
529578
connection[i].snd.size);
579+
connection[i].flags |= CONN_SENT_STR;
530580
maybe_reply (i);
531581
} else {
532582
/* There is no connection for this socket pair. */
@@ -543,6 +593,7 @@ static int process_rts (uint8_t source, uint8_t *data)
543593
connection[i].snd.lsock,
544594
connection[i].snd.rsock,
545595
connection[i].snd.size);
596+
connection[i].flags |= CONN_SENT_STR;
546597
}
547598

548599
return 9;
@@ -597,6 +648,7 @@ static int process_str (uint8_t source, uint8_t *data)
597648
connection[i].rcv.lsock,
598649
connection[i].rcv.rsock,
599650
connection[i].rcv.link);
651+
connection[i].flags |= CONN_SENT_RTS;
600652
maybe_reply (i);
601653
} else {
602654
/* There is no connection for this socket pair. */
@@ -614,6 +666,7 @@ static int process_str (uint8_t source, uint8_t *data)
614666
connection[i].rcv.lsock,
615667
connection[i].rcv.rsock,
616668
connection[i].rcv.link);
669+
connection[i].flags |= CONN_SENT_RTS;
617670
}
618671

619672
return 9;
@@ -671,6 +724,52 @@ static int process_cls (uint8_t source, uint8_t *data)
671724
return 8;
672725
}
673726

727+
static void send_rts (int i)
728+
{
729+
if (connection[i].flags & CONN_SENT_RTS)
730+
return;
731+
fprintf (stderr, "NCP: Send ICP RTS %u:%u link %d.\n",
732+
connection[i].rcv.lsock, connection[i].rcv.rsock,
733+
connection[i].rcv.link);
734+
ncp_rts (connection[i].host, connection[i].rcv.lsock,
735+
connection[i].rcv.rsock, connection[i].rcv.link);
736+
connection[i].flags |= CONN_SENT_RTS;
737+
}
738+
739+
static void send_str_and_rts (int i)
740+
{
741+
if ((connection[i].flags & CONN_SENT_STR) == 0) {
742+
fprintf (stderr, "NCP: Send ICP STR %u:%u byte size %d.\n",
743+
connection[i].snd.lsock, connection[i].snd.rsock,
744+
connection[i].snd.size);
745+
ncp_str (connection[i].host, connection[i].snd.lsock,
746+
connection[i].snd.rsock, connection[i].snd.size);
747+
connection[i].flags |= CONN_SENT_STR;
748+
}
749+
when_rfnm (i, send_rts);
750+
check_rfnm (connection[i].host);
751+
}
752+
753+
static void send_cls_snd (int i)
754+
{
755+
fprintf (stderr, "NCP: Close ICP %u:%u link %d.\n",
756+
connection[i].snd.lsock, connection[i].snd.rsock,
757+
connection[i].snd.link);
758+
ncp_cls (connection[i].host,
759+
connection[i].snd.lsock, connection[i].snd.rsock);
760+
connection[i].snd.size = connection[i].rcv.size = -1;
761+
}
762+
763+
static void send_cls_rcv (int i)
764+
{
765+
fprintf (stderr, "NCP: Close ICP %u:%u link %d.\n",
766+
connection[i].rcv.lsock, connection[i].rcv.rsock,
767+
connection[i].rcv.link);
768+
ncp_cls (connection[i].host,
769+
connection[i].rcv.lsock, connection[i].rcv.rsock);
770+
connection[i].snd.size = connection[i].rcv.size = -1;
771+
}
772+
674773
static int process_all (uint8_t source, uint8_t *data)
675774
{
676775
int i, j;
@@ -708,21 +807,9 @@ static int process_all (uint8_t source, uint8_t *data)
708807
fprintf (stderr, "NCP: New connection %d.\n", j);
709808
connection[j].snd.size = 8;
710809
connection[j].rcv.link = 44;
711-
fprintf (stderr, "NCP: Send ICP STR %u:%u byte size %d.\n",
712-
connection[j].snd.lsock, connection[j].snd.rsock,
713-
connection[j].snd.size);
714-
ncp_str (connection[j].host, connection[j].snd.lsock,
715-
connection[j].snd.rsock, connection[j].snd.size);
716-
fprintf (stderr, "NCP: Send ICP RTS %u:%u link %d.\n",
717-
connection[j].rcv.lsock, connection[j].rcv.rsock,
718-
connection[j].rcv.link);
719-
ncp_rts (connection[j].host, connection[j].rcv.lsock,
720-
connection[j].rcv.rsock, connection[j].rcv.link);
721-
fprintf (stderr, "NCP: Close ICP %u:%u link %d.\n",
722-
connection[i].snd.lsock, connection[i].snd.rsock, connection[i].snd.link);
723-
ncp_cls (connection[i].host,
724-
connection[i].snd.lsock, connection[i].snd.rsock);
725-
connection[i].snd.size = connection[i].rcv.size = -1;
810+
when_rfnm (j, send_str_and_rts);
811+
when_rfnm (i, send_cls_snd);
812+
check_rfnm (source);
726813
}
727814
return 7;
728815
}
@@ -877,6 +964,7 @@ static int process_rrp (uint8_t source, uint8_t *data)
877964
{
878965
fprintf (stderr, "NCP: recieved RRP from %03o.\n", source);
879966
hosts[source].flags |= HOST_ALIVE;
967+
check_rrp (source);
880968
return 0;
881969
}
882970

@@ -956,7 +1044,7 @@ static void process_regular (uint8_t *packet, int length)
9561044
if (connection[i].flags & CONN_CLIENT) {
9571045
uint32_t s = sock (&packet[9]);
9581046
fprintf (stderr, "NCP: ICP link %u socket %u.\n", link, s);
959-
ncp_cls (source, connection[i].rcv.lsock, connection[i].rcv.rsock);
1047+
when_rfnm (i, send_cls_rcv);
9601048
connection[i].snd.rsock = s;
9611049

9621050
j = find_rcv_sockets (source, connection[i].rcv.lsock+2, s+1);
@@ -969,23 +1057,11 @@ static void process_regular (uint8_t *packet, int length)
9691057
connection[j].snd.size = 8;
9701058
connection[j].rcv.link = 45;
9711059
fprintf (stderr, "NCP: New connection %d.\n", j);
972-
fprintf (stderr, "NCP: Send ICP STR %u:%u byte size %d.\n",
973-
connection[j].snd.lsock, connection[j].snd.rsock,
974-
connection[j].snd.size);
975-
ncp_str (connection[j].host,
976-
connection[j].snd.lsock,
977-
connection[j].snd.rsock,
978-
connection[j].snd.size);
979-
fprintf (stderr, "NCP: Send ICP RTS %u:%u link %d.\n",
980-
connection[j].rcv.lsock, connection[j].rcv.rsock,
981-
connection[j].rcv.link);
982-
ncp_rts (connection[j].host,
983-
connection[j].rcv.lsock,
984-
connection[j].rcv.rsock,
985-
connection[j].rcv.link);
1060+
when_rfnm (j, send_str_and_rts);
9861061
}
9871062
connection[j].listen = connection[i].rcv.rsock;
9881063
connection[j].flags |= CONN_GOT_SOCKET;
1064+
check_rfnm (connection[j].host);
9891065
maybe_reply (j);
9901066

9911067
return;
@@ -1028,6 +1104,7 @@ static void process_rfnm (uint8_t *packet, int length)
10281104
fprintf (stderr, "NCP: Ready for next message to host %03o link %u.\n",
10291105
host, packet[2]);
10301106
hosts[host].outstanding_rfnm--;
1107+
check_rfnm (host);
10311108
}
10321109

10331110
static void process_full (uint8_t *packet, int length)
@@ -1177,6 +1254,13 @@ static void app_echo (void)
11771254
ncp_eco (host, app[2]);
11781255
}
11791256

1257+
static void app_open_send_rts (int i)
1258+
{
1259+
// Send first ICP RTS.
1260+
ncp_rts (connection[i].host, connection[i].rcv.lsock,
1261+
connection[i].rcv.rsock, connection[i].rcv.link);
1262+
}
1263+
11801264
static void app_open (void)
11811265
{
11821266
uint32_t socket;
@@ -1187,22 +1271,22 @@ static void app_open (void)
11871271
fprintf (stderr, "NCP: Application open socket %u on host %03o.\n",
11881272
socket, host);
11891273

1190-
if ((hosts[host].flags & HOST_ALIVE) == 0) {
1191-
// We haven't communicated with this host yet.
1192-
ncp_rst (host);
1193-
// Wait for RRP.
1194-
}
1195-
11961274
// Initiate a connection.
11971275
i = make_open (host, 1002, socket, 0, 0);
11981276
connection[i].rcv.link = 42; //Receive link.
11991277
connection[i].flags |= CONN_CLIENT;
12001278
memcpy (&connection[i].client.addr, &client, len);
12011279
connection[i].client.len = len;
12021280

1203-
// Send first ICP RTS.
1204-
ncp_rts (connection[i].host, connection[i].rcv.lsock,
1205-
connection[i].rcv.rsock, connection[i].rcv.link);
1281+
if ((hosts[host].flags & HOST_ALIVE) == 0) {
1282+
// We haven't communicated with this host yet.
1283+
ncp_rst (host);
1284+
// Wait for RRP, then send RTS.
1285+
when_rrp (i, app_open_send_rts);
1286+
} else {
1287+
// Ok to send RTS directly.
1288+
app_open_send_rts (i);
1289+
}
12061290
}
12071291

12081292
static void app_listen (void)

0 commit comments

Comments
 (0)