Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions afpacket/afpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (h *TPacket) bindToInterface(ifaceName string) error {
}
h.ifIndex = ifIndex
s := &unix.SockaddrLinklayer{
Protocol: endian.Htons(uint16(unix.ETH_P_ALL)),
Protocol: endian.Htons(h.opts.protocol),
Ifindex: ifIndex,
}
return unix.Bind(h.fd, s)
Expand Down Expand Up @@ -255,7 +255,7 @@ func NewTPacket(opts ...interface{}) (h *TPacket, err error) {
if h.opts, err = parseOptions(opts...); err != nil {
return nil, err
}
fd, err := unix.Socket(unix.AF_PACKET, int(h.opts.socktype), int(endian.Htons(unix.ETH_P_ALL)))
fd, err := unix.Socket(unix.AF_PACKET, int(h.opts.socktype), int(endian.Htons(h.opts.protocol)))
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions afpacket/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ const (
SocketDgram = OptSocketType(unix.SOCK_DGRAM)
)

// OptProtocol is the protocol that should be received by the socket.
// It can be passed into NewTPacket.
//
// This should be an IEEE 802.3 protocol number in host byte order. To receive
// packets of all protocols, use [unix.ETH_P_ALL] (this is the default). If set
// to zero, no packets are received.
type OptProtocol uint16

// OptInterface is the specific interface to bind to.
// It can be passed into NewTPacket.
type OptInterface string
Expand Down Expand Up @@ -126,6 +134,7 @@ type options struct {
pollTimeout time.Duration
version OptTPacketVersion
socktype OptSocketType
protocol uint16
iface string
}

Expand All @@ -137,6 +146,7 @@ var defaultOpts = options{
pollTimeout: DefaultPollTimeout,
version: TPacketVersionHighestAvailable,
socktype: SocketRaw,
protocol: unix.ETH_P_ALL,
}

func parseOptions(opts ...interface{}) (ret options, err error) {
Expand All @@ -155,6 +165,8 @@ func parseOptions(opts ...interface{}) (ret options, err error) {
ret.pollTimeout = time.Duration(v)
case OptTPacketVersion:
ret.version = v
case OptProtocol:
ret.protocol = uint16(v)
case OptInterface:
ret.iface = string(v)
case OptSocketType:
Expand Down
Loading