-
Notifications
You must be signed in to change notification settings - Fork 1
/
firewall.c
243 lines (196 loc) · 6.14 KB
/
firewall.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/// \file firewall.c
/// \brief Reads IP packets from a named pipe, examines each packet,
/// and writes allowed packets to an output named pipe.
/// Author: Chris Dickens (RIT CS)
///
///
/// This file contains proprietary information. Distribution is limited
/// to Rochester Institute of Technology faculty, students currently enrolled
/// in CSCI243: The Mechanics of Programming, graders, and student lab
/// instructors. Further distribution requires written approval from the
/// Rochester Institute of Technology Computer Science department. The
/// content of this file is protected as an unpublished work.
///
/// Copyright 2015 Rochester Institute of Technology
///
///
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "filter.h"
#define MAX_PACKET_LENGTH 2500
/// Type used to control the mode of the firewall
typedef enum FilterMode_e
{
MODE_BLOCK_ALL = 1,
MODE_ALLOW_ALL = 2,
MODE_FILTER = 3
} FilterMode;
/// The input named pipe, "ToFirewall"
static FILE* InPipe = NULL;
/// The output named pipe, "FromFirewall"
static FILE* OutPipe = NULL;
/// Controls the mode of the firewall
volatile FilterMode Mode = MODE_FILTER;
/// The main function that performs the actual packet read, filter, and write.
/// The return value and parameter must match those expected by pthread_create.
/// @param args A pointer to a filter
/// @return Always NULL
static void* FilterThread(void* args);
/// Displays the menu of commands that the user can choose from.
static void DisplayMenu(void);
/// Opens the input and output named files.
/// @return True if successful
static bool OpenPipes(void);
/// Reads a packet from the input name pipe.
/// @param buf Destination buffer to write the packet into
/// @param bufLength The length of the supplied destination buffer
/// @param len The length of the packet
/// @return True if successful
static bool ReadPacket(unsigned char* buf, int* charsRead, int* len);
/// The main function. Creates a filter, configures it, launches the
/// filtering thread, handles user input, and cleans up resources when
/// exiting. The intention is to run this program with a command line
/// argument specifying the configuration file to use.
/// @param argc Number of command line arguments
/// @param argv Command line arguments
/// @return EXIT_SUCCESS or EXIT_FAILURE
int main(int argc, char* argv[])
{
// TODO: implement function
char input[20];
short mode;
pthread_t filterThread;
int rc;
if (argc != 2){
fprintf(stderr, "usage: firewall <config_file>\n");
return EXIT_SUCCESS;
}
IpPktFilter filter = CreateFilter();
if (!ConfigureFilter(filter, argv[1])){
return EXIT_FAILURE;
}
rc = pthread_create(&filterThread, NULL, FilterThread, (void*) filter);
if (rc){
fprintf(stderr, "Could not create filter thread");
return EXIT_FAILURE;
}
DisplayMenu();
while(Mode){
printf("> ");
char* s = fgets(input, 20 , stdin);
if (s){};
mode = ((short) input[0]) -48;
if (mode == MODE_BLOCK_ALL){
Mode = MODE_BLOCK_ALL;
}
else if (mode == MODE_ALLOW_ALL){
Mode = MODE_ALLOW_ALL;
}
else if (mode == MODE_FILTER){
Mode = MODE_FILTER;
}
else if (mode == 0){
Mode = 0;
pthread_join(filterThread, NULL);
break;
}
}
DestroyFilter(filter);
return EXIT_SUCCESS;
}
/// Runs as a thread and handles each packet. It is responsible
/// for reading each packet in its entirety from the input pipe,
/// filtering it, and then writing it to the output pipe. The
/// single void* parameter matches what is expected by pthread.
/// @param args An IpPktFilter
/// @return Always NULL
static void* FilterThread(void* args)
{
// TODO: implement function
IpPktFilter filter = (IpPktFilter) args;
unsigned char buffer[MAX_PACKET_LENGTH];
int pktLength;
int charsRead;
bool isFinished = true;
bool isSuccess;
OpenPipes();
while(Mode){
if (feof(InPipe)==0){
if (isFinished ){
isSuccess = fread(&pktLength, sizeof(int), 1, InPipe);
if (isSuccess){};
}
isFinished = ReadPacket(buffer, &charsRead, &pktLength);
bool isBlocked = false;
if (Mode == MODE_BLOCK_ALL){
isBlocked = true;
}
else if (Mode == MODE_ALLOW_ALL){
isBlocked = false;
}
else if (Mode == MODE_FILTER){
isBlocked = ! FilterPacket(filter, buffer);
}
else if (Mode == 0){
break;
}
if (!isBlocked){
fwrite(&pktLength, sizeof(int), 1, OutPipe);
fwrite(buffer, sizeof(unsigned char), pktLength, OutPipe);
fflush(OutPipe);
}
pktLength -= charsRead;
}
}
fclose(InPipe);
fclose(OutPipe);
return NULL;
}
/// Print a menu and a prompt to stdout
static void DisplayMenu(void)
{
printf("\n1. Block All\n");
printf("2. Allow All\n");
printf("3. Filter\n");
printf("0. Exit\n");
}
/// Open the input and output named pipes that are used for reading
/// and writing packets.
/// @return True if successful
static bool OpenPipes(void)
{
InPipe = fopen("ToFirewall", "rb");
if(InPipe == NULL)
{
perror("ERROR, failed to open pipe ToFirewall:");
return false;
}
OutPipe = fopen("FromFirewall", "wb");
if(OutPipe == NULL)
{
perror("ERROR, failed to open pipe FromFirewall:");
return false;
}
return true;
}
/// Read an entire IP packet from the input pipe
/// @param buf Destination buffer for storing the packet
/// @param charsRead The number of chars read into the buffer
/// @param len The length of the packet
/// @return True if a packet was successfully read
static bool ReadPacket(unsigned char* buf, int* charsRead, int* len)
{
// TODO: implement function
unsigned int bytes_to_read = fread(buf,sizeof(char),*len,InPipe);
bool isFinished = false;
if (bytes_to_read==sizeof(char)*(*len))
isFinished = true;
else
isFinished = false;
*charsRead = bytes_to_read/sizeof(char);
return isFinished;
}