Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get source and destination Ip from TCP packet #307

Open
prashantkalkar opened this issue Aug 17, 2023 · 2 comments
Open

How to get source and destination Ip from TCP packet #307

prashantkalkar opened this issue Aug 17, 2023 · 2 comments

Comments

@prashantkalkar
Copy link

Hi,
I am trying to capture tcp traffic (similar to what tcpdump captures). From the tcp packet I am trying to get the source and destination port and source and destination IP addresses.
Here is how I am getting the tcpPacket

TcpPacket tcpPacket = pcapHandle.getNextPacketEx().get(TcpPacket.class);

But I can't see any method to obtain source and destination IPs. I tried looking into the tcp headers. I also tried looking into the payload like this.

tcpPacket.getPayload().get(IpV4Packet.class)
But this just provide null as payload is of type UnknownPacket.

Is there any way to get the IP information for TCP traffic?

@airxiechao
Copy link

TcpPacket is a type of payload for IpV4Packet. You can first capture the IpV4Packet, which contains IP address, then extract the payload of TcpPacket.

@itboy2009
Copy link

add dependency to ./pcap4j-sample/pom.xml

<dependency>
  <groupId>org.pcap4j</groupId>
  <artifactId>pcap4j-packetfactory-propertiesbased</artifactId>
  <version>${version}</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants