Java IDS中的多线程

Java IDS中的多线程,java,multithreading,ip,packet-sniffers,intrusion-detection,Java,Multithreading,Ip,Packet Sniffers,Intrusion Detection,我必须为我的大学项目开发一个IDS。我可以使用嗅探器和算法的java代码。我必须使它能够支持每秒1 GB的以太网流量。为此,我们计划合并多线程,并在双核机器上运行代码。我打算在IP的基础上为每个客户端创建一个单独的线程。 程序的主函数调用类packetLoader{implementspacketreceiver}的方法openInterface()。方法openInterface()打开NIC接口并开始捕获数据包。 我是否应该更改openInterface()的此方法以合并多线程?我应该在什么

我必须为我的大学项目开发一个
IDS
。我可以使用嗅探器和算法的java代码。我必须使它能够支持每秒1 GB的以太网流量。为此,我们计划合并
多线程
,并在双核机器上运行代码。我打算在
IP
的基础上为每个客户端创建一个单独的线程。 程序的主函数调用类
packetLoader
{implements
packetreceiver
}的方法
openInterface()
。方法
openInterface()
打开
NIC
接口并开始捕获数据包。 我是否应该更改
openInterface()
的此方法以合并
多线程
?我应该在什么时候开始制作线程?我应该根据什么参数创建单独的线程?我应该如何实现所需的
多线程

干杯:)

public void openInterface(字符串过滤器,int-numOfPackets){
试一试{
如果(!devName.startsWith(NIC_NAME_PREFIX)){
如果(numOfPackets==-1)
包装抽样率=1;
否则{
packetsamplinglatio=numOfPackets/(两倍)totalPcapFilePackets;
}
}
//jpcaptor captor=null;
if(devName.startsWith(NIC_NAME_前缀)){
System.err.println(“内部openinterface”);
NetworkInterface[]DeviceList=JpcapCaptor.getDeviceList();
系统错误println(“……内部openinterface 2”);
字符串nicName=devName.substring(NIC_NAME_PREFIX.length());
int-nicID=-1;
对于(int i=0;i=0){
captor=JpcapCaptor.openDevice(设备列表[1],
NIC_SNAPLEN,true,NIC_超时);
System.err.println(“……设备已打开,可使用”)捕获数据包;
System.err.println(“NIC_SNAPLEN=“+NIC_SNAPLEN+”和NIC_TIMEOUT=“+NIC_TIMEOUT”);
}
否则{
System.err.println(“网络接口”+nicName
+“找不到!”);
System.err.println(“可用标签”);

对于(int k=0;k我不确定是否会为每个客户机(IP地址)生成一个新线程。如果只是针对大学项目,这可能还可以,但对于更真实的场景,如果客户机数量增加,这可能是一个过度杀伤力(和性能损失)。相反,我会创建一个工作线程池(请参见
java.util.concurrent.Executors
java.util.concurrent.ExecutorService
)具有固定大小(
n+1
2*n
的CPU是一个良好的开端),并将要分析的数据包传递给它们。 在您的情况下,可以在调用
getPacket()
for
循环中的代码示例末尾执行。当然,必须在应用程序启动时初始化执行器池

除此之外,通常实现IDS并不是一件小事,因为要使它真正正确,单独分析每个数据包是不够的。由于IP碎片化,数据包以碎片形式出现,因此可能需要将它们合并在一起以正确检测入侵。但这与你问题的范围

public void openInterface(String filter, int numOfPackets){
    try {
        if (!devName.startsWith(NIC_NAME_PREFIX)) {             
            if(numOfPackets == -1)
                packetSamplingRatio = 1;
            else {
                packetSamplingRatio = numOfPackets/(double)totalPcapFilePackets;
            }
        }

        //JpcapCaptor captor = null;
        if (devName.startsWith(NIC_NAME_PREFIX)) {
                        System.err.println(".........inside openinterface");
            NetworkInterface[] devicesList = JpcapCaptor.getDeviceList();
                                         System.err.println(".........inside openinterface 2");

            String nicName = devName.substring(NIC_NAME_PREFIX.length());
            int nicID = -1;
            for (int i = 0; i < devicesList.length; i++) {

                                System.err.println(".........inside openinterface 3");
                if (devicesList[i].name.equals(nicName)){
                                        System.err.println("Device no:" + i + "=" +devicesList[i].name);
                                        System.err.println("capturing on device= " + devicesList[i].name);
                    nicID = i;}
            }
            if (nicID >= 0){

                                captor = JpcapCaptor.openDevice(devicesList[1],
                        NIC_SNAPLEN, true, NIC_TIMEOUT);
                            System.err.println(".........Device is open for packet capturing with");
                            System.err.println("NIC_SNAPLEN = " + NIC_SNAPLEN + " and NIC_TIMEOUT=" + NIC_TIMEOUT);

                            }
            else {
                System.err.println("Network interface " + nicName
                        + "cannot be found!");
                System.err.println("Availabel NICs:");
                for(int k=0; k<devicesList.length; k++) {
                    System.out.println("- " + devicesList[k]);
                }
                System.exit(1);
            }
        } else {
                        System.err.println(".........inside else");
            captor = JpcapCaptor.openFile(devName);
        }

        if (filter != null){
            captor.setFilter(filter, true);
                   ;
                    }// Start reading packets
                    System.err.println(".........filter checked");
                    //PacketStorage ps = new PacketStorage(); 
        //captor.loopPacket(numOfPackets, this);
                    //captor.processPacket(numOfPackets, this);
                    for(int j =0; j<numOfPackets ; j++){
                    captor.getPacket();

                    System.err.println(".........captured packet" + j);

                    }
                    System.err.println(".........after capture.looppacket");
    }

    catch (IOException e) {
        System.err.println("Exception in openDevice " + e);
        System.exit(1);
    }
}