From 35865bfd8c15163a6963812e3419514336966802 Mon Sep 17 00:00:00 2001 From: Dima Golomozy Date: Thu, 17 Jun 2021 20:20:19 +0300 Subject: [PATCH] Capture interfaces with ip (#944) * capture nic only with ips * pcap.Interface and net.Interface flags are not the same. using net.Flags --- capture/capture.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/capture/capture.go b/capture/capture.go index f8a885a..67b4c24 100644 --- a/capture/capture.go +++ b/capture/capture.go @@ -451,6 +451,7 @@ func (l *Listener) setInterfaces() (err error) { if err != nil { return } + for _, pi := range pifis { var ni net.Interface for _, i := range ifis { @@ -460,29 +461,37 @@ func (l *Listener) setInterfaces() (err error) { } } - if net.Flags(pi.Flags)&net.FlagLoopback != 0 { + if ni.Flags&net.FlagLoopback != 0 { l.loopIndex = ni.Index } - if net.Flags(pi.Flags)&net.FlagUp == 0 { + if ni.Flags&net.FlagUp == 0 { continue } + if isDevice(l.host, pi) { l.Interfaces = []pcap.Interface{pi} return } - for _, addr := range pi.Addresses { - if addr.IP.String() == l.host { - l.Interfaces = []pcap.Interface{pi} - return - } + + if len(pi.Addresses) != 0 { + l.Interfaces = append(l.Interfaces, pi) } } - l.Interfaces = pifis return } func isDevice(addr string, ifi pcap.Interface) bool { - return addr == ifi.Name + if addr == ifi.Name { + return true + } + + for _, _addr := range ifi.Addresses { + if _addr.IP.String() == addr { + return true + } + } + + return false } func interfaceAddresses(ifi pcap.Interface) []string {