我想使用ping6请求监控ipv6设备。请告诉我有没有任何开源库可以发送和接收Ping6请求。我尝试使用JpCap,但无法成功,我收到以下错误消息
java.io.IOException:仅限IPv4数据包 支持 在jpcap.JpcapSender.nativeSendPacket(原生 方法) 在jpcap.JpcapSender.sendPacket(JpcapSender.java:93) 在Ping6.sendPing6Request(Ping6.java:131) 在Ping6.main(Ping6.java:40)
请帮我解决这个问题。有什么办法可以使用JpCap发送Ping6,
非常感谢您的想法。
-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;
}
}
} */
}
}
}
我的实现如下,
答案 0 :(得分:1)
其实我是jpcap的新手,但是你的网络接口可能不支持ipv6,或者你已经禁用了网络接口的ipv6功能? 在Windows 7中执行以下操作以启用(如果可用)ipv6用于网络接口:
Internet Protocol Version 6 (TCP/IP v6)
希望它有所帮助。
答案 1 :(得分:0)
我不知道JpCap,但对我而言,您似乎正在创建IPv4 IPPacket和IPv6 ICMPPacket。您正在IPPacket上使用setIPv4Parameter()并使用IPv4地址指定目标地址。我怀疑是否可以像这样发送IPv6 ICMPPacket。