problem capture PlayLoad field generate BufferUnderflowException

2 replies [Last post]
alvaroc
Offline
Joined: 06/02/2016
first: sorry my english. I am capture packet of the network between two computers,(I want only playload field of TCP header). actually i use byte []array=packet.getByteArray(0,packet.size()) for extract datas from packet. no problem. the problem happen when use TCP.getPayLoad();(my object Tcp). this line tell BufferUnderflowException. adjunt code Sad( Sad(
import java.util.ArrayList;
import java.util.Date;
import org.jnetpcap.*;
import org.jnetpcap.nio.JBuffer;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.JPacketHandler;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
import org.jnetpcap.protocol.network.Ip4;
import org.jnetpcap.protocol.tcpip.Tcp;
import org.jnetpcap.protocol.tcpip.Udp;



public class CapturaRed extends Thread {
    private ArrayList<PcapIf> dispositivos=new ArrayList<PcapIf>();
    private StringBuilder error=new StringBuilder();
    private ArrayList packetes=new ArrayList();
    private PcapIf dispositivo=null;
    private int ban;
    private MindrayPacket MP=new MindrayPacket();
   
   
   
   
   
   
   
    public void obteneDispo(){
    ban=Pcap.findAllDevs(dispositivos, error);
        if(ban==Pcap.NOT_OK || dispositivos.isEmpty()){
            System.out.println("error leyendo los dispos");
            System.out.println(error.toString());
        }
    }
   
    public void listarDispositivos(){
        String descripcion=null;
        for(int j=0;j<dispositivos.size();j++){
            dispositivo=dispositivos.get(j);
            descripcion=dispositivo.getDescription();
            if(descripcion !=null){
                System.out.println("Nombre del dispositivo :"+dispositivo.getName());
                System.out.println("No :"+j);
                System.out.println("Descripcion del Dispositivo :"+descripcion);
            }
            dispositivosPorEnlace(j);
        }
        capturarDatosDeRed(dispositivos.get(1));
    }
       
       public void dispositivosPorEnlace(int indice){
           ArrayList<PcapAddr> dispo=(ArrayList<PcapAddr>) dispositivos.get(indice).getAddresses();
           PcapAddr dis=null;
           for(int j=0;j<dispo.size();j++){
           dis=dispo.get(j);
            System.out.println("Direccion IP: "+dis.getAddr().toString());
            System.out.println("Mascara de sub red: "+dis.getNetmask().toString());
            } 
       }
      
      
       public void capturarDatosDeRed(PcapIf dispo){
           int alcanzeCaptura= 64*1024;
           int bandera =Pcap.MODE_PROMISCUOUS;
           int tiemposalida= 10*1000;
         
           Pcap pcap=Pcap.openLive(dispo.getName(),alcanzeCaptura, bandera, tiemposalida, error);
           if(pcap==null){
               System.out.println("Error abriendo dispositivo de captura"+
                       error.toString());
           }
           System.out.println("datos optenidos del dispositivo");
           System.out.println("des: "+dispo.getDescription());
           PcapPacketHandler<String> jpacketHandler=new PcapPacketHandler<String>() {;
                Tcp TCP=new Tcp();
                Ip4 ip=new Ip4();
                Udp UDP=new Udp();
               
               
             
            @Override
            public void nextPacket(PcapPacket paqute, String user) {
                ArrayList packetDat=new ArrayList();
                ArrayList aux=new ArrayList();
                 int auxiliar[]=new int[2];
                    if(paqute.hasHeader(TCP.ID)){
                        int tama=paqute.size();
                       paqute.getHeader(TCP);
                       JBuffer buffer= paqute;
                       byte array[]=buffer.getByteArray(0, tama)//actually;
                       //byte array[]=TCP.getPayload();
                       //i try byte array[]=paqute.getByteArray(TCP.getPayloadOffset(),TCP.getPayloadLength() );                     //and byte   //array[]=paqute.getByteArray(paqute.getInt(TCP.getPayloadOffset()),TCP.getPayloadLength());
                       System.out.println("fuente :"+TCP.source());
                       System.out.println("Destinio :"+TCP.destination());
                       System.out.println("Paquetes recibido el: "+new Date(paqute.getCaptureHeader().timestampInMillis()));
                       System.out.println("el tamaño del paqute capturado  :"+paqute.getCaptureHeader().caplen());
                       System.out.println("el tamño del paquete original :"+paqute.getCaptureHeader().wirelen());
                       System.out.println(paqute);

}
this one part of code, thak yout help, output adjunt.
alvaroc
Offline
Joined: 06/02/2016
Hi, after much search, i

Hi, after much search, i found a solution.

ok, the first is clear the problem.

when i asked, byte array[]=TCP.getPayload() i not check the lenght of payload TCP.

so for avoid the problem, i alone add the next line if(TCP.getPayloadLength()!=0).

i hope can help.

PD: sorry for my english.

alvaroc
Offline
Joined: 06/02/2016
Hi, after much search, i

Crying Laughing out loud Shocked Batting Eyelashes Hi, after much search, i found a solution.

ok, the first is clear the problem.

when i asked, byte array[]=TCP.getPayload() i not check the lenght of payload TCP.

so for avoid the problem, i alone add the next line if(TCP.getPayloadLength()!=0).

i hope can help.

PD: sorry for my english.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.