How can I update the source port or destination port in a packet???

No replies
ampineda
Offline
Joined: 09/24/2012

Hello,
This is my problem... How can I update the source port or destination port in a packet???
In my project I am reading the packets from a *.pcap file and sending over network.
But I have to change the port when I send the packets
Thank you for your time and help.

This is the code:
public void nextPacket(PcapPacket packet, String user) {
try{
System.out.printf("Received at %s caplen=%-4d len=%-4d \n",
new Date(packet.getCaptureHeader().timestampInMillis()),
packet.getCaptureHeader().caplen(), // Length actually captured
packet.getCaptureHeader().wirelen() // Original length
);

//*** TODO: CHANGE THE PORT OF THE PACKET!!!!!!!!!!!!!!!!
if (packet.hasHeader(Tcp.ID)) {
/*
* Now get our tcp header definition (accessor) peered with actual
* memory that holds the tcp header within the packet.
*/

packet.getHeader(tcp);

System.out.printf("tcp.dst_port=%d%n", tcp.destination());
System.out.printf("tcp.src_port=%d%n", tcp.source());
System.out.printf("tcp.ack=%x%n", tcp.ack());
}

//*** SINCRONIZAR LA REPRODUCCIÓN
if (prevTime != 0){
long espera = packet.getCaptureHeader().timestampInMillis() - prevTime;
Thread.sleep(espera);
}
prevTime = packet.getCaptureHeader().timestampInMillis();

if (pOut.sendPacket(packet) != Pcap.OK) {
System.err.println(pOut.getErr());
}
}
catch (Exception er){
System.err.println(er.getMessage());
}

}