2012-12-13 13 views
0

我必須爲我的大學項目開發​​IDS。 java代碼的嗅探器和算法可供我使用。我必須啓用它以支持1 GB以太網流量/秒。爲此,我們計劃合併multi-threading並在雙核機器上運行代碼。我打算根據IP爲每個客戶製作一個單獨的線程。 程序的主要功能調用類packetLoader {implements packetReciever}的方法openInterface()。方法openInterface()打開接口NIC並開始捕獲數據包。 我應該改變這種方法openInterface()納入multi-threading?在哪一點我應該開始製作線程?在什麼參數的基礎上,我應該做出單獨的線程?我應該如何執行所需的multi-threadingIDS中的多線程

歡呼:)

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); 
    } 
} 

回答

0

我不知道我是否會催生爲每個客戶端(IP地址),一個新的線程。如果僅用於大學項目,這可能是可以的,但對於更真實的場景,如果客戶端數量變大,它可能是一種矯枉過正的情況(以及性能下降)。相反,我會創建固定大小的工作線程池(參見java.util.concurrent.Executorsjava.util.concurrent.ExecutorService)(CPU的n+12*n是一個好的開始),並將它們傳遞給要分析的數據包。 在你的情況下,它可以在for循環的代碼示例結束時完成,你可以調用getPacket()。當然,執行程序池必須在應用程序啓動時初始化。

除了所有這些,通常實現IDS並不是一件小事,因爲要使它非常正確,僅分析每個數據包是不夠的。由於IP分片,數據包會成爲碎片,因此可能需要將它們中的一部分合並在一起以正確檢測入侵。但是,這是你的問題範圍之外的不同故事...