klingon

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

package com.xxxxxx.xxxxxx;

 

import java.net.InetAddress;

import java.util.ArrayList;

import java.util.GregorianCalendar;

import java.util.List;

import jpcap.JpcapCaptor;

import jpcap.JpcapSender;

import jpcap.NetworkInterface;

import jpcap.packet.EthernetPacket;

import jpcap.packet.ICMPPacket;

import jpcap.packet.IPPacket;

 

public class JPing {

   

    private JpcapCaptor capter;

    private JpcapSender sender;

    private ICMPPacket p;

    private List<String> listResult = new ArrayList<String>();

    private static final int PINGCOUNT = 4;

    private static final int DEVICEINDEX = 3;

 

    /**

     *

     * @param ip

     */

    public void ping(String ip) {

        try {

            NetworkInterface[] devices = JpcapCaptor.getDeviceList();

            capter = JpcapCaptor.openDevice(devices[DEVICEINDEX], 200, false, 20);

            sender = capter.getJpcapSenderInstance();

            capter.setFilter("icmp", true);

           

            p = new ICMPPacket();

            p.type = ICMPPacket.ICMP_ECHO;

            p.setIPv4Parameter(0, false, false, false, 0, false,false, false, 0, 1010101,

                    100, IPPacket.IPPROTO_ICMP,

                    devices[DEVICEINDEX].addresses[1].address, InetAddress.getByName(ip));

            p.data = "data".getBytes();

            EthernetPacket ethernetPacket = new EthernetPacket();

            ethernetPacket.frametype = EthernetPacket.ETHERTYPE_IP;

            ethernetPacket.src_mac = devices[DEVICEINDEX].mac_address;

            ethernetPacket.dst_mac = new byte[] { (byte) 0x00, (byte) 0x0e, (byte) 0x6a,

                    (byte) 0x8f, (byte) 0x52, (byte) 0x00 };

            p.datalink = ethernetPacket;

           

            listResult.add("Pinging " + p.dst_ip + " with " + p.data.length + " bytes of data: ");

           

            startCapThread(capter);

            for (int i = 0; i < PINGCOUNT; i++) {

                p.sec = 0;

                p.usec = new GregorianCalendar().getTimeInMillis();

                p.seq = (short) (1000 + i);

                p.id = (short) (999 + i);

                sender.sendPacket(p);

                try {

                    Thread.sleep(1000);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

    /**

     * ½ÓÊÕICMP±¨ÎÄ»ù±¾·½·¨

     *

     * @param capter

     */

    public void getIcmpPacket(JpcapCaptor capter) {

        try {

            int flag = PINGCOUNT;

            long tmp = 0;

            String tmpStr = null;

            ICMPPacket rp;

            while (flag > 0) {

 

                rp = (ICMPPacket) capter.getPacket();

                if ((rp != null) && rp.type == ICMPPacket.ICMP_UNREACH_TOSNET && rp.seq - rp.id == 1 ) {

                    System.out.println("Reply from "+ rp.src_ip.getHostAddress() + ": Cannot access the target host");

                    listResult.add("Reply from "+ rp.src_ip.getHostAddress() + ": Cannot access the target host");

                    flag--;

                    continue;

                }

                if ((rp != null) && rp.type == ICMPPacket.ICMP_UNREACH_PORT && rp.seq - rp.id == 1 ) {

                    System.out.println("Reply from "+ rp.src_ip.getHostAddress() + ": Cannot access the target host");

                    listResult.add("Reply from "+ rp.src_ip.getHostAddress() + ": Packet overtime");

                    flag--;

                    continue;

                }

 

                if ((rp == null) || rp.type != ICMPPacket.ICMP_ECHOREPLY || rp.seq - rp.id != 1 ) {

                    continue;

                }

                tmp = (rp.sec * 1000 + rp.usec / 1000 - p.sec

                        * 1000 - p.usec);

                if (tmp <= 0)

                    tmpStr = " < 1 ms ";

                else

                    tmpStr = "= " + tmp + " ms  ";

 

                System.out.println("Reply from " + rp.src_ip.getHostAddress() + ": bytes = " + rp.data.length

                        + " time " + tmpStr + "TTL = " + rp.hop_limit);

 

                listResult.add("Reply from " + rp.src_ip.getHostAddress()

                        + ": bytes = " + rp.data.length + " time " + tmpStr

                        + "TTL = " + rp.hop_limit);

                flag--;

 

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

 

    }

 

    /**

     * ÆôÓöàÏ߳̽ÓÊÕICMP±¨ÎÄ

     *

     * @param capter

     */

    public void startCapThread(final JpcapCaptor capter) {

 

        Runnable runner = new Runnable() {

            public void run() {

                getIcmpPacket(capter);

            }

        };

        new Thread(runner).start();

    }

 

    public static void main(String[] args) {

        //

        NetworkInterface[] devices = JpcapCaptor.getDeviceList();

        if (args.length < 1) {

            System.out

                    .println("Usage: java ICMP <device index (e.g., 0, 1..)>");

            for (int i = 0; i < devices.length; i++) {

                System.out.println(i + ":" + devices[i].name + "("

                        + devices[i].description + ")");

                if (2 == devices[i].addresses.length) {

                    System.out.println(devices[i].addresses[0].address.getHostAddress() + "" +

                            devices[i].addresses[0].address.getHostAddress());

                }

            }

            System.exit(0);

        }

 

        new JPing().ping("202.108.22.5");

    }

}

posted on 2011-12-09 14:47  klingon  阅读(533)  评论(0)    收藏  举报