aboutsip / pkts

Pure java based pcap library capable of reading and writing to/from pcaps.
Other
198 stars 92 forks source link

How to create a Packet without parent? #92

Open tun100 opened 6 years ago

tun100 commented 6 years ago

Hi, I'm working. Sorry to distrub again.
I wanna create a PcapOutputStream instance and invoke it's write method to write a packet into which is a Ipv4 packet.
When I try to init a packet, the Packet constructor request me to pass a parent packet instance as Argument, but I don't know where is the parent packet can init and the parent packet don't need pass a parent packet argument. Because I want to create a new pcap file by PcapOutputStream, but I don't know if there's a factory or defaultImpl way to init a parent packet or a create packet correct way, Could you offer a example like unit test code? Thanks!

aboutsip commented 6 years ago

Hey,

I'm on the bus right now but may find some time later but check out the unit tests, there should be complete examples of how to create a full Packet, all the way from layer 7 and down.

tun100 commented 6 years ago

@aboutsip thanks

tun100 commented 6 years ago

I found. The unit test code is in folder pkts-core/src/test/java/io/pkts/packet/impl/, seems like there's a TransportPacketFactory to use.

tun100 commented 6 years ago

@aboutsip I try to invoke the PacketFactory.getInstance().getTransportFactory().create(...), but the packet will be UDP whatever the protocol you pass into the arguments, I see the method detail, like this:

    public TransportPacket create(Protocol protocol, byte[] srcAddress, int srcPort, byte[] destAddress, int destPort, Buffer payload) throws IllegalArgumentException, IllegalProtocolException {
        TransportPacket pkt = this.createUdpInternal(payload);
        IPv4Packet ipPkt = (IPv4Packet)pkt.getParentPacket();
        ipPkt.setSourceIP(srcAddress[0], srcAddress[1], srcAddress[2], srcAddress[3]);
        ipPkt.setDestinationIP(destAddress[0], destAddress[1], destAddress[2], destAddress[3]);
        pkt.setDestinationPort(destPort);
        pkt.setSourcePort(srcPort);
        ipPkt.reCalculateChecksum();
        return pkt;
    }

seems like the Protocol protocol argument won't be used in this method, and all packet will be create by private method createUdpInternal method, Is there something not right? I may need to create a packet base IP and not base UDP.

tun100 commented 6 years ago

I used to changed the Buffer payload value of the create method which is a argument. the protocol which display in wireshark of the pcap file seems like will be judge by the buffer format, not judged by Protocol arguments.