-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcap-test.c
More file actions
72 lines (60 loc) · 2.36 KB
/
Copy pathpcap-test.c
File metadata and controls
72 lines (60 loc) · 2.36 KB
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
#include <pcap.h>
#include <stdbool.h>
#include <stdio.h>
#include "libnet-headers.h"
#include <netinet/ether.h>
void usage() {
printf("syntax: pcap-test <interface>\n");
printf("sample: pcap-test wlan0\n");
}
typedef struct {
char* dev_;
} Param;
Param param = {
.dev_ = NULL
};
bool parse(Param* param, int argc, char* argv[]) {
if (argc != 2) {
usage();
return false;
}
param->dev_ = argv[1];
return true;
}
int main(int argc, char* argv[]) {
if (!parse(¶m, argc, argv))
return -1;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* pcap = pcap_open_live(param.dev_, BUFSIZ, 1, 1000, errbuf);
if (pcap == NULL) {
fprintf(stderr, "pcap_open_live(%s) return null - %s\n", param.dev_, errbuf);
return -1;
}
while (true) {
struct pcap_pkthdr* header;
const u_char* packet;
int res = pcap_next_ex(pcap, &header, &packet);
if (res == 0) continue;
if (res == PCAP_ERROR || res == PCAP_ERROR_BREAK) {
printf("pcap_next_ex return %d(%s)\n", res, pcap_geterr(pcap));
break;
}
struct libnet_ethernet_hdr* ethernet = (struct libnet_ethernet_hdr*)packet;
struct libnet_ipv4_hdr* ipv4 = (struct libnet_ipv4_hdr*)(packet + sizeof(struct libnet_ethernet_hdr));
struct libnet_tcp_hdr* tcp = (struct libnet_tcp_hdr*)(packet + sizeof(struct libnet_ethernet_hdr) + sizeof(struct libnet_ipv4_hdr));
unsigned char * payload = (unsigned char *)(packet + sizeof(struct libnet_ethernet_hdr) + sizeof(struct libnet_ipv4_hdr) + sizeof(struct libnet_tcp_hdr));
int payload_size = header->len - (sizeof(struct libnet_ethernet_hdr) + sizeof(struct libnet_ipv4_hdr) + sizeof(struct libnet_tcp_hdr));
if (payload_size > 20) {
payload_size = 20;
}
printf("Ethernet: src mac=%s, dst mac=%s\n", ether_ntoa((const struct ether_addr *) ðernet->ether_shost), ether_ntoa((const struct ether_addr *) ðernet->ether_dhost));
printf("IP: src ip=%s, dst ip=%s\n", inet_ntoa(ipv4->ip_src), inet_ntoa(ipv4->ip_dst));
printf("TCP: src port=%d, dst port=%d\n", ntohs(tcp->th_sport), ntohs(tcp->th_dport));
printf("Payload (Hex): ");
for (int i = 0; i < payload_size; i++) {
printf("%02X ", payload[i]);
}
printf("\n\n");
}
pcap_close(pcap);
}