Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Add reactor, netty, and nio packet handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ardikars committed Nov 30, 2018
1 parent cdda10a commit b16a177
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 43 deletions.
3 changes: 2 additions & 1 deletion gradle/configure.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ext {

NAME = 'Jxnet'
GROUP = 'com.ardikars.jxnet'
VERSION = '1.4.8.Final'
VERSION = '1.4.9.RC1'
DESCRIPTION = 'Jxnet is a java library for capturing and sending network packet.'

NDK_HOME = "${System.env.NDK_HOME}"
Expand All @@ -34,6 +34,7 @@ ext {
COMMON_VERSION = '1.2.2.Final'
JXPACKET_VERSION = '1.2.0.Final'
NETTY_VERSION = '4.1.31.Final'
REACTOR_VERSION = 'Californium-RELEASE'
JNR_VERSION = '2.1.9'

SPRING_BOOT_VERSION = '2.0.4.RELEASE'
Expand Down
3 changes: 3 additions & 0 deletions jxnet-spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${SPRING_BOOT_VERSION}")
mavenBom("com.ardikars.jxpacket:jxpacket:${JXPACKET_VERSION}")
mavenBom("io.netty:netty-bom:${NETTY_VERSION}")
mavenBom("io.projectreactor:reactor-bom:${REACTOR_VERSION}")
}
}

Expand All @@ -21,6 +23,7 @@ dependencies {
implementation ("com.ardikars.common:common-util")
implementation ("com.ardikars.jxpacket:jxpacket-common")
implementation ("com.ardikars.jxpacket:jxpacket-core")
implementation ("io.projectreactor:reactor-core")
implementation ("org.slf4j:slf4j-api:${SLF4J_VERSION}")
testImplementation ("junit:junit:${JUNIT_VERSION}")
testImplementation ("org.mockito:mockito-core:${MOCKITO_VERSION}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -206,4 +208,16 @@ public StringBuilder errbuf() {
return new StringBuilder(PCAP_ERRBUF_SIZE);
}

/**
* Thread pool.
* @return returns {@link ExecutorService} object.
*/
@Bean("com.ardikars.jxnet.spring.boot.autoconfigure.executorService")
public ExecutorService executorService() {
if (this.properties.getNumberOfThread() == 0) {
return Executors.newCachedThreadPool();
}
return Executors.newFixedThreadPool(this.properties.getNumberOfThread());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class JxnetConfigurationProperties {

private Pcap.PcapType pcapType;

private Integer numberOfThread;

/**
* Initialize field.
*/
Expand Down Expand Up @@ -108,6 +110,9 @@ public void initialize() {
if (file == null) {
file = null;
}
if (numberOfThread == null) {
numberOfThread = 0;
}
}

public String getSource() {
Expand Down Expand Up @@ -214,4 +219,12 @@ public void setPcapType(Pcap.PcapType pcapType) {
this.pcapType = pcapType;
}

public Integer getNumberOfThread() {
return numberOfThread;
}

public void setNumberOfThread(Integer numberOfThread) {
this.numberOfThread = numberOfThread;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2015-2018 Jxnet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;
import io.netty.buffer.ByteBuf;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* Callback function used for capturing packets.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.9
*/
public interface NettyBufferHandler<T> {

/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Future<ByteBuf> packet) throws ExecutionException, InterruptedException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2015-2018 Jxnet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* Callback function used for capturing packets.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.9
*/
public interface NioBufferHandler<T> {

/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Future<ByteBuffer> packet) throws ExecutionException, InterruptedException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2015-2018 Jxnet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ardikars.jxnet.spring.boot.autoconfigure;

import com.ardikars.jxnet.PcapPktHdr;
import com.ardikars.jxpacket.common.Packet;
import java.util.concurrent.ExecutionException;
import reactor.core.publisher.Mono;

/**
* Callback function used for capturing packets.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.9
*/
public interface ReactorPacketHandler<T> {

/**
* Next available packet.
* @param argument user argument.
* @param header pcap header.
* @param packet {@link Packet} object.
* @throws ExecutionException execution exception.
* @throws InterruptedException interrupted exception.
*/
void next(T argument, PcapPktHdr header, Mono<Packet> packet) throws ExecutionException, InterruptedException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2015-2018 Jxnet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ardikars.jxnet.spring.boot.autoconfigure.annotation;

import com.ardikars.jxnet.spring.boot.autoconfigure.constant.PacketHandlerType;
import com.ardikars.jxnet.spring.boot.autoconfigure.selector.JxpacketConfigurationSelector;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

/**
* Enable packet handler configuration.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.9
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(JxpacketConfigurationSelector.class)
public @interface EnablePacket {

/**
* Packet handler type.
* @return a {@link PacketHandlerType} object.
*/
PacketHandlerType packetHandlerType() default PacketHandlerType.JXPACKET;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2015-2018 Jxnet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ardikars.jxnet.spring.boot.autoconfigure.constant;

/**
* Packet handler type.
*
* @author <a href="mailto:[email protected]">Ardika Rommy Sanjaya</a>
* @since 1.4.9
*/
public enum PacketHandlerType {

JXPACKET, NETTY_BUFFER, NIO_BUFFER, REACTOR, RXJAVA

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@
import com.ardikars.jxpacket.core.ip.ip6.Routing;
import com.ardikars.jxpacket.core.tcp.Tcp;
import com.ardikars.jxpacket.core.udp.Udp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
Expand All @@ -56,30 +53,16 @@
public class JxpacketAutoconfiguration {

private final Boolean autoRegister;
private final Integer numberOfThread;

/**
*
* @param properties jxpacket configuration properties.
*/
public JxpacketAutoconfiguration(JxpacketConfigurationProperties properties) {
this.autoRegister = properties.getAutoRegister();
this.numberOfThread = properties.getNumberOfThread();
register();
}

/**
* Thread pool.
* @return returns {@link ExecutorService} object.
*/
@Bean("com.ardikars.jxnet.spring.boot.autoconfigure.jxpacket.executorService")
public ExecutorService executorService() {
if (this.numberOfThread == 0) {
return Executors.newCachedThreadPool();
}
return Executors.newFixedThreadPool(this.numberOfThread);
}

private void register() {
if (this.autoRegister) {
DataLinkLayer.register(DataLinkLayer.EN10MB, new Ethernet.Builder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class JxpacketConfigurationProperties {

private Boolean autoRegister;

private Integer numberOfThread;

/**
* Initialize properties.
*/
Expand All @@ -42,9 +40,6 @@ public void initialize() {
if (autoRegister == null) {
this.autoRegister = false;
}
if (numberOfThread == null) {
numberOfThread = 0;
}
}

public Boolean getAutoRegister() {
Expand All @@ -55,12 +50,4 @@ public void setAutoRegister(Boolean autoRegister) {
this.autoRegister = autoRegister;
}

public Integer getNumberOfThread() {
return numberOfThread;
}

public void setNumberOfThread(Integer numberOfThread) {
this.numberOfThread = numberOfThread;
}

}
Loading

0 comments on commit b16a177

Please sign in to comment.