Jnetpcap Payload modify in UDP packet

franktiello

i would modify the content of Data in the UDP Packet read from a pcap file and send it on the network. In the following example i write a string "User data" and it work correctly but if my data require more space than the previous payload opened, i get error, how i can increase dimension of payload data taken from the original pcap file?

            Pcap pcap_off = Pcap.openOffline(fileName, errorBuf);  //open original packet
            PcapPacket temp= new PcapPacket(JMemory.Type.POINTER);
            pcap_off.nextEx(temp); //only one UDp packet
            JBuffer buff=new JBuffer(temp.size());  
            Ethernet eth=temp.getHeader(new Ethernet()); 
            Ip4 ip=temp.getHeader(new Ip4());
            Udp udp=temp.getHeader(new Udp());
            Payload data=temp.getHeader(new Payload()); 

            InetAddress dst = InetAddress.getByName("10.0.0.10");
            ip.destination(dst.getAddress()); //modify ip dst
            ip.checksum(ip.calculateChecksum()); 
            eth.transferTo(buff);  
            ip.transferTo(buff, 0, ip.size(), eth.size()); 

            *byte[] userdata = new String("User data").getBytes();* 
            *data.setByteArray(0,userdata);*
            *data.transferTo(buff, 0, data.size(), eth.size() + ip.size()+ udp.size());* 

            int cs = udp.calculateChecksum(); //ricalcolo il checksum UDP
            udp.setUShort(6, cs);  //correct UDP checksum
            udp.transferTo(buff, 0, udp.size(), eth.size() + ip.size());

            JPacket new_packet =new JMemoryPacket(JProtocol.ETHERNET_ID,buff); //new packet 

Many thanks to any answer.

Mark Bednarczyk

You can't resize the buffer. You have to allocate a bigger buffer then the original packet so you have room to expand.

In your code, copy the packet from pcap buffer to jbuffer first, possibly one header at a time instead of the entire packet at once and make your changes as you go, one header at a time. The bigger buffer will give you room to include payload of any size.

For efficiency on windows systems you can also use the sendqueue which will allow you to compose many packets in a single large buffer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related