-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
133 lines (116 loc) · 3.76 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
#include "ft_malcolm.h"
// This is the global variable
_mc_t_data _mc_g_data;
/* Get the list of all network interfaces and find an active one
with an assigned IP address, which will be the interface we can use */
int _mc_display_interface(void)
{
struct ifaddrs *ifaddr, *ifa;
char active_interface[IFNAMSIZ];
// Retrieve the list of network interfaces
if (getifaddrs(&ifaddr) < 0)
{
fprintf(stderr, _MC_RED_CROSS _MC_RED_COLOR " ERROR: %s\n", strerror(errno));
return 1;
}
// Traverse the list of network interfaces
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
// Check if the interface has an assigned IPv4 address and is up
if (ifa->ifa_addr != NULL && /* has an assigned IP address */
ifa->ifa_addr->sa_family == AF_INET && /* IPv4 */
(ifa->ifa_flags & IFF_UP) && /* is active and connected to our device */
!(ifa->ifa_flags & IFF_LOOPBACK) && /* not localhost */
!(ifa->ifa_flags & IFF_NOARP)) /* accepts ARP */
{
// Get the interface name
_mc_memcpy(active_interface, ifa->ifa_name, IFNAMSIZ);
break;
}
}
// Print the active interface name
if (_mc_strlen(active_interface) > 0)
printf(_MC_GREEN_ASTERISK " Active Interface: %s\n\n", active_interface);
else {
fprintf(stderr, _MC_RED_CROSS _MC_RED_COLOR
" No active interface with IPv4 address found"
_MC_RESET_COLOR "\n\n");
freeifaddrs(ifaddr);
return 1;
}
// Free the memory allocated by getifaddrs
freeifaddrs(ifaddr);
return 0;
}
int _mc_check_argc(size_t argc, char *argv[])
{
// Check if we have the right number of arguments
if (argc < 5 || argc > 6)
{
fprintf(stderr,
_MC_RED_CROSS _MC_RED_COLOR " Wrong number of argument."
_MC_RESET_COLOR "\n\n");
_mc_print_usage();
return 1;
}
// The 5th argument is only present when the "-v" (verbose) option is used
// and can only exist at argument position 1 or 5
if (argc == 6)
{
if (_mc_strncmp(argv[1], "-v", _mc_strlen(argv[1])) == 0 ||
_mc_strncmp(argv[5], "-v", _mc_strlen(argv[5])) == 0)
_mc_g_data.verbose = true;
else {
_mc_print_usage();
fprintf(stderr,
_MC_RED_CROSS _MC_RED_COLOR " Unknown 5th argument"
_MC_RESET_COLOR "\n\n");
return 1;
}
printf(_MC_GREEN_ASTERISK " Verbose mode\n");
}
return 0;
}
int _mc_validate_and_assign_args(char *argv[])
{
// The only condition in which the ip and mac arguments will not begin
// at argv[1] is when the '-v' option is present at argv[1]
size_t start = _mc_strncmp(argv[1], "-v", _mc_strlen(argv[1])) == 0 ? 2 : 1;
int ip_or_mac = 0;
printf("\n");
for (size_t i=start; i < start + 4; ++i)
{
if (
((ip_or_mac % 2 == 0 &&
_mc_is_ip_address_ipv4(argv[i]) == false) ||
(ip_or_mac % 2 != 0 &&
_mc_is_mac_address_valid(argv[i]) == false))) return 1;
++ip_or_mac;
}
printf("\n");
// Assign
_mc_convert_string_to_byte_ip(argv[start], _mc_g_data.host_ip);
_mc_convert_mac_string_to_bytes(argv[start + 1], _mc_g_data.host_mac);
_mc_convert_string_to_byte_ip(argv[start + 2], _mc_g_data.target_ip);
_mc_convert_mac_string_to_bytes(argv[start + 3], _mc_g_data.target_mac);
return 0;
}
int main(int argc, char *argv[])
{
// Check the given arguments and assign them to our globals
if (_mc_check_argc(argc, argv) == _MC_ERROR ||
_mc_validate_and_assign_args(argv) == _MC_ERROR) return 1;
// Check for root privileges
if (getuid() == 0)
printf(_MC_GREEN_ASTERISK " Root privileges\n");
else {
fprintf(stderr,
_MC_RED_CROSS " Not running with root privileges. Quitting...\n");
return 1;
}
// Display the detected interface and run the program
if (_mc_display_interface() == _MC_ERROR ||
_mc_start_sniffing_paquets() == _MC_ERROR) return 1;
printf("Exiting program...\n\n");
return 0;
}