How to send Ping6 request using Java library (JpCap)

两盒软妹~` 提交于 2019-12-12 22:18:15

问题


I would like to monitor the ipv6 devices using ping6 request. Please let me know is there any open source library is available to send and receive Ping6 request. I tried with JpCap but couldn't succeed and I got the below error message

java.io.IOException: only IPv4 packet is supported at jpcap.JpcapSender.nativeSendPacket(Native Method) at jpcap.JpcapSender.sendPacket(JpcapSender.java:93) at Ping6.sendPing6Request(Ping6.java:131) at Ping6.main(Ping6.java:40)

Please help me how to solve this problem. Is there any way I can send Ping6 using JpCap,

Your thoughts are highly appreciated.

-R.Ravikumar

/**
 *  $Id:$
 *
 * This class is used to send the ping6 request to the agent.
 * 
 *  To get these details we are using JpCap open sourec tool.
 *
 */ 

import jpcap.*;
import jpcap.packet.*;

import java.net.*;
import java.util.*;


/**
 * Ping6.java
 * @author R.Ravikumar 
 * @version 1.0 (Dec 13, 2010).
 *
 */

public class Ping6 implements PacketReceiver
{

 private static Ping6 ping6 = null;

 byte[] gatewayMac = null;
 JpcapCaptor captor = null;
 InetAddress intfNIC = null;
 Map<String,byte[]> ipVsMac = null;
        jpcap.NetworkInterface nic = null;

 public static void main(String args[])
 {
  Ping6 p6 = Ping6.getInstance();
  p6.sendPing6Request(); 
 } 

 public synchronized static Ping6 getInstance()
 {
  if( ping6 == null)
  {
   ping6 = new Ping6();
  }
  return ping6;
 }

 private Ping6()
 {
  //Default Constructor.
  initialize(); 
 }

 private void initialize()
 {
  DefaultGatewayDetails gateway = new DefaultGatewayDetails();
  gatewayMac = gateway.getgatewayMacAddress();
  ipVsMac = gateway.getIpVsMacDetails();
  startCapture();
 }

 public boolean startCapture()
 {
  try
  {
  jpcap.NetworkInterface[] nicList =JpcapCaptor.getDeviceList();

  networkLoop :
   for(jpcap.NetworkInterface machineNIC : nicList)
   {
    for(NetworkInterfaceAddress intfAddr : machineNIC.addresses)
    {
     intfNIC = intfAddr.address;
     if( intfNIC instanceof Inet6Address)
     {
      nic = machineNIC;
      break networkLoop;
     }
    }
   }
   if( nic == null)
   {
     System.err.println("Unable to find the local network interface.");
     return false;
   }

   captor=JpcapCaptor.openDevice(nic,2000,false,0);
  /* PacketReceiver receiver = new PacketReceiver();
          receiver.captor = captor;
   receiver.start(); */
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
   return false;
  }
  return true;
 }

 public void sendPing6Request()
 {
  try
  {
   //create icmp version6 packet.
   ICMPPacket icmp=new ICMPPacket(); 
   icmp.type = ICMPPacket.IPPROTO_IPv6; 
   icmp.seq=100; 
   icmp.id=0; 
   icmp.setIPv6Parameter(0, 0, IPPacket.IPPROTO_IPv6_ICMP, 1, intfNIC, InetAddress.getByName("fe80::3d07:7d5e:f831:e76a")); 
   icmp.data="data".getBytes(); 

   EthernetPacket ether=new EthernetPacket();
   ether.frametype=EthernetPacket.ETHERTYPE_IP;
   ether.src_mac= ipVsMac.get(intfNIC.getHostAddress()); 
   ether.dst_mac=gatewayMac;
   icmp.datalink=ether;

   //create top layer IPv4 packet 
   IPPacket ipPacket = new IPPacket(); 
   ipPacket.setIPv4Parameter(0,false,false,false,0,false,false,false, 
     0,2697,128,IPPacket.IPPROTO_IPv6, 
     InetAddress.getByName("rravikumar"),InetAddress.getByName("192.168.118.2")); 
   ipPacket.datalink = ether; 
   icmp.ippacket = ipPacket;

                     or
                     /*
                       //create icmp version6 packet.
   ICMPPacket icmp=new ICMPPacket(); 
   icmp.type = ICMPPacket.IPPROTO_IPv6; 
   icmp.seq=100; 
   icmp.id=0; 
   icmp.setIPv6Parameter(0, 0, IPPacket.IPPROTO_IPv6_ICMP, 1, intfNIC, InetAddress.getByName("fe80::3d07:7d5e:f831:e76a")); 
   icmp.data="data".getBytes(); 

   EthernetPacket ether=new EthernetPacket();
   ether.frametype=EthernetPacket.ETHERTYPE_IP;
   ether.src_mac= ipVsMac.get(intfNIC.getHostAddress()); 
   ether.dst_mac=gatewayMac;
   icmp.datalink=ether;
                      */ 


   JpcapSender sender=JpcapSender.openDevice(nic);
   sender.sendPacket(icmp);
  }
  catch(Exception exp)
  {
   exp.printStackTrace();
  } 
 }

 public void receivePacket(Packet packet) 
 {
  if(packet instanceof ICMPPacket)
  {
  /* ICMPPacket icmpPacket = (ICMPPacket) packet;
   if( (externalIp.equals(icmpPacket.src_ip) && intfIpList.contains(icmpPacket.dst_ip)) ||
     (externalIp.equals(icmpPacket.dst_ip) && intfIpList.contains(icmpPacket.src_ip))) 
   {
    DatalinkPacket datalink = icmpPacket.datalink;
    if(datalink instanceof EthernetPacket)
    {
     if(externalIp.equals(icmpPacket.src_ip))
     {
          gatewayMac = ((EthernetPacket) datalink).src_mac;
     }
     else
     {
      gatewayMac = ((EthernetPacket) datalink).dst_mac;
     }
    }
   } */
  }
 }

}

My implementation is given below,


回答1:


Actually I'm new to jpcap but maybe your Network Interface does not support ipv6 or you had disabled the ipv6 feature of your network interface? in windows seven do following to enable (if available) ipv6 for network interface:

  1. right click over network con in task bar (right lower corner of screen)
  2. click open network and sharing center
  3. click change adapter setting (left upper corner)
  4. right click the network interface (i.e Local Area Connection) and click properties
  5. Check Internet Protocol Version 6 (TCP/IP v6)
  6. Click Install
  7. Then Apply and OK.

Hope it helps.




回答2:


I don't know JpCap, but for me it looks like you are creating an IPv4 IPPacket and an IPv6 ICMPPacket. You are using setIPv4Parameter() on the IPPacket and specify the destination address using an IPv4 address. I doubt that it is possible to send an the IPv6 ICMPPacket like that.



来源:https://stackoverflow.com/questions/4452814/how-to-send-ping6-request-using-java-library-jpcap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!