
I'am a newbie to java, i have written a small code of jpcap and i get following error, any help will be appreciated,
error:
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at jpcap.Jpcap.
here is my code
package jpcap;
import dao.DAO;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import jpcap.packet.EthernetPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.UDPPacket;
public class Jpcap {
JpcapCaptor captor = null;
int index = 0;
jpcap.NetworkInterface[] devices;
{
devices = JpcapCaptor.getDeviceList();
try{
//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.setFilter("udp", true);
}catch(IOException e){}
}
public void sendBroadCast(Packet pack) {
try {
InetAddress src = null;
InetAddress dest = null;
if (pack instanceof IPPacket) {
IPPacket ipp = (IPPacket)pack;
src = ipp.dst_ip;
dest = ipp.src_ip;
}
//obtain MAC address of the default gateway
InetAddress pingAddr=InetAddress.getByName(dest.toString());
captor.setFilter("udp and dst host "+pingAddr.getHostAddress(),true);
byte[] gwmac=null;
while(true){
Packet ping=captor.getPacket();
if(ping==null){
System.out.println("cannot obtain MAC address of default gateway.");
}else if(Arrays.equals(((EthernetPacket)ping.datalink).dst_mac, devices[index].mac_address)) {
continue;
}
gwmac=((EthernetPacket)ping.datalink).dst_mac;
break;
}
//Send packet
JpcapSender sender=JpcapSender.openDevice(devices[index]);
UDPPacket sendPacket=new UDPPacket(12345,54321);//UDPPacket(src_port,dest_port);
sendPacket.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,100,IPPacket.IPPROTO_UDP,
src,dest);
String outData = DAO.getBroadCast();
sendPacket.data=outData.getBytes();
EthernetPacket ether=new EthernetPacket(); //get ethernet packet
ether.frametype=EthernetPacket.ETHERTYPE_IP; //set frame type as IP
//setting src nd dest MAC address
ether.src_mac= devices[index].mac_address;
ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};
//set datalink frame of packet as ether
sendPacket.datalink=ether;
for(int i=0;i<10;i++) {
sender.sendPacket(sendPacket);
}
} catch (UnknownHostException ex) {
Logger.getLogger(Jpcap.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
}
}
public static void main(String[] args) {
// set index of the interface that you want to open.
Jpcap j = new Jpcap();
j.receivePack();
}