Class WinPcap

49 篇文章 1 订阅

org.jnetpcap.winpcap 
Class WinPcap

java.lang.Object
  extended by org.jnetpcap.Pcap
      extended by org.jnetpcap.winpcap.WinPcap

public class WinPcap
    
    
     
     extends 
     
     Pcap
    
    

Class peered with native pcap_t structure providing WinPcap specific extensions to libpcap library. To access WinPcap extensions, you must use WinPcap class and its methods. WinPcap class extends Pcap class so you have all of the typeical Pcap class functionality. WinPcap provides many additional methods which are only available on platforms what support WinPcap. First you must use static WinPcap.isSupported() method call which will return a boolean that will indicate if WinPcap extensions are supported on this particular platform. If you try and use any method in this class when WinPcap extensions are not supported, another words WinPcap.isSupported() returned false, every method in this calls will throw unchecked PcapExtensionNotAvailableException. Of course,isSupported call itself never throws an exception. So its safe to use on any platform.

 // Before using any WinPcap code
 if (WinPcap.isSupported() == false) {
        return; // Can't use WinPcap extensions
 }
 

Using WinPcap class

For the most part, you use WinPcap the same way you would use Pcap class. WinPcap class provides many different static methods, and the same main three methods that Pcap does to open a capture session, plus one extra. They are:
  • openLive - opens a live capture from network interface
  • openOffline - opens a capture file
  • openDead - opens a dummy capture for filter compiling
  • open - special open command that uses the source string syntax to accomplish the same tasks as the three openXXX methods before it.
There are also several addition methods:
  • findAllDevsEx - extended version of Pcap.findAllDevs which allows you to not only find network interfaces, but also PCAP files. This can be done locally or remotely.
  • liveDump - which can dump captured packets to a savefile automatically at the kernel level.
  • sendQueueTransmit - and related method, which allow raw packets to be sent in bulk, efficiently.
  • setMinToCopy, setMode and setBuf - allow tweaking of kernel buffers and enable/disable statistical captures
  • offlineFilter - ability to apply the BPF filter on your own packets without a capture
  • setSampling - changes the mode of the capture where only samples of a capture are retruend. packets
  • statEx - extended statistics that include counters on RPCAP remote connection

Using WinPcap.findAllDevsEx

The new method uses source string and WinPcapRmtAuth object and allows remote lookups of interfraces and files. A local lookup:
 String source = "rpcap://";
 List<PcapIf> alldevs = new ArrayList<PcapIf>();
 
 int r = WinPcap.findAllDevsEx(source, auth, alldevs, errbuf);
 if (r != Pcap.OK) {
        fail(errbuf.toString());
        return;
 }
 
 System.out.println("device list is " + alldevs);
 
Now we have a list of PcapIf objects. You can use PcapIf.getName() which contains already properly formatted name to be passed to WinPcap.open call.

Using WinPcap.open method

Once you have a reference to a WinPcap object, you can then call any of its dynamic methods. Here is a straight forward example how to open a capture session and then close it:
 WinPcap pcap = WinPcap.openLive(device, snaplen, flags, timeout, errbuf);
 // Do something
 pcap.close();
 
This is identical to Pcap.openLive method with the exception that WinPcap object is returned. WinPcap extends Pcap. Here is the same example this time using WinPcap's source string code and a bogus device name (you will need to substitute your own actual device name):
  String source = "rpcap:Device//NPF_{BC81C4FC-242F-4F1C-9DAD-EA9523CC992D}";
  int snaplen = 64 * 1024;
  int flags = Pcap.MODE_NON_PROMISCUOUS;
  int timeout = 1000;
  WinPcapRmtAuth auth = null;
  StringBuilder errbuf = new StringBuilder();
 
  WinPcap pcap = WinPcap.open(source, snaplen, flags, timeout, auth, errbuf);
  if (pcap == null) {
        System.err.println(errbuf.toString());
        return;
  }
  pcap.close(); }
 
We use open method which takes a WinPcapRmtAuth object. We could set username and password in it, but we chose the 'NULL' authentication method. The remote server has to be configured with a '-n' command line argument to access 'NULL' authentication.

Author:
Mark Bednarczyk, Sly Technologies, Inc.
See Also:
Pcap

Field Summary
static intMODE_CAPT 
          default capture mode
static intMODE_MONITOR 
          monitor mode
static intMODE_STAT 
          statistical mode
static intOPENFLAG_DATATX_UDP 
          Defines if the data trasfer (in case of a remote capture) has to be done with UDP protocol and can only be used with WinPcap.open.
static intOPENFLAG_MAX_RESPONSIVENESS 
          This flag configures the adapter for maximum responsiveness and can only be used with WinPcap.open.
static intOPENFLAG_NOCAPTURE_LOCAL 
          Defines if the local adapter will capture its own generated traffic and can only be used with WinPcap.open.
static intOPENFLAG_NOCAPTURE_RPCAP 
          Defines if the remote probe will capture its own generated traffic and can only be used with WinPcap.open.
static intSRC_FILE 
          Used to create a source string using method createSrcStr, which will be used to open a local capture file.
static intSRC_IFLOCAL 
          Used to create a source string using method createSrcStr, which will be used to open a local network interface.
static intSRC_IFREMOTE 
          Used to create a source string using method createSrcStr,which will be used to open a remote connection (could be file, or network interface on remote system).
static intTRANSMIT_SYNCH_ASAP 
          Flag used with sendQueueTransmit(WinPcapSendQueue, int), to tell kernel to send packets as fast as possible, without synchronizing with packet timestamps found in headers.
static intTRANSMIT_SYNCH_USE_TIMESTAMP 
          Flag used with sendQueueTransmit(WinPcapSendQueue, int), to tell kernel to send packets at the rate that is determined by the timestamp with in the sendqueue.
 
Fields inherited from class org.jnetpcap.Pcap
DISPATCH_BUFFER_FULLJNETPCAP_LIBRARY_NAMELOOP_INFINATELOOP_INTERRUPTEDMODE_BLOCKINGMODE_NON_BLOCKINGMODE_NON_PROMISCUOUSMODE_PROMISCUOUSNEXT_EX_EOF,NEXT_EX_NOT_OKNEXT_EX_OKNEXT_EX_TIMEDOUTNOT_OKOK
 
Method Summary
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值