我必须为我的大学项目开发一个IDS
。用于嗅探器和算法的java代码可供我使用。我必须使它支持1 GB以太网流量/秒。为此,我们计划整合multi-threading
并在双核机器上运行代码。我打算在IP
的基础上为每个客户端创建一个单独的线程。程序的主函数调用类packetLoader
{implements packetReciever
}的方法openInterface()
。openInterface()
方法打开NIC
接口并开始捕获数据包。我应该改变openInterface()
的这个方法来合并multi-threading
吗?在哪一点我应该开始制作线程?我应该根据什么参数来创建单独的线程?我应该如何实现所需的multi-threading
干杯:)
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);
}
}
发布于 2012-12-13 17:28:52
我不确定是否会为每个客户端(IP地址)产生一个新线程。如果它只用于大学项目,它可能是可以的,但对于更真实的场景,如果客户数量变得更大,它可能会过度杀伤力(和性能打击)。相反,我会创建一个固定大小的工作线程池(请参阅java.util.concurrent.Executors
和java.util.concurrent.ExecutorService
) (从n+1
到2*n
的CPU是一个很好的开始),并向它们传递要分析的数据包。在您的例子中,它可以在您调用getPacket()
的for
循环中的代码示例的末尾完成。当然,执行器池必须在应用程序启动时初始化。
除此之外,通常实现IDS并不是一件微不足道的事情,因为要真正正确地实现它,单独分析每个数据包是不够的。由于IP碎片,数据包是以碎片形式传入的,因此可能需要将其中的几个合并在一起,以正确检测入侵。但这不在你的问题范围之内……
https://stackoverflow.com/questions/13856065
复制相似问题