-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·246 lines (201 loc) · 5.14 KB
/
main.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
244
245
246
/**
A simple packet analyzer utility using pcap library.
Created as a school assigment for Computer Networks curse.
Matej Bellus
**/
#define LOG_FILE "log.txt"
#pragma pack(1)
//#include "libpcap/pcap.h"
#include <pcap.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdarg.h"
#define ETH2_TYPE 2
#define IP4_TYPE 0x0008 // 0x0800 -> 0x0008 due to endianness
#define ARP_TYPE 0x0608 // 0x8060 -> 0x0608 due to endianness
#define ICMP_TYPE 0x01
#define UDP_TYPE 0x11
#define TCP_TYPE 0x06
#define R_ANY -1
#define R_ALLOW 1
#define R_DENY 0
#define R_IN 3
#define R_OUT 4
#define R_IP 5
#define R_MAC 6
//#define DEFAULT_ACTION R_DENY
#define DEFAULT_ACTION R_ALLOW
char log_b[1024];
int p1out = 0;
int p2out = 0;
int p1in = 0;
int p2in = 0;
// custom includes
#include "functions.h"
#include "stats.h"
#include "eth_parser.h"
#include "l3_parser.h"
#include "l4_parser.h"
#include "l5_parser.h"
pthread_mutex_t mutex;
int pause_rendering = 0;
Port *p1, *p2;
#include "mac_table.h"
#include "rules.h"
#include "config.h"
#include "port_listener.h"
void print_usage(){
printf("\nSoftware switch implementation by Matej Bellus (c) \n");
printf("Usage: s_switch [-l] [-1 <interface_1> -2 <interface_2>] \n\n");
printf(" -l List available interfaces\n");
printf(" -1 First interface\n");
printf(" -2 Second inteface\n");
printf("\nEXAMPLE sudo ./bin/s_switch -1 eth0 -2 wlan0\n");
}
void list_interfaces(){
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t * alldevs;
pcap_if_t * d;
pcap_findalldevs(&alldevs, errbuf);
for(d=alldevs; d; d = d->next){
printf("%s\n", d->name);
}
}
void signal_handler(){
my_log("Ctrl C - cya ;) ");
//printf("\033[?1049l"); // go back
exit(0);
}
void clear_log(){
my_log("Clear log..");
FILE * f;
f = fopen(LOG_FILE, "w");
fclose(f);
}
int main(int argc, char *argv[])
{
signal (SIGINT, signal_handler);
//printf("\033[?1049h\033[H");
clear_log();
my_log("Software switch starting...");
my_log(pcap_lib_version());
int option = 0, ret;
char c;
pthread_t config_thread;
char errbuf[PCAP_ERRBUF_SIZE];
p1 = create_port_struct(1);
p2 = create_port_struct(2);
while ((option = getopt(argc, argv,"h l 1:2: m")) != -1) {
switch (option) {
case '1' :
strcpy(p1->name, optarg);
break;
case '2' :
strcpy(p2->name, optarg);
break;
case 'l':
list_interfaces();
exit(0);
case 'm':
mock_rule();
break;
case 'h':
default: print_usage();
exit(EXIT_FAILURE);
}
}
p1->handle = pcap_create(p1->name, errbuf);
if ( (ret = pcap_setdirection(p1->handle, PCAP_D_IN)) != 0){
printf("pcap_setdirection returned %i\n", ret);
my_log("pcap_setdirection failed");
pcap_perror(p1->handle, 0);
//exit(-1);
}
if ( pcap_set_promisc(p1->handle, 1) != 0){
printf("pcap_set_promisc returned \n%s\n", pcap_geterr(p1->handle));
my_log("pcap_set_promisc failed");
pcap_perror(p1->handle, 0);
exit(-1);
}
if ( pcap_set_immediate_mode(p1->handle, 1) != 0){
printf("pcap_set_immediate_mode returned \n%s\n", pcap_geterr(p1->handle));
my_log("pcap_set_immediate_mode failed");
pcap_perror(p1->handle, 0);
exit(-1);
}
if ( pcap_activate(p1->handle)){
printf("Failed to open interface %s\n", pcap_geterr(p1->handle));
exit(-1);
} else {
sprintf(log_b, "Handle activated for %s", p1->name);
my_log(log_b);
}
p2->handle = pcap_create(p2->name, errbuf);
if ( pcap_setdirection(p2->handle, PCAP_D_OUT) != 0){
my_log("pcap_setdirection failed");
pcap_perror(p2->handle, 0);
//exit(-1);
}
if ( pcap_set_promisc(p2->handle, 1) != 0){
my_log("pcap_set_promisc failed");
pcap_perror(p2->handle, 0);
exit(-1);
}
if ( pcap_set_immediate_mode(p2->handle, 1) != 0){
my_log("pcap_set_immediate_mode failed");
pcap_perror(p2->handle, 0);
exit(-1);
}
if ( pcap_activate(p2->handle)){
printf("Failed to open interface %s\n", pcap_geterr(p2->handle));
exit(-1);
} else {
sprintf(log_b, "Handle activated for %s", p2->name);
my_log(log_b);
}
//exit(0);
my_log("Deleting mac table..");
clear_mac();
sprintf(log_b, "Default action is %s", (DEFAULT_ACTION == R_ALLOW)? "R_ALLOW" : "R_DENY");
my_log(log_b);
my_log("Creating threads...");
pthread_mutex_init(&mutex, NULL);
if ( pthread_create(&(p1->thread), 0, port_listener, (void *)p1) ){
my_log("Error creating p1 thread");
exit(-1);
}
if ( pthread_create(&(p2->thread), 0, port_listener, (void *)p2) ){
my_log("Error creating p2 thread");
exit(-1);
}
pthread_create(&config_thread, 0, config, 0);
while (1) {
mac_delete_old_entries(5);
if(pause_rendering == 1)
continue;
// render here
system("clear");
print_mac();
print_rules();
print_stats_header();
print_stats(p1->in, "1 IN");
print_stats(p1->out, "1 OUT");
print_stats(p2->in, "2 IN");
print_stats(p2->out, "2 OUT");
printf("p1in: %i\tp1out: %i\tp2in: %i\tp2out: %i\n", p1in, p1out, p2in, p2out);
sleep(1);
}
pthread_join(config_thread, 0);
pthread_join(p1->thread, 0);
pthread_join(p2->thread, 0);
//printf("\033[?1049l"); // go back
return 0;
}