RawSocketChannel extends OioByteStreamChannel { @Override protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if(!(remoteAddress instanceof RawSocketAddress)) { logger.error("Expecting remote address of type RawSocketAddress"); pipeline().fireExceptionCaught( new PcapException("Expecting remote address of type RawSocketAddress")); return; } this.localAddress = localAddress; remoteRawSocketAddress = (RawSocketAddress) remoteAddress; String deviceName = getDeviceName(remoteRawSocketAddress); if(deviceName == null) { logger.error("Network device not specified and couldn't detect it automatically"); pipeline().fireExceptionCaught( new PcapException("Network device not specified and couldn't detect it automatically")); return; } PcapNetworkInterface nif = Pcaps.getDevByName(deviceName); handle = nif.openLive(65535, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10); if(logger.isDebugEnabled()) { logger.debug(String.format("Listening on device %s", deviceName)); } String filter = config.getFilterString(localAddress, remoteAddress); if(filter.length() > 0) { handle.setFilter(filter, BpfProgram.BpfCompileMode.OPTIMIZE); } ByteBuf buffer = Unpooled.buffer(); loopThread = new Thread(() -> { try { handle.loop(-1, new PacketListener() { @Override public void gotPacket(Packet packet) { buffer.writeBytes(config.getPacketHandler().getData(packet)); } }); } catch (PcapNativeException | NotOpenException e) { logger.error("Pcap4j loop thread died!", e); pipeline().fireExceptionCaught(e); } catch (InterruptedException e) { logger.warn("PCAP Loop Thread was interrupted (hopefully intentionally)", e); Thread.currentThread().interrupt(); } }); loopThread.start(); activate(new PcapInputStream(buffer), new DiscardingOutputStream()); } RawSocketChannel(); @Override ChannelConfig config(); @Override boolean isOpen();  }
@Test @RequirePcap @Disabled("Disabled as currently we can't seem to be able to get it working on Jenkins") public void doConnect() throws Exception { Channel channel = null; final EventLoopGroup workerGroup = new OioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup); bootstrap.channel(RawSocketChannel.class); bootstrap.handler(new ChannelInitializer<RawSocketChannel>() { @Override protected void initChannel(RawSocketChannel ch) throws Exception { System.out.println("Initialize Buffer!"); ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println(ByteBufUtil.prettyHexDump(((ByteBuf) msg))); } }); ch.pipeline().addLast(new ChannelHandlerAdapter() { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); } }); } }); PcapNetworkInterface loopbackDevice = Pcaps.findAllDevs().stream().filter( PcapNetworkInterface::isLoopBack).findFirst().orElse(null); assertNotNull(loopbackDevice); final ChannelFuture f = bootstrap.connect(new RawSocketAddress(loopbackDevice.getName())); f.sync(); channel = f.channel(); System.out.println("Channel is connected and ready to use..."); channel.writeAndFlush(Unpooled.wrappedBuffer("Hallo".getBytes())); Thread.sleep(10_000); } finally { if (channel != null) { channel.close().sync(); } workerGroup.shutdownGracefully().sync(); } }