from the man pcap:
a size that's too small could mean that, if too many packets are being
captured and the snapshot length doesn't limit the amount of data
that's buffered, packets could be dropped if the buffer fills up
before the application can read packets from it, while a size that's
too large could use more non- pageable operating system memory than is
necessary to prevent packets from being dropped
The ideal size of the buffer depends on the use case.
What you could do to find this ideal size is by using dichotomy:
- set a huge but system supported size at first
- with pcap_stats() to get the number of dropped packets
- compile again and run.
note if you have dropped packets.
then set to your current size
- with pcap_stats() to get the number of dropped packets
- compile again and run.
note if you have dropped packets.
then set size to (huge size + current size)/2
- with pcap_stats() to get the number of dropped packets
- compile again and run.
note if you have dropped packets.
...
Note that you can reduce the amount of buffer use by setting the snapshot lenght with pcap_set_snaplen()
If, when capturing, you capture the entire contents of the packet,
that requires more CPU time to copy the packet to your application,
more disk and possibly network bandwidth to write the packet data to a
file, and more disk space to save the packet. If you don't need the
entire contents of the packet - for example, if you are only
interested in the TCP headers of packets - you can set the "snapshot
length" for the capture to an appropriate value. If the snapshot
length is set to snaplen, and snaplen is less than the size of a
packet that is captured, only the first snaplen bytes of that packet
will be captured and provided as packet data
you can get the current snaplen with shotpcap_snapshot()