-
Notifications
You must be signed in to change notification settings - Fork 3
/
Search.cpp
150 lines (107 loc) · 2.47 KB
/
Search.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
#include <cstring>
void KMPtable( char pattern[], int *f) ///suffix - prefix table ..
{
int m = strlen(pattern);
int k;
f[0] = -1;
for (int i = 1; i < m; i++)
{
k = f[i - 1];
while (k >= 0)
{
if (pattern[k] == pattern[i - 1])
break;
else
k = f[k];
}
f[i] = k + 1;
}
}
bool KMP(char pattern[], char text[])
{
int m = strlen(pattern);
int n = strlen(text);
int f[m];
KMPtable(pattern, f);
int i = 0; //point the position of text
int k = 0; //point the position of pattern
while (i < n)
{
if (k == -1)
{
i++;
k = 0;
}
else if (text[i] == pattern[k])
{
k++;
i++;
if (k == m)
return true;
}
else
k = f[k];
}
return false;
}
///////////text Searching
void searchByText(char txtForSearch[],int numbersOfPackets)
{
int i=0;
bool isfound = false;
printToConsole();
while(i<numbersOfPackets)
{
if(KMP(txtForSearch,readableData[i]))
{
printBasic(i+1,i+1); //it takes 1st parameter as a packet number, but here i pass indecies , so +1,
printf("Packet Details:\n");
cout<<COLOR_GREEN;
printDetails(i+1,i+1);
cout<<COLOR_RESET;
isfound = true;
}
i++;
}if(!isfound) printf("No text Found sorry\n");
}
///////////ip searching...
bool isMatchSearchingIP(unsigned char ip[],unsigned char ipForMatch1[],unsigned char ipForMatch2[])
{
if(!isMatchIP(ip,ipForMatch1))
{
if(!isMatchIP(ip,ipForMatch2))
{
return false;
}
else return true;
}else return true;
}
void searchByIP(char ipForSearch[],int numbersOfPackets)
{
char dot;
unsigned char ip[4];
unsigned int tempIp[4];
sscanf(ipForSearch, "%d%c%d%c%d%c%d",&tempIp[0],&dot,&tempIp[1],&dot,&tempIp[2],&dot,&tempIp[3]);
for(int i=0;i<4;i++)
{
ip[i] = (unsigned char)tempIp[i]; //converting the ip to unsigned char
}
int i=0;
bool isfound = false ;
printToConsole();
while(i<numbersOfPackets)
{
if(isMatchSearchingIP(ip,sourceIpAddress[i],destinationIpAddress[i]))
{
printf("Packet Basic:\n");
printToConsole();
printBasic(i+1,i+1); //it takes 1st parameter as a packet number, but here i pass indecies , so +1,
printf("Packet Details:\n");
cout<<COLOR_GREEN;
printDetails(i+1,i+1);
cout<<COLOR_RESET;
isfound = true;
}
i++;
}if(!isfound) printf("No ip Found sorry\n");
}