Skip to content

Instantly share code, notes, and snippets.

@aboutsip
Last active December 19, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboutsip/5896237 to your computer and use it in GitHub Desktop.
Save aboutsip/5896237 to your computer and use it in GitHub Desktop.
Simple example showing how to use pkts.io's stream support. In this example, we are only interested in SIP streams.
import io.pkts.Pcap;
import io.pkts.packet.sip.SipPacket;
import io.pkts.streams.SipStream;
import io.pkts.streams.Stream;
import io.pkts.streams.StreamHandler;
import io.pkts.streams.StreamListener;
import io.pkts.streams.impl.DefaultStreamHandler;
import java.io.FileNotFoundException;
import java.io.IOException;
// Step 1 - Open the pcap containing our traffic.
final Pcap pcap = Pcap.openStream("my_traffic.pcap");
// Step 2 - Instead of implementing our own PacketHandler we will be
// using a StreamHandler provided for us by the io.pkts.streams
// library. It has a DefaultStreamHandler (which obviously
// implements the FrameHandler) that will detect new streams
// and call a StreamListener when appropriate.
final StreamHandler streamHandler = new DefaultStreamHandler();
// Step 3 - In this simple example we will just supply a very basic
// StreamListener for SipMessages only. All we will do is
// print to std out when a new event occurs for a stream.
streamHandler.addStreamListener(new StreamListener<SipPacket>() {
@Override
public void startStream(final Stream<SipPacket> stream, final SipPacket packet) {
System.out.println("New SIP stream detected. Stream id: " + stream.getStreamIdentifier());
System.out.println("SipMessage was: " + packet.getInitialLine());
}
@Override
public void packetReceived(final Stream<SipPacket> stream, final SipPacket packet) {
System.out.println("Received a new SIP message for stream: " + stream.getStreamIdentifier());
System.out.println("SipMessage was: " + packet.getInitialLine());
}
@Override
public void endStream(final Stream<SipPacket> stream) {
System.out.println("The stream ended. Stream id: " + stream.getStreamIdentifier());
}
});
// Step 4 - Call the loop function as usual but pass in the StreamHandler
// instead of your own "raw" PacketHandler.
pcap.loop(streamHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment