-
Notifications
You must be signed in to change notification settings - Fork 4
/
io.c
218 lines (175 loc) · 6.86 KB
/
io.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
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 "io.h"
/***********************************************************************
* Static variables for the input/output files
***********************************************************************/
static FILE *routingTable;
static FILE *inputFile;
static FILE *outputFile;
/********************************************************************
* Initalize file descriptors
*
* routingTableName contains FIB info (argv[1] of main function)
* inputFileName contains IP addresses (argv[2] of main function)
*
***********************************************************************/
int initializeIO(char *routingTableName, char *inputFileName){
char outputFileName[100];
routingTable = fopen(routingTableName, "r");
if (routingTable == NULL) return ROUTING_TABLE_NOT_FOUND;
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
fclose(routingTable);
return INPUT_FILE_NOT_FOUND;
}
sprintf(outputFileName, "%s%s", inputFileName, OUTPUT_NAME);
outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
fclose(routingTable);
fclose(inputFile);
return CANNOT_CREATE_OUTPUT;
}
return OK;
}
/***********************************************************************
* Close the input/output files
***********************************************************************/
void freeIO() {
fclose(inputFile);
fclose(outputFile);
fclose(routingTable);
}
/***********************************************************************
* Write explanation for error identifier (verbose mode)
***********************************************************************/
void printIOExplanationError(int result){
switch(result) {
case ROUTING_TABLE_NOT_FOUND:
printf("Routing table not found\n");
exit(0);
case INPUT_FILE_NOT_FOUND:
printf("Input file not found\n");
exit(0);
case BAD_ROUTING_TABLE:
printf("Bad routing table structure\n");
exit(0);
case BAD_INPUT_FILE:
printf("Bad input file structure\n");
exit(0);
case PARSE_ERROR:
printf("Parse error\n");
exit(0);
case CANNOT_CREATE_OUTPUT:
printf("Cannot create output file\n");
exit(0);
case REACHED_EOF:
printf("Reached End Of File\n");
exit(0);
default:
printf("Unknown error\n");
exit(0);
}
exit(0);
}
/***********************************************************************
* Read one entry in the FIB
*
* It should be noted that prefix, prefixLength and outInterface are
* pointers since they are used as output parameters
*
***********************************************************************/
int readFIBLine(uint32_t *prefix, int *prefixLength, int *outInterface){
int n[4], result;
result = fscanf(routingTable, "%i.%i.%i.%i/%i\t%i\n", &n[0], &n[1], &n[2], &n[3], prefixLength, outInterface);
if (result == EOF) return REACHED_EOF;
else if (result != 6) return BAD_ROUTING_TABLE;
else{
//remember that pentium architecture is little endian
*prefix = (n[0]<<24) + (n[1]<<16) + (n[2]<<8) + n[3];
//*prefix = n[0]*pow(2,24) + n[1]*pow(2,16) + n[2]*pow(2,8) + n[3];
return OK;
}
}
/***********************************************************************
* Read one entry in the input packet file
*
* Again, it should be noted that IPAddress is a pointer since it is used
* as output parameter
*
***********************************************************************/
int readInputPacketFileLine(uint32_t *IPAddress){
int n[4], result;
result = fscanf(inputFile, "%i.%i.%i.%i\n", &n[0], &n[1], &n[2], &n[3]);
if (result == EOF) return REACHED_EOF;
else if (result != 4) return BAD_INPUT_FILE;
else{
//remember that pentium architecture is little endian
*IPAddress = (n[0]<<24) + (n[1]<<16) + (n[2]<<8) + n[3];
//*IPAddress = n[0]*pow(2,24) + n[1]*pow(2,16) + n[2]*pow(2,8) + n[3];
return OK;
}
}
/***********************************************************************
* Print a line to the output file
*
* gettimeofday(&initialTime, NULL) must be called right before the lookup function
*
* gettimeofday(&finalTime, NULL) must be called right after the lookup function
*
* The lookup function must return (either as output parameter or as return value)
* the number of hash tables that have been accessed for every IP address
*
***********************************************************************/
void printOutputLine(uint32_t IPAddress, int outInterface, struct timeval *initialTime, struct timeval *finalTime,
double *searchingTime, int numberOfHashtables) {
unsigned long sec, usec;
usec = finalTime->tv_usec - initialTime->tv_usec;
if (usec > finalTime->tv_usec) initialTime->tv_sec += 1;
sec = finalTime->tv_sec - initialTime->tv_sec;
*searchingTime = 1000000*sec + usec;
//remember that output interface equals 0 means no matching
//remember that if no matching but default route is specified in the FIB, the default output interface
//must be stored to avoid dropping the packet (i.e., MISS)
if (!outInterface){
fprintf(outputFile,"%i.%i.%i.%i;%s;%i;%.0lf\n",IPAddress >> 24, (IPAddress >> 16) & 0x000000ff, (IPAddress >> 8) & 0x000000ff, IPAddress & 0x000000ff , "MISS",numberOfHashtables, *searchingTime);
}
else{
fprintf(outputFile,"%i.%i.%i.%i;%i;%i;%.0lf\n",IPAddress >> 24, (IPAddress >> 16) & 0x000000ff, (IPAddress >> 8) & 0x000000ff, IPAddress & 0x000000ff , outInterface,numberOfHashtables, *searchingTime);
}
}
/***********************************************************************
* Print execution summary to the output file
*
* It should be noted that:
*
* averageTableAccesses = totalTableAccesses/processedPackets
*
* averagePacketProcessingTime = totalPacketProcessingTime/processedPackets
*
***********************************************************************/
void printSummary(int processedPackets, double averageTableAccesses, double averagePacketProcessingTime){
fprintf(outputFile, "\nPackets processed= %i\n", processedPackets);
fprintf(outputFile, "Average table accesses= %.2lf\n", averageTableAccesses);
fprintf(outputFile,"Average packet processing time (usecs)= %.2lf\n", averagePacketProcessingTime);
printMemoryTimeUsage();
}
/***********************************************************************
* Print memory and CPU time
*
* For more info: man getrusage
*
***********************************************************************/
void printMemoryTimeUsage(){
float user_time, system_time;
long int memory;
struct rusage usage;
if (getrusage (RUSAGE_SELF, &usage)){
printf("Resource measurement failed.\n");
}
else{
user_time = (float)usage.ru_utime.tv_sec+(float)usage.ru_utime.tv_usec/1000000;
system_time = (float)usage.ru_stime.tv_sec+(float)usage.ru_stime.tv_usec/1000000;
memory = usage.ru_maxrss;
fprintf(outputFile, "Memory (Kbytes) = %ld\n", memory );
fprintf(outputFile, "CPU Time (secs)= %.6f\n\n", user_time+system_time);
}
}