I/O Multiplexing
Capturing on multiple interfaces with single thread.
Minimum version: 1.1.x or above.
public static void main(String[] _args) throws Exception {
var service = Service.Creator.create("PcapService");
var dev1 = service.interfaces();
var dev2 = dev1.next();
var pcap1 = service.live(dev1, new DefaultLiveOptions());
var pcap2 = service.live(dev2, new DefaultLiveOptions());
PacketHandler<Pcap> handler =
(args, header, buffer) -> {
System.out.println("Args : " + args);
System.out.println("Header : " + header);
System.out.println("Packet : " + buffer);
};
var selector = service.selector();
selector.register(pcap1);
selector.register(pcap2);
var timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO);
try {
var selectables = selector.select(timeout);
var iterator = selectables.iterator();
while (iterator.hasNext()) {
var next = iterator.next();
var pcap = (Pcap) next;
try {
pcap.dispatch(1, handler, pcap);
} catch (BreakException e) {
System.err.println(e);
}
}
} catch (TimeoutException e) {
System.err.println(e);
}
selector.close();
pcap1.close();
pcap2.close();
}
fun main(args: Array<String>) {
val service = Service.Creator.create("PcapService")
val dev1 = service.interfaces()
val dev2 = dev1.next()
val pcap1 = service.live(dev1, DefaultLiveOptions())
val pcap2 = service.live(dev2, DefaultLiveOptions())
val handler = PacketHandler { args: Pcap, header: PacketHeader, buffer: PacketBuffer ->
println("Args : $args")
println("Header : $header")
println("Packet : $buffer")
}
val selector = service.selector()
selector.register(pcap1)
selector.register(pcap2)
val timeout: Timeout = DefaultTimeout(1000000L, Timeout.Precision.MICRO)
val selectables = selector.select(timeout)
val iterator: Iterator<Selectable> = selectables.iterator()
while (iterator.hasNext()) {
val next = iterator.next()
val pcap = next as Pcap
pcap.dispatch(1, handler, pcap)
}
selector.close()
pcap1.close()
pcap2.close()
}
def main(_args: Array[String]): Unit = {
val service = Service.Creator.create("PcapService")
val dev1 = service.interfaces()
val dev2 = dev1.next()
val pcap1 = service.live(dev1, new DefaultLiveOptions())
val pcap2 = service.live(dev2, new DefaultLiveOptions())
val handler = new PacketHandler[(Pcap)] {
override def gotPacket(args: Pcap, header: PacketHeader, buffer: PacketBuffer): Unit = {
println("Args : ", args)
println("Header : ", header)
println("Packet : ", buffer)
}
}
val selector = service.selector()
selector.register(pcap1)
selector.register(pcap2)
val timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO)
val selectables = selector.select(timeout)
val iterator = selectables.iterator()
while (iterator.hasNext) {
val selectable = iterator.next()
val pcap = selectable.asInstanceOf[Pcap]
pcap.dispatch(1, handler, pcap)
}
selector.close()
pcap1.close()
pcap2.close()
}
static void main(String[] _args) {
def service = Service.Creator.create("PcapService")
def dev1 = service.interfaces()
def dev2 = dev1.next()
def pcap1 = service.live(dev1, new DefaultLiveOptions())
def pcap2 = service.live(dev2, new DefaultLiveOptions())
PacketHandler<Pcap> handler = new PacketHandler<Pcap>() {
@Override
void gotPacket(Pcap args, PacketHeader header, PacketBuffer buffer) {
println("Args : " + args)
println("Header : " + header)
println("Packet : " + buffer)
}
}
def selector = service.selector()
selector.register(pcap1)
selector.register(pcap2)
def timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO)
try {
Iterable<Selectable> selectables = selector.select(timeout)
Iterator<Selectable> iterator = selectables.iterator()
while (iterator.hasNext()) {
Selectable next = iterator.next()
Pcap pcap = (Pcap) next
try {
pcap.dispatch(1, handler, pcap)
} catch (BreakException e) {
System.err.println(e)
}
}
} catch (TimeoutException e) {
System.err.println(e)
}
selector.close()
pcap1.close()
pcap2.close()
}
Since 1.4.0
public static void main(String[] args) throws Exception {
final Service service = Service.Creator.create("PcapService");
final Interface interfaces = service.interfaces();
final Selector selector = service.selector();
final Iterator<Interface> iterator = interfaces.iterator();
while (iterator.hasNext()) {
final Interface source = iterator.next();
if ((source.flags() & Interface.PCAP_IF_UP) != 0) {
final Pcap live = service.live(source, new DefaultLiveOptions());
final Selection selection = live.register(selector, Selection.OPERATION_READ, null);
assert selection.interestOperations() == Selection.OPERATION_READ;
}
}
final Timeout timeout = new DefaultTimeout(1000000L, Timeout.Precision.MICRO);
for (int i = 0; i < 100; i++) {
try {
final int nSelected =
selector.select(
new Consumer<Selection>() {
@Override
public void accept(Selection selection) {
if (selection.isReadable()) {
final Pcap pcap = (Pcap) selection.selectable();
try {
pcap.dispatch(
1,
new PacketHandler<String>() {
@Override
public void gotPacket(
String s, PacketHeader packetHeader, PacketBuffer packetBuffer) {
System.out.println("Header : " + packetHeader);
}
},
"");
selection.interestOperations(Selection.OPERATION_WRITE);
} catch (BreakException e) {
System.err.println(e);
} catch (ErrorException e) {
System.err.println(e);
} catch (TimeoutException e) {
System.err.println(e);
}
} else if (selection.isWritable()) {
selection.interestOperations(Selection.OPERATION_READ);
}
}
},
timeout);
System.out.println("N selected : " + nSelected);
} catch (TimeoutException e) {
//
}
}
}
Soon
Last updated