1

I am using JPcap Library (Keita Fujii) to capture http packages from my wifi device. That works pretty well, but if the content size of a http response is to large, the packages are fragmented. Actually the psh-Flag of TCPPacket-Class helps me to find out if the response is fragmented, but is this the best method? I am looking for a good solution to merge the data of the fragments. Does someone can give me a hint?

    JpcapCaptor captor = JpcapCaptor.openDevice(devices[1], 65535, true,1000);
    captor.setFilter("tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)",true);

    while (true) {
        Packet packet = captor.getPacket();
        if (packet == null || packet == Packet.EOF)
            break;
        TCPPacket tcppacl = (TCPPacket) packet;
        if (!tcppacl.psh){
            //wait for next package...
leppie
  • 115,091
  • 17
  • 196
  • 297
Philipp Li
  • 499
  • 6
  • 22

1 Answers1

0

My current solution is the following:

class TCPPacketReciver implements PacketReceiver {

    Map<Long, TCPBodyData> tcpBodys = new HashMap<Long, TCPBodyData>();

    @Override
    public void receivePacket(Packet packet) {
        TCPPacket tcppacl = (TCPPacket) packet;
        byte[] body = addBodyData(tcppacl);
        if(tcppacl.psh){
            //body is complete
            //do something else...
        }
    }

    private byte[] addBodyData(TCPPacket packet) {
        TCPBodyData tcpBodyData;
        Long ack = new Long(packet.ack_num);
        if (tcpBodys.containsKey(ack)){
            tcpBodyData = tcpBodys.get(ack);
            tcpBodyData.addBytes(packet.data);
        }else{
            tcpBodyData = new TCPBodyData(packet.data);
            tcpBodys.put(ack, tcpBodyData);
        }

        if (packet.psh){
            tcpBodys.remove(ack);
        }

        return tcpBodyData.getBytes();
    }

    private class TCPBodyData {

        byte[] bytes = null;

        public TCPBodyData(byte[] bytes) {
            this.bytes = bytes;
        }

        public void addBytes(byte[] bytes) {
            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                outputStream.write(this.bytes);
                outputStream.write(bytes);
                this.bytes = outputStream.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public byte[] getBytes() {
            return bytes;
        }
    }
}

but I am still interested in any other solution. Thank you.

Philipp Li
  • 499
  • 6
  • 22