Last active
August 29, 2015 14:07
-
-
Save kurochan/77fc5edd382fc00e34b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.pcap4j.sample; | |
import java.io.IOException; | |
import java.sql.Timestamp; | |
import java.util.List; | |
import java.nio.ByteBuffer; | |
import org.pcap4j.core.BpfProgram.BpfCompileMode; | |
import org.pcap4j.core.NotOpenException; | |
import org.pcap4j.core.PacketListener; | |
import org.pcap4j.core.PcapHandle; | |
import org.pcap4j.core.PcapNativeException; | |
import org.pcap4j.core.PcapNetworkInterface; | |
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode; | |
import org.pcap4j.core.PcapStat; | |
import org.pcap4j.packet.Packet; | |
import org.pcap4j.packet.TcpPacket; | |
import org.pcap4j.util.NifSelector; | |
import com.sun.jna.Platform; | |
@SuppressWarnings("javadoc") | |
public class Loop { | |
private static final String COUNT_KEY | |
= Loop.class.getName() + ".count"; | |
private static final int COUNT | |
= Integer.getInteger(COUNT_KEY, 5); | |
private static final String READ_TIMEOUT_KEY | |
= Loop.class.getName() + ".readTimeout"; | |
private static final int READ_TIMEOUT | |
= Integer.getInteger(READ_TIMEOUT_KEY, 10); // [ms] | |
private static final String SNAPLEN_KEY | |
= Loop.class.getName() + ".snaplen"; | |
private static final int SNAPLEN | |
= Integer.getInteger(SNAPLEN_KEY, 65536); // [bytes] | |
public static void main(String[] args) throws PcapNativeException, NotOpenException { | |
String filter = args.length != 0 ? args[0] : ""; | |
System.out.println(COUNT_KEY + ": " + COUNT); | |
System.out.println(READ_TIMEOUT_KEY + ": " + READ_TIMEOUT); | |
System.out.println(SNAPLEN_KEY + ": " + SNAPLEN); | |
System.out.println("\n"); | |
PcapNetworkInterface nif; | |
try { | |
nif = new NifSelector().selectNetworkInterface(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
if (nif == null) { | |
return; | |
} | |
System.out.println(nif.getName() + "(" + nif.getDescription() + ")"); | |
final PcapHandle handle | |
= nif.openLive(SNAPLEN, PromiscuousMode.PROMISCUOUS, READ_TIMEOUT); | |
if (filter.length() != 0) { | |
handle.setFilter( | |
filter, | |
BpfCompileMode.OPTIMIZE | |
); | |
} | |
PacketListener listener | |
= new PacketListener() { | |
@Override | |
public void gotPacket(Packet packet) { | |
Timestamp ts = new Timestamp(handle.getTimestampInts() * 1000L); | |
ts.setNanos(handle.getTimestampMicros() * 1000); | |
System.out.println(ts); | |
System.out.println(packet); | |
TcpPacket tcp = packet.get(TcpPacket.class); | |
if (tcp != null) { | |
System.out.println("=== TCP Option ==="); | |
List<TcpPacket.TcpOption> options = tcp.getHeader().getOptions(); | |
for (TcpPacket.TcpOption option : options) { | |
if (!option.getKind().valueAsString().equals("8")) { | |
break; | |
} | |
byte[] data = option.getRawData(); | |
StringBuffer sb = new StringBuffer(); | |
for ( byte b : data ){ | |
int i = 0xFF & (int)b; | |
String str = Integer.toHexString(i); | |
sb.append(str); | |
} | |
System.out.println(sb.toString()); | |
} | |
} | |
} | |
}; | |
try { | |
handle.loop(COUNT, listener); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
PcapStat ps = handle.getStats(); | |
System.out.println("ps_recv: " + ps.getNumPacketsReceived()); | |
System.out.println("ps_drop: " + ps.getNumPacketsDropped()); | |
System.out.println("ps_ifdrop: " + ps.getNumPacketsDroppedByIf()); | |
if (Platform.isWindows()) { | |
System.out.println("bs_capt: " + ps.getNumPacketsCaptured()); | |
} | |
handle.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment