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

Add a flag (-R) to terminate capture after specified time #684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

steinbrecher
Copy link
Contributor

@steinbrecher steinbrecher commented Jun 10, 2018

Context/Background: I needed this feature to be able to capture short bursts
(single ms to tens of ms) on high-rate interfaces without packet loss. Searching
produced some results of people trying to do timed captures using timeout(1) or
other coarser-grained checks. OS-level checks rely on the running time of
the program, not the time interval of packets captured, making it hard to capture
short bursts. Alternatively, for multiple second captures, some people use the log
rotation time + restriction of number of log files to take a timed capture, but this
is both hacky and only provides resolution in seconds, not milliseconds or
microseconds

Adding this check seems more reliable, easier to use, and provides finer-grained
control than any of the above methods.

The check works by adding a function call to each of the callback functions
passed to pcap; it saves the time of the first packet seen and then compares
later packets to that time. Time interval is specified at as an argument to -R
with units of microseconds, milliseconds, seconds, minutes, or hours. Internally,
this gets converted to an integer number of microseconds.

Updated the generated manpage and the printed usage. Haven't added any
tests; I didn't see any for other command line flags like -c, but can
add a couple if desired.

-R was chosen as the flag as shorthand for "runtime"; the longopt is set to
--max-runtime.

Works by adding a check to the callback function passed to pcap;
saves the time of the first packet seen and then compares later packets
to that time. Time interval is specified with units of microseconds,
milliseconds, seconds, minutes, or hours. Internally, this gets converted
to an integer number of microseconds.

Updated the generated manpage and the printed usage. Haven't added any
tests; I didn't see any for other command line flags like -c, but can
add a couple if desired.
@steinbrecher steinbrecher changed the title Added a flag (-R) to terminate capture after specified time Add a flag (-R) to terminate capture after specified time Jun 10, 2018
@infrastation
Copy link
Member

This seems to implement the request made in #338.

@steinbrecher
Copy link
Contributor Author

Needs a quick cleanup & rebase if the feature is of interest -- @infrastation how is that determined?

@infrastation
Copy link
Member

As another user points it out, there is the timeout command that does the job on the OSes traditional for tcpdump, so there are arguments pro and contra embedding timeout into tcpdump. Requesting more feedback on the tcpdump-workers mailing list would not be a bad idea, it seems to me.

@mcr mcr added the improvement label May 3, 2019
@mcr mcr added this to the release_after_next milestone May 3, 2019
@mcr
Copy link
Member

mcr commented May 3, 2019

Can you use a long option?

@@ -688,6 +754,7 @@ static const struct option longopts[] = {
#ifdef HAVE_PCAP_SETDIRECTION
{ "direction", required_argument, NULL, 'Q' },
#endif
{ "max-runtime", required_argument, NULL, 'R', },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same indentation as other code.

Note: tab stops are set every eight spaces, not every four spaces, this having originally been written on UN*X. :-) If you're reading it with a program with tab stops set every four spaces, it won't display correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will clean up when I rebase :)

@guyharris
Copy link
Member

Note that without an interval timer (setitimer()/alarm()) on UN*X, and an equivalent on Windows, there's no guarantee, on a network with a low level of traffic, that there won't be an arbitrary amount of time after the time expires when the capture terminates.

@steinbrecher
Copy link
Contributor Author

steinbrecher commented May 3, 2019

Note that without an interval timer (setitimer()/alarm()) on UN*X, and an equivalent on Windows, there's no guarantee, on a network with a low level of traffic, that there won't be an arbitrary amount of time after the time expires when the capture terminates.

Yeah, very true. Thoughts on adding an interval timer equal to the selected time +1 second (or something equally "long" relative to a short time selection)?

@steinbrecher
Copy link
Contributor Author

Can you use a long option?

--max-runtime is a substitute for -R in the code as is

@danielhoherd
Copy link

Any chance of finishing this feature? I was looking for it today.

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

Successfully merging this pull request may close these issues.

5 participants