-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.cpp
218 lines (197 loc) · 8.23 KB
/
tools.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
#include "tools.h"
QString FormatData(unsigned char* data , int Size)
{
QString res;
for(int i=0 ; i < Size ; i++)
{
if( i!=0 && i%16==0) //if one line of hex printing is complete...
{
//res += " ";
for(int j=i-16 ; j<i ; j++)
{
if(data[j]>=32 && data[j]<=128)
res += (unsigned char)data[j]; //if its a number or alphabet
else res += "."; //otherwise print a dot
}
res += "\n";
// fprintf(logfile,"\n");
}
if( i==Size-1) //print the last spaces
{
for(int j=0;j<15-i%16;j++)
res += " "; //extra spaces
res += " ";
for(int j=i-i%16 ; j<=i ; j++)
{
if(data[j]>=32 && data[j]<=128)
res += (unsigned char)data[j];
else
res += ".";
}
res += "\n";
}
}
return res;
}
QString FormatHexData (unsigned char* data , int Size)
{
QString res;
for(int i=0 ; i < Size ; i++)
{
if(i%16==0)
res += "\n";
res += " ";
res += QString::fromStdString( NumberToHexString((unsigned int)data[i]));
}
return res;
}
QString FormatHeaderData(paquet *pkt)
{
QString data;
if (pkt->ether_offset == 0)
data += FormatIpv6Header(pkt);
else if (pkt->ether_offset == 14 || pkt->ether_offset == 18)
data += FormatIpHeader(pkt);
else if (pkt->ether_offset == -2)
data += FormatArpHeader(pkt);
if (pkt->type == "tcp")
data += FormatTcpHeader(pkt);
else if (pkt->type == "udp")
data += FormatUdpHeader(pkt);
else if (pkt->type == "icmp")
data += FormatIcmpHeader(pkt);
return data;
}
QString FormatIpHeader(paquet *pkt)
{
QString data;
data += "IP HEADER\n";
data += QString::fromLatin1("|-IP Version : ") +
QString::number((unsigned int)pkt->ip_hdr->version) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP Header Lenght DWORDS : ") +
QString::number((unsigned int)pkt->ip_hdr->ihl) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP Header Lenght Bytes : ") +
QString::number((unsigned int)pkt->ip_hdr->ihl * 4) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP Type of Service : ") +
QString::number((unsigned int)pkt->ip_hdr->tos) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP Total lenght : ") +
QString::number(ntohs(pkt->ip_hdr->tot_len)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP identification : ") +
QString::number(ntohs(pkt->ip_hdr->id)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP Time To Live : ") +
QString::number((unsigned int)pkt->ip_hdr->ttl) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP protocol : ") +
QString::number((unsigned int)pkt->ip_hdr->protocol) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP checksum : ") +
QString::number(ntohs(pkt->ip_hdr->check)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP source : ") +
QString::fromStdString(pkt->source) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-IP destination : ") +
QString::fromStdString(pkt->destination) +
QString::fromLatin1("\n");
return data;
}
QString FormatIpv6Header(paquet *pkt)
{
QString data;
return data;
}
QString FormatArpHeader(paquet *pkt)
{
QString data;
return data;
}
QString FormatTcpHeader(paquet *pkt)
{
QString data;
data += "TCP HEADER\n";
data += QString::fromLatin1("|-TCP Source Port : ") +
QString::fromStdString(pkt->sourcePort) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Destination Port : ") +
QString::fromStdString(pkt->destinationPort) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Sequence Number : ") +
QString::number(ntohl(pkt->tcp_hdr->seq)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Acknowledge Number : ") +
QString::number(ntohl(pkt->tcp_hdr->ack_seq)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Urgent Flag : ") +
QString::number((unsigned int)pkt->tcp_hdr->urg) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Acknowledgment Flag : ") +
QString::number(ntohs(pkt->tcp_hdr->ack)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Push Flag : ") +
QString::number(ntohs(pkt->tcp_hdr->psh)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Reset Flag : ") +
QString::number(ntohs(pkt->tcp_hdr->rst)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Synchronize Flag : ") +
QString::number(ntohs(pkt->tcp_hdr->syn)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Finish Flag : ") +
QString::number(ntohs(pkt->tcp_hdr->fin)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Window : ") +
QString::number(ntohs(pkt->tcp_hdr->window)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Checksum : ") +
QString::number(ntohs(pkt->tcp_hdr->check)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-TCP Urgent pointer : ") +
QString::number(ntohs(pkt->tcp_hdr->urg_ptr)) +
QString::fromLatin1("\n");
return data;
}
QString FormatUdpHeader(paquet *pkt)
{
QString data;
data += "UDP HEADER\n";
data += QString::fromLatin1("|-UDP Source Port : ") +
QString::fromStdString(pkt->sourcePort) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-UDP Destination Port : ") +
QString::fromStdString(pkt->destinationPort) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-UDP Lenght : ") +
QString::number(ntohs(pkt->udp_hdr->len)) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-UDP Checksum : ") +
QString::number(ntohs(pkt->udp_hdr->check)) +
QString::fromLatin1("\n");
return data;
}
QString FormatIcmpHeader(paquet *pkt)
{
QString data;
data += "ICMP HEADER\n";
data += QString::fromLatin1("|-ICMP Type : ") +
QString::number((unsigned int)pkt->icmp_hdr->type) +
QString::fromLatin1("\n");
if (pkt->icmp_hdr->type == ICMP_ECHO)
data += QString::fromLatin1(" (ICMP Echo)\n");
else if (pkt->icmp_hdr->type == ICMP_ECHOREPLY)
data += QString::fromLatin1(" (ICMP Echo reply)\n");
else if (pkt->icmp_hdr->type == ICMP_TIME_EXCEEDED)
data += QString::fromLatin1(" (ICMP Expired)\n");
data += QString::fromLatin1("|-ICMP Code : ") +
QString::number((unsigned int)pkt->icmp_hdr->code) +
QString::fromLatin1("\n");
data += QString::fromLatin1("|-ICMP Checksum : ") +
QString::number(ntohs(pkt->icmp_hdr->checksum)) +
QString::fromLatin1("\n");
return data;
}