Skip to content

Commit

Permalink
Supporting bpf on packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ggadon committed Feb 13, 2017
1 parent d4d837a commit 7e566d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pcap4j-test-coverage/src/
# Gradle
build/
.gradle/
.idea
gradle-app.setting
.gradletasknamecache

27 changes: 27 additions & 0 deletions pcap4j-core/src/main/java/org/pcap4j/core/BpfProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ public String getExpression() {
return expression;
}

/**
* Apply the filter on a given packet.
* Return true if the packet given passes the filter that is built from this program.
*
* @param packet the packet to apply the filter on
* @return true if the packet passes the filter
*/
public boolean applyFilter(byte[] packet) {
return applyFilter(packet, packet.length, packet.length);
}

/**
* Apply the filter on a given packet.
* Return true if the packet given passes the filter that is built from this program.
*
* @param packet the packet to apply the filter on
* @param buflen the buffer len to use
* @return true if the packet passes the filter
*/
public boolean applyFilter(byte[] packet, int wirelen, int buflen) {
if (program.bf_insns == null) {
program.read();
}

return NativeMappings.bpf_filter(program.bf_insns, packet, wirelen, buflen) != 0;
}

/**
*
* @return true if the bpf_program represented by this object is freed;
Expand Down
4 changes: 4 additions & 0 deletions pcap4j-core/src/main/java/org/pcap4j/core/NativeMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ static native int pcap_compile_nopcap(
int optimize, int mask
);


// u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int)
static native int bpf_filter(bpf_insn.ByReference bpf_insn, byte[] packet, int wirelen, int buflen);

// int pcap_setfilter(pcap_t *p, struct bpf_program *fp)
static native int pcap_setfilter(Pointer p, bpf_program fp);

Expand Down

0 comments on commit 7e566d7

Please sign in to comment.