posts - 156,  comments - 601,  trackbacks - 0
          在網(wǎng)絡(luò)上看到了這個(gè)項(xiàng)目,本人對(duì)這個(gè)不太了解,但挺興趣所以也推薦給大家,希望能一起學(xué)習(xí)。

          jNetPcap是libpcap的一個(gè)Java完整封裝。jNetPcap使 用與libpcap相同風(fēng)格的API。libpcap是unix/linux平臺(tái)下的網(wǎng)絡(luò)數(shù)據(jù)包捕獲函數(shù)庫,大多數(shù)網(wǎng)絡(luò)監(jiān)控軟件都以它為基礎(chǔ)。 Libpcap可以在絕大多數(shù)類unix平臺(tái)下工作。Libpcap提供了系統(tǒng)獨(dú)立的用戶級(jí)別網(wǎng)絡(luò)數(shù)據(jù)包捕獲接口,并充分考慮到應(yīng)用程序的可移植性。

          jNetPcap 官方網(wǎng)站:http://jnetpcap.com/

          下面是官方上的一些演示示例:
          ClassicPcapExample.java

            1 /**
            2  * Copyright (C) 2008 Sly Technologies, Inc. This library is free software; you
            3  * can redistribute it and/or modify it under the terms of the GNU Lesser
            4  * General Public License as published by the Free Software Foundation; either
            5  * version 2.1 of the License, or (at your option) any later version. This
            6  * library is distributed in the hope that it will be useful, but WITHOUT ANY
            7  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
            8  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
            9  * details. You should have received a copy of the GNU Lesser General Public
           10  * License along with this library; if not, write to the Free Software
           11  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
           12  */
           13 package org.jnetpcap.examples;
           14 
           15 import java.nio.ByteBuffer;
           16 import java.util.ArrayList;
           17 import java.util.Date;
           18 import java.util.List;
           19 
           20 import org.jnetpcap.Pcap;
           21 import org.jnetpcap.PcapHandler;
           22 import org.jnetpcap.PcapIf;
           23 
           24 /**
           25  * This example is the classic libpcap example shown in nearly every tutorial on
           26  * libpcap. It gets a list of network devices, presents a simple ASCII based
           27  * menu and waits for user to select one of those interfaces. We will just
           28  * select the first interface in the list instead of taking input to shorten the
           29  * example. Then it opens that interface for live capture. Using a packet
           30  * handler it goes into a loop to catch a few packets, say 10. Prints some
           31  * simple info about the packets, and then closes the pcap handle and exits.
           32  * 
           33  * @author Mark Bednarczyk
           34  * @author Sly Technologies, Inc.
           35  */
           36 @SuppressWarnings("deprecation")
           37 public class ClassicPcapExample {
           38 
           39   public static void main(String[] args) {
           40         List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
           41         StringBuilder errbuf = new StringBuilder();     // For any error msgs
           42         
           43         /********************************************
           44          * 取得設(shè)備列表
           45          ********************************************/
           46         int r = Pcap.findAllDevs(alldevs, errbuf);
           47         if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
           48             System.err.printf("Can't read list of devices, error is %s", errbuf
           49                 .toString());
           50             return;
           51         }
           52 
           53         System.out.println("Network devices found:");
           54 
           55         int i = 0;
           56         for (PcapIf device : alldevs) {
           57             System.out.printf("#%d: %s [%s]\n", i++, device.getName(), device
           58                 .getDescription());
           59         }
           60 
           61         PcapIf device = alldevs.get(2); // We know we have atleast 1 device
           62         System.out.printf("\nChoosing '%s' on your behalf:\n", device
           63             .getDescription());
           64     
           65         /***************************************
           66          * 打開選中的設(shè)備
           67          ***************************************/
           68         int snaplen = 64 * 1024;           // Capture all packets, no trucation
           69         int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
           70         int timeout = 10 * 1000;           // 10 seconds in millis
           71         Pcap pcap = Pcap
           72             .openLive(device.getName(), snaplen, flags, timeout, errbuf);
           73 
           74         if (pcap == null) {
           75             System.err.printf("Error while opening device for capture: "
           76                 + errbuf.toString());
           77             return;
           78         }
           79         
           80         /**********************************************************************
           81          * Third we create a packet hander which will be dispatched to from the
           82          * libpcap loop.
           83          **********************************************************************/
           84         PcapHandler<String> printSummaryHandler = new PcapHandler<String>() {
           85 
           86             public void nextPacket(String user, long seconds, int useconds,
           87                 int caplen, int len, ByteBuffer buffer) {
           88                 Date timestamp = new Date(seconds * 1000 + useconds/1000); // In millis
           89 
           90                 System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
           91                     timestamp.toString(), // timestamp to 1 ms accuracy
           92                     caplen, // Length actually captured
           93                     len,    // Original length of the packet
           94                     user    // User supplied object
           95                     );
           96             }
           97         };
           98 
           99         /************************************************************
          100          * Fourth we enter the loop and tell it to capture 10 packets
          101          ************************************************************/
          102         pcap.loop(10, printSummaryHandler, "jNetPcap rocks!");
          103 
          104         /*
          105          * Last thing to do is close the pcap handle
          106          */
          107         pcap.close();
          108     }
          109 }
          110 

          PcapDumperExample.java
          package org.jnetpcap.examples;

          import java.io.File;
          import java.nio.ByteBuffer;
          import java.util.ArrayList;
          import java.util.List;

          import org.jnetpcap.Pcap;
          import org.jnetpcap.PcapDumper;
          import org.jnetpcap.PcapHandler;
          import org.jnetpcap.PcapIf;


          public class PcapDumperExample {
            
          public static void main(String[] args) {
              List
          <PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
              StringBuilder errbuf = new StringBuilder();     // For any error msgs

              
          /***************************************************************************
               * First get a list of devices on this system
               *************************************************************************
          */
              
          int r = Pcap.findAllDevs(alldevs, errbuf);
              
          if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
                System.err.printf(
          "Can't read list of devices, error is %s\n"
                  errbuf.toString());
                
          return;
              }
              PcapIf device 
          = alldevs.get(1); // We know we have atleast 1 device

              
          /***************************************************************************
               * Second we open up the selected device
               *************************************************************************
          */
              
          int snaplen = 64 * 1024;           // Capture all packets, no trucation
              int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
              int timeout = 10 * 1000;           // 10 seconds in millis
              Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
              
          if (pcap == null) {
                System.err.printf(
          "Error while opening device for capture: %s\n"
                  errbuf.toString());
                
          return;
              }
                          
              
          /***************************************************************************
               * Third we create a PcapDumper and associate it with the pcap capture
               **************************************************************************
          */
              String ofile 
          = "tmp-capture-file.cap";
              PcapDumper dumper 
          = pcap.dumpOpen(ofile); // output file

              
          /***************************************************************************
               * Fouth we create a packet handler which receives packets and tells the 
               * dumper to write those packets to its output file
               *************************************************************************
          */
              PcapHandler
          <PcapDumper> dumpHandler = new PcapHandler<PcapDumper>() {

                
          public void nextPacket(PcapDumper dumper, long seconds, int useconds,
                  
          int caplen, int len, ByteBuffer buffer) {

                  dumper.dump(seconds, useconds, caplen, len, buffer);
                }
              };

              
          /***************************************************************************
               * Fifth we enter the loop and tell it to capture 10 packets. We pass
               * in the dumper created in step 3
               *************************************************************************
          */
              pcap.loop(
          10, dumpHandler, dumper);
                          
              File file 
          = new File(ofile);
              System.out.printf(
          "%s file has %d bytes in it!\n", ofile, file.length());
                          

              
          /***************************************************************************
               * Last thing to do is close the dumper and pcap handles
               *************************************************************************
          */
              dumper.close(); 
          // Won't be able to delete without explicit close
              pcap.close();
                          
              
          if (file.exists()) {
                file.delete(); 
          // Cleanup
              }
            }
          }

          注:運(yùn)行demo時(shí),需要注意的情況:
          jNetPcap 類庫是都通JNI,調(diào)用系統(tǒng)的動(dòng)態(tài)鏈接庫來實(shí)現(xiàn)與底層設(shè)備的交互。所以運(yùn)行時(shí)需要加載。解決辦法如下:
          設(shè)置 -Djava.library.path參數(shù)
          java -Djava.library.path=c:\jnetpcap\lib -jar myJNetPcapApp.jar


          Good Luck!
          Yours Matthew!
          posted on 2008-11-27 22:55 x.matthew 閱讀(11810) 評(píng)論(1)  編輯  收藏 所屬分類: 最新開源動(dòng)態(tài)
          主站蜘蛛池模板: 石泉县| 内丘县| 临武县| 清镇市| 洛浦县| 阿勒泰市| 沙洋县| 乌海市| 铜鼓县| 佛冈县| 银川市| 襄汾县| 德清县| 泰和县| 荣昌县| 张家界市| 弥勒县| 那坡县| 漳平市| 巴彦县| 土默特左旗| 蒲江县| 黄石市| 鱼台县| 舒城县| 玉林市| 慈利县| 乐亭县| 德钦县| 亳州市| 大英县| 北辰区| 贵溪市| 大方县| 巴马| 鄂尔多斯市| 文成县| 会同县| 新兴县| 乐业县| 新化县|