npcap/npcap_handle.c
2025-08-23 00:04:19 +02:00

69 lines
2.5 KiB
C

#include "npcap_handle.h"
int handle_eth(struct ethhdr* eth) {
printf("\nEthernet:\n");
printf("\tSource : %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("\tDestination : %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("\tProtocole : 0x%04x\n", ntohs(eth->h_proto));
return 0;
}
int handle_arp(struct arphdr_c* arp) {
printf("\n\tARP:\n");
printf("\t\tCode d'opération ARP : %d\n", arp->opcode);
printf("\t\tMAC Source : %02x:%02x:%02x:%02x:%02x:%02x\n",
arp->sender_mac[0], arp->sender_mac[1], arp->sender_mac[2],
arp->sender_mac[3], arp->sender_mac[4], arp->sender_mac[5]);
printf("\t\tIP Source : %d.%d.%d.%d\n",
arp->sender_ip[0], arp->sender_ip[1], arp->sender_ip[2], arp->sender_ip[3]);
printf("\t\tMAC Destination : %02x:%02x:%02x:%02x:%02x:%02x\n",
arp->target_mac[0], arp->target_mac[1], arp->target_mac[2],
arp->target_mac[3], arp->target_mac[4], arp->target_mac[5]);
printf("\t\tIP Destination : %d.%d.%d.%d\n",
arp->target_ip[0], arp->target_ip[1], arp->target_ip[2], arp->target_ip[3]);
return 0;
}
int handle_ip(struct iphdr* ip) {
char srcip[INET_ADDRSTRLEN];
char dstip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &ip->saddr, srcip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &ip->daddr, dstip, INET_ADDRSTRLEN);
char* protostr;
switch (ip->protocol) {
case 0x01:
protostr = "ICMP";
break;
case 0x06:
protostr = "TCP";
break;
case 0x11:
protostr = "UDP";
break;
default:
protostr = "Unexpected";
break;
}
printf("\n\tIPv4:\n");
printf("\t\tVersion : %d\n", ip->version);
printf("\t\tIHL : %d\n", ip->ihl);
printf("\t\tToS : %d\n", ip->tos);
printf("\t\tLongueur : %d\n", ip->tot_len);
printf("\t\tID : %d\n", ip->id);
// printf("\t\tFlags : %d%d%d\n");
// printf("\t\tFragment offset:");
printf("\t\tTTL : %d\n", ip->ttl);
printf("\t\tProtocole : 0x%02x (%s)\n", ip->protocol, protostr);
printf("\t\tHeader cksm: %d\n", ip->check);
printf("\t\tIP Source : %s\n", srcip);
printf("\t\tIP Dest. : %s\n", dstip);
return 0;
}