-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_TCP_state.c
207 lines (178 loc) · 4.92 KB
/
get_TCP_state.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
#include<stdio.h>
#include <stddef.h>
enum event {
APP_PASSIVE_OPEN, APP_ACTIVE_OPEN, APP_SEND, APP_CLOSE,
APP_TIMEOUT, RCV_SYN, RCV_ACK, RCV_SYN_ACK, RCV_FIN, RCV_FIN_ACK
};
enum state {
ERROR, CLOSED, LISTEN, SYN_SENT, SYN_RCVD, ESTABLISHED,
CLOSE_WAIT, LAST_ACK, FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT,
};
typedef struct state_evt_node
{
enum state current_state;
enum event coming_evt;
enum state next_state;
}t_state_evt_node;
t_state_evt_node state_evt_map[]={
// closed
{
.current_state = CLOSED,
.coming_evt=APP_PASSIVE_OPEN,
.next_state=LISTEN,
},
{
.current_state = CLOSED,
.coming_evt=APP_ACTIVE_OPEN ,
.next_state=SYN_SENT,
},
// Listen
{
.current_state = LISTEN,
.coming_evt=RCV_SYN ,
.next_state=SYN_RCVD,
},
{
.current_state = LISTEN,
.coming_evt=APP_SEND ,
.next_state=SYN_SENT,
},
{
.current_state = LISTEN,
.coming_evt=APP_CLOSE ,
.next_state=CLOSED,
},
// SYN_RCVD
{
.current_state = SYN_RCVD,
.coming_evt=APP_CLOSE ,
.next_state=FIN_WAIT_1,
},
{
.current_state = SYN_RCVD,
.coming_evt=RCV_ACK ,
.next_state=ESTABLISHED,
},
//SYN_SNED
{
.current_state = SYN_SENT,
.coming_evt=RCV_SYN ,
.next_state=SYN_RCVD,
},
{
.current_state = SYN_SENT,
.coming_evt=RCV_SYN_ACK ,
.next_state=ESTABLISHED ,
},
{
.current_state = SYN_SENT,
.coming_evt=APP_CLOSE ,
.next_state=CLOSED,
},
//ESTABLISHED
{
.current_state = ESTABLISHED,
.coming_evt=APP_CLOSE ,
.next_state=FIN_WAIT_1,
},
{
.current_state = ESTABLISHED,
.coming_evt=RCV_FIN ,
.next_state=CLOSE_WAIT,
},
//FIN_WAIT_1
{
.current_state = FIN_WAIT_1,
.coming_evt=RCV_FIN ,
.next_state=CLOSING,
},
{
.current_state = FIN_WAIT_1,
.coming_evt=RCV_FIN_ACK ,
.next_state=TIME_WAIT,
},
{
.current_state = FIN_WAIT_1,
.coming_evt=RCV_ACK ,
.next_state=FIN_WAIT_2,
},
//CLOSING
{
.current_state = CLOSING,
.coming_evt=RCV_ACK ,
.next_state=TIME_WAIT,
},
//FIN_WAIT_2
{
.current_state = FIN_WAIT_2,
.coming_evt=RCV_FIN ,
.next_state=TIME_WAIT,
},
//TIME_WAIT
{
.current_state = TIME_WAIT,
.coming_evt=APP_TIMEOUT ,
.next_state=CLOSED,
},
//CLOSE_WAIT
{
.current_state = CLOSE_WAIT,
.coming_evt=APP_CLOSE ,
.next_state=LAST_ACK,
},
//LAST_ACK
{
.current_state = LAST_ACK,
.coming_evt=RCV_ACK ,
.next_state=CLOSED,
},
};
enum state get_TCP_state (size_t n, const enum event events[n])
{
//initial state
// for(int x =0; x< sizeof(state_evt_map)/sizeof(t_state_evt_node);x++)
// {
// state_evt_map[x].current_state == CLOSED
// }
enum state next_state;
if( state_evt_map[0].coming_evt == events[0])
{
next_state = state_evt_map[0].next_state;
}
else if(state_evt_map[1].coming_evt == events[0])
{
next_state = state_evt_map[1].next_state;
/* code */
}else
{
return ERROR;
}
for(int y=0, x =1; x<n; x++)
{
for( y =0; y< sizeof(state_evt_map)/sizeof(t_state_evt_node); y++)
{
if((state_evt_map[y].current_state == next_state)&&
(state_evt_map[y].coming_evt == events[x]))
{
next_state = state_evt_map[y].next_state;
break;
}
}
if(sizeof(state_evt_map)/sizeof(t_state_evt_node) == y)
{
printf("x =%d, next_state=%d\r\n", x,next_state);
return ERROR;
}
}
return next_state;
}
const enum event event1[]={1,3,7,5,0,7};
int main()
{
// for(int x = 0; x< sizeof(event1)/sizeof(event1[0]);x++)
// printf(" event: %d\r\n",event1[x]);
// printf(" sizeof(event1)= %d\r\n",sizeof(event1)/sizeof(int));
// printf("hello world");
printf("STATE =%d\r\n",get_TCP_state(6,event1));
return 0;
}