java socket 網路攔截

來源:互聯網
上載者:User

本程式用eclipse編輯在J2SDK6.0+WinPcap 3.1+Jpcap 0.5下編譯運行

/*******************
* JpcapTip.java
*/
package m;

import jpcap.PacketReceiver;
import jpcap.JpcapCaptor;
import jpcap.packet.*;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
//import java.net.InetAddress;
//import java.net.UnknownHostException;

public class JpcapTip implements PacketReceiver {

public void receivePacket(Packet packet) {
   System.out.println("********************************************");
   /*IP資料報報文頭*/
   byte[] l=packet.header;     
   /*
   for (int t=0;t<21;t++){
    System.out.print(l[t]+" *** ");
   }
   */
   String str="";
    System.out.print("報文頭 : ");
    for (int i=0;i<l.length;i++) {
     //str=str+l;
     int m=0;
     m=l[i];
     m=m<<24;
     m=m>>>24;
     str=str+Integer.toHexString(m);
     //System.out.print("     ***     "+l[i]);
    }
   System.out.println(str);
   int d=l.length;
   System.out.println("首部長度 :"+(d*8)+"bit");
  
   /*分析源IP地址和目的IP地址*/
   /*分析協議類型*/
   /**
   if(packet.getClass().equals(IPPacket.class)) {
    IPPacket ipPacket=(IPPacket)packet;
    byte[] iph=ipPacket.option;
    String iphstr=new String(iph);
    System.out.println(iphstr);
   }
   */
   if(packet.getClass().equals(ARPPacket.class))
   {
    System.out.println("協議類型 :ARP協議");
    try {
     ARPPacket arpPacket = (ARPPacket)packet;
     System.out.println("源網卡MAC地址為 :"+arpPacket.getSenderHardwareAddress());
     System.out.println("源IP地址為 :"+arpPacket.getSenderProtocolAddress());
     System.out.println("目的網卡MAC地址為 :"+arpPacket.getTargetHardwareAddress());
     System.out.println("目的IP地址為 :"+arpPacket.getTargetProtocolAddress());
    
    
    } catch( Exception e ) {
     e.printStackTrace();
    } 
   }
   else 
    if(packet.getClass().equals(UDPPacket.class))
    {
     System.out.println("協議類型 :UDP協議");
     try {
      UDPPacket udpPacket = (UDPPacket)packet;
      System.out.println("源IP地址為 :"+udpPacket.src_ip);
      int tport = udpPacket.src_port;
      System.out.println("源連接埠為:"+tport);
      System.out.println("目的IP地址為 :"+udpPacket.dst_ip);
      int lport = udpPacket.dst_port;
      System.out.println("目的連接埠為:"+lport);
     } catch( Exception e ) {
      e.printStackTrace();
     } 
    }
   else
    if(packet.getClass().equals(TCPPacket.class)) {
     System.out.println("協議類型 :TCP協議");
     try {
      TCPPacket tcpPacket = (TCPPacket)packet;
      int tport = tcpPacket.src_port;
      System.out.println("源IP地址為 :"+tcpPacket.src_ip);
      System.out.println("源連接埠為:"+tport);
      System.out.println("目的IP地址為 :"+tcpPacket.dst_ip);
      int lport = tcpPacket.dst_port;
      System.out.println("目的連接埠為:"+lport);
     } catch( Exception e ) {
      e.printStackTrace();
     }
    }
   else
    if(packet.getClass().equals(ICMPPacket.class))
     System.out.println("協議類型 :ICMP協議");
   else
     System.out.println("協議類型 :GGP、EGP、JGP協議或OSPF協議或ISO的第4類運輸協議TP4");
    
    
   
   /*IP資料報文資料*/
   byte[] k=packet.data;   
   String str1="";
    System.out.print("資料 : ");
     for(int i=0;i<k.length;i++) {
      //int m=0;
      //m=k[i];
      //m=m<<24;
      //m=m>>>24;
      //str1=str+Integer.toHexString(m);
      str1 = new String(k);
      //str1=str1+k[i];
      //System.out.print("     ***     "+k[i]);
     }
     System.out.println(str1);
   System.out.println("資料報類型 : "+packet.getClass());

   System.out.println("********************************************");
}

public static void main(String[] args) throws Exception{
   // TODO 自動產生方法存根
  
   NetworkInterface[] devices = JpcapCaptor.getDeviceList();        //.getDeviceList();.
   //for (int i =0; i<devices.length;i++) {
    int a=0;
    //try {
    /*本網資訊*/
    byte[] b=devices[1].mac_address; //網卡物理地址
    //}
    //catch() {}
    System.out.print("網卡MAC : 00");   
    for (int j=0;j<b.length;j++){
     //a=a<<8;
     a=b[j];
     a=a<<24;
     a=a>>>24;
     System.out.print(Integer.toHexString(a));
    }
    System.out.println();
    NetworkInterfaceAddress[] k=devices[1].addresses;
   
    //System.out.println("網卡MAC : "+Integer.toHexString(a));
    for(int n=0;n<k.length;n++) {
     System.out.println("本機IP地址 : "+k[n].address);     //本機IP地址
     System.out.println("子網路遮罩   : "+k[n].subnet);      //子網路遮罩
    }
    System.out.println("網路連接類型 : "+devices[1].datalink_description);
   //}
   NetworkInterface deviceName = devices[1];
   /*將網卡設為混雜模式下用網路裝置deviceName*/
   JpcapCaptor jpcap =JpcapCaptor.openDevice(deviceName, 2000, false, 1);           //openDevice(deviceName,1028,false,1);
   jpcap.loopPacket(-1,new JpcapTip());
}

}

 

 

轉自:http://hi.baidu.com/wr5666/blog/item/a50e32cf03e34e3fb700c878.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.