Skip to content

Commit

Permalink
pcap_set_fanout_linux: use a proper flag to enable
Browse files Browse the repository at this point in the history
Hacking around int to assume values lower than 0 will disable fanout is
risky.
int size is platform dependent assuming its size is risky.
  • Loading branch information
tsnoam committed Apr 16, 2020
1 parent a8f5ee2 commit 929be78
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pcap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ struct pcap_opt {
*/
#ifdef __linux__
int protocol; /* protocol to use when creating PF_PACKET socket */
int fanout; /* If >= 0 - fanout is enabled. 16 lower bits are the group id; upper bits are the mode. */
int fanout_enabled; /* If evaluates to TRUE, fanout mode is enabled; default: FALSE. */
int fanout_opt; /* 16 lower bits are the group id; upper bits are the mode. */
#endif
#ifdef _WIN32
int nocapture_local;/* disable NPF loopback */
Expand Down
13 changes: 5 additions & 8 deletions pcap-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3734,8 +3734,8 @@ activate_sock(pcap_t *handle, int is_any_device)
* Add the packet socket into FANOUT group, if needed.
*/
#ifdef PACKET_FANOUT
if (handle->opt.fanout > 0) {
val = handle->opt.fanout;
if (handle->opt.fanout_enabled) {
val = handle->opt.fanout_opt;
if (setsockopt(sock_fd, SOL_PACKET, PACKET_FANOUT,
&val, sizeof(val)) < 0) {
pcap_fmt_errmsg_for_errno(handle->errbuf,
Expand Down Expand Up @@ -7152,18 +7152,15 @@ pcap_set_protocol_linux(pcap_t *p, int protocol)
}

int
pcap_set_fanout_linux(pcap_t *p, uint16_t mode, uint16_t group_id)
pcap_set_fanout_linux(pcap_t *p, int enable, uint16_t mode, uint16_t group_id)
{
#ifdef PACKET_FANOUT
if (pcap_check_activated(p)) {
return (PCAP_ERROR_ACTIVATED);
}

if (mode >= 0) {
p->opt.fanout = (((int)mode) << 16) | ((int)group_id & 0xffff);
} else {
p->opt.fanout = -1;
}
p->opt.fanout_enabled = 1;
p->opt.fanout_opt = (((int)mode) << 16) | ((int)group_id & 0xffff);

return 0;
#else
Expand Down
3 changes: 2 additions & 1 deletion pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,8 @@ pcap_create_common(char *ebuf, size_t size)
*/
#ifdef __linux__
p->opt.protocol = 0;
p->opt.fanout = -1;
p->opt.fanout_enabled = 0;
p->opt.fanout_opt = 0;
#endif
#ifdef _WIN32
p->opt.nocapture_local = 0;
Expand Down
2 changes: 1 addition & 1 deletion pcap/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ PCAP_API const char *pcap_tstamp_type_val_to_description(int);

#ifdef __linux__
PCAP_API int pcap_set_protocol_linux(pcap_t *, int);
PCAP_API int pcap_set_fanout_linux(pcap_t *, uint16_t, uint16_t);
PCAP_API int pcap_set_fanout_linux(pcap_t *, int, uint16_t, uint16_t);
#endif

/*
Expand Down
11 changes: 6 additions & 5 deletions pcap_set_fanout_linux.3pcap
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ among processes
#include <pcap/pcap.h>
.LP
.ft B
int pcap_set_fanout_linux(pcap_t *p, int8_t mode, uint16_t group_id);
int pcap_set_fanout_linux(pcap_t *p, int enable, uint16_t mode, uint16_t group_id);
.ft
.fi
.SH DESCRIPTION
On network intreface devices on Linux,
On Linux network interface devices,
.B pcap_set_fanout_linux()
sets the fanout configuration to be used in the
.BR socket (2)
call to create a capture socket when the handle is activated.
This function is only provided on Linux, and, if it is used on any device other
than a network interface, it will have no effect.
.LP
.IR enable
is a boolean flag to control if fanout is enabled (default is FALSE).
.LP
.IR mode
is the fanout mode and flags to use. More information on the various modes can
be found in
.BR packet (7)\c
\&. If
.IR mode
is lower than 0, fanout will be disabled (this is the default).
\&.
.LP
.IR group_id
is a unique identifier of the socket group.
Expand Down

0 comments on commit 929be78

Please sign in to comment.