-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
579 lines (480 loc) · 21.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <net/if.h>
#include <regex.h>
#define BUFFSIZE 65536
#define MAX_IP_LENGTH 45 // Maximum length of IPv6 address is 45 characters
#define MY_APP_PORT 8080
struct ip_filter {
/*PROTOCOL*/
int use_status; /*0 --> not being used, 1 --> source_filter is being used, 2--> dest_filter_is_being_used, 4-> both is being used*/
// 4th filter is not implemented in this version
struct sockaddr_in source_filter;
struct sockaddr_in dest_filter;
};
void get_interface_ip(struct sockaddr_in *localipptr, const char *interface);
void packet_capture(const char *interface, int len, int command, struct ip_filter *myfilterptr);
void display_ethernet_header(struct ethhdr *eth);
void display_ip_packet(struct iphdr *ip, struct sockaddr_in source, struct sockaddr_in dest);
void display_udp_header(struct udphdr *udp);
void display_tcp_header(struct tcphdr *tcp);
void display_udp_payload (unsigned char *data, int remaining_data );
void display_tcp_packet_thumbnail(struct ethhdr *eth, struct iphdr *ip, struct tcphdr *tcp, struct sockaddr_in source, struct sockaddr_in dest);
void display_udp_packet_thumbnail(struct ethhdr *eth, struct iphdr *ip, struct udphdr *udp, struct sockaddr_in source, struct sockaddr_in dest);
// Function to get interface ip
void get_interface_ip(struct sockaddr_in *localipptr, const char *interface) {
int n;
struct ifreq ifr;
n = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , interface , IFNAMSIZ - 1);
ioctl(n, SIOCGIFADDR, &ifr);
close(n);
memcpy(localipptr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
}
int validateIPAddress(char *ip_address) {
regex_t ipv4_regex, ipv6_regex;
int ret;
// Regular expressions for IPv4 and IPv6 addresses
ret = regcomp(&ipv4_regex, "^([0-9]{1,3}\\.){3}[0-9]{1,3}$", REG_EXTENDED);
if (ret != 0) {
printf("Failed to compile IPv4 regex\n");
return -1;
}
ret = regcomp(&ipv6_regex, "^[0-9a-fA-F:]+$", REG_EXTENDED);
if (ret != 0) {
printf("Failed to compile IPv6 regex\n");
regfree(&ipv4_regex);
return -1;
}
// Matching the input IP address with both IPv4 and IPv6 regex
ret = regexec(&ipv4_regex, ip_address, 0, NULL, 0);
if (ret == 0) {
regfree(&ipv4_regex);
regfree(&ipv6_regex);
return 4; // IPv4 address
}
ret = regexec(&ipv6_regex, ip_address, 0, NULL, 0);
if (ret == 0) {
regfree(&ipv4_regex);
regfree(&ipv6_regex);
return 6; // IPv6 address
}
// Neither IPv4 nor IPv6
regfree(&ipv4_regex);
regfree(&ipv6_regex);
return 0;
}
void display_ethernet_header(struct ethhdr *eth) {
// Visual delimiter to separate current packet from the previous one
printf("\n🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻\n");
// Display the Ethernet header information
printf("🌐 Ethernet Header 🌐\n");
printf("--------------------------------------------------\n");
printf("🔸 Source MAC Address : %02X-%02X-%02X-%02X-%02X-%02X\n",
eth->h_source[0], eth->h_source[1], eth->h_source[2],
eth->h_source[3], eth->h_source[4], eth->h_source[5]);
printf("🔸 Destination MAC Address : %02X-%02X-%02X-%02X-%02X-%02X\n",
eth->h_dest[0], eth->h_dest[1], eth->h_dest[2],
eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
printf("🔸 Protocol : 0x%04X\n", ntohs(eth->h_proto));
printf("--------------------------------------------------\n");
}
void display_ip_packet(struct iphdr *ip, struct sockaddr_in source, struct sockaddr_in dest) {
// Populate the source and destination addresses
source.sin_addr.s_addr = ip->saddr;
dest.sin_addr.s_addr = ip->daddr;
// Display the IP header information
printf("\n🔵🔷🔹 IP Packet Overview 🔹🔷🔵\n");
printf("==============================================\n");
printf("🔸 Version : %d\n", (unsigned int)ip->version);
printf("🔸 Header Length : %d DWORDS (%d Bytes)\n", (unsigned int)ip->ihl, ((unsigned int)(ip->ihl)) * 4);
printf("🔸 Type of Service : %d\n", (unsigned int)ip->tos);
printf("🔸 Total Length : %d Bytes\n", ntohs(ip->tot_len));
printf("🔸 Identification : %d\n", ntohs(ip->id));
printf("🔸 Time To Live (TTL) : %d\n", (unsigned int)ip->ttl);
printf("🔸 Protocol : %d\n", (unsigned int)ip->protocol);
printf("🔸 Header Checksum : 0x%04X\n", ntohs(ip->check));
printf("==============================================\n");
// Display the source and destination IP addresses
printf("🌍 Source IP Address : %s\n", inet_ntoa(source.sin_addr));
printf("🌍 Destination IP Address : %s\n", inet_ntoa(dest.sin_addr));
printf("==============================================\n");
printf("✨ End of IP Packet Overview ✨\n");
}
void display_tcp_header(struct tcphdr *tcp) {
printf("\n🌐 TCP Header Information 🌐\n");
printf("--------------------------------------------------\n");
printf("🔹 Source Port : %u\n", ntohs(tcp->source));
printf("🔹 Destination Port : %u\n", ntohs(tcp->dest));
printf("🔹 Sequence Number : %u\n", ntohl(tcp->seq));
printf("🔹 Acknowledge Number : %u\n", ntohl(tcp->ack_seq));
printf("🔹 Header Length : %d DWORDS (%d BYTES)\n", (unsigned int)tcp->doff, (unsigned int)tcp->doff * 4);
printf("--------------------------------------------------\n");
printf("🔸 Flags 🔸\n");
printf(" 🟢 URG : %d\n", (unsigned int)tcp->urg);
printf(" 🟢 ACK : %d\n", (unsigned int)tcp->ack);
printf(" 🟢 PSH : %d\n", (unsigned int)tcp->psh);
printf(" 🟢 RST : %d\n", (unsigned int)tcp->rst);
printf(" 🟢 SYN : %d\n", (unsigned int)tcp->syn);
printf(" 🟢 FIN : %d\n", (unsigned int)tcp->fin);
printf("--------------------------------------------------\n");
printf("🔹 Window Size : %d\n", ntohs(tcp->window));
printf("🔹 Checksum : 0x%04X\n", ntohs(tcp->check));
printf("🔹 Urgent Pointer : %d\n", ntohs(tcp->urg_ptr));
printf("--------------------------------------------------\n");
printf("🌟 End of TCP Header Information 🌟\n");
}
void display_tcp_packet_thumbnail(struct ethhdr *eth, struct iphdr *ip, struct tcphdr *tcp, struct sockaddr_in source, struct sockaddr_in dest) {
source.sin_addr.s_addr = ip->saddr;
dest.sin_addr.s_addr = ip->daddr;
// for low level protocols which is not using ip it will show 0
printf("\n [[ 🌐🌐TCP Packet :: ipv%d Source: %s :: ", (unsigned int)ip->version, inet_ntoa(source.sin_addr));
printf("Dest: %s Source Port: %u Dest Port: %u]]",inet_ntoa(dest.sin_addr),ntohs(tcp->source), ntohs(tcp->dest));
/* printf("\n[[ TCP Packet :: ipv%d :: Source: %s :: Dest: %s ]]\n", (unsigned int)ip->version, inet_ntoa(source.sin_addr), inet_ntoa(dest.sin_addr)); */
}
void display_udp_packet_thumbnail(struct ethhdr *eth, struct iphdr *ip, struct udphdr *udp, struct sockaddr_in source, struct sockaddr_in dest) {
source.sin_addr.s_addr = ip->saddr;
dest.sin_addr.s_addr = ip->daddr;
// for low level protocols which is not using ip it will show 0
printf("\n [[📡📡 UDP Packet :: ipv%d Source: %s :: ", (unsigned int)ip->version, inet_ntoa(source.sin_addr));
printf("Dest: %s Source Port: %u Dest Port: %u]]",inet_ntoa(dest.sin_addr), ntohs(udp->source), ntohs(udp->dest));
}
void display_other_packets_thumbnail(struct ethhdr *eth, struct iphdr *ip, struct sockaddr_in source, struct sockaddr_in dest) {
source.sin_addr.s_addr = ip->saddr;
dest.sin_addr.s_addr = ip->daddr;
// for low level protocols which is not using ip it will show 0
printf("\n [[ 🛰️🛰️ OTHER Packet :: ipv%d Source: %s :: ", (unsigned int)ip->version, inet_ntoa(source.sin_addr));
printf("Dest: %s ]]",inet_ntoa(dest.sin_addr));
/* printf("\n[[ TCP Packet :: ipv%d :: Source: %s :: Dest: %s ]]\n", (unsigned int)ip->version, inet_ntoa(source.sin_addr), inet_ntoa(dest.sin_addr)); */
}
void display_tcp_payload (unsigned char *data, int remaining_data, int srcport, int dstport) {
printf("\n-------TCP PAYLOAD--------------\n");
for(int i=0;i<remaining_data;i++) {
if (i != 0 && i % 16 == 0)
printf("\n");
printf(" %.2X ", data[i]);
}
printf("\n---------------------------------------------------\n\n");
}
void display_udp_header(struct udphdr *udp) {
printf("\n🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡🟡\n");
// Display the UDP header information
printf("\n🌊 UDP Header 🌊\n");
printf("============================================\n");
printf("🔹 Source Port : %d\n", ntohs(udp->source));
printf("🔹 Destination Port : %d\n", ntohs(udp->dest));
printf("🔹 Length : %d\n", ntohs(udp->len));
printf("🔹 Checksum : 0x%04X\n", ntohs(udp->check));
printf("============================================\n");
// End delimiter for the current packet
printf("🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\n");
}
void display_udp_payload (unsigned char *data, int remaining_data ) {
printf("\n-------UDP PAYLOAD--------------\n");
for(int i=0;i<remaining_data;i++) {
if (i != 0 && i % 16 == 0)
printf("\n");
printf(" %.2X ", data[i]);
}
printf("\n---------------------------------------------------\n\n");
}
void packet_capture(const char *interface, int len, int command, struct ip_filter *myfilterptr) {
printf("Got command = %d", command);
int sock;
sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(myfilterptr->use_status == 0) {
printf("Not using any ip filter\n");
}else if(myfilterptr->use_status == 1) {
printf("Source filter is being used..\n");
}else if(myfilterptr->use_status == 2) {
printf("Destination filter is being used..\n");
}else if(myfilterptr->use_status == 3) {
printf("Both filter is being used\n");
}else{
printf("Invalid Filter\n");
exit(0);
}
/* exit(0); */
if(sock == -1)
perror("Failed to create a socket");
// binding the interface to the socket
if(setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface, len) == -1){
fprintf(stderr, "Failed to select the chosen interface");
}
printf("Binding to the specific interface was successful..\n");
unsigned char *buffer = (unsigned char*)malloc(BUFFSIZE);
memset(buffer, 0, BUFFSIZE);
struct sockaddr saddr;
struct sockaddr_in source, dest;
memset(&source, 0, sizeof(source));
memset(&dest, 0, sizeof(dest));
int saddr_len = sizeof(saddr);
int buflen;
while(1) { // infinite loop
buflen = recvfrom(sock,buffer, BUFFSIZE, 0, &saddr, (socklen_t *)&saddr_len); // capturing the packet
if(buflen == -1)
perror("Failed to receive the packet: ");
struct ethhdr *eth = (struct ethhdr*)(buffer); // separating ethernet information from buffer
unsigned short iphdrlen;
struct iphdr *ip = (struct iphdr*)(buffer + sizeof(struct ethhdr)); // separting ip information from buffer
unsigned int *ip_bytes = (unsigned int *)malloc(10*sizeof(unsigned int));
iphdrlen = ip->ihl*4; //IHL means Internet Header Length (IHL), which is the number of 32-bit words in the header. So we have to multiply the IHL by 4 to get the size of the header in bytes:
/* getting pointer to udp header*/
int remaining_data;
unsigned char *data;
source.sin_addr.s_addr = ip->saddr;
dest.sin_addr.s_addr = ip->daddr;
if (myfilterptr->use_status == 2 && (ip->daddr != myfilterptr->dest_filter.sin_addr.s_addr) ||
myfilterptr->use_status == 1 && (ip->saddr != myfilterptr->source_filter.sin_addr.s_addr)) {
// if source/destination filter is set but the current packet doesn't doesn't math
// no need to go through each case below
/* printf("IP filter was set, but the packet doesn't match the used filter\n"); */
}else{
// if source filter is set and ip packet does not match the desitnation filter
switch(command) {
/*
filter:protocol
1) display_all_packet_thumbnails (DEFAULT)
2) display_tcp_udp_thumbnails
3) display_tcp_udp_packet_details
4) display_tcp_packet_details
5) display_udp_packet_details
6) display_all_packet_details
7) display_packets_to_ip
8_ display_packets_from_ip
9_Monitor_my_application
10_port based filter (not implemented in this version)
11) analyse_the_traffic_coming_to_a_port
*/
/* 1) display_all_packet_thumbnails (DEFAULT) */
case 1:
if(ip->protocol == 6) {
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr)); // extracting tcp from the buffer
display_tcp_packet_thumbnail(eth, ip, tcp, source, dest);
}else if(ip->protocol == 17) {
struct udphdr *udp = (struct udphdr *)(buffer + iphdrlen + sizeof(struct ethhdr));
display_udp_packet_thumbnail(eth, ip, udp, source, dest);
}else{
display_other_packets_thumbnail(eth, ip, source, dest);
}
break;
/* 2) display_tcp_udp_thumbnails */
case 2:
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr)); // extracting tcp from the buffer
display_tcp_packet_thumbnail(eth, ip, tcp, source, dest);
struct udphdr *udp = (struct udphdr *)(buffer + iphdrlen + sizeof(struct ethhdr));
display_udp_packet_thumbnail(eth, ip, udp, source, dest);
break;
/* 3) display_tcp_udp_packet_details */
case 3:
if(ip->protocol == 6) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
display_tcp_header(tcp);
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
display_tcp_payload(data, remaining_data, ntohs(tcp->source), ntohs(tcp->dest));
}
if(ip->protocol == 17) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct udphdr *udp=(struct udphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
display_udp_header(udp);
display_udp_payload(data, remaining_data);
}
break;
/* 4) display_tcp_packet_details */
case 4:
if(ip->protocol == 6) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
display_tcp_header(tcp);
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
display_tcp_payload(data, remaining_data, ntohs(tcp->source), ntohs(tcp->dest));
}
break;
/* 5) display_udp_packet_details */
case 5:
if(ip->protocol == 17) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct udphdr *udp=(struct udphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
display_udp_header(udp);
display_udp_payload(data, remaining_data);
}
break;
/* 6) display_all_packet_details */
case 6:
if(ip->protocol == 6) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
display_tcp_header(tcp);
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
display_tcp_payload(data, remaining_data, ntohs(tcp->source), ntohs(tcp->dest));
}
else if(ip->protocol == 17) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct udphdr *udp=(struct udphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
display_udp_header(udp);
display_udp_payload(data, remaining_data);
}else{
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
}
break;
/* 7) display_packets_to_ip */
case 7:
source.sin_addr.s_addr = ip->daddr;
dest.sin_addr.s_addr = ip->saddr;
// Using destination filter
if (myfilterptr->use_status == 2) {
if(ip->daddr == myfilterptr->dest_filter.sin_addr.s_addr) {
if(ip->protocol == 6) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
display_tcp_header(tcp);
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
display_tcp_payload(data, remaining_data, ntohs(tcp->source), ntohs(tcp->dest));
}
if(ip->protocol == 17) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct udphdr *udp=(struct udphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
display_udp_header(udp);
display_udp_payload(data, remaining_data);
}
// here display all the tcp and udp packets that is going to the parameterized ip
}
}
break;
/* 8_ display_packets_from_ip */
case 8:
// Using source filter
/* printf("CASE 8"); */
if (myfilterptr->use_status == 1) {
if(ip->saddr == myfilterptr->source_filter.sin_addr.s_addr) {
if(ip->protocol == 6) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct tcphdr *tcp= (struct tcphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
display_tcp_header(tcp);
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct tcphdr));
display_tcp_payload(data, remaining_data, ntohs(tcp->source), ntohs(tcp->dest));
}
if(ip->protocol == 17) {
display_ethernet_header(eth);
display_ip_packet(ip, source, dest);
struct udphdr *udp=(struct udphdr*)(buffer + iphdrlen + sizeof(struct ethhdr));
data = (buffer + iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
remaining_data = buflen - (iphdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr));
display_udp_header(udp);
display_udp_payload(data, remaining_data);
}
}
}
break;
}
} // ip didn't match
// clearing the buffer
memset(buffer, 0, BUFFSIZE);
}
printf("\n");
close(sock);
}
int main() {
// getting interface name
const char *interface;
interface = "wlp2s0"; // name of my wifi-interface
int len = strnlen(interface, IFNAMSIZ);
if (len == IFNAMSIZ) {
fprintf(stderr, "Too long iface name");
return -1;
}
struct sockaddr_in localip;
get_interface_ip(&localip, interface);
printf("IP address of interface %s is %s", interface, inet_ntoa(localip.sin_addr));
int command = 1;
printf("\n1) Display All Packet Thumbnails\n");
printf("2) Display TCP/UDP Thumbnails\n");
printf("3) Display TCP/UDP Packet Details\n");
printf("4) Display TCP Packet Details\n");
printf("5) Display UDP Packet Details\n");
printf("6) Display All Packet Details\n");
printf("7) Display Packets to IP\n");
printf("8) Display Packets from IP\n");
printf("Choose one of the options below: ");
scanf("%d",&command);
struct ip_filter *myfilterptr, myfilter;
myfilterptr = &myfilter;
char from_filter_ip[INET_ADDRSTRLEN];
char to_filter_ip[INET_ADDRSTRLEN];
strcpy(from_filter_ip, "0.0.0.0");
strcpy(to_filter_ip, "0.0.0.0");
myfilterptr->use_status = 0;
if(command == 8) {
myfilterptr->use_status = 1;
printf("Enter the source ip: ");
getchar();
scanf("%45s", from_filter_ip); // Read up to 45 characters (maximum for IPv6)
// perform ip validation
int version = validateIPAddress(from_filter_ip);
if(version == 4 || version == 6) {
printf("IP validated...\n");
printf("From Filter IP = %s",from_filter_ip);
}else{
printf("Invalid ip");
return 0;
}
}
if(command == 7) {
myfilterptr->use_status = 2;
printf("Enter the destination ip: ");
getchar();
scanf("%45s", to_filter_ip); // Read up to 45 characters (maximum for IPv6)
// perform ip validation
int version = validateIPAddress(to_filter_ip);
if(version == 4 || version == 6) {
printf("IP validated...\n");
printf("To Filter IP = %s",to_filter_ip);
}else{
printf("Invalid ip");
return 0;
}
}
struct sockaddr_in from_filter_addr;
struct sockaddr_in to_filter_addr;
inet_pton(AF_INET,from_filter_ip, &from_filter_addr.sin_addr);
inet_pton(AF_INET, to_filter_ip, &to_filter_addr.sin_addr);
myfilterptr->source_filter = from_filter_addr;
myfilterptr->dest_filter = to_filter_addr;
packet_capture(interface, len, command, myfilterptr); // calling packet capture from main
return 0;
}