-
Notifications
You must be signed in to change notification settings - Fork 4
/
sniff-pcap.c
executable file
·67 lines (54 loc) · 1.98 KB
/
sniff-pcap.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
#include <pcap.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
int packet_length = header->len;
struct ether_header *eth = (struct ether_header *) packet;
if (ntohs(eth->ether_type) == ETHERTYPE_IP) {
struct iphdr *ip = (struct iphdr *) (packet + sizeof(struct ether_header));
if (ip->protocol == IPPROTO_ICMP) {
struct icmphdr *icmp = (struct icmphdr *) (packet + sizeof(struct ether_header) + sizeof(struct iphdr));
printf(" Protocol: ICMP\n");
printf(" From: %s\n", inet_ntoa(*(struct in_addr *) &ip->saddr));
printf(" To: %s\n", inet_ntoa(*(struct in_addr *) &ip->daddr));
printf(" Type: ");
if (icmp->type == ICMP_ECHO || icmp->type == ICMP_ECHOREPLY) {
if (icmp->type == ICMP_ECHO) {
printf("ping request\n");
} else if (icmp->type == ICMP_ECHOREPLY) {
printf("ping reply\n");
}
if (packet_length > (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct icmphdr))) {
printf(" Payload: ");
puts((char *) packet + (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct icmphdr)));
}
}
printf("\n");
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Enter adapter name");
return 0;
}
pcap_t *handle;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
char filter_exp[] = "icmp";
bpf_u_int32 net;
// Step 1: Open live pcap session on NIC with given adapter
handle = pcap_open_live(argv[1], BUFSIZ, 1, 1000, errbuf);
// Step 2: Compile filter_exp into BPF psuedo-code
pcap_compile(handle, &fp, filter_exp, 0, net);
pcap_setfilter(handle, &fp);
printf("Capturing ICMP Packets.....\n");
// Step 3: Capture packets
pcap_loop(handle, -1, got_packet, NULL);
pcap_close(handle); //Close the handle
return 0;
}