Mihail HRS
Mihail HRS

Reputation: 115

Linux. Can packets pass libpcap by?

I am writing a linux program that controls internet traffic. In other words, how much bytes I have used while some amount of time. I use a Pcap4J for java (implementation of libpcap) and I have question about it. What happens if my program hasn't proceeded a package while a new one has arrived.
1. It slows down the download(upload) rate for the whole OS?
2. It skips a new one, and my program will never know that it passed by?

In other words, I've downloaded the 1G of data on my computer. How many bytes my program get: 100% or it may be passed my program by but still got the destination place!
And give me know if it is a bad idea to write a control traffic app using this lib!

Upvotes: 0

Views: 53

Answers (1)

Oppen
Oppen

Reputation: 433

Your application loses packets. In your words, they pass by.

However, if your idea is to have a metric of how many packets went in and out of your system in a given time, there are definitely better ways to achieve it.

On Linux you can just do a script that does something like this:

DEVICE=eth0
RX0=$(cat /sys/net/$DEVICE/statistics/rx_bytes)
TX0=$(cat /sys/net/$DEVICE/statistics/tx_bytes)
while : ; do
  sleep 5
  RX1=$(cat /sys/net/$DEVICE/statistics/rx_bytes)
  TX1=$(cat /sys/net/$DEVICE/statistics/tx_bytes)
  echo "RX bytes: $(($RX1-$RX0))"
  echo "TX bytes: $(($TX1-$TX0))"
  RX0=RX1
  TX0=TX1
done

You can adjust times or whether is a parameter, I think you'll get the idea.

Upvotes: 1

Related Questions