-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket_sorting
More file actions
160 lines (115 loc) · 4.55 KB
/
Packet_sorting
File metadata and controls
160 lines (115 loc) · 4.55 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
//I cant define functions yet, so unfortunately everything is in main
//I tried to be as clear as possible anyway
int main()
{
// - 1 -
// "read_packet_file()"
/* Here I'm going to get two variables for later use:
- total_str: The whole file as a single string, an array of all characters
- n_total_chars: The total number of characters */
FILE *file;
file= fopen("sample_packets.txt", "r"); //change sample_packets.txt for the name of your file
if (file == NULL)
{
fprintf(stderr, "Error opening file. %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
//Calculate the total number of chars in the file
int n_total_chars= 1; //+1 for null char
char c;
while ((c = fgetc(file)) != EOF) //reads the chars til end of file
{
n_total_chars++;
}
/* I had to close and open the file to use the same loop again
in order to set all the chars to a "string" variable
(I guess there is a much better way to do this) */
fclose(file);
file= fopen("sample_packets.txt", "r");
char total_str[n_total_chars]; //string size will be the number of chars in the file
int i=0;
while ((c = fgetc(file)) != EOF) //store chars in total_str
{
total_str[i]=c;
i++;
}
//I dont need the file anymore, so I close it
fclose(file);
// - 2 -
// "parse_mac_dst()"
/* Using the char index, here is a loop to append the mac dst addresses
into a two dimensional array called:
- mac_array
I has n_lines mac addresses (1 per line) and each string is
the size of the mac address length */
//(Doesnt actually work as expected)
char sample_line[]="00:00:5E:00:53:AF_2C-54-91-88-C9-E3_192.158.1.38_192.158.1.41_IPV4";
char sample_mac[]="2C-54-91-88-C9-E3";
// Read sample lines to calculate the size of future strings and index
int n_c_line=strlen(sample_line)+1; //number of chars per line (+ null char)
int n_c_mac=strlen(sample_mac)+1; // number of chars in destination mac address
int n_lines= n_total_chars / n_c_line; //number of lines
char mac_array[n_lines][n_c_mac]; //array of mac strings
int mac, j, total_index;
int i_start=18; //char index of mac address, where the first one starts
//I cant figure out why mac_array[any index] sometimes is twice the size it should be
for (mac=0; mac < n_lines; mac++) //number of addresses
{
for (j=0; j < n_c_mac-1; j++) //loop over each address
{
total_index= i_start+(mac*n_c_line)+j; //index of the addresses chars
mac_array[mac][j]=total_str[total_index];
}
}
// - 3 -
//"filter_mac_list()"
/* Create new 2d array:
- filtered_array
It iterates over the strings in mac_array and skips the ones that have
duplicates in a later index and saves the ones that dont have duplicates */
char filtered_array[n_lines][18]; // n_c_mac= 18
int a, f, isfalse;
int f_index=0; // Index of the filtered array
for (f=0; f < n_lines; f++) // Index of the unfiltered array
{
for (a=1; a < n_lines; a++)
{
if (f==n_lines-1) // In case it is the last string, so we end loop
{
strcpy(filtered_array[f_index], mac_array[f]);
break;
}
else
{
isfalse=strcmp(mac_array[f], mac_array[f+a]); // 0 If equal
if (isfalse==0)
break; /* skip string if it has any duplicate in a later index,
it will not be included in the filtered list */
else if (a!=n_lines-1)
continue; //Check next index if we hadnt checked the last one yet
else
strcpy(filtered_array[f_index],mac_array[f]);
f_index++;
/* Store actual string in filtered_array, because it is different to all in the
later index and increment index for the filtered_array */
}
}
}
// - 4-
//"print_mac_list()"
/* Display filtered list of mac addresses */
printf("Filtered destination mac addresses:\n\n%s", "[");
int h=0;
for (h=0; h < f_index; h++)
{
if (h==f_index-1)
printf("%s]\n", filtered_array[h]);
else
printf("%s,\n\n", filtered_array[h]);
}
return 0;
}