[英]Multi-threading in IDS
我必須為我的大學項目開發一個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);
}
}
我不確定是否要為每個客戶端(IP地址)生成一個新線程。 如果僅用於大學項目,則可以,但是對於更真實的場景,如果客戶數量增加,則可能會過大(並影響性能)。 相反,我將創建一個固定大小的工作線程池(請參閱java.util.concurrent.Executors
和java.util.concurrent.ExecutorService
)( n+1
至2*n
個CPU是一個好的開始),然后將它們傳遞給要分析的數據包。 在您的情況下,可以在for
循環的代碼示例結尾處調用getPacket()
。 當然,執行程序池必須在應用程序啟動時進行初始化。
除此之外,一般而言,實施IDS並不是一件容易的事,因為要使其正確使用,僅對每個數據包進行單獨分析是不夠的。 由於IP分段,數據包會分成碎片,因此可能有必要將其中的幾個合並在一起以正確檢測入侵。 但這與問題范圍不同...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.