feat: added basic tcp, udp and icmp handling

This commit is contained in:
Emi Aline Boucly 2025-08-23 00:22:48 +02:00
parent a85bcc8ca2
commit 885be66d5e
5 changed files with 70 additions and 4 deletions

13
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,13 @@
{
"files.associations": {
"ip_icmp.h": "c",
"tcp.h": "c",
"features-time64.h": "c",
"packet.h": "c",
"types.h": "c",
"socket.h": "c",
"stdint.h": "c",
"cstdint": "c",
"stdio.h": "c"
}
}

BIN
npcap Executable file

Binary file not shown.

16
npcap.c
View file

@ -1,11 +1,13 @@
// //
// Created by ako on 8/22/25. // Created by ako on 8/22/25.
// //
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/if_ether.h> #include <netinet/if_ether.h>
@ -15,7 +17,10 @@
#include "nethdr.h" #include "nethdr.h"
#include "npcap_handle.h" #include "npcap_handle.h"
#include <netpacket/packet.h> #include <netpacket/packet.h>
#include <net/if.h> // if_nametoindex() #include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
void cleanup(int socket, unsigned char* buffer) { void cleanup(int socket, unsigned char* buffer) {
close(socket); close(socket);
@ -103,13 +108,16 @@ int main()
switch (ip->protocol) { switch (ip->protocol) {
case 0x01: case 0x01:
handle_icmp(); struct icmphdr *icmp = (struct icmphdr *) (buffer + sizeof(struct ethhdr) + ip->ihl * 4);
handle_icmp(icmp);
break; break;
case 0x06: case 0x06:
handle_tcp(); struct tcphdr *tcp = (struct tcphdr *) (buffer + sizeof(struct ethhdr) + ip->ihl * 4);
handle_tcp(tcp);
break; break;
case 0x11: case 0x11:
handle_udp(); struct udphdr *udp = (struct udphdr *) (buffer + sizeof(struct ethhdr) + ip->ihl * 4);
handle_udp(udp);
break; break;
default: default:
break; break;

View file

@ -67,3 +67,41 @@ int handle_ip(struct iphdr* ip) {
return 0; return 0;
} }
int handle_icmp(struct icmphdr* icmp) {
printf("\n\t\tICMP:\n");
printf("\t\t\tType : %d\n", icmp->type);
printf("\t\t\tCode : %d\n", icmp->code);
printf("\t\t\tChecksum : %d\n", icmp->checksum);
return 0;
}
int handle_tcp(struct tcphdr* tcp) {
printf("\n\t\tTCP:\n");
printf("\t\t\tPort Source : %d\n", ntohs(tcp->source));
printf("\t\t\tPort Destination : %d\n", ntohs(tcp->dest));
printf("\t\t\tSeq Number : %u\n", ntohl(tcp->seq));
printf("\t\t\tAck Number : %u\n", ntohl(tcp->ack_seq));
printf("\t\t\tData Offset : %d\n", tcp->doff);
printf("\t\t\tFlags : ");
if (tcp->urg) printf("URG ");
if (tcp->ack) printf("ACK ");
if (tcp->psh) printf("PSH ");
if (tcp->rst) printf("RST ");
if (tcp->syn) printf("SYN ");
if (tcp->fin) printf("FIN ");
printf("\n");
printf("\t\t\tWindow Size : %d\n", ntohs(tcp->window));
printf("\t\t\tChecksum : %d\n", ntohs(tcp->check));
printf("\t\t\tUrgent Pointer : %d\n", tcp->urg_ptr);
return 0;
}
int handle_udp(struct udphdr* udp) {
printf("\n\t\tUDP:\n");
printf("\t\t\tPort Source : %d\n", ntohs(udp->source));
printf("\t\t\tPort Destination : %d\n", ntohs(udp->dest));
printf("\t\t\tLength : %d\n", ntohs(udp->len));
printf("\t\t\tChecksum : %d\n", ntohs(udp->check));
return 0;
}

View file

@ -4,10 +4,17 @@
#include "nethdr.h" #include "nethdr.h"
#include <stdio.h> #include <stdio.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
int handle_eth(struct ethhdr* eth); int handle_eth(struct ethhdr* eth);
int handle_arp(struct arphdr_c* arp); int handle_arp(struct arphdr_c* arp);
int handle_ip(struct iphdr* ip); int handle_ip(struct iphdr* ip);
int handle_icmp(struct icmphdr* icmp);
int handle_tcp(struct tcphdr* tcp);
int handle_udp(struct udphdr* udp);
#endif #endif