-
Notifications
You must be signed in to change notification settings - Fork 0
/
ancp_fsm.c
392 lines (352 loc) · 10.8 KB
/
ancp_fsm.c
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
40
41
42
43
44
45
46
47
48
49
50
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/time.h>
#include "ancp.h"
#include "utils.h"
#include "bitset.h"
#include "logg.h"
#include "ancp_fsm.h"
#include "ancp_fsmtbl.h"
/*
* update_peer_verifier:
*
* if message is a SYNACK or SYN
* save the foll fields
* Sender Instance
* Sender Port
* Sender Name
* Partition-ID
*/
bool update_peer_verifier (unsigned char *msgin, ancp_state_t *state)
{
adj_code_t code;
adj_msg_hdr_t *msg;
DEBUG_DET("In %s()\n", __FUNCTION__);
code = get_adj_mtype(msgin);
if ((code == SYN) || (code == SYNACK)) {
msg = (adj_msg_hdr_t*) msgin;
state->receiver_instance = get_3byte(msg->receiver_instance);
memcpy(state->receiver_name, msg->sender_name, MAX_NAME);
state->partition_id = msg->partition_id;
DEBUG_FSM("FSM: update_peer_verifier success\n");
return true;
}
DEBUG_FSM("FSM: update_peer_verifier failed \n");
return false;
}
/*
* reset_link:
*
* generate new instance number for link
* set to 0:
* Sender Instance
* Sender Port
* Sender Name
* Partition-ID
* (of the other end [stored])
*/
void
reset_link (int fd, unsigned char *msgin, ancp_state_t *state)
{
DEBUG_DET("In %s()\n", __FUNCTION__);
state->sender_instance ++;
state->receiver_instance = 0;
memset(state->receiver_name, 0, MAX_NAME);
state->partition_id = 0;
DEBUG_FSM("FSM: link reset on fd %d\n", fd);
send_SYN(fd, msgin, state);
state->curr_state = SYNSENT;
}
/*
* A
*
* if (Sender Instance of incoming message == stored Sender Instance)
* return TRUE;
*/
bool A (unsigned char *msgin, ancp_state_t *state)
{
adj_msg_hdr_t *msg;
DEBUG_DET("In %s()\n", __FUNCTION__);
msg = (adj_msg_hdr_t*) msgin;
if(state->sender_instance == get_3byte(msg->sender_instance)) {
DEBUG_FSM("FSM: A() success \n");
return true;
}
DEBUG_FSM("FSM: A() failed \n");
return false;
}
/*
* B
*
* if ( Sender Instance of incoming message == stored Sender Instance &&
* Sender Port of incoming message == stored Sender Port &&
* Sender Name of incoming message == stored Sender Name &&
* Partition-ID of incoming message == stored Partition-ID)
* return TRUE;
*/
bool B (unsigned char *msgin, ancp_state_t *state)
{
adj_msg_hdr_t *msg;
DEBUG_DET("In %s()\n", __FUNCTION__);
msg = (adj_msg_hdr_t*) msgin;
if ((state->receiver_instance == get_3byte(msg->sender_instance)) &&
(state->partition_id == msg->partition_id) &&
(0 == memcmp(state->receiver_name, msg->sender_name, MAX_NAME))){
DEBUG_FSM("FSM: B() success \n");
return true;
}
DEBUG_FSM("FSM: B() failed \n");
return false;
}
/*
* C
*
* if ( Receiver Instance of incoming message == our Sender Instance &&
* Receiver Name of incoming message == our Sender Name &&
* Receiver Port of incoming message == our Sender Port &&
* Partition-ID of incoming message == our Partition-ID)
* return TRUE;
* return FALSE;
*/
bool C (unsigned char *msgin, ancp_state_t *state)
{
adj_msg_hdr_t *msg;
DEBUG_DET("In %s()\n", __FUNCTION__);
msg = (adj_msg_hdr_t*) msgin;
if ((state->sender_instance == get_3byte(msg->receiver_instance)) &&
(state->partition_id == msg->partition_id) &&
(0 == memcmp(state->sender_name, msg->receiver_name, MAX_NAME))){
DEBUG_FSM("FSM: C() success \n");
return true;
}
DEBUG_FSM("FSM: C() failed \n");
return false;
}
/* FSM state functions */
void start_syn(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state START, sending first SYN\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
send_SYN(fd, msgin, state);
state->curr_state = SYNSENT;
}
void synsent_synack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNSENT, received event SYNACK \n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (C(msgin,state)) {
update_peer_verifier(msgin, state);
send_ACK(fd, msgin, state);
state->curr_state = ESTAB;
} else {
send_RSTACK(fd, msgin, state);
state->curr_state = SYNSENT;
}
}
void synsent_syn(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNSENT, received event SYN\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
update_peer_verifier(msgin, state);
send_SYNACK(fd, msgin, state);
state->curr_state = SYNRCVD;
}
void synsent_ack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNSENT, received event ACK\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
send_RSTACK(fd, msgin, state);
state->curr_state = SYNSENT;
}
void synrcvd_ack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNRCVD, received event ACK \n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (B(msgin,state) && C(msgin,state) ) {
send_ACK(fd, msgin, state);
state->curr_state = ESTAB;
} else {
send_RSTACK(fd, msgin, state);
state->curr_state = SYNRCVD;
}
}
void synsent_unk(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNSENT, received event UNKNOWN\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (state->syn_cnt < 2)
send_SYN(fd, msgin, state); /* Note 1: Syn Throttle */
}
void synrcvd_syn(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNRCVD, received event SYN\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
update_peer_verifier(msgin, state);
send_SYNACK(fd, msgin, state);
state->curr_state = SYNRCVD;
}
void synrcvd_synack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNRCVD, received event SYNACK\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (C(msgin,state)){
update_peer_verifier(msgin, state);
send_ACK(fd, msgin, state);
state->curr_state = ESTAB;
} else {
send_RSTACK(fd, msgin, state);
state->curr_state = SYNRCVD;
}
}
void synrcvd_unk(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state SYNRCVD, received event UNKNOWN\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (state->synack_cnt < 2)
send_SYNACK(fd, msgin, state);/* Note 1: SynAck Throttle */
}
void estab_ack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state ESTAB, received event ACK \n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
if (B(msgin,state) && C(msgin,state) ) {
/* Note 3: ACK Throttle */
if(state->ack_cnt < 1)
send_ACK(fd,msgin, state);
state->curr_state = ESTAB;
} else {
send_RSTACK(fd,msgin, state);
state->curr_state = ESTAB;
}
}
void estab_synacksyn(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state ESTAB, received event SYN/SYNACK\n", fd);
DEBUG_DET("In %s()\n", __FUNCTION__);
/* GSMPv3 $11, Note 2: ACK Throttle */
if (state->ack_cnt < 2)
send_ACK(fd, msgin, state);
state->curr_state = ESTAB;
}
void rstack(int fd, unsigned char *msgin, ancp_state_t *state )
{
DEBUG_FSM("FSM: %d state %s, received event RSTACK\n", fd,
state_str(state->curr_state));
DEBUG_DET("In %s()\n", __FUNCTION__);
if (A(msgin,state) && B(msgin,state) && state->curr_state != SYNSENT)
reset_link(fd, msgin, state);
}
/* Send Functions */
void
send_SYNACK(int fd, unsigned char *m, ancp_state_t *state)
{
unsigned char *p;
int sz;
p = calloc (1, ADJACENCY_MSG_SIZE);
sz = build_adj_message(&p, SYNACK, state);
DEBUG_DET("FSM: SYNACK message built - %d bytes\n", sz);
DEBUG_PAK("Sending:\n");
dump_hex(p, sz);
if (send(fd, p, sz, 0) == -1) {
perror("send_SYNACK");
} else {
DEBUG_FSM("FSM: %d SYNACK sent\n", fd);
state->synack_cnt++;
}
free(p);
}
void send_SYN(int fd, unsigned char *m, ancp_state_t *state)
{
unsigned char *p;
int sz;
p = calloc (1, ADJACENCY_MSG_SIZE);
sz = build_adj_message(&p, SYN, state);
DEBUG_DET("FSM: SYN message built - %d bytes\n", sz);
DEBUG_PAK("Sending:\n");
dump_hex(p, sz);
if (send(fd, p, sz, 0) == -1) {
perror("send_SYN");
} else {
DEBUG_FSM("FSM: %d SYN sent\n", fd);
state->syn_cnt++;
}
free(p);
}
void
send_RSTACK(int fd, unsigned char *m, ancp_state_t *state)
{
unsigned char *p;
int sz;
p = calloc (1, ADJACENCY_MSG_SIZE);
sz = build_adj_message(&p, RSTACK, state);
DEBUG_DET("FSM: RSTACK message built - %d bytes\n", sz);
DEBUG_PAK("Sending:\n");
dump_hex(p, sz);
if (send(fd, p, sz, 0) == -1)
perror("send_RSTACK");
else
DEBUG_FSM("FSM: %d RSTACK sent\n", fd);
free(p);
}
void send_ACK(int fd, unsigned char *m, ancp_state_t *state)
{
unsigned char *p;
int sz;
p = calloc (1, ADJACENCY_MSG_SIZE);
sz = build_adj_message(&p, ACK, state);
DEBUG_DET("FSM: ACK message built - %d bytes\n", sz);
DEBUG_PAK("Sending:\n");
dump_hex(p, sz);
if (send(fd, p, sz, 0) == -1) {
perror("send_ACK");
} else {
DEBUG_FSM("FSM: %d ACK sent\n", fd);
state->ack_cnt ++;
}
free(p);
}
/* move the state machine on fd, for event */
void
fsm_init (int fd, conn_t *conn)
{
DEBUG_DET("In %s()\n", __FUNCTION__);
ancp_fsm(fd, NULL, 0, conn, NULL);
}
void
ancp_fsm (int fd, unsigned char *msgin, int n, conn_t *conn,
void (*ack_cbk) (int, conn_t*))
{
adj_code_t event;
ancp_state_t *state = conn->state;
DEBUG_DET("In %s()\n", __FUNCTION__);
event = UNKNOWN;
if (msgin) {
// TODO check if this is a adjacency message, only then decode
// this is the first point of message decode - so only if state
// is ESTAB, then decode other messages, else discard
decode_adj_message(msgin, n, state);
event = get_adj_mtype(msgin);
// TODO can this be done in the decode_adj_message? i.e, return event,
// and update the dead_timer as well
}
if (event == ACK && ack_cbk) {
/*
* Received an ack - call the process handler to do its job
* like updating keepalive timer etc
*/
ack_cbk(fd, conn);
}
/* use state table */
if( ancp_fsm_tbl[state->curr_state][event]) {
ancp_fsm_tbl[state->curr_state][event](fd, msgin, state);
DEBUG_DET("After FSM move, FD %d state:\n", fd);
} else {
DEBUG_ERR("FD %d, for event %s cannot go to NULL state\n",
fd, adj_code_str(event));
}
print_state(state);
}