Question

I would like to ask you a specific question: I'm trying to develop a software switch with the jNetPcap 1.4 library on Java 1.7, the goal is to have a program, that can forward packets received on port no. 1 to port no. 2 and vice versa (just on these two NIC-s).

I can see all the incoming packets on both of the interfaces, but when it comes to forwarding, I have a problem: if I send a packet on interface X with pcap.sendPacket(PcapPacket) the nextPacket() function inside the PcapPacketHandler() class will see it also, and will treat is a "new" packet, insted of just ignoring it, because it was sent by the same Pcap instance.

Is there any way to ignore the packets, that were sent by the pcap.sendPacket(PcapPacket) function (so they do not appear again in the nextPacket() function)?

I don't know much about the underlying winpcap library, but I'm sure SharpPcap in C# has this type of functionality. Doesn't jNetPcap have it also, or am I missing something?

Things I've tried:

  1. Using pcap.inject() instead of pcap.sendPacket(), but my NIC doesn't seem to support this function
  2. Setting the capture direction to inbound only with pcap.setDirection(Direction.IN) - this seems to have absolutely no effect, the packets are captured just as before setting it.

Ps. unfortunately I have to write it in Java, if the jNetPcap library does not have this functionality, please, could you advise how to solve the problem? I'm thinking of buffering the sent packets to an array of some type and checking every newly detected packet to be the same as one packet in the array - but this seems to be a computationally complex solution.

Here's my sample code (not the whole, just the relevant pieces):

// Init
Pcap pcap = Pcap.openLive(devices.get(0).getName(), 64*1024, Pcap.MODE_PROMISCUOUS, 1000, errbuf);  

...


PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  
    public void nextPacket(PcapPacket packet, String user) {
        // The duplicate packet that is sent in the sendAll() function also appears here
        // Process the packet and add it to the forwarding buffer if needed
        buffer.addPacket(packet);
    }  
}

// This is called in a separate thread, if some packets were added to the buffer
public void sendAll(){
    while(buffer.hasNext() != 0){
        pcap.sendPacket(buffer.getNextPacket());
    } 
}

Same goes for the second interface.

Thank you for your help in advance.

Was it helpful?

Solution

I realize this is an old and maybe not so popular topic, but I finally solved this by adding a buffer and constantly checking for duplicates. Since the code is complex, I decided to post the key functionality and how do I ignore the frame based on jnetpcap.com recommendation.

The code:

// we're inside the nextPacket(PcapPacket currentPacket, String user) method
buffer.lock();
for(int j = 0; j<buffer.size(); j++){
    if(currentPacket.getCaptureHeader().caplen() == buffer.get(j).getCaptureHeader().caplen() 
        && packet.size() == buffer.get(j).size()){

        buffer.erasePacket(j);

        return;
    }

}
buffer.unlock();
// continue packet processing

Feel free to use it in your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top