-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_utils.c
68 lines (58 loc) · 1.89 KB
/
print_utils.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
#include "ft_malcolm.h"
void _mc_print_usage(void)
{
printf(
"Usage: [HOST IP] [HOST MAC] [TARGET IP] [TARGET MAC]\n\n"
"In that particular order.\n"
"HOST = this computer\n"
"TARGET = your target sending the ARP request\n\n"
"IP addresses have to be in the IPv4 format,\n"
"MAP addresses have to be in format xx:xx:xx:xx:xx:xx\n"
"(Separator can be either ':' or '-')\n"
"And letters can be both lower and upper case letters\n"
"-v: verbose\n\n"
);
}
void _mc_print_mac(const unsigned char* mac)
{
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
void _mc_print_ip(const unsigned char* ip)
{
printf("%u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
}
void _mc_print_packet_info()
{
// Extract the sender IP and MAC addresses
unsigned char* sender_mac = _mc_g_data.arp_packet->arp_sha;
unsigned char* sender_ip = _mc_g_data.arp_packet->arp_spa;
// Extract the target IP and MAC addresses
unsigned char* target_mac = _mc_g_data.arp_packet->arp_tha;
unsigned char* target_ip = _mc_g_data.arp_packet->arp_tpa;
// Only display packet details if in verbose mode
if (_mc_g_data.verbose == true)
{
// Print the sender and target IP and MAC addresses
printf(_MC_GREEN_COLOR "Sender MAC:\t");
_mc_print_mac(sender_mac);
printf("Sender IP:\t");
_mc_print_ip(sender_ip);
printf(_MC_RED_COLOR "Target MAC:\t");
_mc_print_mac(target_mac);
printf("Target IP:\t");
_mc_print_ip(target_ip);
printf(_MC_RESET_COLOR "\n");
}
}
void _mc_print_hostname(const char *ip_addr)
{
struct in_addr addr;
struct hostent *host;
// Convert IP address from string to binary form
if (inet_pton(AF_INET, ip_addr, &addr) <= 0) return;
// Retrieve host information by IP address
host = gethostbyaddr(&addr, sizeof(addr), AF_INET);
if (host == NULL) printf("Hostname: unknown\n");
else printf("Hostname: %s\n", host->h_name);
}