-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcpflowstat.cpp
259 lines (214 loc) · 8.81 KB
/
tcpflowstat.cpp
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
#include "tcpflowstat.h"
TCPFlowStat::TCPFlowStat(){
tcpconnstate=0;
simulsyn=0;
simulsynackstate=SIMUL_SYNACK_NOT_RECEIVED;
tcpconnsetuptime=0;
cltretxbytes=0; svrretxbytes=0;
};
int TCPFlowStat::getPacketDirection(u_int srcip, u_int dstip, u_short srcport, u_short dstport){
if (cltip==srcip && svrip==dstip && cltport==srcport && svrport==dstport) return PKTSENDER_CLT;
if (cltip==dstip && svrip==srcip && cltport==dstport && svrport==srcport) return PKTSENDER_SVR;
return -1;
}
int TCPFlowStat::isNewFlow(struct ip* ip, struct tcphdr* tcphdr){
if (tcphdr->syn==1 && tcphdr->ack!=1) return 1;
return 0;
}
int TCPFlowStat::isMyPacket(struct ip* ip, struct tcphdr* tcphdr) {
if (!(cltip==ip->ip_src.s_addr && svrip==ip->ip_dst.s_addr \
&& cltport==tcphdr->source && svrport==tcphdr->dest) \
&&
!(cltip==ip->ip_dst.s_addr && svrip==ip->ip_src.s_addr \
&& cltport==tcphdr->dest && svrport==tcphdr->source)) return 0;
int pktdir=getPacketDirection(ip->ip_src.s_addr, ip->ip_dst.s_addr, tcphdr->source, tcphdr->dest);
/* if (tcphdr->syn==1 && )
return 0;
if (tcphdr->syn==1 && tcphdr->ack==1 \
&& !(tcpconnstate==TCPCONSTATE_SYN_SEND && pktsdr==PKTSENDER_)){
return 0;
}*/
if (tcphdr->syn==1 && tcphdr->ack!=1 \
&& !(tcpconnstate==TCPCONSTATE_SYN_SEND && pktdir==PKTSENDER_SVR)) {
return 0;
}
if (tcpconnstate==TCPCONSTATE_FIN){
return 0;
}
return 1;
}
void TCPFlowStat::swapcltsvr(){
//only swap those updated before TCPCONSTATE_ESTABLISHED
u_int ti=cltip; cltip=svrip; svrip=ti;
u_short ts=cltport; cltport=svrport; svrport=ts;
ti=cltseq; cltseq=svrseq; svrseq=ti;
ti=cltackseq; cltackseq=svrackseq; svrackseq=ti;
ti=cltinitseq; cltinitseq=svrinitseq; svrinitseq=ti;
};
char * TCPFlowStat::getStrAddr(u_int ip){
char* res=(char*)malloc(100);
sprintf(res, "%d.%d.%d.%d", ip>>24,(ip>>16)&0xFF,(ip>>8)&0xFF,(ip)&0xFF);
return res;
}
void TCPFlowStat::printStat(){
//if (1 && strcmp(getStrAddr(cltip),"33.161.36.29")==0 && strcmp(getStrAddr(svrip),"10.170.231.69")==0)
printf("cltip:%s svrip:%s cltport:%d svrport:%d cltseq:%u cltackseq:%u svrseq:%u svrackseq:%u\n",\
getStrAddr(cltip),getStrAddr(svrip), cltport,svrport,cltseq,cltackseq,svrseq,svrackseq);
}
void TCPFlowStat::addPacket(struct ip* ip, struct tcphdr* tcphdr, double ts){
if (!isNewFlow(ip,tcphdr) && isMyPacket(ip, tcphdr)!=1) return;
int pktdir=getPacketDirection(ip->ip_src.s_addr, ip->ip_dst.s_addr, tcphdr->source, tcphdr->dest);
int tcpdatalen=ip->ip_len-ip->ip_hl*4-tcphdr->doff*4;
//printf ("TCPDataLen:%d\n",tcpdatalen);
//printStat();
switch (tcpconnstate){
case TCPCONSTATE_CLOSED: {
if (tcphdr->syn==1 && tcphdr->ack!=1){
syntime=ts;
cltip=ip->ip_src.s_addr;
svrip=ip->ip_dst.s_addr;
cltport=tcphdr->source;
svrport=tcphdr->dest;
cltseq=tcphdr->seq;
svrackseq=tcphdr->seq+1; cltinitseq=tcphdr->seq+1;
flowinitby=FLOWINITBYCLT;
printf ("CLOSED:");
printStat();
tcpconnstate=TCPCONSTATE_SYN_SEND;
}
else {
printf("Unknown TCP packet.\n");
};
};break;
case TCPCONSTATE_SYN_SEND:{
if (tcphdr->syn==1 && tcphdr->ack==1 && tcphdr->ack_seq==svrackseq){
synacktime=ts;
syntosynacktime=synacktime-syntime;
svrseq=tcphdr->seq;
cltackseq=tcphdr->seq+1; svrinitseq=tcphdr->seq+1;
cltseq=tcphdr->ack_seq;
tcpconnstate=TCPCONSTATE_SYN_RECEIVED;
printf ("SYN_SEND1:");
printStat();
}
else if (tcphdr->syn==1 && tcphdr->ack!=1 && pktdir==PKTSENDER_SVR){
//simultanous syn sent from both side
simulsyn=1;
svrseq=tcphdr->seq;
cltackseq=tcphdr->seq+1; svrinitseq=tcphdr->seq+1;
tcpconnstate=TCPCONSTATE_SYN_RECEIVED;
printf ("SYN_SEND2:");
printStat();
}
else{
printf("Unknown TCP packet.\n");
};
};break;
case TCPCONSTATE_SYN_RECEIVED: {
if (tcphdr->syn!=1 &&tcphdr->ack==1 && tcphdr->seq==cltseq && tcphdr->ack_seq==cltackseq){
acktime=ts;
synacktoacktime=acktime-synacktime;
if (synacktoacktime>syntosynacktime){
//the server side is the local device
swapcltsvr();
flowinitby=FLOWINITBYSVR;
}
tcpconnsetuptime=acktime-syntime;
pktdir=getPacketDirection(ip->ip_src.s_addr, ip->ip_dst.s_addr, tcphdr->source, tcphdr->dest);
if (pktdir==PKTSENDER_CLT){
cltseq=tcphdr->seq+tcpdatalen;
cltackseq=tcphdr->ack_seq;
svrseq=tcphdr->ack_seq;
}
if (pktdir==PKTSENDER_SVR){
svrseq=tcphdr->seq+tcpdatalen;
svrackseq=tcphdr->ack_seq;
cltseq=tcphdr->ack_seq;
}
printf ("SYN_RECEIVED1:");
printStat();
tcpconnstate=TCPCONSTATE_ESTABLISHED;
}
else
if (tcphdr->syn==1 && tcphdr->ack==1 && simulsyn==1){
//simultanous syn sent from both side
if (pktdir==PKTSENDER_CLT && tcphdr->seq==cltseq && tcphdr->ack_seq==cltackseq){
if (simulsynackstate==SIMUL_SYNACK_NOT_RECEIVED)
simulsynackstate==SIMUL_SYNACK_NOT_RECEIVED;
if (simulsynackstate==SIMUL_SYNACK_RECEIVED)
tcpconnstate=TCPCONSTATE_ESTABLISHED;
}
if (pktdir==PKTSENDER_SVR && tcphdr->seq==svrseq && tcphdr->ack_seq==svrackseq){
if (simulsynackstate==SIMUL_SYNACK_NOT_RECEIVED)
simulsynackstate==SIMUL_SYNACK_NOT_RECEIVED;
if (simulsynackstate==SIMUL_SYNACK_RECEIVED)
tcpconnstate=TCPCONSTATE_ESTABLISHED;
}
if (tcpconnstate==TCPCONSTATE_ESTABLISHED){
tcpconnsetuptime=ts-syntime;
cltseq=svrackseq;
svrseq=cltackseq;
}
}
else {
printf("Unknown TCP packet.\n");
};
};break;
case TCPCONSTATE_ESTABLISHED: {
if (pktdir==PKTSENDER_CLT){
//calc metrics first
if (tcphdr->seq > cltseq){
//printf("client seq is greater than expected, may be pcap's fault.\n");
}
if (tcphdr->seq < cltseq){
//has re-transmission
int retxb=cltseq-tcphdr->seq;
if (tcpdatalen<retxb)
retxb=tcpdatalen;
cltretxbytes+=retxb;
cltretxnum+=1;
//printf("client retx %d bytes.\n", retxb);
};
//the last thing: update seq
if (tcphdr->seq+tcpdatalen > cltseq) {
cltseq=tcphdr->seq+tcpdatalen;
}
if (tcphdr->ack_seq >= cltackseq) {
cltackseq=tcphdr->ack_seq;
};
printf ("ESTABLISHED1:");
printStat();
};
if (pktdir==PKTSENDER_SVR){
if (tcphdr->seq > svrseq){
//printf("svr seq is greater than expected, some server data are delayed or lost.\n");
}
if (tcphdr->seq < svrseq) {
int retxb=svrseq-tcphdr->seq;
if (tcpdatalen<retxb)
retxb=tcpdatalen;
svrretxbytes+=retxb;
svrretxnum+=1;
//printf("server retx %d bytes.\n", retxb);
};
//the last thing: update seq
if (tcphdr->seq+tcpdatalen > svrseq) {
svrseq=tcphdr->seq+tcpdatalen;
}
if (tcphdr->ack_seq >= svrackseq) {
svrackseq=tcphdr->ack_seq;
};
printf ("SYN_RECEIVED2:");
printStat();
};
if (tcphdr->fin==1 || tcphdr->rst==1){
tcpconnstate=TCPCONSTATE_FIN;
}
};break;
case TCPCONSTATE_FIN:{
};break;
default: {
printf("Unknown TCP connection state.\n");
};break;
};
}