Skip to main content

Posts

Jxnet: Reading Packets From A Dump File Without Callback

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapPktHdr; public class ReadingPacketsFromADumpFileWithoutCallback { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); Pcap pcap = Jxnet.PcapOpenOffline("dump.pcap", errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } PcapPktHdr h = new PcapPktHdr(); ByteBuffer b = null; while((b = Jxnet.PcapNext(pcap, h)) != null) { System.out.println(h); System.out.println(b); } Jxnet.PcapClose(pcap); } }
Recent posts

Jxnet: Reading Packets From A Dump File With Callback

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; public class ReadingPacketsFromADumpFileWithCallback { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); Pcap pcap = Jxnet.PcapOpenOffline("dump.pcap", errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } PcapHandler callback = new PcapHandler () { @Override public void nextPacket(String user, PcapPktHdr h, ByteBuffer b) { System.out.println(h); System.out.println(b); } }; Jxnet.PcapLoop(pcap, -1, callback, null); Jxnet.PcapClose(pcap); } }

Jxnet: Saving Packets To A Dump File With Callback

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapDumper; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; public class SavingPacketsToADumpFileWithCallback { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); String source = Jxnet.PcapLookupdev(errbuf); if(source == null) { System.err.println(errbuf.toString()); return; } Pcap pcap = Jxnet.PcapOpenLive(source, 65535, 1, 2000, errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } PcapDumper pcap_dumper = Jxnet.PcapDumpOpen(pcap, "dump.pcap"); if(pcap_dumper == null) { System.err.println(Jxnet.PcapGetErr(pcap)); Jxnet.PcapClose(pcap); return; } PcapHandler callback = new PcapHandler () { @Override public void nextPacket(PcapDumper pcap_dumper, PcapPktHdr h, ByteBuffer sp) { Jxne

Jxnet: Sending Packet

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; public class SendingPacket { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); String source = Jxnet.PcapLookupdev(errbuf); if(source == null) { System.err.println(errbuf.toString()); return; } Pcap pcap = Jxnet.PcapOpenLive(source, 65535, 1, 2000, errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } ByteBuffer bb = ByteBuffer.allocateDirect(14); bb.put(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}); bb.put(new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef, (byte) 0xc0, (byte) 0xfe}); bb.put((byte) 0x0806); // ARP if(Jxnet.PcapSendPacket(pcap, bb, bb.limit()) == 0) { System.out.println("OK"); } else { System.err.println(Jxnet.PcapGetErr(pcap)); } Jxnet.PcapClose(pcap); } }

Jxnet: Filtering The Traffic

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.BpfProgram; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; import com.ardikars.jxnet.PcapStat; public class FilteringTheTraffic { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); String source = Jxnet.PcapLookupdev(errbuf); BpfProgram fp = new BpfProgram(); String filter = "ip"; int netmask = 0xffffff; PcapStat stat = new PcapStat(); if(source == null) { System.err.println(errbuf.toString()); return; } Pcap pcap = Jxnet.PcapOpenLive(source, 65535, 1, 2000, errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } if(Jxnet.PcapDatalink(pcap)!= 1) { System.out.println(source + " is not an Ethernet"); Jxnet.PcapClose(pcap); return; } if(Jxnet.PcapCompile(pcap, fp, filter, 1, net

Jxnet: Capturing The Packets Without Callback

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapPktHdr; public class CapturingThePacketsWithoutCallback { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); String source = Jxnet.pcapLookupdev(errbuf); if(source == null) { System.err.println(errbuf.toString()); return; } Pcap pcap = Jxnet.pcapOpenLive(source, 65535, 1, 2000, errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } ByteBuffer b = null; PcapPktHdr h = new PcapPktHdr(); for(int i=0; i<10; i++) { b = Jxnet.pcapNext(pcap, h); System.out.println(h); System.out.println(b); } Jxnet.pcapClose(pcap); } }

Jxnet: Capturing The Packets With Callback

package com.ardikars.test.jxnet; import java.nio.ByteBuffer; import com.ardikars.jxnet.Jxnet; import com.ardikars.jxnet.Pcap; import com.ardikars.jxnet.PcapHandler; import com.ardikars.jxnet.PcapPktHdr; public class CapturingThePacketsWithCallback { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); String source = Jxnet.pcapLookupdev(errbuf); if(source == null) { System.err.println(errbuf.toString()); return; } Pcap pcap = Jxnet.pcapOpenLive(source, 65535, 1, 2000, errbuf); if(pcap == null) { System.err.println(errbuf.toString()); return; } PcapHandler callback = new PcapHandler () { @Override public void nextPacket(Object u, PcapPktHdr h, ByteBuffer b) { System.out.println(h); System.out.println(b); } }; Jxnet.pcapLoop(pcap, 10, callback, null); Jxnet.pcapClose(pcap); } }