Test filtering created - made for testing filtering as per tutorial.
This commit is contained in:
parent
b41e84e259
commit
a16f7f73a1
1 changed files with 50 additions and 0 deletions
50
test_filtering.cpp
Normal file
50
test_filtering.cpp
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <pcap.h>
|
||||||
|
|
||||||
|
// Initalize variables.
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
pcap_t *handle; /* Session handle */
|
||||||
|
string dev; /* Device to sniff on */
|
||||||
|
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
|
||||||
|
struct bpf_program fp; /* The compiled filter expression */
|
||||||
|
char filter_exp[] = "port 23"; /* The filter expression */
|
||||||
|
bpf_u_int32 mask; /* The netmask of our sniffing device */
|
||||||
|
bpf_u_int32 net; /* The IP of our sniffing device */
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char *dev, errbuf[PCAP_ERRBUF_SIZE];
|
||||||
|
|
||||||
|
dev = pcap_lookupdev(errbuf);
|
||||||
|
|
||||||
|
if (dev == NULL) {
|
||||||
|
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
|
||||||
|
return(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
|
||||||
|
fprintf(stderr, "Can't get netmask for device %s\n", dev);
|
||||||
|
net = 0;
|
||||||
|
mask = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
|
||||||
|
|
||||||
|
if(handle == NULL) {
|
||||||
|
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
|
||||||
|
return(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
|
||||||
|
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
|
||||||
|
return(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pcap_setfilter(handle, &fp) == -1) {
|
||||||
|
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
|
||||||
|
return(2);
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue