-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.c
168 lines (146 loc) · 4.35 KB
/
helper.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
#include "helper.h"
#include <netinet/tcp.h> //Provides declarations for tcp header
#include <netinet/ip.h> //Provides declarations for ip header
#define UDP_HEADER_SIZE 8
int has_fin_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_FIN )
return 1;
else
return 0;
}
int has_syn_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_SYN )
return 1;
else
return 0;
}
int has_rst_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_RST )
return 1;
else
return 0;
}
int has_push_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_PUSH )
return 1;
else
return 0;
}
int has_ack_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_ACK )
return 1;
else
return 0;
}
int has_urg_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_URG )
return 1;
else
return 0;
}
/*
int has_ece_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_ECE )
return 1;
else
return 0;
}
int has_cwr_flag(const u_char *buffer){
struct iphdr *iph = (struct iphdr*)(buffer);
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
if(tcpheader->th_flags & TH_CWR )
return 1;
else
return 0;
}
*/
int get_payload_len(const u_char *buffer, uint32_t proto)
{
struct iphdr *iph = (struct iphdr*)(buffer);
if (proto==6)
{
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
int tcpdatalen = ntohs(iph->tot_len) - (tcpheader->doff * 4) - (iph->ihl * 4);
return tcpdatalen;
}
else if(proto==17)
{
struct udphdr * udpheader = (struct udphdr*)(buffer + iph->ihl * 4);
int udpdatalen = ntohs(iph->tot_len) - UDP_HEADER_SIZE- (iph->ihl * 4);
return udpdatalen;
}
}
int get_header_len(const u_char *buffer, uint32_t proto)
{
struct iphdr *iph = (struct iphdr*)(buffer);
if (proto==6)
{
struct tcphdr *tcpheader = (struct tcphdr*)(buffer + iph->ihl*4);
return tcpheader->doff*4;
}
else if(proto==17)
{
//size of the UDP header is 8
// ref https://www.winpcap.org/pipermail/winpcap-users/2007-September/002104.html
return 8;
struct udphdr * udpheader = (struct udphdr*)(buffer + iph->ihl * 4);
int udpdatalen = ntohs(iph->tot_len) - (iph->ihl * 4);
return udpdatalen;
}
}
double max(double a, double b)
{
if(a>=b) return a;
else return b;
}
double min(double a, double b)
{
if(a<=b) return a;
else return b;
}
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
// preserve *y
struct timeval yy = *y;
y = &yy;
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
struct timeval get_max(struct timeval a, struct timeval b)
{
long a_ms = a.tv_sec*MCS_IN_SEC + a.tv_usec;
long b_ms = b.tv_sec*MCS_IN_SEC + b.tv_usec;
if(a_ms>b_ms)
return a;
else
return b;
}